Permalink
Browse files

Adding specs for strings methods

  • Loading branch information...
1 parent a504449 commit 3c02111a0314e9ab6b8a3947d3f862facb2b26d2 @guilleiguaran committed Feb 22, 2011
Showing with 63 additions and 9 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 .rspec
  3. +5 −5 lib/fakeredis/keys.rb
  4. +4 −4 lib/fakeredis/strings.rb
  5. +52 −0 spec/strings.rb
View
1 .gitignore
@@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
+.rvmrc
View
1 .rspec
@@ -0,0 +1 @@
+-b -fn --color
View
10 lib/fakeredis/keys.rb
@@ -62,11 +62,11 @@ def ttl(key)
def type(key)
case value = @data[key]
- when nil: "none"
- when String: "string"
- when Hash: "hash"
- when Array: "list"
- when Set: "set"
+ when nil then "none"
+ when String then "string"
+ when Hash then "hash"
+ when Array then "list"
+ when Set then "set"
end
end
View
8 lib/fakeredis/strings.rb
@@ -14,7 +14,7 @@ def decr(key)
def decrby(key, by)
@data[key] = (@data[key] || "0")
- @data[key] = (@data[key].to_i - by).to_s
+ @data[key] = (@data[key].to_i - by.to_i).to_s
end
def get(key)
@@ -47,7 +47,7 @@ def incr(key)
def incrby(key, by)
@data[key] = (@data[key] || "0")
- @data[key] = (@data[key].to_i + by).to_s
+ @data[key] = (@data[key].to_i + by.to_i).to_s
end
def mget(keys)
@@ -89,8 +89,8 @@ def strlen(key)
end
- alias get []
- alias set []=
+ alias [] get
+ alias []= set
end
include StringsMethods
View
52 spec/strings.rb
@@ -0,0 +1,52 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe "FakeRedis::StringsMethods" do
+
+ before(:each) do
+ @client = FakeRedis::Redis.new
+ end
+
+ it "should set and get a value" do
+ @client.set("key1", "1")
+
+ @client.get("key1").should == "1"
+ end
+
+ it "shouldn't get a value from an un-existing key" do
+ @client.get("key2").should == nil
+ end
+
+ it "should append a value to a existing value" do
+ @client.set("key1", "Hello")
+ @client.append("key1", " World")
+
+ @client.get("key1").should == "Hello World"
+ end
+
+ it "should decrement by 1 a value" do
+ @client.set("counter", "1")
+ @client.decr("counter")
+
+ @client.get("counter").should == "0"
+ end
+
+ it "should decrement by a given number a value" do
+ @client.set("counter", "10")
+ @client.decrby("counter", "5")
+
+ @client.get("counter").should == "5"
+ end
+
+ it "should get bits from a value" do
+ @client.set("key1", "a")
+
+ @client.getbit("key1", 1).should == "1"
+ @client.getbit("key1", 2).should == "1"
+ @client.getbit("key1", 3).should == "0"
+ @client.getbit("key1", 4).should == "0"
+ @client.getbit("key1", 5).should == "0"
+ @client.getbit("key1", 6).should == "0"
+ @client.getbit("key1", 7).should == "1"
+ end
+
+end

0 comments on commit 3c02111

Please sign in to comment.