Path: blob/main/contrib/lyaml/spec/lib_lyaml_functional_spec.yaml
178586 views
# LYAML binding for Lua 5.1, 5.2, 5.3 & 5.41# Copyright (C) 2013-2022 Gary V. Vaughan23before:4this_module = 'lyaml.functional'5global_table = '_G'67exported_apis = {'NULL', 'anyof', 'id', 'iscallable', 'isnull'}89M = require(this_module)1011nop = function() end1213fail = function() return nil end14pass = function() return false end15throw = function() error 'oh noes!' end1617parmlist = pack(18nil,19false,2042,21'str',22io.stderr,23{},24nop,25setmetatable({}, {__call=nop})26)272829specify functional:30- context when required:31- context by name:32- it does not touch the global table:33expect(show_apis{added_to=global_table, by=this_module}).to_equal{}34- it exports the decumented apis:35t = {}36for k in pairs(M) do t[#t + 1] = k end37expect(t).to_contain.a_permutation_of(exported_apis)383940- describe anyof:41- before:42f = M.anyof4344- it returns a callable:45expect(f{nop}).to_be_callable()46expect(f{nop, nop}).to_be_callable()47- it returns a lazy function that calls arguments if necessary:48expect(f{pass, throw}()).not_to_raise 'any error'49expect(f{pass, throw}()).not_to_be(nil)50- it silently skips non-callable arguments:51expect(f(list({nil, false, true}))()).to_be(nil)52expect(f{1, 2, pass, 'pass'}()).not_to_be(nil)53- it returns non-nil if any callable returns non-nil:54expect(f{pass, pass, fail}()).not_to_be(nil)55expect(f{pass, fail}()).not_to_be(nil)56expect(f{fail, pass}()).not_to_be(nil)57- it returns nil if all callables are nil:58expect(f{fail}()).to_be(nil)59expect(f{fail, fail}()).to_be(nil)60expect(f{fail, fail, fail}()).to_be(nil)61- it propagates data to all callables:62expect(f{fail, function(...) return select('#', ...) end}(nil)).to_be(1)63expect(f{function(...) return select('#', ...) end, fail}(nil, false)).to_be(2)64expect(f{function(...) return select('#', ...) end, pass}(nil, false)).to_be(2)65- it returns the first non-nil callables result:66expect(f{fail, function(...) return ... end}(42)).to_be(42)67expect(f{function(...) return ... end, fail}(42)).to_be(42)68expect(f{pass, fail}(42)).to_be(false)69expect(f{fail, pass}(42)).to_be(false)70- it propagates only the first return value:71expect(f{fail, function(...) return ... end}(1, 2, 5)).to_be(1)72expect(f{function(...) return ... end, fail}(1, 2, 5)).to_be(1)73expect(f{function(...) return ... end, pass}(1, 2, 5)).to_be(1)747576- describe id:77- before:78f = M.id7980- it returns its own argument:81expect(f(false)).to_be(false)82expect(f(42)).to_be(42)83- it handles nil argumen:84expect(f(nil)).to_be(nil)85- it handles missing argument:86expect(f()).to_be()87- it returns multiple arguments:88expect(f(nil, 1, fn, false, nil)).to_be(nil, 1, fn, false, nil)899091- describe iscallable:92- before:93f = M.iscallable9495- it returns callable for a callable:96expect(f(f)).to_be(f)97expect(f(setmetatable({}, {__call=f}))).to_be(f)98- it returns nil for a non-callable:99expect(f()).to_be(nil)100expect(f(nil)).to_be(nil)101expect(f(false)).to_be(nil)102expect(f(true)).to_be(nil)103expect(f'str').to_be(nil)104expect(f(42)).to_be(nil)105expect(f(setmetatable({}, {__index={}}))).to_be(nil)106expect(f(setmetatable({}, {__call=42}))).to_be(nil)107108109- describe isnull:110- before:111NULL = M.NULL112f = M.isnull113114- it returns 'true' for a NULL argument:115expect(f(NULL)).to_be(true)116- it returns 'false' for any argument other than NULL:117for i=1,parmlist.n do118expect(f(parmlist[i])).to_be(false)119end120121122123