government / internal / lisptest / lisptest_test.go

@ 9d8c149e89e015823df22a42b5d70029e7e44e25 | history


package lisptest

import (
	"fmt"
	"government/sexpr"
	"strings"
	"testing"

	"government/eval"
)

type recorder struct{ errors []string }

func (r *recorder) Helper() {}
func (r *recorder) Errorf(format string, args ...any) {
	r.errors = append(r.errors, fmt.Sprintf(format, args...))
}

func TestAnnotationsAttachToPrecedingForm(t *testing.T) {
	f, err := Parse("t.gov.lisp", `
(define x 1)
x            ;=> 1
(car
  '())
; a plain comment
             ;=! needs a pair
(+ x 1) (+ x 2) ;=> 3
`)
	if err != nil {
		t.Fatal(err)
	}
	got := []string{}
	for _, c := range f.Cases {
		got = append(got, fmt.Sprintf("%v %d %q", c.Pos, c.Expect, c.Want))
	}
	want := []string{
		`2:1 0 ""`,
		`3:1 1 "1"`,
		`4:1 2 "needs a pair"`,
		`8:1 0 ""`,
		`8:9 1 "3"`,
	}
	if strings.Join(got, "\n") != strings.Join(want, "\n") {
		t.Errorf("cases:\n%s\nwant:\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
	}
}

func TestParseErrors(t *testing.T) {
	tests := []struct{ src, want string }{
		{";=> 1\n(+ 1 0)", "t.gov.lisp:1:1: annotation before any form"},
		{"1 ;=> 1\n;=> 1", "t.gov.lisp:2:1: second annotation for the form at 1:1"},
		{"1 ;=>", "t.gov.lisp:1:3: empty annotation"},
		{"(1", "t.gov.lisp:1:1: unterminated list"},
	}
	for _, tt := range tests {
		if _, err := Parse("t.gov.lisp", tt.src); err == nil || err.Error() != tt.want {
			t.Errorf("Parse(%q) error = %v, want %q", tt.src, err, tt.want)
		}
	}
}

// Every kind of mismatch is reported, with the failing subexpression's
// position, and evaluation continues past failures.
func TestRunReportsFailures(t *testing.T) {
	f, err := Parse("t.gov.lisp", `(+ 1 1)      ;=> 3
(+ 1 1)      ;=! overflow
(car '())    ;=> 1
(/ 1 0)      ;=! not a function
(+ 1
   (car 5))
(+ 2 2)      ;=> 4
`)
	if err != nil {
		t.Fatal(err)
	}
	r := &recorder{}
	Run(r, f, eval.NewTopLevel())
	want := []string{
		"t.gov.lisp:1:1: (+ 1 1)\n\tgot  2\n\twant 3",
		"t.gov.lisp:2:1: (+ 1 1)\n\tgot value 2, want an error containing \"overflow\"",
		"t.gov.lisp:3:1: (car (quote ()))\n\tunexpected error E-TYPE: car needs a pair, got () (at 3:1: (car (quote ())))",
		"t.gov.lisp:4:1: (/ 1 0)\n\tgot error E-ARITH: division by zero (at 4:1: (/ 1 0)), want one containing \"not a function\"",
		"t.gov.lisp:5:1: (+ 1 (car 5))\n\tunexpected error E-TYPE: car needs a pair, got 5 (at 6:4: (car 5))",
	}
	if strings.Join(r.errors, "\n---\n") != strings.Join(want, "\n---\n") {
		t.Errorf("reported:\n%s\n\nwant:\n%s", strings.Join(r.errors, "\n---\n"), strings.Join(want, "\n---\n"))
	}
}

func TestWithForms(t *testing.T) {
	f, err := Parse("t.gov.lisp", "(+ 1 1) ;=> 2\n(car 1) ;=! needs a pair\n")
	if err != nil {
		t.Fatal(err)
	}
	other, err := sexpr.ReadAll("(+ 1 1) (car 1)")
	if err != nil {
		t.Fatal(err)
	}
	g, err := f.WithForms(other)
	if err != nil {
		t.Fatal(err)
	}
	if g.Cases[0].Form != other[0] || g.Cases[1].Want != "needs a pair" || f.Cases[0].Form == other[0] {
		t.Error("WithForms did not swap the forms and keep the annotations")
	}
	if _, err := f.WithForms(other[:1]); err == nil {
		t.Error("a form count mismatch was accepted")
	}
}