Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appending to unique list with limit should trim from the end #56

Merged
merged 1 commit into from Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/kredis/types/unique_list.rb
Expand Up @@ -16,7 +16,7 @@ def append(elements)
multi do
remove elements
super
ltrim (limit - 1), -1 if limit
ltrim -limit, -1 if limit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that going from the tail end isn't zero-indexed, e.g. the base is -1 and not -0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it matches Ruby with ranges:

irb(main):001:0> [1,2,3,4][-3..-1]
=> [2, 3, 4]

end if Array(elements).flatten.any?
end
alias << append
Expand Down
14 changes: 13 additions & 1 deletion test/types/unique_list_test.rb
@@ -1,7 +1,7 @@
require "test_helper"

class UniqueListTest < ActiveSupport::TestCase
setup { @list = Kredis.unique_list "myuniquelist" }
setup { @list = Kredis.unique_list "myuniquelist", limit: 5 }

test "append" do
@list.append(%w[ 1 2 3 ])
Expand Down Expand Up @@ -51,4 +51,16 @@ class UniqueListTest < ActiveSupport::TestCase
@list.append [ 1, 2 ]
assert @list.exists?
end

test "appending over limit" do
@list.append(%w[ 1 2 3 4 5 ])
@list.append(%w[ 6 7 8 ])
assert_equal %w[ 4 5 6 7 8 ], @list.elements
end

test "prepending over limit" do
@list.prepend(%w[ 1 2 3 4 5 ])
@list.prepend(%w[ 6 7 8 ])
assert_equal %w[ 8 7 6 5 4 ], @list.elements
end
end