Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Literal::Array: implement #replace, and #insert. add #prepend alias #142

Merged
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
32 changes: 32 additions & 0 deletions lib/literal/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ def freeze
super
end

def insert(index, *value)
Literal.check(actual: value, expected: @__collection_type__) do |c|
c.fill_receiver(receiver: self, method: "#insert")
end

@__value__.insert(index, *value)
self
end

def last(...)
@__value__.last(...)
end
Expand Down Expand Up @@ -196,6 +205,27 @@ def reject!(...)
self
end

def replace(value)
case value
when Array
Literal.check(actual: value, expected: @__collection_type__) do |c|
c.fill_receiver(receiver: self, method: "#replace")
end

@__value__.replace(value)
when Literal::Array(@__type__)
@__value__.replace(value.__value__)
when Literal::Array
raise Literal::TypeError.new(
context: Literal::TypeError::Context.new(expected: @__type__, actual: value.__type__)
)
else
raise ArgumentError.new("#replace expects Array argument")
end

self
end

def sample(...)
@__value__.sample(...)
end
Expand Down Expand Up @@ -231,4 +261,6 @@ def unshift(value)
@__value__.unshift(value)
self
end

alias_method :prepend, :unshift
end
45 changes: 45 additions & 0 deletions test/array.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,48 @@
expect { array.push("4") }.to_raise(Literal::TypeError)
expect { array.push(4, "5") }.to_raise(Literal::TypeError)
end

test "#insert inserts single element at index offset" do
array = Literal::Array(Integer).new(1, 2, 3)

expect((array.insert(1, 4)).to_a) == [1, 4, 2, 3]
end

test "#insert inserts multiple elements at index offset" do
array = Literal::Array(Integer).new(1, 2, 3)

expect((array.insert(1, 4, 5, 6)).to_a) == [1, 4, 5, 6, 2, 3]
end

test "#insert raises if any type is wrong" do
array = Literal::Array(Integer).new(1, 2, 3)

expect { array.insert(1, "4") }.to_raise(Literal::TypeError)
expect { array.insert(1, 4, "5", 6) }.to_raise(Literal::TypeError)
end

test "#replace replaces with passed in array" do
array = Literal::Array(Integer).new(1, 2, 3)

expect((array.replace([4, 5, 6])).to_a) == [4, 5, 6]
end

test "#replace replaces with passed in Literal::Array" do
array = Literal::Array(Integer).new(1, 2, 3)
other = Literal::Array(Integer).new(4, 5, 6)

expect((array.replace(other)).to_a) == [4, 5, 6]
end

test "#replace raises if type of any element in array is wrong" do
array = Literal::Array(Integer).new(1, 2, 3)

expect { array.replace([1, "4"]) }.to_raise(Literal::TypeError)
expect { array.replace(Literal::Array(String).new("4", "5")) }.to_raise(Literal::TypeError)
end

test "#replace raises with non-array argument" do
array = Literal::Array(Integer).new(1, 2, 3)

expect { array.replace("not an array") }.to_raise(ArgumentError)
end
Loading