diff --git a/CHANGELOG.md b/CHANGELOG.md index e9ef493..655b067 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/classlist.rb b/lib/classlist.rb index d2428f5..dedc0c4 100644 --- a/lib/classlist.rb +++ b/lib/classlist.rb @@ -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) diff --git a/test/classlist/test_operation.rb b/test/classlist/test_operation.rb index 546bb86..a57152b 100644 --- a/test/classlist/test_operation.rb +++ b/test/classlist/test_operation.rb @@ -4,6 +4,7 @@ require "classlist/add" require "classlist/remove" +require "classlist/reset" class TestClasslistOperation < Minitest::Test def test_add @@ -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") +