lunarmodules / busted

Elegant Lua unit testing.
https://lunarmodules.github.io/busted/
MIT License
1.39k stars 184 forks source link

BUG: Busted incorrectly shows "value was: %s" for "no value" #674

Closed appgurueu closed 2 years ago

appgurueu commented 2 years ago
Error → ...ta_structures/sorted_set/binary_search_tree_spec.lua @ 3
Binary search tree
...ta_structures/sorted_set/binary_search_tree_spec.lua:30: Expected to be truthy, but value was:
%s

This is because busted uses string.format("... %s ... ", expected), which does nothing if expected is nothing.

Trivially reproducible using assert.truthy().

Tieske commented 2 years ago

Thx for the report.

Since the pattern is %s is expects a string anyway. So just wrapping the expected variable in a tostring() should be good enough.

Mind sending a PR?

appgurueu commented 2 years ago

Turns out this gets rather tricky: First of all, the formatting isn't done by busted, but by luassert, which in turn uses say. While luassert will happily format & forward the arguments it gets to say, it doesn't know how many arguments to expect. So for this to be fixed:

Of course, luassert assertions could store how many arguments they expect. This will effectively be redundant though (it's exactly the same as the count of %s in the format strings).

Tieske commented 2 years ago

luassert must format missing arguments as nothing (note that "no argument" is not exactly the same as nil)

technically true, but Luassert will already in many places report 'nil'

Tieske commented 2 years ago

Fails:

$ lua -e "assert = require('luassert'); assert.truthy()"
lua: (command line):1: Expected to be truthy, but value was:
%s

Works:

$ lua -e "assert = require('luassert'); assert.truthy(nil)"
lua: (command line):1: Expected to be truthy, but value was:
(nil)

Compare to equal behaviour:

$ lua -e "assert = require('luassert'); assert.equal(true, nil)"
lua: (command line):1: Expected objects to be equal.
Passed in:
(nil)
Expected:
(boolean) true

But if not specifying it throws an error;

$ lua -e "assert = require('luassert'); assert.equal(true)"
lua: .../1.19.3.2/luarocks/share/lua/5.1/luassert/assertions.lua:115: the 'equals' function requires a minimum of 2 arguments, got: 1

So I guess the fix should be to throw an error if nothing is passed in.