mirror of
https://github.com/littensy/charm.git
synced 2026-10-22 04:14:08 +00:00
test: prettify test library
This commit is contained in:
parent
b61d0b3b86
commit
554010f1bd
6 changed files with 129 additions and 27 deletions
|
|
@ -3,7 +3,7 @@ name = "littensy/charm-sync"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
registry = "https://github.com/UpliftGames/wally-index"
|
registry = "https://github.com/UpliftGames/wally-index"
|
||||||
realm = "shared"
|
realm = "shared"
|
||||||
exclude = ["Charm.luau"]
|
exclude = ["tests"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
Charm = "littensy/charm@^0.10"
|
Charm = "littensy/charm@^0.10"
|
||||||
|
|
|
||||||
|
|
@ -3,5 +3,6 @@ name = "littensy/charm"
|
||||||
version = "0.10.0"
|
version = "0.10.0"
|
||||||
registry = "https://github.com/UpliftGames/wally-index"
|
registry = "https://github.com/UpliftGames/wally-index"
|
||||||
realm = "shared"
|
realm = "shared"
|
||||||
|
exclude = ["tests"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ name = "littensy/react-charm"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
registry = "https://github.com/UpliftGames/wally-index"
|
registry = "https://github.com/UpliftGames/wally-index"
|
||||||
realm = "shared"
|
realm = "shared"
|
||||||
exclude = ["Charm.luau", "React.luau"]
|
exclude = ["tests"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
Charm = "littensy/charm@^0.10"
|
Charm = "littensy/charm@^0.10"
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ name = "littensy/vide-charm"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
registry = "https://github.com/UpliftGames/wally-index"
|
registry = "https://github.com/UpliftGames/wally-index"
|
||||||
realm = "shared"
|
realm = "shared"
|
||||||
exclude = ["Charm.luau", "Vide.luau"]
|
exclude = ["tests"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
Charm = "littensy/charm@^0.10"
|
Charm = "littensy/charm@^0.10"
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,10 @@ local suite = require("./suite")
|
||||||
local describe = suite.describe
|
local describe = suite.describe
|
||||||
local finish = suite.finish
|
local finish = suite.finish
|
||||||
|
|
||||||
|
if not script then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
_G.__DEV__ = true
|
_G.__DEV__ = true
|
||||||
|
|
||||||
describe("charm-sync", function()
|
describe("charm-sync", function()
|
||||||
|
|
|
||||||
145
tests/suite.luau
145
tests/suite.luau
|
|
@ -1,64 +1,161 @@
|
||||||
|
local color = {
|
||||||
|
white = function(text: string | number)
|
||||||
|
return `\27[37;1m{text}\27[0m`
|
||||||
|
end,
|
||||||
|
gray = function(text: string | number)
|
||||||
|
return `\27[30;1m{text}\27[0m`
|
||||||
|
end,
|
||||||
|
green = function(text: string | number)
|
||||||
|
return `\27[32;1m{text}\27[0m`
|
||||||
|
end,
|
||||||
|
red = function(text: string | number)
|
||||||
|
return `\27[31;1m{text}\27[0m`
|
||||||
|
end,
|
||||||
|
blue = function(text: string | number)
|
||||||
|
return `\27[34;1m{text}\27[0m`
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
if script then
|
||||||
|
local function prepend(prefix: string?): (string | number) -> string
|
||||||
|
return function(text: string | number): string
|
||||||
|
return if prefix then `{prefix} {text}` else `{text}`
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
color.white = prepend()
|
||||||
|
color.gray = prepend()
|
||||||
|
color.green = prepend(utf8.char(0x1F7E2)) -- green circle
|
||||||
|
color.red = prepend(utf8.char(0x1F534)) -- red circle
|
||||||
|
color.blue = prepend(utf8.char(0x1F535)) -- blue circle
|
||||||
|
end
|
||||||
|
|
||||||
|
local WALL = color.gray("│")
|
||||||
|
local PASS = color.green("PASS") .. WALL
|
||||||
|
local FAIL = color.red("FAIL") .. WALL
|
||||||
|
local SKIP = color.blue("SKIP") .. WALL
|
||||||
|
local ERROR = color.red("ERROR") .. WALL
|
||||||
|
|
||||||
local results: { string } = {}
|
local results: { string } = {}
|
||||||
local stack: { string } = {}
|
local stack: { string } = {}
|
||||||
local errors: { string } = {}
|
local errors: { string } = {}
|
||||||
local tests = 0
|
|
||||||
|
local skipDepth = 0
|
||||||
|
local skipCount = 0
|
||||||
|
|
||||||
|
local testsOk = 0
|
||||||
|
local testsFailed = 0
|
||||||
|
local testsSkipped = 0
|
||||||
|
local filesOk = 0
|
||||||
|
local filesFailed = 0
|
||||||
|
|
||||||
|
local start = os.clock()
|
||||||
|
|
||||||
local function log(result: string, index: number?)
|
local function log(result: string, index: number?)
|
||||||
local index = index or #results + 1
|
if index then
|
||||||
local indent = string.rep(" ", #stack - 1)
|
table.insert(results, index, result)
|
||||||
|
else
|
||||||
|
table.insert(results, result)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
results[index] = indent .. result
|
local function skip()
|
||||||
|
skipDepth += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local function test(name: string, callback: () -> ())
|
local function test(name: string, callback: () -> ())
|
||||||
tests += 1
|
local path = color.gray(table.concat(stack, " > "))
|
||||||
|
local line = color.red("-1:")
|
||||||
|
|
||||||
|
if skipDepth > 0 then
|
||||||
|
testsSkipped += 1
|
||||||
|
skipCount += 1
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
table.insert(stack, name)
|
table.insert(stack, name)
|
||||||
|
|
||||||
local success = xpcall(callback, function(err)
|
local success = xpcall(callback, function(err)
|
||||||
local path = table.concat(stack, " > ")
|
line = color.red(`{debug.info(3, "l")}:`)
|
||||||
table.insert(errors, `\n🔴 {path}:\n{err}`)
|
table.insert(errors, `\n{ERROR} {path} > {line}{color.white(name)}\n{err}`)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if not success then
|
if success then
|
||||||
log(`🔴 {name}`)
|
testsOk += 1
|
||||||
|
else
|
||||||
|
testsFailed += 1
|
||||||
|
log(`{FAIL} {path} {color.gray(">")} {line}{color.white(name)}`)
|
||||||
end
|
end
|
||||||
|
|
||||||
table.remove(stack)
|
table.remove(stack)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function describe(name: string, callback: () -> ())
|
local function describe(name: string, callback: () -> ())
|
||||||
|
local path = if #stack > 0 then color.gray(table.concat(stack, " > ")) .. color.gray(" > ") else ""
|
||||||
|
local index = #results + 1
|
||||||
|
local start = os.clock()
|
||||||
|
|
||||||
|
local initialTestsOk = testsOk
|
||||||
|
local initialTestsFailed = testsFailed
|
||||||
|
local initialSkipDepth = skipDepth
|
||||||
|
|
||||||
|
skipCount = 0
|
||||||
|
|
||||||
table.insert(stack, name)
|
table.insert(stack, name)
|
||||||
table.insert(results, name)
|
|
||||||
|
|
||||||
local index = #results
|
|
||||||
local errorCount = #errors
|
|
||||||
callback()
|
callback()
|
||||||
|
|
||||||
if #errors > errorCount then
|
|
||||||
log(`🔴 {name}`, index)
|
|
||||||
else
|
|
||||||
log(`🟢 {name}`, index)
|
|
||||||
end
|
|
||||||
|
|
||||||
table.remove(stack)
|
table.remove(stack)
|
||||||
|
|
||||||
|
skipDepth = initialSkipDepth
|
||||||
|
|
||||||
|
local skippedEveryTest = initialTestsOk == testsOk and initialTestsFailed == testsFailed and skipCount > 0
|
||||||
|
local passedEveryTest = testsFailed == initialTestsFailed
|
||||||
|
|
||||||
|
local millis = string.format("%.2fms", (os.clock() - start) * 1000)
|
||||||
|
local skips = if skipCount > 0 then color.blue(` ({skipCount} skipped) `) else " "
|
||||||
|
|
||||||
|
if skippedEveryTest then
|
||||||
|
filesOk += 1
|
||||||
|
log(`{SKIP} {path .. color.white(name)}{skips}`, index)
|
||||||
|
elseif passedEveryTest then
|
||||||
|
filesOk += 1
|
||||||
|
log(`{PASS} {path .. color.white(name)}{skips}{color.gray(millis)}`, index)
|
||||||
|
else
|
||||||
|
filesFailed += 1
|
||||||
|
log(`{FAIL} {path .. color.white(name)}{skips}{color.gray(millis)}`, index)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function finish()
|
local function finish()
|
||||||
local output = { `\n🔵 {tests - #errors} / {tests} tests passed\n` }
|
local tests = testsOk + testsFailed + testsSkipped
|
||||||
|
local files = filesOk + filesFailed
|
||||||
|
local failed = testsFailed + filesFailed > 0
|
||||||
|
local millis = math.round((os.clock() - start) * 1000)
|
||||||
|
local output = {}
|
||||||
|
|
||||||
|
table.insert(output, color.blue("Test Results\n"))
|
||||||
|
|
||||||
for _, result in results do
|
for _, result in results do
|
||||||
table.insert(output, result)
|
table.insert(output, result)
|
||||||
end
|
end
|
||||||
|
|
||||||
print(table.concat(output, "\n") .. "\n")
|
local redOrGreen: (string | number) -> string = if failed then color.red else color.green
|
||||||
|
|
||||||
if #errors > 0 then
|
table.insert(output, "")
|
||||||
error(`{#errors} test(s) failed:\n{table.concat(errors, "\n")}\n`)
|
table.insert(output, `Test Files {redOrGreen(`{filesOk} passed`)} ({color.gray(`{files}`)})`)
|
||||||
|
table.insert(output, ` Tests {redOrGreen(`{testsOk} passed`)} ({color.gray(`{tests}`)})`)
|
||||||
|
table.insert(output, ` Duration {color.blue(`{millis}ms`)}`)
|
||||||
|
|
||||||
|
print("\n" .. table.concat(output, "\n") .. "\n")
|
||||||
|
|
||||||
|
if failed then
|
||||||
|
print(`{color.red(`{testsFailed} errors:`)}\n{table.concat(errors, "\n")}\n`)
|
||||||
|
error("Tests failed")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
test = test,
|
test = test,
|
||||||
describe = describe,
|
describe = describe,
|
||||||
|
skip = skip,
|
||||||
finish = finish,
|
finish = finish,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue