Skip to content

Commit

Permalink
RSpec: use expect assertion syntax (#258)
Browse files Browse the repository at this point in the history
Back in RSpec 2.11, a new expectation syntax was introduced. Check out
the blog post that outlines the shortcomings of the `should` syntax and
how these are resolved with the new `expect` syntax.

https://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/

I propose the RSpec test suite be migrated to this `expect` syntax.
Luckily this is quite easy as there is a conversion tool that does all
the heavy lifting: [transpec](http://yujinakayama.me/transpec/).

I ran this tool across the test suite and this pull request consists of
the changes.
  • Loading branch information
orien committed Jan 28, 2023
1 parent b8b995c commit 4b8fdc5
Show file tree
Hide file tree
Showing 166 changed files with 1,421 additions and 1,350 deletions.
8 changes: 4 additions & 4 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
require 'spec_helper'

describe 'client' do
RSpec.describe 'client' do
context '#reconnect' do
it 'reconnects' do
redis = MockRedis.new
redis.reconnect.should == redis
expect(redis.reconnect).to eq(redis)
end
end

context '#connect' do
it 'connects' do
redis = MockRedis.new
redis.connect.should == redis
expect(redis.connect).to eq(redis)
end
end

Expand All @@ -30,7 +30,7 @@
context '#with' do
it 'supports with' do
redis = MockRedis.new
redis.with { |c| c.set('key', 'value') }.should == 'OK'
expect(redis.with { |c| c.set('key', 'value') }).to eq('OK')
end
end
end
28 changes: 14 additions & 14 deletions spec/cloning_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe 'MockRedis#clone' do
RSpec.describe 'MockRedis#clone' do
before do
@mock = MockRedis.new
end
Expand All @@ -17,32 +17,32 @@
end

it 'copies the stored data to the clone' do
@clone.get('foo').should == 'bar'
expect(@clone.get('foo')).to eq('bar')
end

it 'performs a deep copy (string values)' do
@mock.del('foo')
@clone.get('foo').should == 'bar'
expect(@clone.get('foo')).to eq('bar')
end

it 'performs a deep copy (list values)' do
@mock.lpop('foolist')
@clone.lrange('foolist', 0, 1).should == ['bar']
expect(@clone.lrange('foolist', 0, 1)).to eq(['bar'])
end

it 'performs a deep copy (hash values)' do
@mock.hset('foohash', 'bar', 'quux')
@clone.hgetall('foohash').should == { 'bar' => 'baz' }
expect(@clone.hgetall('foohash')).to eq({ 'bar' => 'baz' })
end

it 'performs a deep copy (set values)' do
@mock.srem('fooset', 'bar')
@clone.smembers('fooset').should == ['bar']
expect(@clone.smembers('fooset')).to eq(['bar'])
end

it 'performs a deep copy (zset values)' do
@mock.zadd('foozset', 2, 'bar')
@clone.zscore('foozset', 'bar').should == 1.0
expect(@clone.zscore('foozset', 'bar')).to eq(1.0)
end
end

Expand All @@ -55,17 +55,17 @@
end

it 'copies the expiration times' do
@clone.ttl('foo').should > 0
expect(@clone.ttl('foo')).to be > 0
end

it 'deep-copies the expiration times' do
@mock.persist('foo')
@clone.ttl('foo').should > 0
expect(@clone.ttl('foo')).to be > 0
end

it 'deep-copies the expiration times' do
@clone.persist('foo')
@mock.ttl('foo').should > 0
expect(@mock.ttl('foo')).to be > 0
end
end

Expand All @@ -80,16 +80,16 @@
end

it 'makes sure the clone is in a transaction' do
lambda do
expect do
@clone.exec
end.should_not raise_error
end.not_to raise_error
end

it 'deep-copies the queued commands' do
@clone.incrby('foo', 8)
@clone.exec.should == [1, 3, 7, 15]
expect(@clone.exec).to eq([1, 3, 7, 15])

@mock.exec.should == [1, 3, 7]
expect(@mock.exec).to eq([1, 3, 7])
end
end
end
8 changes: 4 additions & 4 deletions spec/commands/append_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
require 'spec_helper'

describe '#append(key, value)' do
RSpec.describe '#append(key, value)' do
before { @key = 'mock-redis-test:append' }

it 'returns the new length of the string' do
@redises.set(@key, 'porkchop')
@redises.append(@key, 'sandwiches').should == 18
expect(@redises.append(@key, 'sandwiches')).to eq(18)
end

it 'appends value to the previously-stored value' do
@redises.set(@key, 'porkchop')
@redises.append(@key, 'sandwiches')

@redises.get(@key).should == 'porkchopsandwiches'
expect(@redises.get(@key)).to eq('porkchopsandwiches')
end

it 'treats a missing key as an empty string' do
@redises.append(@key, 'foo')
@redises.get(@key).should == 'foo'
expect(@redises.get(@key)).to eq('foo')
end

it_should_behave_like 'a string-only command'
Expand Down
4 changes: 2 additions & 2 deletions spec/commands/auth_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe '#auth(password) [mock only]' do
RSpec.describe '#auth(password) [mock only]' do
it "just returns 'OK'" do
@redises.mock.auth('foo').should == 'OK'
expect(@redises.mock.auth('foo')).to eq('OK')
end
end
4 changes: 2 additions & 2 deletions spec/commands/bgrewriteaof_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe '#bgrewriteaof [mock only]' do
RSpec.describe '#bgrewriteaof [mock only]' do
it 'just returns a canned string' do
@redises.mock.bgrewriteaof.should =~ /append/
expect(@redises.mock.bgrewriteaof).to match(/append/)
end
end
4 changes: 2 additions & 2 deletions spec/commands/bgsave_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe '#bgsave [mock only]' do
RSpec.describe '#bgsave [mock only]' do
it 'just returns a canned string' do
@redises.mock.bgsave.should =~ /saving/
expect(@redises.mock.bgsave).to match(/saving/)
end
end
14 changes: 7 additions & 7 deletions spec/commands/bitcount_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
require 'spec_helper'

describe '#bitcount(key [, start, end ])' do
RSpec.describe '#bitcount(key [, start, end ])' do
before do
@key = 'mock-redis-test:bitcount'
@redises.set(@key, 'foobar')
end

it 'gets the number of set bits from the key' do
@redises.bitcount(@key).should == 26
expect(@redises.bitcount(@key)).to eq(26)
end

it 'gets the number of set bits from the key in an interval' do
@redises.bitcount(@key, 0, 1000).should == 26
@redises.bitcount(@key, 0, 0).should == 4
@redises.bitcount(@key, 1, 1).should == 6
@redises.bitcount(@key, 1, -2).should == 18
expect(@redises.bitcount(@key, 0, 1000)).to eq(26)
expect(@redises.bitcount(@key, 0, 0)).to eq(4)
expect(@redises.bitcount(@key, 1, 1)).to eq(6)
expect(@redises.bitcount(@key, 1, -2)).to eq(18)
end

it 'treats nonexistent keys as empty strings' do
@redises.bitcount('mock-redis-test:not-found').should == 0
expect(@redises.bitcount('mock-redis-test:not-found')).to eq(0)
end

it_should_behave_like 'a string-only command'
Expand Down
94 changes: 47 additions & 47 deletions spec/commands/bitfield_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe '#bitfield(*args)' do
RSpec.describe '#bitfield(*args)' do
before :each do
@key = 'mock-redis-test:bitfield'
@redises.set(@key, '')
Expand All @@ -14,21 +14,21 @@

context 'with a :get command' do
it 'gets a signed 8 bit value' do
@redises.bitfield(@key, :get, 'i8', 0).should == [78]
@redises.bitfield(@key, :get, 'i8', 8).should == [104]
@redises.bitfield(@key, :get, 'i8', 16).should == [-59]
expect(@redises.bitfield(@key, :get, 'i8', 0)).to eq([78])
expect(@redises.bitfield(@key, :get, 'i8', 8)).to eq([104])
expect(@redises.bitfield(@key, :get, 'i8', 16)).to eq([-59])
end

it 'gets multiple values with multiple command args' do
@redises.bitfield(@key, :get, 'i8', 0,
expect(@redises.bitfield(@key, :get, 'i8', 0,
:get, 'i8', 8,
:get, 'i8', 16).should == [78, 104, -59]
:get, 'i8', 16)).to eq([78, 104, -59])
end

it 'gets multiple values using positional offsets' do
@redises.bitfield(@key, :get, 'i8', '#0',
expect(@redises.bitfield(@key, :get, 'i8', '#0',
:get, 'i8', '#1',
:get, 'i8', '#2').should == [78, 104, -59]
:get, 'i8', '#2')).to eq([78, 104, -59])
end

it 'shows an error with an invalid type' do
Expand All @@ -49,102 +49,102 @@

context 'with a :set command' do
it 'sets the bit values for an 8 bit signed integer' do
@redises.bitfield(@key, :set, 'i8', 0, 63).should == [78]
@redises.bitfield(@key, :set, 'i8', 8, -1).should == [104]
@redises.bitfield(@key, :set, 'i8', 16, 123).should == [-59]
expect(@redises.bitfield(@key, :set, 'i8', 0, 63)).to eq([78])
expect(@redises.bitfield(@key, :set, 'i8', 8, -1)).to eq([104])
expect(@redises.bitfield(@key, :set, 'i8', 16, 123)).to eq([-59])

@redises.bitfield(@key, :get, 'i8', 0,
expect(@redises.bitfield(@key, :get, 'i8', 0,
:get, 'i8', 8,
:get, 'i8', 16).should == [63, -1, 123]
:get, 'i8', 16)).to eq([63, -1, 123])
end

it 'sets multiple values with multiple command args' do
@redises.bitfield(@key, :set, 'i8', 0, 63,
expect(@redises.bitfield(@key, :set, 'i8', 0, 63,
:set, 'i8', 8, -1,
:set, 'i8', 16, 123).should == [78, 104, -59]
:set, 'i8', 16, 123)).to eq([78, 104, -59])

@redises.bitfield(@key, :get, 'i8', 0,
expect(@redises.bitfield(@key, :get, 'i8', 0,
:get, 'i8', 8,
:get, 'i8', 16).should == [63, -1, 123]
:get, 'i8', 16)).to eq([63, -1, 123])
end
end

context 'with an :incrby command' do
it 'returns the incremented by value for an 8 bit signed integer' do
@redises.bitfield(@key, :incrby, 'i8', 0, 1).should == [79]
@redises.bitfield(@key, :incrby, 'i8', 8, -1).should == [103]
@redises.bitfield(@key, :incrby, 'i8', 16, 5).should == [-54]
expect(@redises.bitfield(@key, :incrby, 'i8', 0, 1)).to eq([79])
expect(@redises.bitfield(@key, :incrby, 'i8', 8, -1)).to eq([103])
expect(@redises.bitfield(@key, :incrby, 'i8', 16, 5)).to eq([-54])
end

context 'with an overflow of wrap (default)' do
context 'for a signed integer' do
it 'wraps the overflow to the minimum and increments from there' do
@redises.bitfield(@key, :get, 'i8', 24).should == [78]
@redises.bitfield(@key, :overflow, :wrap,
:incrby, 'i8', 0, 200).should == [22]
expect(@redises.bitfield(@key, :get, 'i8', 24)).to eq([78])
expect(@redises.bitfield(@key, :overflow, :wrap,
:incrby, 'i8', 0, 200)).to eq([22])
end

it 'wraps the underflow to the maximum value and decrements from there' do
@redises.bitfield(@key, :overflow, :wrap,
:incrby, 'i8', 16, -200).should == [-3]
expect(@redises.bitfield(@key, :overflow, :wrap,
:incrby, 'i8', 16, -200)).to eq([-3])
end
end

context 'for an unsigned integer' do
it 'wraps the overflow back to zero and increments from there' do
@redises.bitfield(@key, :get, 'u8', 24).should == [78]
@redises.bitfield(@key, :overflow, :wrap,
:incrby, 'u8', 24, 233).should == [55]
expect(@redises.bitfield(@key, :get, 'u8', 24)).to eq([78])
expect(@redises.bitfield(@key, :overflow, :wrap,
:incrby, 'u8', 24, 233)).to eq([55])
end

it 'wraps the underflow to the maximum value and decrements from there' do
@redises.bitfield(@key, :get, 'u8', 32).should == [84]
@redises.bitfield(@key, :overflow, :wrap,
:incrby, 'u8', 32, -233).should == [107]
expect(@redises.bitfield(@key, :get, 'u8', 32)).to eq([84])
expect(@redises.bitfield(@key, :overflow, :wrap,
:incrby, 'u8', 32, -233)).to eq([107])
end
end
end

context 'with an overflow of sat' do
it 'sets the overflowed value to the maximum' do
@redises.bitfield(@key, :overflow, :sat,
:incrby, 'i8', 0, 256).should == [127]
expect(@redises.bitfield(@key, :overflow, :sat,
:incrby, 'i8', 0, 256)).to eq([127])
end

it 'sets the underflowed value to the minimum' do
@redises.bitfield(@key, :overflow, :sat,
:incrby, 'i8', 16, -256).should == [-128]
expect(@redises.bitfield(@key, :overflow, :sat,
:incrby, 'i8', 16, -256)).to eq([-128])
end
end

context 'with an overflow of fail' do
it 'raises a redis error on an out of range value' do
@redises.bitfield(@key, :overflow, :fail,
:incrby, 'i8', 0, 256).should == [nil]
expect(@redises.bitfield(@key, :overflow, :fail,
:incrby, 'i8', 0, 256)).to eq([nil])

@redises.bitfield(@key, :overflow, :fail,
:incrby, 'i8', 16, -256).should == [nil]
expect(@redises.bitfield(@key, :overflow, :fail,
:incrby, 'i8', 16, -256)).to eq([nil])
end

it 'retains the original value after a failed increment' do
@redises.bitfield(@key, :get, 'i8', 0).should == [78]
@redises.bitfield(@key, :overflow, :fail,
:incrby, 'i8', 0, 256).should == [nil]
@redises.bitfield(@key, :get, 'i8', 0).should == [78]
expect(@redises.bitfield(@key, :get, 'i8', 0)).to eq([78])
expect(@redises.bitfield(@key, :overflow, :fail,
:incrby, 'i8', 0, 256)).to eq([nil])
expect(@redises.bitfield(@key, :get, 'i8', 0)).to eq([78])
end
end

context 'with multiple overflow commands in one transaction' do
it 'handles the overflow values correctly' do
@redises.bitfield(@key, :overflow, :sat,
expect(@redises.bitfield(@key, :overflow, :sat,
:incrby, 'i8', 0, 256,
:incrby, 'i8', 8, -256,
:overflow, :wrap,
:incrby, 'i8', 0, 200,
:incrby, 'i8', 16, -200,
:overflow, :fail,
:incrby, 'i8', 0, 256,
:incrby, 'i8', 16, -256).should == [127, -128, 71, -3, nil, nil]
:incrby, 'i8', 16, -256)).to eq([127, -128, 71, -3, nil, nil])
end
end

Expand All @@ -160,10 +160,10 @@

context 'with a mixed set of commands' do
it 'returns the correct outputs' do
@redises.bitfield(@key, :set, 'i8', 0, 38,
expect(@redises.bitfield(@key, :set, 'i8', 0, 38,
:set, 'i8', 8, -99,
:incrby, 'i8', 16, 1,
:get, 'i8', 0).should == [78, 104, -58, 38]
:get, 'i8', 0)).to eq([78, 104, -58, 38])
end
end
end

0 comments on commit 4b8fdc5

Please sign in to comment.