local Z=require(script.Parent.TestPlanner)local X=require(script.Parent.TestRunner)local v=require(script.Parent.Reporters.TextReporter)local o={}local function h(Z)return Z:gsub("\037\046\115\112\101\099\036","")end local function g(Z)return Z:IsA("\077\111\100\117\108\101\083\099\114\105\112\116")and Z.Name:match("\037\046\115\112\101\099\036")end local function b(Z,X)X=X or game local v={}local o=Z if o.Name=="\105\110\105\116\046\115\112\101\099"then o=o.Parent end while o~=nil and o~=X do table.insert(v,h(o.Name))o=o.Parent end table.insert(v,h(X.Name))return v end local function S(Z)local X=""local v=true for Z,o in ipairs(Z)do if v then X=o v=false else X=o..(" "..X)end end return X end function o.getModulesImpl(o,Z,X,v)X=X or{}v=v or Z if g(v)then local o=require(v)local h=b(v,Z)local g=S(h)table.insert(X,{method=o;path=h;pathStringForSorting=g:lower()})end end function o.getModules(X,Z)local v={}X:getModulesImpl(Z,v)for o,h in ipairs(Z:GetDescendants())do X:getModulesImpl(Z,v,h)end return v end function o.run(b,o,h,g)h=h or v g=g or{}local S=g.showTimingInfo or false local i=g.testNamePattern local w=g.extraEnvironment or{}if type(o)~="\116\097\098\108\101"then error(("\066\097\100 \097\114\103\117\109\101\110\116 \035\049 \116\111 \084\101\115\116\066\111\111\116\115\116\114\097\112\058\114\117\110\046 \069\120\112\101\099\116\101\100 \116\097\098\108\101\044 \103\111\116 \037\115"):format(typeof(o)),2)end local K=tick()local P={}for Z,X in ipairs(o)do local v=b:getModules(X)for Z,X in ipairs(v)do table.insert(P,X)end end local x=tick()local d=Z.createPlan(P,i,w)local s=tick()local J=X.runPlan(d)local z=tick()h.report(J)local Y=tick()if S then local Z={("\084\111\111\107 \037\102 \115\101\099\111\110\100\115 \116\111 \108\111\099\097\116\101 \116\101\115\116 \109\111\100\117\108\101\115"):format(x-K),("\084\111\111\107 \037\102 \115\101\099\111\110\100\115 \116\111 \099\114\101\097\116\101 \116\101\115\116 \112\108\097\110"):format(s-x);("\084\111\111\107 \037\102 \115\101\099\111\110\100\115 \116\111 \114\117\110 \116\101\115\116\115"):format(z-s);("\084\111\111\107 \037\102 \115\101\099\111\110\100\115 \116\111 \114\101\112\111\114\116 \116\101\115\116\115"):format(Y-z)}print(table.concat(Z,"\n"))end return J end return o
--[[
	Provides an interface to quickly run and report tests from a given object.
]]

local TestPlanner = require(script.Parent.TestPlanner)
local TestRunner = require(script.Parent.TestRunner)
local TextReporter = require(script.Parent.Reporters.TextReporter)

local TestBootstrap = {}

local function stripSpecSuffix(name)
	return (name:gsub("%.spec$", ""))
end
local function isSpecScript(aScript)
	return aScript:IsA("ModuleScript") and aScript.Name:match("%.spec$")
end

local function getPath(module, root)
	root = root or game

	local path = {}
	local last = module

	if last.Name == "init.spec" then
		-- Use the directory's node for init.spec files.
		last = last.Parent
	end

	while last ~= nil and last ~= root do
		table.insert(path, stripSpecSuffix(last.Name))
		last = last.Parent
	end
	table.insert(path, stripSpecSuffix(root.Name))

	return path
end

local function toStringPath(tablePath)
	local stringPath = ""
	local first = true
	for _, element in ipairs(tablePath) do
		if first then
			stringPath = element
			first = false
		else
			stringPath = element .. " " .. stringPath
		end
	end
	return stringPath
end

function TestBootstrap:getModulesImpl(root, modules, current)
	modules = modules or {}
	current = current or root

	if isSpecScript(current) then
		local method = require(current)
		local path = getPath(current, root)
		local pathString = toStringPath(path)

		table.insert(modules, {
			method = method,
			path = path,
			pathStringForSorting = pathString:lower()
		})
	end
end

--[[
	Find all the ModuleScripts in this tree that are tests.
]]
function TestBootstrap:getModules(root)
	local modules = {}

	self:getModulesImpl(root, modules)

	for _, child in ipairs(root:GetDescendants()) do
		self:getModulesImpl(root, modules, child)
	end

	return modules
end

--[[
	Runs all test and reports the results using the given test reporter.

	If no reporter is specified, a reasonable default is provided.

	This function demonstrates the expected workflow with this testing system:
	1. Locate test modules
	2. Generate test plan
	3. Run test plan
	4. Report test results

	This means we could hypothetically present a GUI to the developer that shows
	the test plan before we execute it, allowing them to toggle specific tests
	before they're run, but after they've been identified!
]]
function TestBootstrap:run(roots, reporter, otherOptions)
	reporter = reporter or TextReporter

	otherOptions = otherOptions or {}
	local showTimingInfo = otherOptions["showTimingInfo"] or false
	local testNamePattern = otherOptions["testNamePattern"]
	local extraEnvironment = otherOptions["extraEnvironment"] or {}

	if type(roots) ~= "table" then
		error(("Bad argument #1 to TestBootstrap:run. Expected table, got %s"):format(typeof(roots)), 2)
	end

	local startTime = tick()

	local modules = {}
	for _, subRoot in ipairs(roots) do
		local newModules = self:getModules(subRoot)

		for _, newModule in ipairs(newModules) do
			table.insert(modules, newModule)
		end
	end

	local afterModules = tick()

	local plan = TestPlanner.createPlan(modules, testNamePattern, extraEnvironment)
	local afterPlan = tick()

	local results = TestRunner.runPlan(plan)
	local afterRun = tick()

	reporter.report(results)
	local afterReport = tick()

	if showTimingInfo then
		local timing = {
			("Took %f seconds to locate test modules"):format(afterModules - startTime),
			("Took %f seconds to create test plan"):format(afterPlan - afterModules),
			("Took %f seconds to run tests"):format(afterRun - afterPlan),
			("Took %f seconds to report tests"):format(afterReport - afterRun),
		}

		print(table.concat(timing, "\n"))
	end

	return results
end

return TestBootstrap