spec/strings_spec.rb in fakeredis-0.7.0 vs spec/strings_spec.rb in fakeredis-0.8.0
- old
+ new
@@ -241,12 +241,12 @@
expect(@client.get("key1")).to eq("value1")
expect(@client.ttl("key1")).to eq(30)
end
it "should set the value of a key, only if the key does not exist" do
- @client.set("key1", "test value")
- @client.setnx("key1", "new value")
+ expect(@client.setnx("key1", "test value")).to eq(true)
+ expect(@client.setnx("key1", "new value")).to eq(false)
@client.setnx("key2", "another value")
expect(@client.get("key1")).to eq("test value")
expect(@client.get("key2")).to eq("another value")
end
@@ -282,8 +282,37 @@
@client.set("key1", "foobar")
expect(@client.bitcount("key1", 0, 0)).to eq(4)
expect(@client.bitcount("key1", 1, 1)).to eq(6)
expect(@client.bitcount("key1", 0, 1)).to eq(10)
+ end
+
+ describe "#bitpos" do
+ it "should return -1 when there's no key" do
+ expect(@client.bitpos("key", 0)).to eq(-1)
+ end
+
+ it "should return -1 for empty key" do
+ @client.set("key", "")
+ expect(@client.bitpos("key", 0)).to eq(-1)
+ end
+
+ it "should return position of the bit in a string" do
+ @client.set("key", "foobar") # 01100110 01101111 01101111
+ expect(@client.bitpos("key", 1)).to eq(1)
+ end
+
+ it "should return position of the bit correctly with UTF-8 strings" do
+ @client.set("key", "判") # 11100101 10001000 10100100
+ expect(@client.bitpos("key", 0)).to eq(3)
+ end
+
+ it "should return position of the bit in a string given a range" do
+ @client.set("key", "foobar")
+
+ expect(@client.bitpos("key", 1, 0)).to eq(1)
+ expect(@client.bitpos("key", 1, 1, 2)).to eq(9)
+ expect(@client.bitpos("key", 0, 1, -1)).to eq(8)
+ end
end
end
end