forked from Roblox/testez
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec.lua
99 lines (74 loc) · 2.29 KB
/
spec.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
local LOAD_MODULES = {
{"lib", "TestEZ"},
{"tests", "TestEZTests"},
}
-- This makes sure we can load Lemur and other libraries that depend on init.lua
package.path = package.path .. ";?/init.lua"
-- If this fails, make sure you've cloned all Git submodules.
local lemur = require("modules.lemur")
--[[
Collapses ModuleScripts named 'init' into their parent folders.
This is the same behavior as Rojo.
]]
local function collapse(root)
local init = root:FindFirstChild("init")
if init then
init.Name = root.Name
init.Parent = root.Parent
for _, child in ipairs(root:GetChildren()) do
child.Parent = init
end
root:Destroy()
root = init
end
for _, child in ipairs(root:GetChildren()) do
if child:IsA("Folder") then
collapse(child)
end
end
return root
end
local habitat = lemur.Habitat.new()
local root = lemur.Instance.new("Folder")
root.Name = "Root"
for _, module in ipairs(LOAD_MODULES) do
local container = habitat:loadFromFs(module[1])
container.Name = module[2]
container.Parent = root
end
root = collapse(root)
local function findUnitTests(container, foundTests)
foundTests = foundTests or {}
for _, child in ipairs(container:GetChildren()) do
if child.Name:match("%.spec$") then
table.insert(foundTests, child)
end
findUnitTests(child, foundTests)
end
return foundTests
end
local function findIntegrationTests(container, foundTests)
foundTests = foundTests or {}
for _, child in ipairs(container:GetChildren()) do
if child:IsA("ModuleScript") then
table.insert(foundTests, child)
end
findUnitTests(child, foundTests)
end
return foundTests
end
-- Run all unit tests, which are located in .spec.lua files next to the source
local unitTests = findUnitTests(root.TestEZ)
print(("Running %d unit tests..."):format(#unitTests))
-- Unit tests are expected to load individual files relative to themselves
for _, test in ipairs(unitTests) do
habitat:require(test)()
end
-- Run all integration tests, which are located in the 'tests' folder
local integrationTests = findIntegrationTests(root.TestEZTests)
print(("Running %d integration tests..."):format(#integrationTests))
-- Integration tests should be passed the root TestEZ object
for _, test in ipairs(integrationTests) do
habitat:require(test)(habitat:require(root.TestEZ))
end
print("All tests passed.")