Testing by Assertions
Nil Check
Check that a required value is present.
required value
Checking both `nil` and an empty string catches two common missing-value cases.
Nil Check
nil_check.lua
Replay: real traced execution (multi-file project)
local raw = "name"
local passed = raw ~= nil and raw ~= ""
local status = "fail"
if passed then
status = "pass"
end
print("raw=" .. tostring(raw))
print("status=" .. status)
local raw = ""
local passed = raw ~= nil and raw ~= ""
local status = "fail"
if passed then
status = "pass"
end
print("raw=" .. tostring(raw))
print("status=" .. status)
local raw = nil
local passed = raw ~= nil and raw ~= ""
local status = "fail"
if passed then
status = "pass"
end
print("raw=" .. tostring(raw))
print("status=" .. status)
raw ← name, passed ← true, status ← fail
1local raw→ name = "name" --@raw="", nil2local passed→ true = rawname ~= nil and raw ~= ""3local status→ fail = "fail"4if passed thenstatus ← pass
3local status = "fail"4if passedtrue then5 status→ pass = "pass"6endprint("raw=" .. tostring(raw))
6end7print("raw=" .. tostring(rawname))8print("status=" .. statuspass)outputraw=name status=pass
raw ← (empty), passed ← false, status ← fail
1local raw→ (empty) = ""2local passed→ false = raw(empty) ~= nil and raw ~= ""3local status→ fail = "fail"4if passed then5 status = "pass"6end7print("raw=" .. tostring(raw(empty)))8print("status=" .. statusfail)outputraw= status=fail
raw ← nil, passed ← false, status ← fail
1local raw→ nil = nil2local passed→ false = rawnil ~= nil and raw ~= ""3local status→ fail = "fail"4if passed then5 status = "pass"6end7print("raw=" .. tostring(rawnil))8print("status=" .. statusfail)outputraw=nil status=fail