|
1 | 1 | package edlib
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "reflect" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/hbollon/go-edlib"
|
7 | 8 | )
|
8 | 9 |
|
| 10 | +var strList []string |
| 11 | + |
| 12 | +func init() { |
| 13 | + strList = []string{ |
| 14 | + "test", |
| 15 | + "tester", |
| 16 | + "tests", |
| 17 | + "testers", |
| 18 | + "testing", |
| 19 | + "tsting", |
| 20 | + "sting", |
| 21 | + } |
| 22 | +} |
| 23 | + |
9 | 24 | func TestStringsSimilarity(t *testing.T) {
|
10 | 25 | type args struct {
|
11 | 26 | str1 string
|
@@ -118,3 +133,96 @@ func TestStringsSimilarity(t *testing.T) {
|
118 | 133 | })
|
119 | 134 | }
|
120 | 135 | }
|
| 136 | + |
| 137 | +func TestFuzzySearch(t *testing.T) { |
| 138 | + type args struct { |
| 139 | + str string |
| 140 | + strList []string |
| 141 | + algo edlib.AlgorithMethod |
| 142 | + } |
| 143 | + tests := []struct { |
| 144 | + name string |
| 145 | + args args |
| 146 | + want string |
| 147 | + }{ |
| 148 | + {"FuzzySearch 'testing'", args{"testnig", strList, edlib.Levenshtein}, "testing"}, |
| 149 | + } |
| 150 | + for _, tt := range tests { |
| 151 | + t.Run(tt.name, func(t *testing.T) { |
| 152 | + if got := edlib.FuzzySearch(tt.args.str, tt.args.strList, tt.args.algo); got != tt.want { |
| 153 | + t.Errorf("FuzzySearch() = %v, want %v", got, tt.want) |
| 154 | + } |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestFuzzySearchThreshold(t *testing.T) { |
| 160 | + type args struct { |
| 161 | + str string |
| 162 | + strList []string |
| 163 | + minSim float32 |
| 164 | + algo edlib.AlgorithMethod |
| 165 | + } |
| 166 | + tests := []struct { |
| 167 | + name string |
| 168 | + args args |
| 169 | + want string |
| 170 | + }{ |
| 171 | + {"FuzzySearch 'testing'", args{"testnig", strList, 0.7, edlib.Levenshtein}, "testing"}, |
| 172 | + {"FuzzySearch 'testing'", args{"hello", strList, 0.7, edlib.Levenshtein}, ""}, |
| 173 | + } |
| 174 | + for _, tt := range tests { |
| 175 | + t.Run(tt.name, func(t *testing.T) { |
| 176 | + if got := edlib.FuzzySearchThreshold(tt.args.str, tt.args.strList, tt.args.minSim, tt.args.algo); got != tt.want { |
| 177 | + t.Errorf("FuzzySearchThreshold() = %v, want %v", got, tt.want) |
| 178 | + } |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func TestFuzzySearchSet(t *testing.T) { |
| 184 | + type args struct { |
| 185 | + str string |
| 186 | + strList []string |
| 187 | + quantity int |
| 188 | + algo edlib.AlgorithMethod |
| 189 | + } |
| 190 | + tests := []struct { |
| 191 | + name string |
| 192 | + args args |
| 193 | + want []string |
| 194 | + }{ |
| 195 | + {"FuzzySearch 'testing'", args{"testnig", strList, 3, edlib.Levenshtein}, []string{"testing", "test", "tester"}}, |
| 196 | + } |
| 197 | + for _, tt := range tests { |
| 198 | + t.Run(tt.name, func(t *testing.T) { |
| 199 | + if got := edlib.FuzzySearchSet(tt.args.str, tt.args.strList, tt.args.quantity, tt.args.algo); !reflect.DeepEqual(got, tt.want) { |
| 200 | + t.Errorf("FuzzySearchSet() = %v, want %v", got, tt.want) |
| 201 | + } |
| 202 | + }) |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +func TestFuzzySearchSetThreshold(t *testing.T) { |
| 207 | + type args struct { |
| 208 | + str string |
| 209 | + strList []string |
| 210 | + quantity int |
| 211 | + minSim float32 |
| 212 | + algo edlib.AlgorithMethod |
| 213 | + } |
| 214 | + tests := []struct { |
| 215 | + name string |
| 216 | + args args |
| 217 | + want []string |
| 218 | + }{ |
| 219 | + {"FuzzySearch 'testing'", args{"testnig", strList, 3, 0.7, edlib.Levenshtein}, []string{"testing", "", ""}}, |
| 220 | + } |
| 221 | + for _, tt := range tests { |
| 222 | + t.Run(tt.name, func(t *testing.T) { |
| 223 | + if got := edlib.FuzzySearchSetThreshold(tt.args.str, tt.args.strList, tt.args.quantity, tt.args.minSim, tt.args.algo); !reflect.DeepEqual(got, tt.want) { |
| 224 | + t.Errorf("FuzzySearchSetThreshold() = %v, want %v", got, tt.want) |
| 225 | + } |
| 226 | + }) |
| 227 | + } |
| 228 | +} |
0 commit comments