I have written a test utility function aequals (assert equals) that expects an actual result and an expected result as arguments. I use it like this:
aequals(fib(8), 21);
But now I have a function with multiple return values:
function stuff() return 1,2,3 end
I want to check its function by either checking it all together:
aequals( stuff(), {1,2,3} );
but this does not work because only "1" is left on the stack for aequals.
Or at least one after the other:
aequals( stuff()[1], 1 );
aequals( stuff()[2], 2 );
aequals( stuff()[3], 3 );
but this gives a syntax error, because stuff returns a tuple, not an array/table.
I tried to fix this using the array-constructor, which supposed to make a tuple into an array/table.
aequals( {stuff()}[1], 1 );
why this is a syntax error I can not understand.
I circumvented this by defining helper functions which I am sure are already in Lua, if I only knew where to look:
function arg0(a,b,c) return a end;
function arg1(a,b,c) return b end;
function arg2(a,b,c) return c end;
aequals( arg0(stuff()), 1 );
While this works it is quite cumbersome and it would be so much nicer to have the whole check in one line... but how?