spec/strings_spec.rb in fakeredis-0.4.3 vs spec/strings_spec.rb in fakeredis-0.5.0
- old
+ new
@@ -1,5 +1,7 @@
+# encoding: UTF-8
+
require 'spec_helper'
module FakeRedis
describe "StringsMethods" do
@@ -47,10 +49,34 @@
it "should allow direct bit manipulation even if the string isn't set" do
@client.setbit("key1", 10, 1)
@client.getbit("key1", 10).should be == 1
end
+ context 'when a bit is previously set to 0' do
+ before { @client.setbit("key1", 10, 0) }
+
+ it 'setting it to 1 returns 0' do
+ expect(@client.setbit("key1", 10, 1)).to eql 0
+ end
+
+ it 'setting it to 0 returns 0' do
+ expect(@client.setbit("key1", 10, 0)).to eql 0
+ end
+ end
+
+ context 'when a bit is previously set to 1' do
+ before { @client.setbit("key1", 10, 1) }
+
+ it 'setting it to 0 returns 1' do
+ expect(@client.setbit("key1", 10, 0)).to eql 1
+ end
+
+ it 'setting it to 1 returns 1' do
+ expect(@client.setbit("key1", 10, 1)).to eql 1
+ end
+ end
+
it "should get a substring of the string stored at a key" do
@client.set("key1", "This a message")
@client.getrange("key1", 0, 3).should be == "This"
@client.substr("key1", 0, 3).should be == "This"
@@ -75,11 +101,11 @@
@client.get("counter").should be == "2"
end
it "should not change the expire value of the key during incr" do
@client.set("counter", "1")
- @client.expire("counter", 600).should be_true
+ @client.expire("counter", 600).should be true
@client.ttl("counter").should be == 600
@client.incr("counter").should be == 2
@client.ttl("counter").should be == 600
end
@@ -90,11 +116,11 @@
@client.get("counter").should be == "0"
end
it "should not change the expire value of the key during decr" do
@client.set("counter", "2")
- @client.expire("counter", 600).should be_true
+ @client.expire("counter", 600).should be true
@client.ttl("counter").should be == 600
@client.decr("counter").should be == 1
@client.ttl("counter").should be == 600
end
@@ -105,11 +131,11 @@
@client.get("counter").should be == "17"
end
it "should not change the expire value of the key during incrby" do
@client.set("counter", "1")
- @client.expire("counter", 600).should be_true
+ @client.expire("counter", 600).should be true
@client.ttl("counter").should be == 600
@client.incrby("counter", "5").should be == 6
@client.ttl("counter").should be == 600
end
@@ -120,11 +146,11 @@
@client.get("counter").should be == "3"
end
it "should not change the expire value of the key during decrby" do
@client.set("counter", "8")
- @client.expire("counter", 600).should be_true
+ @client.expire("counter", 600).should be true
@client.ttl("counter").should be == 600
@client.decrby("counter", "3").should be == 5
@client.ttl("counter").should be == 600
end
@@ -228,9 +254,31 @@
it "should get the length of the value stored in a key" do
@client.set("key1", "abc")
@client.strlen("key1").should be == 3
+ end
+
+ it "should return 0 bits when there's no key" do
+ @client.bitcount("key1").should be == 0
+ end
+
+ it "should count the number of bits of a string" do
+ @client.set("key1", "foobar")
+ @client.bitcount("key1").should be == 26
+ end
+
+ it "should count correctly with UTF-8 strings" do
+ @client.set("key1", '判')
+ @client.bitcount("key1").should be == 10
+ end
+
+ it "should count the number of bits of a string given a range" do
+ @client.set("key1", "foobar")
+
+ @client.bitcount("key1", 0, 0).should be == 4
+ @client.bitcount("key1", 1, 1).should be == 6
+ @client.bitcount("key1", 0, 1).should be == 10
end
end
end