Skip to content

Commit

Permalink
Merge 5800beb into 304981b
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorWu21 committed Aug 30, 2018
2 parents 304981b + 5800beb commit 685c779
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/mock_redis/set_methods.rb
Expand Up @@ -81,11 +81,22 @@ def smove(src, dest, member)
end
end

def spop(key)
def spop(key, count = nil)
with_set_at(key) do |set|
member = set.first
set.delete(member)
member
if count.nil?
member = set.first
set.delete(member)
member
else
members = []
count.times do
member = set.first
break if member.nil?
set.delete(member)
members << member
end
members
end
end
end

Expand Down
15 changes: 15 additions & 0 deletions spec/commands/spop_spec.rb
Expand Up @@ -21,5 +21,20 @@
@redises.spop(@key).should be_nil
end

it 'returns an array if count is not nil' do
@redises.sadd(@key, 'value2')
@redises.spop(@key, 2).should == ['value', 'value2']
end

it 'returns only whats in the set' do
@redises.spop(@key, 2).should == ['value']
@redises.smembers(@key).should == []
end

it 'returns an empty array if count is not nil and the set it empty' do
@redises.spop(@key)
@redises.spop(@key, 100).should == []
end

it_should_behave_like 'a set-only command'
end

0 comments on commit 685c779

Please sign in to comment.