From 4dc517401a2d01d34455498ff805e81f7f4f2674 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Tue, 13 Nov 2012 22:18:06 +0000 Subject: Smartcase matching for vomnibar. Vomnibar queries are case insensitive, unless the query contains a capital letter. --- background_scripts/completion.coffee | 5 ++++- tests/unit_tests/completion_test.coffee | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 28bb1afa..c99a94cb 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -355,7 +355,10 @@ RegexpCache = # Avoid cost of constructing new strings if prefix/suffix are empty (which is expected to be a common case). regexpString = prefix + regexpString if prefix regexpString = regexpString + suffix if suffix - @cache[regexpString] ||= new RegExp(regexpString, "i") + return @cache[regexpString] if @cache[regexpString] + # Smartcase: regexp is case insensitive, unless `string` contains a capital letter. + modifier = if /[A-Z]/.test string then "" else "i" + @cache[regexpString] = new RegExp(regexpString, modifier) # Provides cached access to Chrome's history. As the user browses to new pages, we add those pages to this # history cache. diff --git a/tests/unit_tests/completion_test.coffee b/tests/unit_tests/completion_test.coffee index 08ce46c2..8b6ba3ea 100644 --- a/tests/unit_tests/completion_test.coffee +++ b/tests/unit_tests/completion_test.coffee @@ -258,16 +258,24 @@ context "suggestions", context "RankingUtils", should "do a case insensitive match", -> - assert.isTrue RankingUtils.matches(["aRi"], "MARIO", "MARio") + assert.isTrue RankingUtils.matches(["ari"], "maRio", "mario") should "do a case insensitive match on full term", -> - assert.isTrue RankingUtils.matches(["MaRiO"], "MARIO", "MARio") + assert.isTrue RankingUtils.matches(["mario"], "MARIO", "MARio") should "do a case insensitive match on more than just two terms", -> - assert.isTrue RankingUtils.matches(["aRi"], "DOES_NOT_MATCH", "DOES_NOT_MATCH_EITHER", "MARio") + assert.isTrue RankingUtils.matches(["ari"], "DOES_NOT_MATCH", "DOES_NOT_MATCH_EITHER", "MARio") + + should "do a smartcase match", -> + assert.isTrue RankingUtils.matches(["Mar"], "Mario", "mario") + assert.isFalse RankingUtils.matches(["Mar"], "mario", "mario") + + should "do a smartcase match on full term", -> + assert.isTrue RankingUtils.matches(["Mario"], "Mario") + assert.isFalse RankingUtils.matches(["Mario"], "mario") should "do case insensitive word relevancy (matching)", -> - assert.isTrue RankingUtils.wordRelevancy(["aRi"], "MARIO", "MARio") > 0.0 + assert.isTrue RankingUtils.wordRelevancy(["ari"], "MARIO", "MARio") > 0.0 should "do case insensitive word relevancy (not matching)", -> assert.isTrue RankingUtils.wordRelevancy(["DOES_NOT_MATCH"], "MARIO", "MARio") == 0.0 -- cgit v1.2.3 From 9225a09cec3db2213a1324f20ec6195ad281dbef Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Wed, 14 Nov 2012 05:36:23 +0000 Subject: Clean up regexp unit tests. --- tests/unit_tests/completion_test.coffee | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/unit_tests/completion_test.coffee b/tests/unit_tests/completion_test.coffee index 8b6ba3ea..0d1b90c3 100644 --- a/tests/unit_tests/completion_test.coffee +++ b/tests/unit_tests/completion_test.coffee @@ -258,17 +258,17 @@ context "suggestions", context "RankingUtils", should "do a case insensitive match", -> - assert.isTrue RankingUtils.matches(["ari"], "maRio", "mario") + assert.isTrue RankingUtils.matches(["ari"], "maRio") should "do a case insensitive match on full term", -> - assert.isTrue RankingUtils.matches(["mario"], "MARIO", "MARio") + assert.isTrue RankingUtils.matches(["mario"], "MARio") - should "do a case insensitive match on more than just two terms", -> + should "do a case insensitive match on several terms", -> assert.isTrue RankingUtils.matches(["ari"], "DOES_NOT_MATCH", "DOES_NOT_MATCH_EITHER", "MARio") should "do a smartcase match", -> - assert.isTrue RankingUtils.matches(["Mar"], "Mario", "mario") - assert.isFalse RankingUtils.matches(["Mar"], "mario", "mario") + assert.isTrue RankingUtils.matches(["Mar"], "Mario") + assert.isFalse RankingUtils.matches(["Mar"], "mario") should "do a smartcase match on full term", -> assert.isTrue RankingUtils.matches(["Mario"], "Mario") @@ -280,10 +280,10 @@ context "RankingUtils", should "do case insensitive word relevancy (not matching)", -> assert.isTrue RankingUtils.wordRelevancy(["DOES_NOT_MATCH"], "MARIO", "MARio") == 0.0 - should "every term must match at least one thing (matching)", -> + should "every query term must match at least one thing (matching)", -> assert.isTrue RankingUtils.matches(["cat", "dog"], "catapult", "hound dog") - should "every term must match at least one thing (not matching)", -> + should "every query term must match at least one thing (not matching)", -> assert.isTrue not RankingUtils.matches(["cat", "dog", "wolf"], "catapult", "hound dog") context "RegexpCache", -- cgit v1.2.3 From 049cf4314fc2e97e0f5b09cd8a8d066e2c04eb8a Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Wed, 14 Nov 2012 05:59:21 +0000 Subject: RegexpCache: reorganise and improve tests. --- background_scripts/completion.coffee | 6 ++---- tests/unit_tests/completion_test.coffee | 10 +++++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index c99a94cb..951d0756 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -355,10 +355,8 @@ RegexpCache = # Avoid cost of constructing new strings if prefix/suffix are empty (which is expected to be a common case). regexpString = prefix + regexpString if prefix regexpString = regexpString + suffix if suffix - return @cache[regexpString] if @cache[regexpString] - # Smartcase: regexp is case insensitive, unless `string` contains a capital letter. - modifier = if /[A-Z]/.test string then "" else "i" - @cache[regexpString] = new RegExp(regexpString, modifier) + # Smartcase: Regexp is case insensitive, unless `string` contains a capital letter (testing `string`, not `regexpString`). + @cache[regexpString] ||= new RegExp regexpString, (if /[A-Z]/.test string then "" else "i") # Provides cached access to Chrome's history. As the user browses to new pages, we add those pages to this # history cache. diff --git a/tests/unit_tests/completion_test.coffee b/tests/unit_tests/completion_test.coffee index 0d1b90c3..fb267f63 100644 --- a/tests/unit_tests/completion_test.coffee +++ b/tests/unit_tests/completion_test.coffee @@ -266,10 +266,18 @@ context "RankingUtils", should "do a case insensitive match on several terms", -> assert.isTrue RankingUtils.matches(["ari"], "DOES_NOT_MATCH", "DOES_NOT_MATCH_EITHER", "MARio") - should "do a smartcase match", -> + should "do a smartcase match (positive)", -> assert.isTrue RankingUtils.matches(["Mar"], "Mario") + + should "do a smartcase match (negative)", -> assert.isFalse RankingUtils.matches(["Mar"], "mario") + should "do a match with regexp meta-characters (positive)", -> + assert.isTrue RankingUtils.matches(["ma.io"], "ma.io") + + should "do a match with regexp meta-characters (negative)", -> + assert.isFalse RankingUtils.matches(["ma.io"], "mario") + should "do a smartcase match on full term", -> assert.isTrue RankingUtils.matches(["Mario"], "Mario") assert.isFalse RankingUtils.matches(["Mario"], "mario") -- cgit v1.2.3