@@ -11,6 +11,23 @@ var baseballStrings = []string{
11
11
"braves vs mets" ,
12
12
}
13
13
14
+ var moreBaseballStrings = []string {
15
+ "new york mets vs chicago cubs" ,
16
+ "chicago cubs at new york mets" ,
17
+ "atlanta braves vs pittsbugh pirates" ,
18
+ "new york yankees vs boston red sox" ,
19
+ }
20
+
21
+ var someEmptyStrings = []string {
22
+ "" ,
23
+ "new york mets vs chicago cubs" ,
24
+ "new york yankees vs boston red sox" ,
25
+ "" ,
26
+ "" ,
27
+ }
28
+
29
+ var someNullStrings = []string {}
30
+
14
31
func TestExtractOne (t * testing.T ) {
15
32
query1 := "new york mets at atlanta braves"
16
33
best1 , _ := ExtractOne (query1 , baseballStrings )
@@ -37,10 +54,63 @@ func TestExtractOne(t *testing.T) {
37
54
}
38
55
best6 , _ := ExtractOne (query5 , baseballStrings , customScorer )
39
56
assertMatch (t , query5 , baseballStrings [0 ], best6 .Match )
57
+
58
+ query7 := "los angeles dodgers vs san francisco giants"
59
+ scoreCutoff := 50
60
+ best7 , _ := ExtractOne (query7 , moreBaseballStrings , scoreCutoff )
61
+ if best7 != nil {
62
+ t .Error ("expecting to find no matches" )
63
+ }
64
+
65
+ query8 := "new york mets vs chicago cubs"
66
+ scoreCutoff = 100
67
+ best8 , _ := ExtractOne (query8 , moreBaseballStrings , scoreCutoff )
68
+ if best8 == nil {
69
+ t .Error ("expecting to find a match" )
70
+ }
71
+
72
+ query9 := "new york mets at chicago cubs"
73
+ best9 , _ := ExtractOne (query9 , someEmptyStrings )
74
+ assertMatch (t , query9 , someEmptyStrings [1 ], best9 .Match )
75
+
76
+ query10 := "a, b"
77
+ choices := []string {query10 }
78
+ expectedResult := new (MatchPair )
79
+ expectedResult .Match = query10
80
+ expectedResult .Score = 100
81
+ customScorer = func (s1 , s2 string ) int {
82
+ return Ratio (s1 , s2 )
83
+ }
84
+ partialScorer := func (s1 , s2 string ) int {
85
+ return PartialRatio (s1 , s2 )
86
+ }
87
+
88
+ res , _ := ExtractOne (query10 , choices , customScorer )
89
+ partialRes , _ := ExtractOne (query10 , choices , partialScorer )
90
+ if * res != * expectedResult {
91
+ t .Error ("simple match failed" )
92
+ }
93
+ if * partialRes != * expectedResult {
94
+ t .Error ("simple partial match failed" )
95
+ }
96
+ }
97
+
98
+ func TestDedupe (t * testing.T ) {
99
+ sliceWithDupes := []string {"Frodo Baggins" , "Tom Sawyer" , "Bilbo Baggin" , "Samuel L. Jackson" , "F. Baggins" , "Frody Baggins" , "Bilbo Baggins" }
100
+ res , _ := Dedupe (sliceWithDupes )
101
+ if len (res ) >= len (sliceWithDupes ) {
102
+ t .Error ("expecting Dedupe to remove at least one string from slice" )
103
+ }
104
+
105
+ sliceWithoutDupes := []string {"Tom" , "Dick" , "Harry" }
106
+ res2 , _ := Dedupe (sliceWithoutDupes )
107
+ if len (res2 ) != len (sliceWithoutDupes ) {
108
+ t .Error ("not expecting Dedupe to remove any strings from slice" )
109
+ }
40
110
}
41
111
42
112
func assertMatch (t * testing.T , query , expectedMatch , actualMatch string ) {
43
113
if expectedMatch != actualMatch {
44
- t .Errorf ("Expecting [%v] to find match of [%v]. Actual match: [%v]" , query , expectedMatch , actualMatch )
114
+ t .Errorf ("expecting [%v] to find match of [%v], actual match was [%v]" , query , expectedMatch , actualMatch )
45
115
}
46
116
}
0 commit comments