Open JoshCheek opened 6 years ago
This is where I was going with it:
def each(num_positions, num_insertions)
if num_insertions <= 0
yield []
return
end
num_positions.times do |p|
each(p+1, num_insertions-1) { |rest| yield [p, *rest] }
end
end
at_exit do
assert 1, 1, [[0]] # => true
assert 1, 2, [[0, 0]] # => true
assert 2, 1, [[0], [1]] # => true
assert 2, 2, [[0, 0], [1, 0], [1, 1]] # => true
assert 3, 1, [[0], [1], [2]] # => true
assert 3, 2, [[0, 0], [1, 0], [1, 1], [2, 0], [2, 1], [2, 2]] # => true
end
def assert(pos, ins, expected)
actual = to_enum(:each, pos, ins).to_a
return true if actual == expected
raise RuntimeError, "\nExpected: #{expected.inspect}\nActual: #{actual.inspect}", caller
end
Here I wound up adding a newline to the beginning of the error message, but really, multiline errors (or possibly all errors?) should begin on their own line, rather than trying to put them inline with the error class. (in the example, the expectation is wrong, I made it wrong in order to test the failure case feedback before turing my examples into tests and refactoring the code)