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

Multiline error messages should start on their own line #133

Open
JoshCheek opened this issue Jun 16, 2018 · 1 comment
Open

Multiline error messages should start on their own line #133

JoshCheek opened this issue Jun 16, 2018 · 1 comment

Comments

@JoshCheek
Copy link
Owner

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)

def each(num_positions, num_insertions, &block)
  return if num_positions == 0 || num_insertions == 0
  
  # if num_positions == 1 # => false, true, false, false
  #   num_positions  # => 1
  #   num_insertions # => 1
  #   1.times do |i|
  #     block.call [i] * num_insertions
  #   end
  #   return
  # end
  
  if num_insertions == 1 # => false, true, true
    num_positions.times do |i|
      block.call [i]
    end
    return
  end

  num_positions.times do |i|
    each i+1, num_insertions-1 do |sub_answers|
      block.call [i, *sub_answers]
    end
  end
end

at_exit do
  assert 2, 2, [[0, 0], [1, 0], [1, 1], [2, 0], [2, 1], [2, 2]] # => RuntimeError: \nExpected: [[0, 0], [1, 0], [1, 1], [2, 0], [2, 1], [2, 2]]\nActual:   [[0, 0], [1, 0], [1, 1]]
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

# => :assert

# !> program.rb:28:in `block in <main>':  (RuntimeError)
# !> Expected: [[0, 0], [1, 0], [1, 1], [2, 0], [2, 1], [2, 2]]
# !> Actual:   [[0, 0], [1, 0], [1, 1]]

# ~> RuntimeError
# ~> 
# ~> Expected: [[0, 0], [1, 0], [1, 1], [2, 0], [2, 1], [2, 2]]
# ~> Actual:   [[0, 0], [1, 0], [1, 1]]
# ~>
# ~> program.rb:28:in `block in <main>'
__END__

# .X | 0
# X. | 1
each 2, 1 do |nums|
  nums # => [0], [1]
end

# .XX | 0
# X.X | 1
# XX. | 2
each 3, 1 do |nums|
  nums # => [0], [1], [2]
end

# .. | 0,0
each 1, 2 do |nums|
  nums # => [0, 0]
end

# ..X | 0,0
# .X. | 1,0
# X.. | 1,1
each 2, 2 do |nums|
  nums # => [1, 0]
end

return
# ..XX | 0,0
# .X.X | 1,0
# X..X | 1,1
# .XX. | 2,0
# X.X. | 2,1
# XX.. | 2,2
each 3, 2 do |nums|
  nums # => 
end

__END__
(1)(8)(0) 6 8 0   | 0,0,0
(1)(8) 6 (0) 8 0  | 1,0,0
(1) 6 (8)(0) 8 0  | 1,1,0
6 (1)(8)(0) 8 0   | 1,1,1
(1)(8) 6 8 (0) 0  | 2,0,0
(1) 6 (8) 8 (0) 0 | 2,1,0
6 (1)(8) 8 (0) 0  | 2,1,1
(1) 6 8 (8)(0) 0  | 2,2,0
6 (1) 8 (8)(0) 0  | 2,2,1
6 8 (1)(8)(0) 0   | 2,2,2
@JoshCheek
Copy link
Owner Author

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

bitmoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant