| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestGetOptWords(t *testing.T) { |
| 9 |
tests := []struct { |
| 10 |
opts any |
| 11 |
exp []string |
| 12 |
}{ |
| 13 |
{struct{ feature bool }{}, []string{"feature", "feature!", "nofeature"}}, |
| 14 |
{struct{ feature int }{}, []string{"feature"}}, |
| 15 |
{struct{ feature string }{}, []string{"feature"}}, |
| 16 |
{struct{ feature []string }{}, []string{"feature"}}, |
| 17 |
} |
| 18 |
|
| 19 |
for _, test := range tests { |
| 20 |
result := getOptWords(test.opts) |
| 21 |
if !reflect.DeepEqual(result, test.exp) { |
| 22 |
t.Errorf("at input '%#v' expected '%s' but got '%s'", test.opts, test.exp, result) |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func TestGetLocalOptWords(t *testing.T) { |
| 28 |
tests := []struct { |
| 29 |
localOpts any |
| 30 |
exp []string |
| 31 |
}{ |
| 32 |
{struct{ feature map[string]bool }{}, []string{"feature", "feature!", "nofeature"}}, |
| 33 |
{struct{ feature map[string]int }{}, []string{"feature"}}, |
| 34 |
{struct{ feature map[string]string }{}, []string{"feature"}}, |
| 35 |
{struct{ feature map[string][]string }{}, []string{"feature"}}, |
| 36 |
} |
| 37 |
|
| 38 |
for _, test := range tests { |
| 39 |
result := getLocalOptWords(test.localOpts) |
| 40 |
if !reflect.DeepEqual(result, test.exp) { |
| 41 |
t.Errorf("at input '%#v' expected '%s' but got '%s'", test.localOpts, test.exp, result) |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func TestGetLongest(t *testing.T) { |
| 47 |
tests := []struct { |
| 48 |
s1 string |
| 49 |
s2 string |
| 50 |
exp string |
| 51 |
}{ |
| 52 |
{"", "", ""}, |
| 53 |
{"", "foo", ""}, |
| 54 |
{"foo", "", ""}, |
| 55 |
{"foo", "bar", ""}, |
| 56 |
{"foo", "foobar", "foo"}, |
| 57 |
{"foo", "barfoo", ""}, |
| 58 |
{"foobar", "foobaz", "fooba"}, |
| 59 |
{"год", "гол", "го"}, |
| 60 |
} |
| 61 |
|
| 62 |
for _, test := range tests { |
| 63 |
if got := getLongest(test.s1, test.s2); got != test.exp { |
| 64 |
t.Errorf("at input '%s' and '%s' expected '%s' but got '%s'", test.s1, test.s2, test.exp, got) |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestMatchWord(t *testing.T) { |
| 70 |
tests := []struct { |
| 71 |
s string |
| 72 |
words []string |
| 73 |
matches []compMatch |
| 74 |
longest string |
| 75 |
}{ |
| 76 |
{"", nil, nil, ""}, |
| 77 |
{"", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}, {"bar", "bar"}, {"baz", "baz"}}, ""}, |
| 78 |
{"f", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}}, "foo "}, |
| 79 |
{"b", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "bar"}, {"baz", "baz"}}, "ba"}, |
| 80 |
{"fo", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}}, "foo "}, |
| 81 |
{"ba", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "bar"}, {"baz", "baz"}}, "ba"}, |
| 82 |
{"fo", []string{"bar", "baz"}, nil, "fo"}, |
| 83 |
} |
| 84 |
|
| 85 |
for _, test := range tests { |
| 86 |
matches, longest := matchWord(test.s, test.words) |
| 87 |
|
| 88 |
if !reflect.DeepEqual(matches, test.matches) { |
| 89 |
t.Errorf("at input '%s' with '%s' expected '%v' but got '%v'", test.s, test.words, test.matches, matches) |
| 90 |
} |
| 91 |
|
| 92 |
if longest != test.longest { |
| 93 |
t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.longest, longest) |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func TestMatchList(t *testing.T) { |
| 99 |
tests := []struct { |
| 100 |
s string |
| 101 |
words []string |
| 102 |
matches []compMatch |
| 103 |
longest string |
| 104 |
}{ |
| 105 |
{"", nil, nil, ""}, |
| 106 |
{"", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}, {"bar", "bar"}, {"baz", "baz"}}, ""}, |
| 107 |
{"f", []string{"bar", "baz"}, nil, "f"}, |
| 108 |
{"f", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}}, "foo"}, |
| 109 |
{"b", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "bar"}, {"baz", "baz"}}, "ba"}, |
| 110 |
{"ba", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "bar"}, {"baz", "baz"}}, "ba"}, |
| 111 |
{"foo", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}}, "foo "}, |
| 112 |
{"foo:", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "foo:bar"}, {"baz", "foo:baz"}}, "foo:ba"}, |
| 113 |
{"foo:f", []string{"foo", "bar", "baz"}, nil, "foo:f"}, |
| 114 |
{"foo:b", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "foo:bar"}, {"baz", "foo:baz"}}, "foo:ba"}, |
| 115 |
{"foo:ba", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "foo:bar"}, {"baz", "foo:baz"}}, "foo:ba"}, |
| 116 |
{"bar:b", []string{"foo", "bar", "baz"}, []compMatch{{"baz", "bar:baz"}}, "bar:baz"}, |
| 117 |
{"bar:f", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "bar:foo"}}, "bar:foo"}, |
| 118 |
{"bar:foo", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "bar:foo"}}, "bar:foo "}, |
| 119 |
} |
| 120 |
|
| 121 |
for _, test := range tests { |
| 122 |
matches, longest := matchList(test.s, test.words) |
| 123 |
|
| 124 |
if !reflect.DeepEqual(matches, test.matches) { |
| 125 |
t.Errorf("at input '%s' with '%s' expected '%v' but got '%v'", test.s, test.words, test.matches, matches) |
| 126 |
} |
| 127 |
|
| 128 |
if longest != test.longest { |
| 129 |
t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.longest, longest) |
| 130 |
} |
| 131 |
} |
| 132 |
} |