Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added case sensitivity option and tests.

  • Loading branch information...
commit e7df16d03778073712c4a7508c5279aa684425c6 1 parent 4e021d8
@simple10 simple10 authored
Showing with 101 additions and 47 deletions.
  1. +3 −0  lib/redis_autocomplete.rb
  2. +98 −47 spec/redis_autocomplete_spec.rb
View
3  lib/redis_autocomplete.rb
@@ -3,6 +3,7 @@
class RedisAutocomplete
DEFAULT_DISALLOWED_CHARS = /[^a-zA-Z0-9_-]/
DEFAULT_TERMINAL = '+'
+ DEFAULT_CASE_SENSITIVITY = true
attr_reader :redis, :terminal
@@ -11,10 +12,12 @@ def initialize(set, opts = {})
@redis = Redis.new
@disallowed_chars = opts[:disallowed_chars] || DEFAULT_DISALLOWED_CHARS
@terminal = opts[:terminal] || DEFAULT_TERMINAL
+ @case_sensitive = opts[:case_sensitive].nil? ? DEFAULT_CASE_SENSITIVITY : opts[:case_sensitive]
end
def add_word(word)
w = word.gsub(@disallowed_chars, '')
+ w.downcase! if !@case_sensitive
(1..(w.length)).each { |i| @redis.zadd(@set, 0, w.slice(0, i)) }
@redis.zadd(@set, 0, "#{w}#{@terminal}")
end
View
145 spec/redis_autocomplete_spec.rb
@@ -3,67 +3,118 @@
#require 'redis_autocomplete'
describe RedisAutocomplete do
- before do
+ before :all do
@names = %w[
- ynes
- ynez
- yoko
- yolanda
- yolande
+ Ynes
+ Ynez
+ Yoko
+ Yolanda
+ Yolande
yolane
yolanthe
- aaren
- aarika
- abagael
- abagail
- catherine
- cathi
+ Aaren
+ Aarika
+ Abagael
+ Abagail
+ Catherine
+ Cathi
cathie
cathleen
cathlene
- cathrin
- cathrine
- cathryn
- cathy
- cathyleen
- cati
- catie
- catina
- catlaina
- catlee
- catlin
+ Cathrin
+ Cathrine
+ Cathryn
+ Cathy
+ Cathyleen
+ Cati
+ Catie
+ Catina
+ Catlaina
+ Catlee
+ Catlin
]
@set = :test_female_names
- @r = RedisAutocomplete.new(@set)
- #todo: drop female_names set
- @r.add_words(@names)
end
- describe "#suggest" do
- it "should include words matching prefix" do
- @r.suggest('c').should == %w[
- catherine
- cathi
- cathie
- cathleen
- cathlene
- cathrin
- cathrine
- cathryn
- cathy
- cathyleen
- ]
+ context "with default case sensitivity" do
+ before do
+ @r = RedisAutocomplete.new(@set)
+ @r.redis.zremrangebyscore(@set, 0, 0)
+ @r.add_words(@names)
end
- it "should not include words not matching prefix" do
- @r.suggest('cati').should_not include('cathy')
+
+ describe "#suggest" do
+ it "should include words matching prefix" do
+ @r.suggest('C').should == %w[
+ Catherine
+ Cathi
+ Cathrin
+ Cathrine
+ Cathryn
+ Cathy
+ Cathyleen
+ Cati
+ Catie
+ Catina
+ ]
+ end
+
+ it "should not include words not matching prefix" do
+ @r.suggest('Cati').should_not include('Cathy')
+ end
+
+ it "should not include uppercase when searching on lowercase" do
+ @r.suggest('Y').should_not include('yolane', 'yolanthe')
+ @r.suggest('Y').should == %w[Ynes Ynez Yoko Yolanda Yolande]
+ end
+
+ context "when a max count is supplied" do
+ it "should not include more than 10 matches" do
+ @r.suggest('C').length.should == 10
+ end
+
+ it "should not include more matches than the supplied count" do
+ @r.suggest('C', 4).length.should == 4
+ end
+ end
+ end
+ end
+
+ context "with :case_sensitive => false" do
+ before do
+ @r = RedisAutocomplete.new(@set, :case_sensitive => false)
+ @r.redis.zremrangebyscore(@set, 0, 0)
+ @r.add_words(@names)
end
- context "when a max count is supplied" do
- it "should not include more than 10 matches" do
- @r.suggest('c').length.should == 10
+ describe "#suggest" do
+ it "should include words matching prefix" do
+ @r.suggest('c').should == %w[
+ catherine
+ cathi
+ cathie
+ cathleen
+ cathlene
+ cathrin
+ cathrine
+ cathryn
+ cathy
+ cathyleen
+ ]
end
- it "should not include more matches than the supplied count" do
- @r.suggest('c', 4).length.should == 4
+
+ it "should not include words not matching prefix" do
+ @r.suggest('cati').should_not include('cathy')
+ end
+
+ context "when a max count is supplied" do
+ it "should not include more than 10 matches" do
+ @r.suggest('c').length.should == 10
+ end
+
+ it "should not include more matches than the supplied count" do
+ @r.suggest('c', 4).length.should == 4
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.