diff --git a/lib/literal/array.rb b/lib/literal/array.rb index 8dfc5130..e1f7b6f2 100644 --- a/lib/literal/array.rb +++ b/lib/literal/array.rb @@ -176,15 +176,22 @@ def pop(...) @__value__.pop(...) end - def push(value) - Literal.check(actual: value, expected: @__type__) do |c| - c.fill_receiver(receiver: self, method: "#push") + def push(*value) + case value + when ::Array + Literal::Array(@__type__).new(*value) + else + Literal.check(actual: value, expected: @__type__) do |c| + c.fill_receiver(receiver: self, method: "#push") + end end - @__value__.push(value) + @__value__.push(*value) self end + alias_method :append, :push + def reject(...) __with__(@__value__.reject(...)) end diff --git a/test/array.test.rb b/test/array.test.rb index 110b2ffc..17374dbc 100644 --- a/test/array.test.rb +++ b/test/array.test.rb @@ -90,3 +90,22 @@ assert Literal::Array(Integer) === result expect(array.sort.to_a) == [1, 2, 3] end + +test "#push appends single value" do + array = Literal::Array(Integer).new(1, 2, 3) + + expect((array.push(4)).to_a) == [1, 2, 3, 4] +end + +test "#push appends multiple values" do + array = Literal::Array(Integer).new(1, 2, 3) + + expect((array.push(4, 5)).to_a) == [1, 2, 3, 4, 5] +end + +test "#push raises if any type is wrong" do + array = Literal::Array(Integer).new(1, 2, 3) + + expect { array.push("4") }.to_raise(Literal::TypeError) + expect { array.push(4, "5") }.to_raise(Literal::TypeError) +end