lf/comp_test.go

56 lines
1.3 KiB
Go
Raw Normal View History

2016-09-12 20:33:52 +00:00
package main
import (
"reflect"
"testing"
)
func TestMatchLongest(t *testing.T) {
tests := []struct {
2016-12-18 15:01:45 +00:00
s1 string
s2 string
exp string
2016-09-12 20:33:52 +00:00
}{
{"", "", ""},
{"", "foo", ""},
{"foo", "", ""},
{"foo", "bar", ""},
{"foo", "foobar", "foo"},
{"foo", "barfoo", ""},
{"foobar", "foobaz", "fooba"},
}
for _, test := range tests {
2016-12-18 15:01:45 +00:00
if got := matchLongest(test.s1, test.s2); got != test.exp {
t.Errorf("at input '%s' and '%s' expected '%s' but got '%s'", test.s1, test.s2, test.exp, got)
2016-09-12 20:33:52 +00:00
}
}
}
func TestMatchWord(t *testing.T) {
tests := []struct {
s string
words []string
matches []string
longest string
}{
2016-09-12 20:40:17 +00:00
{"", nil, nil, ""},
{"", []string{"foo", "bar", "baz"}, []string{"foo", "bar", "baz"}, ""},
2016-09-12 20:33:52 +00:00
{"fo", []string{"foo", "bar", "baz"}, []string{"foo"}, "foo "},
{"ba", []string{"foo", "bar", "baz"}, []string{"bar", "baz"}, "ba"},
{"fo", []string{"bar", "baz"}, nil, "fo"},
}
for _, test := range tests {
m, l := matchWord(test.s, test.words)
if !reflect.DeepEqual(m, test.matches) {
t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.matches, m)
}
if l != test.longest {
t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.longest, l)
}
}
}