government / sexpr / reader_test.go

@ 7eeb8a0214368f4491f26f4cf37bd7bf6dc67cd3 | history


package sexpr

import (
	"errors"
	"math"
	"reflect"
	"strings"
	"testing"
)

func TestReadAtoms(t *testing.T) {
	tests := []struct {
		src  string
		want Value
	}{
		{"0", Int(0)},
		{"42", Int(42)},
		{"-42", Int(-42)},
		{"9223372036854775807", Int(math.MaxInt64)},
		{"-9223372036854775808", Int(math.MinInt64)},
		{"3.14", Float(3.14)},
		{"-0.5", Float(-0.5)},
		{"1.0", Float(1)},
		{"1.5e3", Float(1500)},
		{"1.0E-2", Float(0.01)},
		{"1.0e+21", Float(1e21)},
		{"T", T},
		{"F", F},
		{"t", Symbol("t")},
		{"foo", Symbol("foo")},
		{"+", Symbol("+")},
		{"-", Symbol("-")},
		{"<=", Symbol("<=")},
		{"str?", Symbol("str?")},
		{"-x", Symbol("-x")},
		{"a.b", Symbol("a.b")},
		{"x1", Symbol("x1")},
		{"héllo", Symbol("héllo")},
		{`"hi"`, String("hi")},
		{`""`, String("")},
		{`"1"`, String("1")},
	}
	for _, tt := range tests {
		t.Run(tt.src, func(t *testing.T) {
			got := readOne(t, tt.src)
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("Read(%s) = %#v, want %#v", tt.src, got, tt.want)
			}
		})
	}
}

// The printed form is the normal form: reading then printing gives it.
func TestReadNormalizes(t *testing.T) {
	tests := []struct{ src, want string }{
		{"()", "()"},
		{"( )", "()"},
		{"(1 2 3)", "(1 2 3)"},
		{"  ( 1\n\t2   3 )  ", "(1 2 3)"},
		{"(a (b (c)) ())", "(a (b (c)) ())"},
		{"(1 . 2)", "(1 . 2)"},
		{"(1 2 . 3)", "(1 2 . 3)"},
		{"(1 . (2 3))", "(1 2 3)"},
		{"(1 . (2 . (3 . ())))", "(1 2 3)"},
		{"(1 . ())", "(1)"},
		{"((1 . 2) . 3)", "((1 . 2) . 3)"},
		{"'x", "(quote x)"},
		{"'(1 2)", "(quote (1 2))"},
		{"''x", "(quote (quote x))"},
		{"(a 'b c)", "(a (quote b) c)"},
		{"'(1 . 2)", "(quote (1 . 2))"},
		{"(1 . 'x)", "(1 quote x)"},
		{"(+ 1 ; comment\n 2)", "(+ 1 2)"},
		{"(define sq (lambda (x) (* x x)))", "(define sq (lambda (x) (* x x)))"},
		{`("a\nb" "\u{41}")`, `("a\nb" "A")`},
		{"1.50", "1.5"},
		{"(T F)", "(T F)"},
	}
	for _, tt := range tests {
		t.Run(tt.src, func(t *testing.T) {
			if got := Print(readOne(t, tt.src)); got != tt.want {
				t.Errorf("Print(Read(%q)) = %s, want %s", tt.src, got, tt.want)
			}
		})
	}
}

// Print's output must read back to an equal value.
func TestPrintReadRoundTrip(t *testing.T) {
	values := []Value{
		Int(math.MinInt64), Int(math.MaxInt64), Float(0.1), Float(1), Float(-0.0),
		Float(1e21), Float(1.5e-7), Float(math.MaxFloat64), Float(math.SmallestNonzeroFloat64),
		String(""), String("a\"b\\c\nd\te"), String("bell\a\x00\x7f"), String("héllo 😀"),
		T, F, Symbol("x"), Nil,
		List(Int(1), List(Float(2), Nil), String("s")),
		Cons(Int(1), Int(2)),
		ListWithTail(Symbol("c"), Symbol("a"), Symbol("b")),
		Cons(Cons(Int(1), Int(2)), Int(3)),
	}
	for _, v := range values {
		printed := Print(v)
		t.Run(printed, func(t *testing.T) {
			if got := readOne(t, printed); !reflect.DeepEqual(got, v) {
				t.Errorf("Read(Print(v)) = %s, want %s", Print(got), printed)
			}
		})
	}
}

func TestReadAll(t *testing.T) {
	forms, err := ReadAll("(define x 1) ; one\nx\n'y\n")
	if err != nil {
		t.Fatal(err)
	}
	var got []string
	for _, f := range forms {
		got = append(got, Print(f))
	}
	want := []string{"(define x 1)", "x", "(quote y)"}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("ReadAll = %v, want %v", got, want)
	}

	forms, err = ReadAll("  ; only a comment\n")
	if err != nil || len(forms) != 0 {
		t.Errorf("ReadAll(comment only) = %v, %v; want no forms", forms, err)
	}
}

