diff --git a/Gemfile b/Gemfile index 08d8ba0..5c1022a 100644 --- a/Gemfile +++ b/Gemfile @@ -11,4 +11,7 @@ gem 'rubocop', '1.44.1' gem 'simplecov', '~> 0.22.0' gem 'simplecov-lcov', '~> 0.8.0' -gem 'redis', '>= 5.0.0' \ No newline at end of file + +# added gems +# gem 'pry' +gem 'redis', '>= 5.0.0' diff --git a/lib/mock_redis/set_methods.rb b/lib/mock_redis/set_methods.rb index 6318c9a..7dbb6ea 100644 --- a/lib/mock_redis/set_methods.rb +++ b/lib/mock_redis/set_methods.rb @@ -119,13 +119,15 @@ def srandmember(key, count = nil) end def srem(key, members) - members = Array(members).map(&:to_s) - with_set_at(key) do |s| - if members.size > 1 || redis_gem_v5? - size_after(s) { s.delete_if { |m| members.include?(m) } } + if members.is_a?(Array) + orig_size = s.size + members = members.map(&:to_s) + s.delete_if { |m| members.include?(m) } + orig_size - s.size else - !!s.delete?(members.first) + # !!s.delete?(members.to_s) + s.delete?(members.to_s) ? 1 : 0 end end end diff --git a/spec/commands/sadd_spec.rb b/spec/commands/sadd_spec.rb index 696d3bb..2dc4cc3 100644 --- a/spec/commands/sadd_spec.rb +++ b/spec/commands/sadd_spec.rb @@ -4,13 +4,19 @@ before { @key = 'mock-redis-test:sadd' } context 'sadd' do - it 'returns true if the set did not already contain member' do - expect(@redises.sadd(@key, 1)).to eq(true) - end + context "adapts to redis-rd version 4 and 5 outputs" do + include MockRedis::UtilityMethods - it 'returns false if the set did already contain member' do - @redises.sadd(@key, 1) - expect(@redises.sadd(@key, 1)).to eq(false) + let(:positive_response) { redis_gem_v5? ? 1 : true } + let(:negative_response) { redis_gem_v5? ? 0 : false } + it 'returns true if the set did not already contain member' do + expect(@redises.sadd(@key, 1)).to eq(positive_response) + end + + it 'returns false if the set did already contain member' do + @redises.sadd(@key, 1) + expect(@redises.sadd(@key, 1)).to eq(negative_response) + end end it 'adds member to the set' do diff --git a/spec/support/shared_examples/does_not_cleanup_empty_strings.rb b/spec/support/shared_examples/does_not_cleanup_empty_strings.rb index cf14c5f..d8c627f 100644 --- a/spec/support/shared_examples/does_not_cleanup_empty_strings.rb +++ b/spec/support/shared_examples/does_not_cleanup_empty_strings.rb @@ -3,7 +3,7 @@ key = 'mock-redis-test:not-a-string' method = method_from_description(example) - args = [key, args_for_method(method)] + args = args_for_method(method).unshift(key) @redises.set(key, '') expect do diff --git a/spec/support/shared_examples/only_operates_on_sets.rb b/spec/support/shared_examples/only_operates_on_sets.rb index d20d977..4c0f26b 100644 --- a/spec/support/shared_examples/only_operates_on_sets.rb +++ b/spec/support/shared_examples/only_operates_on_sets.rb @@ -3,7 +3,7 @@ key = 'mock-redis-test:set-only' method = method_from_description(example) - args = [key, args_for_method(method)] + args = args_for_method(method).unshift(key) @redises.set(key, 1) expect do