lunarmodules / busted

Elegant Lua unit testing.
https://lunarmodules.github.io/busted/
MIT License
1.39k stars 184 forks source link

false vs. nil #663

Closed AndreasMatthias closed 3 years ago

AndreasMatthias commented 3 years ago

The following code

require('busted.runner')()
x = false
print(x, type(x))

outputs

nil nil

while the expected result would be

false   boolean

Installed version of busted:

$ busted --version
2.0.0-0
aki-cat commented 3 years ago

When you write x = false what you're really doing is _G.x = false (aka declaring a global var x in the current environment). If the current environment does not allow writing on _G, you won't be able to declare anything in the global scope of that environment.

Use local x = false instead.

EDIT: Depending on which lua version you're using, the environment might be set either using setfenv() or _ENV. Either way, use locals, don't pollute the global environment.

AndreasMatthias commented 3 years ago

OK, understook. Thank you!