1
0
Fork 0
mirror of https://github.com/Reuh/candran.git synced 2025-10-27 09:59:29 +00:00
Updated tests, added a few usefull options to #import()
This commit is contained in:
Étienne Fildadut 2017-08-19 19:05:05 +02:00
parent 83156361cd
commit 97454746b8
6 changed files with 197 additions and 53 deletions

View file

@ -1,20 +1,16 @@
print("========================")
print("|| CANDRAN TESTS ||")
print("========================")
local candran = dofile(arg[1] or "../candran.lua")
-- test helper
local results = {} -- tests result
local function test(name, candranCode, result, args)
local function test(name, candranCode, expectedResult, options)
results[name] = { result = "not finished", message = "no info" }
local self = results[name]
-- make code
local success, code = pcall(candran.make, candranCode, args)
local success, code = pcall(candran.make, candranCode, options)
if not success then
self.result = "error"
self.message = "error while making code :\n"..code
self.message = "error while making code:\n"..code
return
end
@ -22,7 +18,7 @@ local function test(name, candranCode, result, args)
local success, func = pcall(loadstring or load, code)
if not success then
self.result = "error"
self.message = "error while loading code :\n"..func
self.message = "error while loading code:\n"..func
return
end
@ -30,14 +26,14 @@ local function test(name, candranCode, result, args)
local success, output = pcall(func)
if not success then
self.result = "error"
self.message = "error while running code :\n"..output
self.message = "error while running code:\n"..output
return
end
-- check result
if output ~= result then
if output ~= expectedResult then
self.result = "fail"
self.message = "invalid result from the code; it returned "..tostring(output).." instead of "..tostring(result)
self.message = "invalid result from the code; it returned "..tostring(output).." instead of "..tostring(expectedResult)
return
else
self.result = "success"
@ -48,10 +44,15 @@ end
-- tests
print("Running tests...")
------------------
-- PREPROCESSOR --
------------------
test("preprocessor", [[
#local foo = true
return true
]], true)
test("preprocessor condition", [[
#local foo = true
#if not foo then
@ -60,23 +61,45 @@ test("preprocessor condition", [[
return true
#end
]], true)
test("preprocessor args table", [[
test("preprocessor candran table", [[
#write(("return %q"):format(candran.VERSION))
]], candran.VERSION)
test("preprocessor output variable", [[
#output = "return 5"
]], 5)
test("preprocessor import function", [[
#import("toInclude")
return toInclude
]], 5)
test("preprocessor include function", [[
#include('toInclude.lua')
]], 5)
test("preprocessor write function", [[
#write("local a = true")
return a
]], true)
test("preprocessor placeholder function", [[
#placeholder('foo')
]], 5, { foo = "return 5" })
test("preprocessor options", [[
#if not foo == "sky" then
# error("Invalid foo argument")
#end
return true
]], true, { foo = "sky" })
test("preprocessor write function", [[
#write("local a = true")
return a
]], true)
test("preprocessor import function", [[
#import("toInclude")
return toInclude
]], 5)
test("preprocessor include function", "a = [[\n#include('toInclude.lua')\n]]\nreturn a",
"local a = 5\nreturn a\n\n")
----------------------
-- SYNTAX ADDITIONS --
----------------------
-- Assignement operators
test("+=", [[
local a = 5
a += 2
@ -97,6 +120,11 @@ local a = 5
a /= 2
return a
]], 5/2)
test("//=", [[
local a = 5
a //= 2
return a
]], 2)
test("^=", [[
local a = 5
a ^= 2
@ -112,6 +140,72 @@ local a = "hello"
a ..= " world"
return a
]], "hello world")
test("and=", [[
local a = true
a and= "world"
return a
]], "world")
test("or=", [[
local a = false
a or= "world"
return a
]], "world")
test("&=", [[
local a = 5
a &= 3
return a
]], 1)
test("|=", [[
local a = 5
a |= 3
return a
]], 7)
test("<<=", [[
local a = 23
a <<= 2
return a
]], 92)
test(">>=", [[
local a = 23
a >>= 2
return a
]], 5)
test("right assigments operators", [[
local a = 5
a =+ 2 assert(a == 7, "=+")
a =- 2 assert(a == -5, "=-")
a =* -2 assert(a == 10, "=*")
a =/ 2 assert(a == 0.2, "=/")
a =// 2 assert(a == 10, "=//")
a =^ 2 assert(a == 1024, "=^")
a =% 2000 assert(a == 976, "=%")
a = "world"
a =.. "hello " assert(a == "hello world", "=..")
a =and true assert(a == "hello world", "=and")
a = false
a =or nil assert(a == false, "=or")
a = 3
a =& 5 assert(a == 1, '=&')
a =| 20 assert(a == 21, "=|")
a =<< 1 assert(a == 2097152, "=<<")
a = 2
a =>> 23 assert(a == 5, "=>>")
]], nil)
test("some left+right assigments operators", [[
local a = 5
a -=+ 2 assert(a == 8, "-=+")
a = "hello"
a ..=.. " world " assert(a == "hello world hello", "..=..")
]], nil)
-- Default function parameters
test("default parameters", [[
local function test(hey, def="re", no, foo=("bar"):gsub("bar", "batru"))
return def..foo
@ -119,22 +213,68 @@ end
return test(78, "SANDWICH", true)
]], "SANDWICHbatru")
-- results
print("=====================")
print("|| RESULTS ||")
print("=====================")
-- @ self alias
test("@ as self alias", [[
local a = {}
function a:hey()
return @ == self
end
return a:hey()
]], true)
test("@ as self alias and indexation", [[
local a = {
foo = "Hoi"
}
function a:hey()
return @.foo
end
return a:hey()
]], "Hoi")
test("@name indexation", [[
local a = {
foo = "Hoi"
}
function a:hey()
return @foo
end
return a:hey()
]], "Hoi")
test("@[expt] indexation", [[
local a = {
foo = "Hoi"
}
function a:hey()
return @["foo"]
end
return a:hey()
]], "Hoi")
-- Short anonymous functions declaration
test("short anonymous function declaration", [[
local a = (arg1)
return arg1
end
return a(5)
]], 5)
test("short anonymous method declaration", [[
local a = :(arg1)
return self + arg1
end
return a(2, 3)
]], 5)
-- results
local resultCounter = {}
local testCounter = 0
for name, test in pairs(results) do
for name, t in pairs(results) do
-- print errors & fails
if test.result ~= "success" then
print("Test \""..name.."\" : "..test.result)
if test.message then print(test.message) end
if t.result ~= "success" then
print(name.." test: "..t.result)
if t.message then print(t.message) end
print("----------")
end
-- count tests results
resultCounter[test.result] = (resultCounter[test.result] or 0) + 1
resultCounter[t.result] = (resultCounter[t.result] or 0) + 1
testCounter = testCounter + 1
end