polishing ruby by ryan davis

assert_in_delta

Published 2012-12-10 @ 12:00

Tagged minitest

“So, supposedly Test::Unit was too big. lib/ruby/1.8/test/unit (without ui classes): 1,124 LOC. lib/ruby/1.9.1/minitest: 1,091 LOC.”

jcoglan made this snarky comment about the size of minitest vs test/unit. Let’s ignore the fact that the original minitest was <99 lines long and enough of test/unit (which I was maintaining at the time) to run all the unit tests on all of my projects. Let’s also ignore the fact that minitest is a lot more than just a test/unit replacement at this point. But apparently we need to re-re-revisit the fact that LOC doesn’t mean anything and complexity means everything.

Let’s revisit assert_in_delta from my Size doesn’t Matter talk. test/unit’s:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public
def assert_in_delta(expected_float, actual_float, delta, message="")
  _wrap_assertion do
    {expected_float => "first float", actual_float => "second float", delta => "delta"}.each do |float, name|
      assert_respond_to(float, :to_f, "The arguments must respond to to_f; the #{name} did not")
    end
    assert_operator(delta, :>=, 0.0, "The delta should not be negative")
    full_message = build_message(message, <<EOT, expected_float, actual_float, delta)
<?> and
<?> expected to be within
<?> of each other.
EOT
    assert_block(full_message) { (expected_float.to_f - actual_float.to_f).abs <= delta.to_f }
  end
end

versus minitest’s:

1
2
3
4
5
def assert_in_delta exp, act, delta = 0.001, msg = nil
  n = (exp - act).abs
  msg = message(msg) { "Expected |#{exp} - #{act}| (#{n}) to be < #{delta}"}
  assert delta >= n, msg
end

Personally, I think that speaks for itself. If that doesn’t convince you, look at the rest of my Size doesn’t Matter talk starting around slide 70 “Minitest Design Rationale”.