Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you get test access to privates with the module() function? #60

Closed
karlp opened this issue Oct 22, 2012 · 5 comments
Closed

How do you get test access to privates with the module() function? #60

karlp opened this issue Oct 22, 2012 · 5 comments
Labels

Comments

@karlp
Copy link
Contributor

karlp commented Oct 22, 2012

The private example using _TEST uses the method of returning a table of functions to be a module, rather than the module(...,package.seeall) method.

how can I test privates this way? Is it even possible?

@DorianGray
Copy link
Contributor

I am not sure. I think Tieske will need to comment on this as he added the _TEST system I believe.

@ajacksified
Copy link
Contributor

looping in @Tieske

@Tieske
Copy link
Member

Tieske commented Oct 26, 2012

will have a look at it, currently travelling and no test system available. Might be a couple of days.

@karlp redesign your modules anyway, as module() has been deprecated in 5.2

@Tieske
Copy link
Member

Tieske commented Oct 29, 2012

Just wrote a quick test setup. For a module exporting is done the other way around, always export, delete when not testing;

The module newmod.lua:

module ("newmod", package.seeall)
local privatef = function()
    return 1
end
local list = { "hello", "world" }
publicf = function()
    return privatef() + 1
end
-- export private elements for test purposes
-- in module case revert export:
-- 1) always create test table
test = {
    _privatef = privatef,
    _list = list,
}
-- 2) now remove it when not being tested
if not _TEST then test = nil end

and the testcode;

describe("Testing a module defined using a module keyword", function()
    setup(function()
        require("newmod")
    end)
    it("tests the public function", function()
        assert.is.equal(newmod.publicf(), 2)
    end)
    it("tests the private function", function()
        assert.is.equal(newmod.test._privatef(), 1)
    end)
end)

But be aware; it's intended to test private functions, but due to its nature it works on everything that is passed by reference (functions, tables, etc. but not on numbers or strings, which are passed by value)

HTH

@karlp
Copy link
Contributor Author

karlp commented Oct 29, 2012

Will have a play with that tomorrow. I can't find anything clear about 5.1 to 5.2 migration, so I can only copy the vast amounts of 5.1 code out there that uses module(), sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants