glob/lexer_test.go

137 lines
2.8 KiB
Go
Raw 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
}{
{
pattern: "hello",
items: []item{
item{item_text, "hello"},
item{item_eof, ""},
},
},
{
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, ""},
},
},
{
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-01-08 20:14:31 +03:00
t.Errorf("#%d wrong %d-th item type: exp: %v; act: %v (%s vs %s)", id, i, exp.t, act.t, exp, act)
2015-12-25 21:08:54 +03:00
break
}
if act.s != exp.s {
2016-01-08 20:14:31 +03:00
t.Errorf("#%d wrong %d-th item contents: exp: %q; act: %q (%s vs %s)", id, i, exp.s, act.s, exp, act)
2015-12-25 21:08:54 +03:00
break
}
}
}
}