CFC-Servers / GLuaTest

An exciting and nimble unit testing framework for GLua projects
https://discord.gg/epJK9Xx3pc
GNU General Public License v3.0
51 stars 2 forks source link

Fix tests that try to stub the hook table #32

Closed brandonsturgeon closed 1 year ago

brandonsturgeon commented 1 year ago

Currently, if you do:

-- code
myProject.CallHook( name, ... )
    hook.Run( name, ... )
end

-- test
func = function()
    local runStub = stub( hook, "Run" )
    myProject.CallHook( "test" )

    expect( runStub ).to.haveBeenCalled()
end

This would fail, because the stub library is trying to stub Run on the fake hook library that we set in the test environment.

The fix presented here would give the hook (and probably timer?) environment tables a metatable that would pass-through SET operations to the real hook (and timer) tables, letting stubs actually stub the real functions.

I didn't notice any side effects from doing this, but I'm going to play with it a bit more before merging.

e: One obvious issue here is that if you stub out any of the real hook methods, it'll fuck up anything in the entire lua environment that wants to use them... including GLuaTest.. If you put an expectation (or other kind of erroring code) in the stub, GLuaTest breaks because it can't find the debug info to report the error properly :/

brandonsturgeon commented 1 year ago

I think being able to stub hook functions is a gimmick, and I believe there are better ways to handle those tests, but this PR will still allow you to do that.

It also fixes some other minor issues I found while I was in there:

  1. Empty stubs don't error when trying to :Restore() them - it's now properly a no-op
  2. Stubs can be hook.Added, but cannot be retrieved directly from hook.GetTable() (they're wrapped in an actual function, basically)
brandonsturgeon commented 1 year ago

I've been playing with this for awhile - it doesn't appear to break any existing functionality 👍