From d0157d93d24c8c7f1a86289efe29e203d98bb072 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Mon, 5 Nov 2012 09:01:39 +0000 Subject: Factor pushMatchingRanges, improve comments/tests 1. Factor out `pushMatchingRanges`: This then allows us to ... 2. Add unit tests for `pushMatchingRanges` In effect, these tests verify where matches are highlighted in suggestions. 3. Added Utils.zip. This helps simplify `pushMatchingRanges` unit tests. 4. Improve comments. --- tests/unit_tests/completion_test.coffee | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tests') diff --git a/tests/unit_tests/completion_test.coffee b/tests/unit_tests/completion_test.coffee index f978c57b..3de1d716 100644 --- a/tests/unit_tests/completion_test.coffee +++ b/tests/unit_tests/completion_test.coffee @@ -152,6 +152,34 @@ context "suggestions", suggestion = new Suggestion(["queryterm"], "tab", "http://ninjawords.com", "ninjawords", returns(1)) assert.equal -1, suggestion.generateHtml().indexOf("http://ninjawords.com") + should "extract ranges matching term (simple case, two matches)", -> + ranges = [] + [ one, two, three ] = [ "one", "two", "three" ] + suggestion = new Suggestion([], "", "", "", returns(1)) + suggestion.pushMatchingRanges("#{one}#{two}#{three}#{two}#{one}", two, ranges) + assert.equal 2, Utils.zip([ ranges, [ [3,6], [11,14] ] ]).filter((pair) -> pair[0][0] == pair[1][0] and pair[0][1] == pair[1][1]).length + + should "extract ranges matching term (two matches, one at start of string)", -> + ranges = [] + [ one, two, three ] = [ "one", "two", "three" ] + suggestion = new Suggestion([], "", "", "", returns(1)) + suggestion.pushMatchingRanges("#{two}#{three}#{two}#{one}", two, ranges) + assert.equal 2, Utils.zip([ ranges, [ [0,3], [8,11] ] ]).filter((pair) -> pair[0][0] == pair[1][0] and pair[0][1] == pair[1][1]).length + + should "extract ranges matching term (two matches, one at end of string)", -> + ranges = [] + [ one, two, three ] = [ "one", "two", "three" ] + suggestion = new Suggestion([], "", "", "", returns(1)) + suggestion.pushMatchingRanges("#{one}#{two}#{three}#{two}", two, ranges) + assert.equal 2, Utils.zip([ ranges, [ [3,6], [11,14] ] ]).filter((pair) -> pair[0][0] == pair[1][0] and pair[0][1] == pair[1][1]).length + + should "extract ranges matching term (no matches)", -> + ranges = [] + [ one, two, three ] = [ "one", "two", "three" ] + suggestion = new Suggestion([], "", "", "", returns(1)) + suggestion.pushMatchingRanges("#{one}#{two}#{three}#{two}#{one}", "does-not-match", ranges) + assert.equal 0, ranges.length + context "RankingUtils", should "do a case insensitive match", -> assert.isTrue RankingUtils.matches(["aRi"], "MARIO", "MARio") -- cgit v1.2.3