1636
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
|
3 |
"{ Package: 'stx:goodies/regression' }"
|
|
4 |
|
|
5 |
"{ NameSpace: RegressionTests }"
|
|
6 |
|
|
7 |
TestCase subclass:#FuzzyMatcherTests
|
|
8 |
instanceVariableNames:''
|
|
9 |
classVariableNames:''
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'tests-Regression-Collections-Utilities'
|
|
12 |
!
|
|
13 |
|
|
14 |
FuzzyMatcherTests comment:'Tests for FuzzyMatcher. '
|
|
15 |
!
|
|
16 |
|
|
17 |
|
|
18 |
!FuzzyMatcherTests methodsFor:'asserting'!
|
|
19 |
|
|
20 |
assert: matcher scoreFor: aString equals: aBlockOrNumber
|
|
21 |
|
|
22 |
matcher match: aString ifScored: [ :score |
|
|
23 |
self assert: score equals: aBlockOrNumber value.
|
|
24 |
^ self
|
|
25 |
].
|
|
26 |
|
|
27 |
self error: 'Expected to match: ', aString
|
|
28 |
! !
|
|
29 |
|
|
30 |
!FuzzyMatcherTests methodsFor:'running'!
|
|
31 |
|
|
32 |
newMatcher
|
|
33 |
|
|
34 |
^ FuzzyMatcher new
|
|
35 |
! !
|
|
36 |
|
|
37 |
!FuzzyMatcherTests methodsFor:'tests'!
|
|
38 |
|
|
39 |
testAllMatchingIn
|
|
40 |
|
|
41 |
| input output |
|
|
42 |
|
|
43 |
input := #('0g' 'ogb' 'gat' 'dog' 'FroG' 'hog' 'goh' 'hgo').
|
|
44 |
output := FuzzyMatcher allMatching: 'og' in: input.
|
|
45 |
|
|
46 |
self assert: output asArray equals: #('ogb' 'dog' 'FroG' 'hog')
|
|
47 |
!
|
|
48 |
|
|
49 |
testAssumptions
|
|
50 |
|
|
51 |
self
|
|
52 |
|
|
53 |
assert: (((1 - 1) * 3) max: -9) equals: 0;
|
|
54 |
assert: ((2 - 1 - 1) negated) equals: 0;
|
|
55 |
|
|
56 |
assert: ('a' findString: 'a' startingAt: 2 caseSensitive: false) equals: 0.
|
|
57 |
!
|
|
58 |
|
|
59 |
testChangePattern
|
|
60 |
|
|
61 |
| matcher |
|
|
62 |
|
|
63 |
matcher := self newMatcher.
|
|
64 |
|
|
65 |
self
|
|
66 |
assert: matcher pattern equals: '';
|
|
67 |
assert: (matcher matches: 'abcdefghijkl');
|
|
68 |
assert: (matcher matches: '').
|
|
69 |
|
|
70 |
matcher pattern: 'afg'.
|
|
71 |
|
|
72 |
self
|
|
73 |
assert: (matcher matches: 'abcdefghijkl');
|
|
74 |
deny: (matcher matches: '').
|
|
75 |
|
|
76 |
!
|
|
77 |
|
|
78 |
testClassSideAPI
|
|
79 |
|
|
80 |
"if the protocol is changed, so should the class comment"
|
|
81 |
FuzzyMatcher class protocols detect: [ :p | p = #'utilities api' ].
|
|
82 |
|
|
83 |
self
|
|
84 |
|
|
85 |
assert: (FuzzyMatcher allMatching: #a in: #(a b ab))
|
|
86 |
equals: #(a ab);
|
|
87 |
|
|
88 |
assert: (FuzzyMatcher allMatching: #a in: { #a -> 1 . #b -> 2 . #acb -> 3 } by: [:each | each key ])
|
|
89 |
equals: { #a -> 1 . #acb -> 3 };
|
|
90 |
|
|
91 |
assert: (FuzzyMatcher allSortedByScoreMatching: #b in: { #aaca .#aAaaB . #Aaab . #baaa . #aaba })
|
|
92 |
equals: #(baaa aaba Aaab aAaaB)
|
|
93 |
|
|
94 |
!
|
|
95 |
|
|
96 |
testEmptyPattern
|
1791
|
97 |
| matcher |
|
1636
|
98 |
|
1791
|
99 |
matcher := self newMatcher.
|
|
100 |
|
|
101 |
self
|
|
102 |
|
|
103 |
assert: matcher pattern isEmpty;
|
|
104 |
|
|
105 |
assert: matcher scoreFor: '' equals: 0;
|
|
106 |
assert: matcher scoreFor: 'abc' equals: -3;
|
1792
|
107 |
assert: matcher scoreFor: ' ' equals: -3.
|
1636
|
108 |
!
|
|
109 |
|
|
110 |
testIsSeperator
|
|
111 |
|
|
112 |
| matcher |
|
|
113 |
|
|
114 |
matcher := self newMatcher.
|
|
115 |
|
|
116 |
self
|
|
117 |
assert: (matcher isSeparator: $_);
|
|
118 |
assert: (matcher isSeparator: $:);
|
|
119 |
deny: (matcher isSeparator: $!!);
|
|
120 |
deny: (matcher isSeparator: $a)
|
|
121 |
|
|
122 |
"Modified: / 13-07-2017 / 13:31:05 / cg"
|
|
123 |
!
|
|
124 |
|
|
125 |
testMatching
|
|
126 |
|
|
127 |
| matcher |
|
|
128 |
|
|
129 |
matcher := self newMatcher.
|
|
130 |
matcher pattern: 'a'.
|
|
131 |
|
|
132 |
self
|
|
133 |
assert: (matcher matches: 'a');
|
|
134 |
assert: (matcher matches: String return, String tab, String lf, String space, 'a');
|
|
135 |
assert: (matcher matches: ',;:_-!!"#¤%&/?+\()a').
|
|
136 |
|
|
137 |
"Modified: / 13-07-2017 / 12:45:24 / cg"
|
|
138 |
!
|
|
139 |
|
|
140 |
testScoreValues
|
|
141 |
|
|
142 |
| matcher |
|
|
143 |
|
|
144 |
matcher := self newMatcher.
|
|
145 |
|
|
146 |
matcher pattern: 'a'.
|
|
147 |
|
|
148 |
self
|
|
149 |
|
|
150 |
assert: matcher
|
|
151 |
scoreFor: 'a'
|
|
152 |
equals: (matcher firstLetterBonus + matcher caseEqualBonus);
|
|
153 |
|
|
154 |
assert: matcher
|
|
155 |
scoreFor: 'A'
|
|
156 |
equals: (matcher firstLetterBonus);
|
|
157 |
|
|
158 |
assert: matcher
|
|
159 |
scoreFor: 'ab'
|
|
160 |
equals: (matcher firstLetterBonus + matcher caseEqualBonus + matcher unmatchedLetterPenalty);
|
|
161 |
|
|
162 |
assert: matcher
|
|
163 |
scoreFor: '1a'
|
|
164 |
equals: (matcher leadingLetterPenalty + matcher unmatchedLetterPenalty + matcher caseEqualBonus);
|
|
165 |
|
|
166 |
assert: matcher
|
|
167 |
scoreFor: '12345a'
|
|
168 |
equals: (matcher maxLeadingLetterPenalty + (5 * matcher unmatchedLetterPenalty ) + (matcher caseEqualBonus)).
|
|
169 |
|
|
170 |
matcher pattern: 'ab'.
|
|
171 |
|
|
172 |
self
|
|
173 |
|
|
174 |
assert: matcher
|
|
175 |
scoreFor: 'ab'
|
|
176 |
equals: [ "delayed bonus calculation since #indexscore is based on state after matching"
|
|
177 |
matcher firstLetterBonus
|
|
178 |
+ matcher adjacencyBonus
|
|
179 |
+ (matcher caseEqualBonus * 2)
|
|
180 |
+ (matcher adjacentCaseEqualBonus)
|
|
181 |
+ matcher indexScore
|
|
182 |
];
|
|
183 |
|
|
184 |
assert: matcher
|
|
185 |
scoreFor: 'A:B'
|
|
186 |
equals: (
|
|
187 |
matcher firstLetterBonus
|
|
188 |
+ matcher separatorBonus
|
|
189 |
+ matcher unmatchedLetterPenalty
|
|
190 |
);
|
|
191 |
|
|
192 |
assert: matcher
|
|
193 |
scoreFor: 'able'
|
|
194 |
equals: [
|
|
195 |
matcher firstLetterBonus
|
|
196 |
+ matcher adjacencyBonus
|
|
197 |
+ matcher adjacentCaseEqualBonus
|
|
198 |
+ (matcher caseEqualBonus * 2)
|
|
199 |
+ (matcher unmatchedLetterPenalty * 2)
|
|
200 |
+ matcher indexScore
|
|
201 |
].
|
|
202 |
!
|
|
203 |
|
|
204 |
testSortedByScore
|
|
205 |
|
|
206 |
| input output |
|
|
207 |
|
|
208 |
input := #('abc' 'a' 'whj' 'cab' 'cat' 'dog' 'ab').
|
|
209 |
|
|
210 |
output := FuzzyMatcher allSortedByScoreMatching: 'a' in: input.
|
|
211 |
|
|
212 |
self assertCollection: output asArray equals: #('a' 'ab' 'abc' 'cab' 'cat')
|
|
213 |
|
|
214 |
! !
|
|
215 |
|
|
216 |
!FuzzyMatcherTests class methodsFor:'documentation'!
|
|
217 |
|
|
218 |
version
|
|
219 |
^ '$Header$'
|
|
220 |
!
|
|
221 |
|
|
222 |
version_CVS
|
|
223 |
^ '$Header$'
|
|
224 |
! !
|
|
225 |
|