@ 69e3ad85cc0f4df2aca759df28acafb505578821 | history
package sexpr
import (
"reflect"
"strings"
"testing"
)
// notesOf reads src and lists its notes by each datum's position in
// reading order, counted from 0.
func notesOf(t *testing.T, src string) map[int]string {
t.Helper()
forms, err := ReadProgram(src)
if err != nil {
t.Fatal(err)
}
notes := map[int]string{}
i := 0
for _, f := range forms {
f.walk(func(d *Datum) {
if d.Note != "" {
notes[i] = d.Note
}
i++
})
}
return notes
}
func TestNotes(t *testing.T) {
tests := []struct {
name string
src string
want map[int]string
}{
{"leading comment", "; the answer\n42", map[int]string{0: "the answer"}},
{"trailing comment", "42 ; the answer\n7", map[int]string{0: "the answer"}},
// (+ 1 2): list 0, + 1, 1 2, 2 3. The list and the 2 end at the
// same place; the comment describes the whole list.
{"trailing a list", "(+ 1 2) ; sum\n", map[int]string{0: "sum"}},
{"inside a list", "(+ 1 ; one\n 2)", map[int]string{2: "one"}},
{"leading, nested", "(f\n ; the argument\n x)", map[int]string{2: "the argument"}},
{"a block of comments", ";; first\n;\n; third\n1", map[int]string{0: "first\n\nthird"}},
// (+ 1 1) is datums 0-3, (car 1) is 4-6.
{"test annotations are notes too", "(+ 1 1) ;=> 2\n(car 1) ;=! pair", map[int]string{0: "=> 2", 4: "=! pair"}},
// 'x reads as (quote x): list 0, quote 1, x 2.
{"quoted datum", "; data\n'x", map[int]string{0: "data"}},
// '(a . b) is (quote (a . b)): list 0, quote 1, list 2, a 3, . 4,
// b 5.
{"dotted tail", "'(a .\n ; tail\n b)", map[int]string{5: "tail"}},
// '(a . (b c)) is (quote (a b c)): the merged-away parentheses
// leave no datum, so the comment goes to b, datum 4.
{"merged dotted tail", "'(a .\n ; rest\n (b c))", map[int]string{4: "rest"}},
{"nothing follows", "1\n; dangling", map[int]string{}},
{"two forms", "; one\n1\n; two\n2", map[int]string{0: "one", 1: "two"}},
}
for _, tt := range tests {
if got := notesOf(t, tt.src); !reflect.DeepEqual(got, tt.want) {
t.Errorf("%s: %q gives %v, want %v", tt.name, tt.src, got, tt.want)
}
}
}
// shape prints a datum tree without positions or notes, to compare the
// reader's datums with DatumOf's.
func shape(d *Datum) string {
if d.Dot {
return "."
}
if len(d.Elems) == 0 {
return Print(d.Value)
}
parts := make([]string, len(d.Elems))
for i, e := range d.Elems {
parts[i] = shape(e)
}
return "[" + strings.Join(parts, " ") + "]"
}
func TestReaderDatumsMatchDatumOf(t *testing.T) {
for _, src := range []string{
"(+ 1 (* 2 3))", "'x", "''x", "'(1 . (2 . (3 . ())))", "'(a . 'b)", "'(a . b)",
`(f () T F "s" 1.5 -2)`, "'(a b . c)", "((lambda (x) x) 1)",
} {
forms, err := ReadProgram(src)
if err != nil {
t.Fatal(err)
}
for _, f := range forms {
if got, want := shape(f), shape(DatumOf(f.Value)); got != want {
t.Errorf("%s: reader gives %s, DatumOf gives %s", src, got, want)
}
}
}
}
func TestDatumPositions(t *testing.T) {
forms, err := ReadProgram("(define f\n '(a . \"s\"))")
if err != nil {
t.Fatal(err)
}
d := forms[0]
quoted := d.Elems[2]
checks := []struct {
name string
d *Datum
start, end Pos
}{
{"outer list", d, Pos{1, 1}, Pos{2, 14}},
{"define", d.Elems[0], Pos{1, 2}, Pos{1, 8}},
{"quoted list", quoted, Pos{2, 3}, Pos{2, 13}},
{"quote mark", quoted.Elems[0], Pos{2, 3}, Pos{2, 4}},
{"dot", quoted.Elems[1].Elems[1], Pos{2, 7}, Pos{2, 8}},
}
for _, c := range checks {
if c.d.Start != c.start || c.d.End != c.end {
t.Errorf("%s: %v-%v, want %v-%v", c.name, c.d.Start, c.d.End, c.start, c.end)
}
}
}
func TestLongNoteKeepsItsText(t *testing.T) {
long := strings.Repeat("abcdefghij ", 45)
got := notesOf(t, "; "+long+"\n1")
if got[0] != strings.TrimRight(long, " ") {
t.Errorf("note changed: %q", got[0])
}
}