Skip to content

Commit

Permalink
clean up after comment on issue
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderMiguel committed Jan 4, 2024
1 parent 012d96f commit 504d2a8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'

# added gems
# gem 'pry'
gem 'redis', '>= 5.0.0'
12 changes: 7 additions & 5 deletions lib/mock_redis/set_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions spec/commands/sadd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/support/shared_examples/only_operates_on_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 504d2a8

Please sign in to comment.