mal-go/read/read_test.go
ajet db1bd5f322
All checks were successful
Go / test (push) Successful in 10s
bring back empty() vs new(val)
2025-11-04 13:03:00 -10:00

50 lines
1.6 KiB
Go

package read
import (
"github.com/stretchr/testify/assert"
"mal-go/list"
"mal-go/symbol"
"testing"
)
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")
}