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
15 changes: 11 additions & 4 deletions lib/literal/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This seemed like the best way of forcing the type check to happen on an array of values. But 🤷‍♂️

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
Expand Down
19 changes: 19 additions & 0 deletions test/array.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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