ginger/parse/lex/lex_test.go

57 lines
1.2 KiB
Go
Raw Normal View History

2014-10-18 21:09:06 +00:00
package lex
import (
"bytes"
. "testing"
)
func TestLexer(t *T) {
m := map[string][]Token{
2014-10-18 16:25:16 +00:00
"": {{eof, ""}},
" \t": {{eof, ""}},
"a b c": {{BareString, "a"},
{BareString, "b"},
{BareString, "c"},
2014-10-18 16:25:16 +00:00
{eof, ""}},
"\"foo\" bar": {{QuotedString, "\"foo\""},
{BareString, "bar"},
2014-10-18 16:25:16 +00:00
{eof, ""}},
"\"foo\nbar\" baz": {{QuotedString, "\"foo\nbar\""},
{BareString, "baz"},
2014-10-18 16:25:16 +00:00
{eof, ""}},
"( foo bar ) baz": {{Open, "("},
{BareString, "foo"},
{BareString, "bar"},
{Close, ")"},
{BareString, "baz"},
2014-10-18 16:25:16 +00:00
{eof, ""}},
"((foo-bar))": {{Open, "("},
{Open, "("},
{BareString, "foo-bar"},
{Close, ")"},
{Close, ")"},
2014-10-18 16:25:16 +00:00
{eof, ""}},
"(\"foo\nbar\")": {{Open, "("},
{QuotedString, "\"foo\nbar\""},
{Close, ")"},
2014-10-18 16:25:16 +00:00
{eof, ""}},
}
for input, output := range m {
buf := bytes.NewBufferString(input)
l := NewLexer(buf)
for i := range output {
tok := l.Next()
if tok == nil {
2014-10-18 16:25:16 +00:00
if output[i].Type == eof {
continue
}
t.Fatalf("input: %q (%d) %#v != %#v", input, i, output[i], tok)
}
if *tok != output[i] {
t.Fatalf("input: %s (%d) %#v != %#v", input, i, output[i], tok)
}
}
}
}