2014-10-18 21:09:06 +00:00
|
|
|
package lex
|
2014-10-18 16:04:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
. "testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLexer(t *T) {
|
|
|
|
m := map[string][]Token{
|
2014-10-18 16:25:16 +00:00
|
|
|
"": {{eof, ""}},
|
|
|
|
" \t": {{eof, ""}},
|
2014-10-18 16:04:48 +00:00
|
|
|
"a b c": {{BareString, "a"},
|
|
|
|
{BareString, "b"},
|
|
|
|
{BareString, "c"},
|
2014-10-18 16:25:16 +00:00
|
|
|
{eof, ""}},
|
2014-10-18 16:04:48 +00:00
|
|
|
"\"foo\" bar": {{QuotedString, "\"foo\""},
|
|
|
|
{BareString, "bar"},
|
2014-10-18 16:25:16 +00:00
|
|
|
{eof, ""}},
|
2014-10-18 16:04:48 +00:00
|
|
|
"\"foo\nbar\" baz": {{QuotedString, "\"foo\nbar\""},
|
|
|
|
{BareString, "baz"},
|
2014-10-18 16:25:16 +00:00
|
|
|
{eof, ""}},
|
2014-10-18 21:24:42 +00:00
|
|
|
"( foo bar ) baz": {{Open, "("},
|
2014-10-18 16:04:48 +00:00
|
|
|
{BareString, "foo"},
|
|
|
|
{BareString, "bar"},
|
2014-10-18 21:24:42 +00:00
|
|
|
{Close, ")"},
|
2014-10-18 16:04:48 +00:00
|
|
|
{BareString, "baz"},
|
2014-10-18 16:25:16 +00:00
|
|
|
{eof, ""}},
|
2014-10-18 21:24:42 +00:00
|
|
|
"((foo-bar))": {{Open, "("},
|
|
|
|
{Open, "("},
|
2014-10-18 16:04:48 +00:00
|
|
|
{BareString, "foo-bar"},
|
2014-10-18 21:24:42 +00:00
|
|
|
{Close, ")"},
|
|
|
|
{Close, ")"},
|
2014-10-18 16:25:16 +00:00
|
|
|
{eof, ""}},
|
2014-10-18 21:24:42 +00:00
|
|
|
"(\"foo\nbar\")": {{Open, "("},
|
2014-10-18 16:04:48 +00:00
|
|
|
{QuotedString, "\"foo\nbar\""},
|
2014-10-18 21:24:42 +00:00
|
|
|
{Close, ")"},
|
2014-10-18 16:25:16 +00:00
|
|
|
{eof, ""}},
|
2014-10-18 16:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2014-10-18 16:04:48 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|