glob/lexer_test.go

193 lines
4.0 KiB
Go
Raw Permalink Normal View History

2015-12-25 21:08:54 +03:00
package glob
import (
"testing"
)
func TestLexGood(t *testing.T) {
2016-01-08 20:14:31 +03:00
for id, test := range []struct {
2015-12-25 21:08:54 +03:00
pattern string
items []item
}{
2016-05-14 22:26:18 +03:00
{
pattern: "",
items: []item{
item{item_eof, ""},
},
},
2015-12-25 21:08:54 +03:00
{
pattern: "hello",
items: []item{
item{item_text, "hello"},
item{item_eof, ""},
},
},
2016-05-14 22:08:32 +03:00
{
pattern: "/{rate,[0-9]]}*",
items: []item{
item{item_text, "/"},
item{item_terms_open, "{"},
item{item_text, "rate"},
item{item_separator, ","},
item{item_range_open, "["},
item{item_range_lo, "0"},
item{item_range_between, "-"},
item{item_range_hi, "9"},
item{item_range_close, "]"},
item{item_text, "]"},
item{item_terms_close, "}"},
item{item_any, "*"},
item{item_eof, ""},
},
},
2016-05-12 00:17:33 +03:00
{
pattern: "hello,world",
items: []item{
item{item_text, "hello,world"},
item{item_eof, ""},
},
},
{
pattern: "hello\\,world",
items: []item{
item{item_text, "hello,world"},
item{item_eof, ""},
},
},
{
pattern: "hello\\{world",
items: []item{
item{item_text, "hello{world"},
item{item_eof, ""},
},
},
2015-12-25 21:08:54 +03:00
{
pattern: "hello?",
items: []item{
item{item_text, "hello"},
item{item_single, "?"},
item{item_eof, ""},
},
},
{
2016-01-08 20:14:31 +03:00
pattern: "hellof*",
2015-12-25 21:08:54 +03:00
items: []item{
2016-01-08 20:14:31 +03:00
item{item_text, "hellof"},
2015-12-25 21:08:54 +03:00
item{item_any, "*"},
item{item_eof, ""},
},
},
{
pattern: "hello**",
items: []item{
item{item_text, "hello"},
2016-01-08 20:14:31 +03:00
item{item_super, "**"},
2015-12-25 21:08:54 +03:00
item{item_eof, ""},
},
},
{
2015-12-26 12:14:30 +03:00
pattern: "[日-語]",
2015-12-25 21:08:54 +03:00
items: []item{
item{item_range_open, "["},
2015-12-26 12:14:30 +03:00
item{item_range_lo, "日"},
2016-01-08 20:14:31 +03:00
item{item_range_between, "-"},
2015-12-26 12:14:30 +03:00
item{item_range_hi, "語"},
2015-12-25 21:08:54 +03:00
item{item_range_close, "]"},
item{item_eof, ""},
},
},
{
2015-12-26 12:14:30 +03:00
pattern: "[!日-語]",
2015-12-25 21:08:54 +03:00
items: []item{
item{item_range_open, "["},
2016-01-08 20:14:31 +03:00
item{item_not, "!"},
2015-12-26 12:14:30 +03:00
item{item_range_lo, "日"},
2016-01-08 20:14:31 +03:00
item{item_range_between, "-"},
2015-12-26 12:14:30 +03:00
item{item_range_hi, "語"},
2015-12-25 21:08:54 +03:00
item{item_range_close, "]"},
item{item_eof, ""},
},
},
{
2015-12-26 12:14:30 +03:00
pattern: "[日本語]",
2015-12-25 21:08:54 +03:00
items: []item{
item{item_range_open, "["},
2016-01-08 20:14:31 +03:00
item{item_text, "日本語"},
2015-12-25 21:08:54 +03:00
item{item_range_close, "]"},
item{item_eof, ""},
},
},
{
2015-12-26 12:14:30 +03:00
pattern: "[!日本語]",
2015-12-25 21:08:54 +03:00
items: []item{
item{item_range_open, "["},
2016-01-08 20:14:31 +03:00
item{item_not, "!"},
item{item_text, "日本語"},
2015-12-25 21:08:54 +03:00
item{item_range_close, "]"},
item{item_eof, ""},
},
},
2016-01-08 20:14:31 +03:00
{
pattern: "{a,b}",
items: []item{
item{item_terms_open, "{"},
item{item_text, "a"},
item{item_separator, ","},
item{item_text, "b"},
item{item_terms_close, "}"},
item{item_eof, ""},
},
},
2016-05-12 10:46:16 +03:00
{
pattern: "/{z,ab}*",
items: []item{
item{item_text, "/"},
item{item_terms_open, "{"},
item{item_text, "z"},
item{item_separator, ","},
item{item_text, "ab"},
item{item_terms_close, "}"},
item{item_any, "*"},
item{item_eof, ""},
},
},
2016-01-08 20:14:31 +03:00
{
pattern: "{[!日-語],*,?,{a,b,\\c}}",
items: []item{
item{item_terms_open, "{"},
item{item_range_open, "["},
item{item_not, "!"},
item{item_range_lo, "日"},
item{item_range_between, "-"},
item{item_range_hi, "語"},
item{item_range_close, "]"},
item{item_separator, ","},
item{item_any, "*"},
item{item_separator, ","},
item{item_single, "?"},
item{item_separator, ","},
item{item_terms_open, "{"},
item{item_text, "a"},
item{item_separator, ","},
item{item_text, "b"},
item{item_separator, ","},
item{item_text, "c"},
item{item_terms_close, "}"},
item{item_terms_close, "}"},
item{item_eof, ""},
},
},
2015-12-25 21:08:54 +03:00
} {
lexer := newLexer(test.pattern)
2016-01-08 20:14:31 +03:00
for i, exp := range test.items {
2015-12-25 21:08:54 +03:00
act := lexer.nextItem()
if act.t != exp.t {
2016-05-12 00:17:33 +03:00
t.Errorf("#%d %q: wrong %d-th item type: exp: %q; act: %q\n\t(%s vs %s)", id, test.pattern, i, exp.t, act.t, exp, act)
2015-12-25 21:08:54 +03:00
}
if act.s != exp.s {
2016-05-12 00:17:33 +03:00
t.Errorf("#%d %q: wrong %d-th item contents: exp: %q; act: %q\n\t(%s vs %s)", id, test.pattern, i, exp.s, act.s, exp, act)
2015-12-25 21:08:54 +03:00
}
}
}
}