You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// JavaScript - Jesttest('the data is peanut butter',done=>{functioncallback(error,data){if(error){done(error);return;}try{expect(data).toBe('peanut butter');done();}catch(error){done(error);}}fetchData(callback);});
We get debug.getinfo(fn, "u").nparams since Lua 5.2 and in LuaJIT (and thus Garry's Mod), which allows us to determine if the fn has any parameters, allowing for a very similar API:
-- Lua - Jestronaut (concept implementation)functiontest(mode, fn)
localfunctionhas_parameter(func)
returndebug.getinfo(func, "u").nparams==1endiftype(fn) =="function" thenifhas_parameter(fn) thenprint(mode..": fn expects a parameter.")
elseprint(mode..": fn does not expect a parameter.")
endelseprint("Second argument is not a valid function.")
endend-- Examplestest("sync", function() end) -- sync: fn does not expect a parameter.test("async", function(done) end) -- async: fn expects a parameter.
Sadly for Lua 5.1 I can't find a way to detect the parameter count in the testing fn, so I guess we could have this alternative syntax for Lua 5.1, which would make it trivial to detect if a test function is long async:
-- Lua - Jestronaut (concept usage for Lua 5.1)test("the data is peanut butter", async(function(done)
localfunctioncallback(error, data)
iferrorthendone(error)
returnendlocalsuccess, err=pcall(function()
expect(data):toBe("peanut butter")
done()
end)
ifnotsuccessthendone(err)
endendfetchData(callback)
end))
I think the biggest challenge will be scheduling the tests in such a way that that they can actually be long running, without locking up all other tests. I'm not very familiar with Lua coroutines, which I think we'll need + I suspect a ton of refactoring might be needed.
Currently there is no way to test async code.
API
Jest allows testing asynchronous code using callbacks:
That API would translate to Lua pretty well:
We get
debug.getinfo(fn, "u").nparams
since Lua 5.2 and in LuaJIT (and thus Garry's Mod), which allows us to determine if the fn has any parameters, allowing for a very similar API:Sadly for Lua 5.1 I can't find a way to detect the parameter count in the testing fn, so I guess we could have this alternative syntax for Lua 5.1, which would make it trivial to detect if a test function is long async:
I think the biggest challenge will be scheduling the tests in such a way that that they can actually be long running, without locking up all other tests. I'm not very familiar with Lua coroutines, which I think we'll need + I suspect a ton of refactoring might be needed.
Minimal prototype 1: https://pastebin.com/redFZMKw
The text was updated successfully, but these errors were encountered: