Skip to content

Commit

Permalink
Merge pull request #56 from excid3/fix-unique-list-with-limit
Browse files Browse the repository at this point in the history
Appending to unique list with limit should trim from the end
  • Loading branch information
kaspth committed Dec 2, 2021
2 parents 6a5b461 + b8edff2 commit db8fc9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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
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

0 comments on commit db8fc9a

Please sign in to comment.