aiq/luazdf

Feature request: Lambda and Test

Closed this issue · 1 comments

I think a good addition to LuaZDF is a compact function definition module (lambda like). I can contribute with a small function (<20 loc) that let you to:

local l = require 'lambda'

assert( 1 == l'x|x+1'(0) )

-- additional statement, only the last expression is returned
assert( 2 == l'x|print("c",x);x+1'(1) )

-- default args are a,b,c,d,e,f,...(vararg)
assert( 1 == l'a+1'(0) )

-- complex lua code (also if this does not really match the use case)
assert( 550 == l[[ x, y |
  local z = 0
  for i = 1, x do
    z = z + i
  end
  ; z * y
]](10,10))

Moreover I have a single function test module, with tap output. I think it has an acceptable usability when used togeter with the previous lambda function:

local t = require 'tiptap'

t( 'Metatest' ) -- suite name (just output)
t( 1, 1 ) -- plain comapare
t( 1, 2 ) -- generic error message
t( 1, 2, '1 should be equal to 2' ) -- custom error message

-- custom compare function
empty_table = function(a,b) return #a==#b, "the tables should have  the same number of items" end
t( {}, {}, empty_table)

-- compact custom compare function
local l = require 'lambda'
t( {}, {}, l'#a==#b, "the tables should have the same number of items"')
t( {}, {}, l'empty_table(a,b), "THE TABLES SHOULD HAVE THE SAME NUMBER OF ITEMS"')

-- test error throw
local function err() error() end
t( 1, err, l'not pcall(b), "It should throw an error"' )

t() -- done, print summary

Note that for now only fail message are reported. The successful test just write "ok" and the test number (while for tap a test description could be added).

Tapered can still be used in the LuaZDF tests. However if internal dependency can be tolerated (just for the tests!), I think this function can substitute tapered.

If you are interested, I can adapt this two functions to luaZDF standard and make a pull request. Suggestions are welcome to improve the interface of this two functions. E.g. It is better to protect the globals of the lambdas? The multi-use test function API is too confusing?

PS. Sorry for using the term "Module", but since I was talking about "Function definition" and "Test function", the term "Function" became a bit overused :)

aiq commented

Both functions are useful additions.

lambda
I know similiar examples from the lua-users wiki or the Lua Mailing List. I personally have no use for myself, but this functions fits perfect into LuaZDF. I would put this function into the fn folder.

tiptap
A really cool idea. The idea with a compare function as third argument is nice. This not just works with the lambda, you can use it perfectly with the already existing functions from LuaZDF like this example:

local t = require 'tiptap'
local like = require 'like'
t( {1,2,3}, {2,3,1}, like )

In general would it be cool to replace tapered with a single function, that is part of LuaZDF itself.

I like the three meanings of the function.

  1. tiptap( str ) -> for suite name
  2. tiptap( act, exp [fn / msg]) -> for the actual test
  3. tiptap() -> to print summary

Normally I am not a big fan of functions that more than one use.
In this case feels your decision elegant.

But please let me add some ideas that I had viewing your example code.

  • I think that test or taptest are better names for this function
  • I would prefer for the actual test a function use like this tiptap( act, exp [ [, cmp], msg ] ). This allows it better to include already existing compare functions from LuaZDF without an additional function that you have to include.

That's my suggestion reading your first informations.
Thanks for your support.