initial checkin
authorClaus Gittinger <cg@exept.de>
Thu, 13 Jul 2017 15:17:46 +0200
changeset 1636 18f80ba74aa8
parent 1635 7fb3c947bad4
child 1637 39e2c06afe6c
initial checkin
RegressionTests__FuzzyMatcherTests.st
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RegressionTests__FuzzyMatcherTests.st	Thu Jul 13 15:17:46 2017 +0200
@@ -0,0 +1,229 @@
+"{ Encoding: utf8 }"
+
+"{ Package: 'stx:goodies/regression' }"
+
+"{ NameSpace: RegressionTests }"
+
+TestCase subclass:#FuzzyMatcherTests
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'tests-Regression-Collections-Utilities'
+!
+
+FuzzyMatcherTests comment:'Tests for FuzzyMatcher. '
+!
+
+
+!FuzzyMatcherTests methodsFor:'asserting'!
+
+assert: matcher scoreFor: aString equals: aBlockOrNumber
+
+	matcher match: aString ifScored: [ :score |
+		self assert: score equals: aBlockOrNumber value.
+		^ self	
+	].
+
+	self error: 'Expected to match: ', aString
+! !
+
+!FuzzyMatcherTests methodsFor:'running'!
+
+newMatcher
+
+	^ FuzzyMatcher new
+! !
+
+!FuzzyMatcherTests methodsFor:'tests'!
+
+testAllMatchingIn
+
+	| input output |
+	
+	input := #('0g' 'ogb' 'gat' 'dog' 'FroG' 'hog' 'goh' 'hgo').
+	output := FuzzyMatcher allMatching: 'og' in: input.
+	
+	self assert: output asArray equals: #('ogb' 'dog' 'FroG' 'hog')
+!
+
+testAssumptions
+
+	self
+	 	
+		assert: (((1 - 1) * 3) max: -9) 	equals: 0;
+	 	assert: ((2 - 1 - 1) negated) 		equals: 0;
+				
+		assert: ('a' findString: 'a' startingAt: 2 caseSensitive: false) equals: 0.
+!
+
+testChangePattern
+
+	| matcher |
+	
+	matcher := self newMatcher.
+	
+	self 
+	assert: matcher pattern equals: '';
+	assert: (matcher matches: 'abcdefghijkl');
+	assert: (matcher matches: '').
+	
+	matcher pattern: 'afg'.
+	
+	self 
+	assert: (matcher matches: 'abcdefghijkl');
+	deny: (matcher matches: '').
+	
+!
+
+testClassSideAPI
+
+	"if the protocol is changed, so should the class comment"
+	FuzzyMatcher class protocols detect: [ :p | p = #'utilities api' ].
+	
+	self 
+	
+		assert: (FuzzyMatcher allMatching: #a in: #(a b ab))
+		equals: #(a ab);
+	
+		assert: (FuzzyMatcher allMatching: #a in: { #a -> 1 . #b -> 2 . #acb -> 3 } by: [:each | each key ])
+		equals: { #a -> 1 . #acb -> 3 };
+		
+		assert: (FuzzyMatcher allSortedByScoreMatching: #b in: { #aaca .#aAaaB . #Aaab . #baaa . #aaba })
+		equals: #(baaa aaba Aaab aAaaB) 	
+		
+!
+
+testEmptyPattern
+
+	| matcher |
+	
+	matcher := self newMatcher.
+	
+	self
+	
+		assert: matcher pattern isEmpty; 
+		
+		assert: matcher scoreFor: '' 		equals: 0;
+		assert: matcher scoreFor: 'abc' 	equals: 0;
+		assert: matcher scoreFor: '   ' 	equals: 0.
+			
+			
+	
+!
+
+testIsSeperator
+
+        | matcher |
+        
+        matcher := self newMatcher.
+        
+        self 
+                assert:         (matcher isSeparator: $_);
+                assert:         (matcher isSeparator: $:);
+                deny:           (matcher isSeparator: $!!);
+                deny:           (matcher isSeparator: $a)
+
+    "Modified: / 13-07-2017 / 13:31:05 / cg"
+!
+
+testMatching
+
+        | matcher |
+        
+        matcher := self newMatcher.
+        matcher pattern: 'a'.
+        
+        self 
+                assert: (matcher matches: 'a');
+                assert: (matcher matches: String return, String tab, String lf, String space, 'a');
+                assert: (matcher matches: ',;:_-!!"#ยค%&/?+\()a').
+
+    "Modified: / 13-07-2017 / 12:45:24 / cg"
+!
+
+testScoreValues
+
+	| matcher |
+	
+	matcher := self newMatcher.
+	
+	matcher pattern: 'a'.
+	
+	self 
+	
+		assert: matcher 
+		scoreFor: 'a' 
+		equals: (matcher firstLetterBonus + matcher caseEqualBonus);
+		
+		assert: matcher 
+		scoreFor: 'A' 
+		equals: (matcher firstLetterBonus);
+		
+		assert: matcher 
+		scoreFor: 'ab' 
+		equals: (matcher firstLetterBonus + matcher caseEqualBonus + matcher unmatchedLetterPenalty);
+		
+		assert: matcher 
+		scoreFor: '1a' 
+		equals: (matcher leadingLetterPenalty + matcher unmatchedLetterPenalty + matcher caseEqualBonus);
+		
+		assert: matcher 
+		scoreFor: '12345a' 
+		equals: (matcher maxLeadingLetterPenalty + (5 * matcher unmatchedLetterPenalty ) + (matcher caseEqualBonus)).
+		
+	matcher pattern: 'ab'.
+	
+	self 
+	
+		assert: matcher 
+		scoreFor: 'ab' 
+		equals: [ "delayed bonus calculation since #indexscore is based on state after matching" 
+			matcher firstLetterBonus 
+			+  matcher adjacencyBonus 
+			+ (matcher caseEqualBonus * 2) 
+			+ (matcher adjacentCaseEqualBonus) 
+			+  matcher indexScore
+		];
+		
+		assert: matcher 
+		scoreFor: 'A:B' 
+		equals: (
+			matcher firstLetterBonus 
+			+ matcher separatorBonus 
+			+ matcher unmatchedLetterPenalty
+		);
+		
+		assert: matcher 
+		scoreFor: 'able' 
+		equals: [ 
+			matcher firstLetterBonus 
+			+  matcher adjacencyBonus 
+			+  matcher adjacentCaseEqualBonus 
+			+ (matcher caseEqualBonus * 2) 
+			+ (matcher unmatchedLetterPenalty * 2)
+			+  matcher indexScore
+		].
+!
+
+testSortedByScore
+
+	| input output |
+	
+	input := #('abc' 'a'  'whj' 'cab' 'cat' 'dog' 'ab').	
+		
+	output := FuzzyMatcher allSortedByScoreMatching: 'a' in: input.
+	
+	self assertCollection: output asArray equals: #('a' 'ab' 'abc' 'cab' 'cat')
+	
+! !
+
+!FuzzyMatcherTests class methodsFor:'documentation'!
+
+version
+    ^ '$Header$'
+!
+
+version_CVS
+    ^ '$Header$'
+! !
+