Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Code of Conduct.
- Add support for Ruby 3.2 and 3.3 (no changes).

### Fixed

- Adding a raw `Classlist` to a set of `Classlist::Operation`s would merge the `Classlist` into the last operation, instead of treating the `Classlist` as an implicit `Classlist::Add` operation (which it should be).

## [1.1.0] - 2022-11-07

### Added
Expand Down
6 changes: 6 additions & 0 deletions lib/classlist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ class ArgumentError < ::ArgumentError; end
class Error < StandardError; end

extend Forwardable

def_delegators :@entries, :each

attr_reader :entries, :operations

# Returns the Classlist resulting from adding other to this classlist.
def +(other)
# When adding a basic Classlist to an existing list, assume an Add operation
if other.is_a?(Classlist) && !other.is_a?(Classlist::Operation)
other = Classlist::Add.new(other.entries.dup)
end

case other
when Classlist::Operation
add_operation(other)
Expand Down
17 changes: 17 additions & 0 deletions test/classlist/test_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require "classlist/add"
require "classlist/remove"
require "classlist/reset"

class TestClasslistOperation < Minitest::Test
def test_add
Expand Down Expand Up @@ -92,6 +93,22 @@ def test_storing_operations_in_a_variable
assert_equal(["this"], result.to_a)
end

def test_adding_after_removing
base = Classlist.new("foo")
removal = Classlist::Remove.new("foo")
addition = Classlist::Add.new("addition")
result = base + removal + addition
assert_equal(["addition"], result.to_a)
end

def test_adding_a_classlist_instance_assumes_add
base = Classlist.new("foo")
removal = Classlist::Remove.new("foo")
addition = Classlist.new("addition")
result = base + removal + addition
assert_equal(["addition"], result.to_a)
end

def test_all_the_operations
result = Classlist.new("start") +
Classlist::Reset.new("with") +
Expand Down