match - match_test.go
1 package match
2
3 import (
4 "errors"
5 "testing"
6 )
7
8 func TestMatch(t *testing.T) {
9 var (
10 matches [][]string
11 nomatches [][]string
12
13 sm = New[int]()
14 )
15
16 for n, test := range [...]struct {
17 Add string
18 Err error
19 Match []string
20 NoMatch []string
21 }{
22 { // 1
23 Add: "abc",
24 Match: []string{"abc"},
25 NoMatch: []string{"ab", "abd", "abcd"},
26 },
27 { // 1
28 Add: "abc",
29 Err: ErrAmbiguous,
30 },
31 { // 1
32 Add: "def",
33 Match: []string{"def"},
34 NoMatch: []string{"ab", "abd", "abcd", "de", "deg", "defg"},
35 },
36 } {
37 if err := sm.AddString(test.Add, n+1); !errors.Is(err, test.Err) {
38 t.Errorf("test %d: expecting error %v, got %v", n+1, test.Err, err)
39 } else {
40 matches = append(matches, test.Match)
41 nomatches = append(nomatches, test.NoMatch)
42
43 for m, toMatch := range matches {
44 for l, match := range toMatch {
45 if v := sm.Match(match); v != m+1 {
46 t.Errorf("test %d.%d.%d: expecting value %d, got %d", n+1, m+1, l+1, m+1, v)
47 }
48 }
49 }
50
51 for m, toNotMatch := range nomatches {
52 for l, match := range toNotMatch {
53 if v := sm.Match(match); v == m+1 {
54 t.Errorf("test %d.%d.%d: expecting to not get value %d, but did", n+1, m+1, l+1, v)
55 }
56 }
57 }
58 }
59 }
60 }
61
62 func TestState(t *testing.T) {
63 sm := New[int]()
64
65 sm.AddString("abcde", 1)
66
67 if state := sm.MatchState("b"); state != (State[int]{sm, 0}) {
68 t.Errorf("test 1: expecting state 0, got %d", state.state)
69 }
70
71 if state := sm.MatchState("a"); state != (State[int]{sm, 2}) {
72 t.Errorf("test 2: expecting state 2, got %d", state.state)
73 } else if state = state.MatchState("b"); state != (State[int]{sm, 3}) {
74 t.Errorf("test 3: expecting state 3, got %d", state.state)
75 } else if v := state.Match("c"); v != 0 {
76 t.Errorf("test 4: expecting value 0, got %d", v)
77 } else if v := state.Match("cde"); v != 1 {
78 t.Errorf("test 5: expecting value 1, got %d", v)
79 }
80 }
81