inline-tests-gem icon indicating copy to clipboard operation
inline-tests-gem copied to clipboard

Getting closer to an ideal syntax

Open drusepth opened this issue 3 years ago • 0 comments

The syntax for inline tests right now technically works, but it's still pretty weird:

tested def integer_division(x, y)
  return Float::INFINITY if y.zero?
  x.to_i / y.to_i
end,
tests do |integer_division|
  assert integer_division(6, 3) == 2, "divides correctly"
  assert integer_division(5, 3) == 1, "uses integer division"
  assert integer_division(0, 3) == 0,  "0 / anything = 0"
  assert integer_division(3, 0) == Float::INFINITY
end

I think the ideal syntax would feel more native to Ruby and less hacky; something like:

def integer_division(x, y)
  return Float::INFINITY if y.zero?
  x.to_i / y.to_i
tests
  assert integer_division(6, 3) == 2, "divides correctly"
  assert integer_division(5, 3) == 1, "uses integer division"
  assert integer_division(0, 3) == 0,  "0 / anything = 0"
  assert integer_division(3, 0) == Float::INFINITY
end

However, I have no idea how we'd split a method definition like that syntax-wise without a bunch of gross boilerplate (which would probably look very similar to what we're already doing). If only Ruby had some kind of preprocessing step we could macro #DEFINE tests end, tests do |f| with...

drusepth avatar Apr 13 '21 22:04 drusepth