Skip to content

Commit

Permalink
Real Redis adds an Array as a stringified member (#284)
Browse files Browse the repository at this point in the history
We were flattening the wrapped array here to get rid of any nesting of
arrays we might have accidentally done by wrapping with an Array literal
(`[]`). Instead, `Kernel#Array` will leave an existing Array alone, and
wrap any single object in an Array. This is consistent with real Redis'
behavior.
  • Loading branch information
stevenharman authored Nov 2, 2023
1 parent 29961e0 commit dd84624
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mock_redis/set_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module SetMethods

def sadd(key, members)
members_class = members.class
members = [members].flatten.map(&:to_s)
members = Array(members).map(&:to_s)
assert_has_args(members, 'sadd')

with_set_at(key) do |s|
Expand Down
5 changes: 5 additions & 0 deletions spec/commands/sadd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
expect(@redises.smembers(@key)).to eq(%w[1 2 3])
end

it 'adds an Array as a stringified member' do
@redises.sadd(@key, [[1], 2, 3])
expect(@redises.smembers(@key)).to eq(%w[[1] 2 3])
end

it 'raises an error if an empty array is given' do
expect do
@redises.sadd(@key, [])
Expand Down

0 comments on commit dd84624

Please sign in to comment.