mal-go/read/read_test.go
ajet ca2485b725
All checks were successful
Go / test (push) Successful in 3s
sprinkle in some PBT
2025-11-05 06:43:24 -10:00

85 lines
2.4 KiB
Go

package read
import (
"fmt"
"mal-go/hash_map"
"mal-go/list"
"mal-go/symbol"
"mal-go/vector"
"testing"
"github.com/stretchr/testify/assert"
"pgregory.net/rapid"
)
func TestReadInt(t *testing.T) {
input := "2"
sym, err, end_pos := readInt(input, 0, "user")
assert.Equal(t, 2, sym, "should read ")
assert.Equal(t, nil, err, "should read without error")
assert.Equal(t, len(input), end_pos, "should read the whole string")
}
func TestReadSymbol(t *testing.T) {
input := "foo"
sym, err, end_pos := readSymbol(input, 0, "user")
assert.Equal(t, sym, symbol.Intern("user", "foo"), "should read user/foo symbol")
assert.Equal(t, err, nil, "should read without error")
assert.Equal(t, end_pos, len(input), "should read the whole string")
}
func TestReadSymbolWithNamespace(t *testing.T) {
input := "foo/bar"
sym, err, end_pos := readSymbol(input, 0, "user")
assert.Equal(t, sym, symbol.Intern("foo", "bar"), "should read user/foo symbol")
assert.Equal(t, err, nil, "should read without error")
assert.Equal(t, end_pos, len(input), "should read the whole string")
}
func TestReadListElem(t *testing.T) {
input := "(1223)"
f, err, pos := readForm(input, 1, "user")
assert.Equal(t, f, 1223, "should num list")
assert.Equal(t, err, nil, "should read without error")
assert.Equal(t, pos, len(input)-1, "should read the whole number")
assert.Equal(t, rune(input[pos]), ')', "should end pointing to ending list char")
}
func TestReadList(t *testing.T) {
input := "(1 2 3)"
l, err, pos := readList(input, 0, "user")
assert.Equal(t, list.Empty().Conj(3).Conj(2).Conj(1), l, "should read list")
assert.Equal(t, nil, err, "should read without error")
assert.Equal(t, len(input), pos, "should read the whole string")
}
func StringifiedFormGen() *rapid.Generator[string] {
return rapid.OneOf(rapid.Custom(func(t *rapid.T) string {
myList := list.Empty()
i := 200
t.Repeat(map[string]func(*rapid.T){
"conj": func(t *rapid.T) {
if i < 50 {
myList = myList.Conj(StringifiedFormGen().Draw(t, "el"))
}
},
})
return myList.String()
}),
rapid.Just(list.Empty().String()),
rapid.Just(hash_map.New().String()),
rapid.Just(vector.New().String()),
rapid.Custom(func(t *rapid.T) string {
return fmt.Sprint(rapid.Int().Draw(t, "i"))
}),
)
}
func TestRead(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
input := StringifiedFormGen().Draw(t, "input")
_, err, _ := readForm(input, 0, "user")
assert.Nil(t, err, err)
})
}