From b96b0b5abbf40cbdbed7952bf35a5a27ddf75928 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Aug 2019 14:58:02 -0300 Subject: Added macro 'luaL_pushfail' The macro 'luaL_pushfail' documents all places in the standard libraries that return nil to signal some kind of failure. It is defined as 'lua_pushnil'. The manual also got a notation (@fail) to document those returns. The tests were changed to be agnostic regarding whether 'fail' is 'nil' or 'false'. --- testes/api.lua | 2 +- testes/db.lua | 6 ++-- testes/errors.lua | 10 +++--- testes/files.lua | 30 ++++++++-------- testes/literals.lua | 2 +- testes/math.lua | 100 ++++++++++++++++++++++++++-------------------------- testes/pm.lua | 22 ++++++------ testes/strings.lua | 6 ++-- testes/utf8.lua | 4 +-- 9 files changed, 91 insertions(+), 91 deletions(-) (limited to 'testes') diff --git a/testes/api.lua b/testes/api.lua index 4f9d6717..b2680633 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -698,7 +698,7 @@ for k, v in ipairs(t) do assert(v1 == v and p) end -assert(debug.getuservalue(4) == nil) +assert(not debug.getuservalue(4)) debug.setuservalue(b, function () return 10 end, 10) collectgarbage() -- function should not be collected diff --git a/testes/db.lua b/testes/db.lua index a64a1130..c43243a6 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -351,12 +351,12 @@ assert(g(0,0) == 30) debug.sethook(nil); -assert(debug.gethook() == nil) +assert(not debug.gethook()) -- minimal tests for setuservalue/getuservalue do - assert(debug.setuservalue(io.stdin, 10) == nil) + assert(not debug.setuservalue(io.stdin, 10)) local a, b = debug.getuservalue(io.stdin, 10) assert(a == nil and not b) end @@ -414,7 +414,7 @@ end, "c") a:f(1,2,3,4,5) assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) assert(XX == 12) -assert(debug.gethook() == nil) +assert(not debug.gethook()) -- testing access to local variables in return hook (bug in 5.2) diff --git a/testes/errors.lua b/testes/errors.lua index 6e7b8004..f9623b1d 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -18,7 +18,7 @@ end local function doit (s) local f, msg = load(s) - if f == nil then return msg end + if not f then return msg end local cond, msg = pcall(f) return (not cond) and msg end @@ -312,8 +312,8 @@ end local function lineerror (s, l) local err,msg = pcall(load(s)) - local line = string.match(msg, ":(%d+):") - assert(tonumber(line) == l) + local line = tonumber(string.match(msg, ":(%d+):")) + assert(line == l or (not line and not l)) end lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2) @@ -359,7 +359,7 @@ local p = [[ g() ]] X=3;lineerror((p), 3) -X=0;lineerror((p), nil) +X=0;lineerror((p), false) X=1;lineerror((p), 2) X=2;lineerror((p), 1) @@ -510,7 +510,7 @@ checksyntax("a\1a = 1", "", "<\\1>", 1) checksyntax("\255a = 1", "", "<\\255>", 1) doit('I = load("a=9+"); a=3') -assert(a==3 and I == nil) +assert(a==3 and not I) print('+') lim = 1000 diff --git a/testes/files.lua b/testes/files.lua index 585e5948..677c0dc2 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -184,7 +184,7 @@ three local f = assert(io.open(file, "r")) -- second item failing l1, n1, n2, dummy = f:read("l", "n", "n", "l") - assert(l1 == "a line" and n1 == nil) + assert(l1 == "a line" and not n1) end assert(os.remove(file)) @@ -228,7 +228,7 @@ assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n") assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e") do -- attempt to read too long number - assert(f:read("n") == nil) -- fails + assert(not f:read("n")) -- fails local s = f:read("L") -- read rest of line assert(string.find(s, "^00*\n$")) -- lots of 0's left end @@ -314,13 +314,13 @@ assert(io.read() == "fourth_line") assert(io.read() == "") -- empty line assert(io.read('n') == 3450) assert(io.read(1) == '\n') -assert(io.read(0) == nil) -- end of file -assert(io.read(1) == nil) -- end of file -assert(io.read(30000) == nil) -- end of file +assert(not io.read(0)) -- end of file +assert(not io.read(1)) -- end of file +assert(not io.read(30000)) -- end of file assert(({io.read(1)})[2] == undef) -assert(io.read() == nil) -- end of file +assert(not io.read()) -- end of file assert(({io.read()})[2] == undef) -assert(io.read('n') == nil) -- end of file +assert(not io.read('n')) -- end of file assert(({io.read('n')})[2] == undef) assert(io.read('a') == '') -- end of file (OK for 'a') assert(io.read('a') == '') -- end of file (OK for 'a') @@ -356,7 +356,7 @@ assert(io.read(string.len(t)) == t) assert(io.read(1) == ' ') assert(io.read(0)) assert(io.read('a') == ';end of file\n') -assert(io.read(0) == nil) +assert(not io.read(0)) assert(io.close(io.input())) @@ -364,7 +364,7 @@ assert(io.close(io.input())) do local function ismsg (m) -- error message is not a code number - return (type(m) == "string" and tonumber(m) == nil) + return (type(m) == "string" and not tonumber(m)) end -- read @@ -393,7 +393,7 @@ assert(io.read"L" == "\n") assert(io.read"L" == "\n") assert(io.read"L" == "line\n") assert(io.read"L" == "other") -assert(io.read"L" == nil) +assert(not io.read"L") io.input():close() local f = assert(io.open(file)) @@ -462,7 +462,7 @@ end -- test for multipe arguments in 'lines' io.output(file); io.write"0123456789\n":close() for a,b in io.lines(file, 1, 1) do - if a == "\n" then assert(b == nil) + if a == "\n" then assert(not b) else assert(tonumber(a) == tonumber(b) - 1) end end @@ -473,13 +473,13 @@ end for a,b,c in io.lines(file, "a", 0, 1) do if a == "" then break end - assert(a == "0123456789\n" and b == nil and c == nil) + assert(a == "0123456789\n" and not b and not c) end collectgarbage() -- to close file in previous iteration io.output(file); io.write"00\n10\n20\n30\n40\n":close() for a, b in io.lines(file, "n", "n") do - if a == 40 then assert(b == nil) + if a == 40 then assert(not b) else assert(a == b - 10) end end @@ -654,7 +654,7 @@ and the rest of the file io.input(file) local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10) assert(io.close(io.input())) -assert(_ == ' ' and __ == nil) +assert(_ == ' ' and not __) assert(type(a) == 'number' and a==123.4 and b==-56e-2) assert(d=='second line' and e=='third line') assert(h==[[ @@ -706,7 +706,7 @@ if not _soft then io.input():seek('set', 0) y = io.read() -- huge line assert(x == y..'\n'..io.read()) - assert(io.read() == nil) + assert(not io.read()) io.close(io.input()) assert(os.remove(file)) x = nil; y = nil diff --git a/testes/literals.lua b/testes/literals.lua index 27f9377d..e101eabf 100644 --- a/testes/literals.lua +++ b/testes/literals.lua @@ -281,7 +281,7 @@ if os.setlocale("pt_BR") or os.setlocale("ptb") then assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1) - assert(tonumber"inf" == nil and tonumber"NAN" == nil) + assert(not tonumber"inf" and not tonumber"NAN") assert(assert(load(string.format("return %q", 4.51)))() == 4.51) diff --git a/testes/math.lua b/testes/math.lua index bad43901..c7dc8285 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -39,7 +39,7 @@ do end assert(math.type(0) == "integer" and math.type(0.0) == "float" - and math.type("10") == nil) + and not math.type("10")) local function checkerror (msg, f, ...) @@ -381,17 +381,17 @@ assert(tonumber(1/0) == 1/0) -- 'tonumber' with strings assert(tonumber("0") == 0) -assert(tonumber("") == nil) -assert(tonumber(" ") == nil) -assert(tonumber("-") == nil) -assert(tonumber(" -0x ") == nil) -assert(tonumber{} == nil) +assert(not tonumber("")) +assert(not tonumber(" ")) +assert(not tonumber("-")) +assert(not tonumber(" -0x ")) +assert(not tonumber{}) assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and tonumber'.01' == 0.01 and tonumber'-1.' == -1 and tonumber'+1.' == 1) -assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and - tonumber'1e' == nil and tonumber'1.0e+' == nil and - tonumber'.' == nil) +assert(not tonumber'+ 0.01' and not tonumber'+.e1' and + not tonumber'1e' and not tonumber'1.0e+' and + not tonumber'.') assert(tonumber('-012') == -010-2) assert(tonumber('-1.2e2') == - - -120) @@ -445,45 +445,45 @@ local function f (...) end end -assert(f(tonumber('fFfa', 15)) == nil) -assert(f(tonumber('099', 8)) == nil) -assert(f(tonumber('1\0', 2)) == nil) -assert(f(tonumber('', 8)) == nil) -assert(f(tonumber(' ', 9)) == nil) -assert(f(tonumber(' ', 9)) == nil) -assert(f(tonumber('0xf', 10)) == nil) +assert(not f(tonumber('fFfa', 15))) +assert(not f(tonumber('099', 8))) +assert(not f(tonumber('1\0', 2))) +assert(not f(tonumber('', 8))) +assert(not f(tonumber(' ', 9))) +assert(not f(tonumber(' ', 9))) +assert(not f(tonumber('0xf', 10))) -assert(f(tonumber('inf')) == nil) -assert(f(tonumber(' INF ')) == nil) -assert(f(tonumber('Nan')) == nil) -assert(f(tonumber('nan')) == nil) +assert(not f(tonumber('inf'))) +assert(not f(tonumber(' INF '))) +assert(not f(tonumber('Nan'))) +assert(not f(tonumber('nan'))) -assert(f(tonumber(' ')) == nil) -assert(f(tonumber('')) == nil) -assert(f(tonumber('1 a')) == nil) -assert(f(tonumber('1 a', 2)) == nil) -assert(f(tonumber('1\0')) == nil) -assert(f(tonumber('1 \0')) == nil) -assert(f(tonumber('1\0 ')) == nil) -assert(f(tonumber('e1')) == nil) -assert(f(tonumber('e 1')) == nil) -assert(f(tonumber(' 3.4.5 ')) == nil) +assert(not f(tonumber(' '))) +assert(not f(tonumber(''))) +assert(not f(tonumber('1 a'))) +assert(not f(tonumber('1 a', 2))) +assert(not f(tonumber('1\0'))) +assert(not f(tonumber('1 \0'))) +assert(not f(tonumber('1\0 '))) +assert(not f(tonumber('e1'))) +assert(not f(tonumber('e 1'))) +assert(not f(tonumber(' 3.4.5 '))) -- testing 'tonumber' for invalid hexadecimal formats -assert(tonumber('0x') == nil) -assert(tonumber('x') == nil) -assert(tonumber('x3') == nil) -assert(tonumber('0x3.3.3') == nil) -- two decimal points -assert(tonumber('00x2') == nil) -assert(tonumber('0x 2') == nil) -assert(tonumber('0 x2') == nil) -assert(tonumber('23x') == nil) -assert(tonumber('- 0xaa') == nil) -assert(tonumber('-0xaaP ') == nil) -- no exponent -assert(tonumber('0x0.51p') == nil) -assert(tonumber('0x5p+-2') == nil) +assert(not tonumber('0x')) +assert(not tonumber('x')) +assert(not tonumber('x3')) +assert(not tonumber('0x3.3.3')) -- two decimal points +assert(not tonumber('00x2')) +assert(not tonumber('0x 2')) +assert(not tonumber('0 x2')) +assert(not tonumber('23x')) +assert(not tonumber('- 0xaa')) +assert(not tonumber('-0xaaP ')) -- no exponent +assert(not tonumber('0x0.51p')) +assert(not tonumber('0x5p+-2')) -- testing hexadecimal numerals @@ -705,19 +705,19 @@ do -- testing floor & ceil assert(eqT(math.tointeger(maxint), maxint)) assert(eqT(math.tointeger(maxint .. ""), maxint)) assert(eqT(math.tointeger(minint + 0.0), minint)) - assert(math.tointeger(0.0 - minint) == nil) - assert(math.tointeger(math.pi) == nil) - assert(math.tointeger(-math.pi) == nil) + assert(not math.tointeger(0.0 - minint)) + assert(not math.tointeger(math.pi)) + assert(not math.tointeger(-math.pi)) assert(math.floor(math.huge) == math.huge) assert(math.ceil(math.huge) == math.huge) - assert(math.tointeger(math.huge) == nil) + assert(not math.tointeger(math.huge)) assert(math.floor(-math.huge) == -math.huge) assert(math.ceil(-math.huge) == -math.huge) - assert(math.tointeger(-math.huge) == nil) + assert(not math.tointeger(-math.huge)) assert(math.tointeger("34.0") == 34) - assert(math.tointeger("34.3") == nil) - assert(math.tointeger({}) == nil) - assert(math.tointeger(0/0) == nil) -- NaN + assert(not math.tointeger("34.3")) + assert(not math.tointeger({})) + assert(not math.tointeger(0/0)) -- NaN end diff --git a/testes/pm.lua b/testes/pm.lua index 4d87fad2..94bb63ca 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -28,10 +28,10 @@ a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end assert(a == 9 and b == 11); a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position assert(a == 11 and b == 11) -assert(string.find('a\0a\0a\0a\0\0ab', 'b\0') == nil) -- check ending -assert(string.find('', '\0') == nil) +assert(not string.find('a\0a\0a\0a\0\0ab', 'b\0')) -- check ending +assert(not string.find('', '\0')) assert(string.find('alo123alo', '12') == 4) -assert(string.find('alo123alo', '^12') == nil) +assert(not string.find('alo123alo', '^12')) assert(string.match("aaab", ".*b") == "aaab") assert(string.match("aaa", ".*a") == "aaa") @@ -57,17 +57,17 @@ assert(f('aaa', 'ab*a') == 'aa') assert(f('aba', 'ab*a') == 'aba') assert(f('aaab', 'a+') == 'aaa') assert(f('aaa', '^.+$') == 'aaa') -assert(f('aaa', 'b+') == nil) -assert(f('aaa', 'ab+a') == nil) +assert(not f('aaa', 'b+')) +assert(not f('aaa', 'ab+a')) assert(f('aba', 'ab+a') == 'aba') assert(f('a$a', '.$') == 'a') assert(f('a$a', '.%$') == 'a$') assert(f('a$a', '.$.') == 'a$a') -assert(f('a$a', '$$') == nil) -assert(f('a$b', 'a$') == nil) +assert(not f('a$a', '$$')) +assert(not f('a$b', 'a$')) assert(f('a$a', '$') == '') assert(f('', 'b*') == '') -assert(f('aaa', 'bb*') == nil) +assert(not f('aaa', 'bb*')) assert(f('aaab', 'a-') == '') assert(f('aaa', '^.-$') == 'aaa') assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab') @@ -101,7 +101,7 @@ end assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o") assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3') assert(f1('=======', '^(=*)=%1$') == '=======') -assert(string.match('==========', '^([=]*)=%1$') == nil) +assert(not string.match('==========', '^([=]*)=%1$')) local function range (i, j) if i <= j then @@ -135,7 +135,7 @@ print('+'); assert(string.match("alo xyzK", "(%w+)K") == "xyz") assert(string.match("254 K", "(%d*)K") == "") assert(string.match("alo ", "(%w*)$") == "") -assert(string.match("alo ", "(%w+)$") == nil) +assert(not string.match("alo ", "(%w+)$")) assert(string.find("(álo)", "%(á") == 1) local a, b, c, d, e = string.match("âlo alo", "^(((.).).* (%w*))$") assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil) @@ -209,7 +209,7 @@ assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) function isbalanced (s) - return string.find(string.gsub(s, "%b()", ""), "[()]") == nil + return not string.find(string.gsub(s, "%b()", ""), "[()]") end assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a")) diff --git a/testes/strings.lua b/testes/strings.lua index 2e0e160f..97875ec0 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -56,13 +56,13 @@ a,b = string.find("123456789", "345") assert(string.sub("123456789", a, b) == "345") assert(string.find("1234567890123456789", "345", 3) == 3) assert(string.find("1234567890123456789", "345", 4) == 13) -assert(string.find("1234567890123456789", "346", 4) == nil) +assert(not string.find("1234567890123456789", "346", 4)) assert(string.find("1234567890123456789", ".45", -9) == 13) -assert(string.find("abcdefg", "\0", 5, 1) == nil) +assert(not string.find("abcdefg", "\0", 5, 1)) assert(string.find("", "") == 1) assert(string.find("", "", 1) == 1) assert(not string.find("", "", 2)) -assert(string.find('', 'aaa', 1) == nil) +assert(not string.find('', 'aaa', 1)) assert(('alo(.)alo'):find('(.)', 1, 1) == 4) assert(string.len("") == 0) diff --git a/testes/utf8.lua b/testes/utf8.lua index acbb181d..5954f6e8 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -30,8 +30,8 @@ local function checksyntax (s, t) assert(assert(load(ts))() == s) end -assert(utf8.offset("alo", 5) == nil) -assert(utf8.offset("alo", -4) == nil) +assert(not utf8.offset("alo", 5)) +assert(not utf8.offset("alo", -4)) -- 'check' makes several tests over the validity of string 's'. -- 't' is the list of codepoints of 's'. -- cgit v1.2.3-55-g6feb