test: separate parent from root

This commit is contained in:
littensy 2025-05-11 14:45:24 -07:00
parent 4e5f656158
commit 546df8cabe

View file

@ -60,7 +60,7 @@ local ERROR = 4
local tests: { Test } = {} local tests: { Test } = {}
local descriptions: { Description } = {} local descriptions: { Description } = {}
local parent: Description = { local root: Description = {
status = PENDING, status = PENDING,
name = "root", name = "root",
only = false, only = false,
@ -71,6 +71,7 @@ local parent: Description = {
skips = 0, skips = 0,
runtime = 0, runtime = 0,
} }
local parent: Description = root
local function describe(name: string, run: () -> ()): Description local function describe(name: string, run: () -> ()): Description
local this: Description = { local this: Description = {
@ -120,7 +121,7 @@ end
local function only() local function only()
parent.only = true parent.only = true
parent.only = true root.only = true
end end
local function testSkip(name: string, run: () -> ()): Test local function testSkip(name: string, run: () -> ()): Test
@ -132,7 +133,7 @@ end
local function testOnly(name: string, run: () -> ()): Test local function testOnly(name: string, run: () -> ()): Test
local this = test(name, run) local this = test(name, run)
this.only = true this.only = true
parent.only = true root.only = true
return this return this
end end
@ -145,17 +146,17 @@ end
local function describeOnly(name: string, run: () -> ()): Description local function describeOnly(name: string, run: () -> ()): Description
local this = describe(name, run) local this = describe(name, run)
this.only = true this.only = true
parent.only = true root.only = true
return this return this
end end
local function isOnly(test: Test): boolean local function isOnly(test: Test): boolean
if not parent.only or test.only then if not root.only or test.only then
return true return true
end end
local description = test.parent local description = test.parent
while description and description ~= parent do while description and description ~= root do
if description.only then if description.only then
return true return true
elseif description.skip then elseif description.skip then
@ -176,7 +177,7 @@ local function isSkip(test: Test): boolean
local description = test.parent local description = test.parent
while description do while description do
if description.only and description ~= parent then if description.only and description ~= root then
return false return false
elseif description.skip then elseif description.skip then
return true return true
@ -246,7 +247,7 @@ end
local function getIndent(node: Description | Test): string local function getIndent(node: Description | Test): string
local depth = 0 local depth = 0
local description = node.parent local description = node.parent
while description and description ~= parent do while description and description ~= root do
depth += 1 depth += 1
description = description.parent description = description.parent
end end
@ -256,7 +257,7 @@ end
local function getPath(node: Description | Test): string local function getPath(node: Description | Test): string
local path = { "" } local path = { "" }
local description = node.parent local description = node.parent
while description and description ~= parent do while description and description ~= root do
table.insert(path, 1, description.name) table.insert(path, 1, description.name)
description = description.parent description = description.parent
end end
@ -295,7 +296,7 @@ local function report()
if description.skips > 0 then if description.skips > 0 then
text ..= " " .. color.blue(`({description.skips} skipped)`) text ..= " " .. color.blue(`({description.skips} skipped)`)
elseif description.fails == 0 then elseif description.fails == 0 then
text ..= " " .. color.gray(`({string.format("%.2fms", description.runtime * 1000)})`) text ..= " " .. color.gray(string.format("%.2fms", description.runtime * 1000))
end end
table.insert(output, text) table.insert(output, text)
@ -352,8 +353,8 @@ local function report()
local describes = local describes =
`{greenOrRed(`{describePasses} passed`, describeFails == 0)} {color.gray(`({#descriptions})`)}` `{greenOrRed(`{describePasses} passed`, describeFails == 0)} {color.gray(`({#descriptions})`)}`
local tests = `{greenOrRed(`{parent.passes} passed`, parent.fails == 0)} {color.gray(`({#tests})`)}` local tests = `{greenOrRed(`{root.passes} passed`, root.fails == 0)} {color.gray(`({#tests})`)}`
local duration = `{color.blue(`{string.format("%.2fms", parent.runtime * 1000)}`)}` local duration = `{color.blue(`{string.format("%.2fms", root.runtime * 1000)}`)}`
table.insert(output, "") table.insert(output, "")
table.insert(output, `Describe {describes}`) table.insert(output, `Describe {describes}`)
@ -363,7 +364,7 @@ local function report()
end end
local function reportErrors() local function reportErrors()
if parent.fails == 0 then if root.fails == 0 then
return return
end end
@ -379,7 +380,7 @@ local function report()
table.insert(output, color.blue("Test Results")) table.insert(output, color.blue("Test Results"))
table.insert(output, "") table.insert(output, "")
for _, child in parent.children do for _, child in root.children do
reportNode(child) reportNode(child)
end end
@ -388,8 +389,8 @@ local function report()
print(table.concat(output, "\n")) print(table.concat(output, "\n"))
if parent.fails > 0 then if root.fails > 0 then
error(color.red(`{parent.fails} test(s) failed`)) error(color.red(`{root.fails} test(s) failed`))
end end
end end
@ -400,7 +401,7 @@ local function finish(config: Config?)
end end
runTests() runTests()
updateDescription(parent) updateDescription(root)
report() report()
end end