What if I told you that it's possible to get helpful failure messages from basic asserts
using idiomatic equality ==
checks? No DSLs in sight.
This is a proof of concept to demonstrate that it's possible... mostly to satisfy my own curiosity. The concepts here could theoretically be expanded to provide a useful extension to existing testing frameworks or perhaps lay a foundation for an entirely new one.
This experiment created with Ruby 3.0.1. Note to self: There's probably a way to do this with TracePoint
instead of monkey patching but I couldn't figure out how to get a reference to the passed variable being compared.
assert true == false
# Failed assertion!
# comparison: true == false
# location: /path/to/test.rb:#
actual = nil
expected = 1
assert actual == expected
# Failed assertion!
# comparison: nil == 1
# location: /path/to/test.rb:#
actual = "bar"
expected = "foo"
assert actual == expected
# Failed assertion!
# comparison: "bar" == "foo"
# location: /path/to/test.rb:#
actual = [1,4]
expected = [1,2,3]
assert actual == expected
# Failed assertion!
# comparison: [1, 4] == [1, 2, 3]
# location: /path/to/test.rb:#
actual = {bar: :foo}
expected = {foo: :bar}
assert actual == expected
# Failed assertion!
# comparison: {:bar=>:foo} == {:foo=>:bar}
# location: /path/to/test.rb:#
Note that the order of actual
and expected
doesn't matter. Use whatever order suites your preferences.
Create the following files locally.
test_demo.rb
test_helper.rb
test_runner.rb
Then run the following command.
ruby /path/to/test_runner.rb