func TestReadErrors(t *testing.T) {
	tests := []struct{ src, want string }{
		{")", "1:1: unexpected )"},
		{"(1 2))", "1:6: unexpected )"},
		{"(1 2", "1:1: unterminated list"},
		{"(a\n  (b c)", "1:1: unterminated list"},
		{"(a (b", "1:4: unterminated list"},
		{".", "1:1: unexpected . outside a list"},
		{"'", "1:1: ' must be followed by a value"},
		{"(')", "1:2: ' must be followed by a value"},
		{"'.", "1:2: unexpected . outside a list"},

		// A pair has exactly one cdr.
		{"(. 1)", "1:2: a dotted pair needs a value before ."},
		{"(1 .)", "1:4: expected a value after ."},
		{"(1 . )", "1:4: expected a value after ."},
		{"(1 .", "1:4: expected a value after ."},
		{"(1 . 2 3)", "1:8: expected ) after the value following ."},
		{"(1 . 2 . 3)", "1:8: only one . is allowed in a list"},
		{"(1 . . 2)", "1:6: only one . is allowed in a list"},
		{"(1 . 2", "1:1: unterminated list"},

		// Numbers must match the grammar exactly.
		{"007", "1:1: malformed number 007"},
		{"1.", "1:1: malformed number 1."},
		{".5", "1:1: malformed number .5"},
		{"-.5", "1:1: malformed number -.5"},
		{"+5", "1:1: malformed number +5"},
		{"1e5", "1:1: malformed number 1e5"},
		{"12abc", "1:1: malformed number 12abc"},
		{"9223372036854775808", "1:1: integer 9223372036854775808 does not fit in 64 bits"},
		{"1.0e400", "1:1: float 1.0e400 is out of range"},

		// Lexer errors surface unchanged.
		{`(a "oops`, "1:4: unterminated string"},
	}
	for _, tt := range tests {
		t.Run(tt.src, func(t *testing.T) {
			_, err := ReadAll(tt.src)
			var se *SyntaxError
			if !errors.As(err, &se) {
				t.Fatalf("ReadAll(%q) error = %v, want SyntaxError", tt.src, err)
			}
			if err.Error() != tt.want {
				t.Errorf("ReadAll(%q) error = %q, want %q", tt.src, err.Error(), tt.want)
			}
		})
	}
}

func TestReadDeepNesting(t *testing.T) {
	const depth = 10_000
	src := strings.Repeat("(", depth) + strings.Repeat(")", depth)
	v := readOne(t, src)
	for i := 0; i < depth-1; i++ {
		v = v.(*Pair).Car
	}
	if v != Value(Nil) {
		t.Errorf("innermost value = %s, want ()", Print(v))
	}
}

func readOne(t *testing.T, src string) Value {
	t.Helper()
	forms, err := ReadAll(src)
	if err != nil {
		t.Fatalf("ReadAll(%q): %v", src, err)
	}
	if len(forms) != 1 {
		t.Fatalf("ReadAll(%q) returned %d forms, want 1", src, len(forms))
	}
	return forms[0]
}

func TestReaderPositions(t *testing.T) {
	src := "(a\n  (b c)\n  'd)\nx\n'(e)"
	r := NewReader(src)
	outer, pos, err := r.ReadWithPos()
	if err != nil || pos != (Pos{1, 1}) {
		t.Fatalf("first form at %v, err %v; want 1:1", pos, err)
	}
	inner := outer.(*Pair).Cdr.(*Pair).Car
	quoted := outer.(*Pair).Cdr.(*Pair).Cdr.(*Pair).Car
	for _, tt := range []struct {
		v    Value
		want Pos
	}{{outer, Pos{1, 1}}, {inner, Pos{2, 3}}, {quoted, Pos{3, 3}}} {
		if got, ok := r.Position(tt.v); !ok || got != tt.want {
			t.Errorf("Position(%s) = %v, %v; want %v", Print(tt.v), got, ok, tt.want)
		}
	}

	atom, pos, _ := r.ReadWithPos()
	if pos != (Pos{4, 1}) {
		t.Errorf("atom x at %v, want 4:1", pos)
	}
	if _, ok := r.Position(atom); ok {
		t.Errorf("atoms should have no recorded position")
	}

	q, pos, _ := r.ReadWithPos()
	if got, ok := r.Position(q); !ok || got != (Pos{5, 1}) || pos != (Pos{5, 1}) {
		t.Errorf("'(e) at %v / %v, want 5:1", got, pos)
	}
	if got, ok := r.Position(q.(*Pair).Cdr.(*Pair).Car); !ok || got != (Pos{5, 2}) {
		t.Errorf("(e) at %v, want 5:2", got)
	}
}

func TestReaderKeepsComments(t *testing.T) {
	r := NewReader("(+ 1 2) ;=> 3\n; note\nx ;=! oops")
	for {
		v, err := r.Read()
		if err != nil {
			t.Fatal(err)
		}
		if v == nil {
			break
		}
	}
	var got []string
	for _, c := range r.Comments() {
		got = append(got, c.Pos.String()+" "+c.Text)
	}
	want := []string{"1:9 => 3", "2:1  note", "3:3 =! oops"}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("Comments() = %q, want %q", got, want)
	}
}

// Errors caused only by input ending early are marked Incomplete, so a REPL
// can ask for more; real mistakes are not.
func TestIncompleteInput(t *testing.T) {
	tests := []struct {
		src        string
		incomplete bool
	}{
		{"(", true},
		{"(+ 1\n  (* 2", true},
		{`"abc`, true},
		{`"abc\`, true},
		{`"\u`, true},
		{`"\u{4`, true},
		{"'", true},
		{"(1 .", true},
		{"(1 . 2", true},
		{")", false},
		{"(')", false},
		{"(1 . )", false},
		{"(1 . 2 3)", false},
		{`"\q"`, false},
		{`"\u{zz}"`, false},
		{"007", false},
	}
	for _, tt := range tests {
		_, err := ReadAll(tt.src)
		se, ok := err.(*SyntaxError)
		if !ok {
			t.Errorf("ReadAll(%q) error = %v, want SyntaxError", tt.src, err)
			continue
		}
		if se.Incomplete != tt.incomplete {
			t.Errorf("ReadAll(%q): Incomplete = %v, want %v (%v)", tt.src, se.Incomplete, tt.incomplete, se)
		}
	}
}