local a=require(script.Parent.TestEnum)local O=require(script.Parent.TestResults)local Y=require(script.Parent.Context)local m=require(script.Parent.ExpectationContext)local S={}S.__index=S function S.new(a)local Y={results=O.new(a);nodeStack={},contextStack={},expectationContextStack={},hasFocusNodes=false}setmetatable(Y,S)return Y end function S.calculateTotals(O)local Y=O.results Y.successCount=0 Y.failureCount=0 Y.skippedCount=0 Y:visitAllNodes(function(O)local m=O.status local S=O.planNode.type if S==a.NodeType.It then if m==a.TestStatus.Success then Y.successCount=Y.successCount+1 elseif m==a.TestStatus.Failure then Y.failureCount=Y.failureCount+1 elseif m==a.TestStatus.Skipped then Y.skippedCount=Y.skippedCount+1 end end end)end function S.gatherErrors(a)local O=a.results O.errors={}O:visitAllNodes(function(a)if#a.errors>0 then for a,Y in ipairs(a.errors)do table.insert(O.errors,Y)end end end)end function S.finalize(a)if#a.nodeStack~=0 then error("\067\097\110\110\111\116 \102\105\110\097\108\105\122\101 \084\101\115\116\082\101\115\117\108\116\115 \119\105\116\104 \110\111\100\101\115 \115\116\105\108\108 \111\110 \116\104\101 \115\116\097\099\107\033",2)end a:calculateTotals()a:gatherErrors()return a.results end function S.pushNode(S,a)local E=O.createNode(a)local w=S.nodeStack[#S.nodeStack]or S.results table.insert(w.children,E)table.insert(S.nodeStack,E)local A=S.contextStack[#S.contextStack]local I=Y.new(A)table.insert(S.contextStack,I)local d=S.expectationContextStack[#S.expectationContextStack]local B=m.new(d)table.insert(S.expectationContextStack,B)end function S.popNode(a)assert(#a.nodeStack>0,"\084\114\105\101\100 \116\111 \112\111\112 \102\114\111\109 \097\110 \101\109\112\116\121 \110\111\100\101 \115\116\097\099\107\033")table.remove(a.nodeStack,#a.nodeStack)table.remove(a.contextStack,#a.contextStack)table.remove(a.expectationContextStack,#a.expectationContextStack)end function S.getContext(a)assert(#a.contextStack>0,"\084\114\105\101\100 \116\111 \103\101\116 \099\111\110\116\101\120\116 \102\114\111\109 \097\110 \101\109\112\116\121 \115\116\097\099\107\033")return a.contextStack[#a.contextStack]end function S.getExpectationContext(a)assert(#a.expectationContextStack>0,"\084\114\105\101\100 \116\111 \103\101\116 \101\120\112\101\099\116\097\116\105\111\110\067\111\110\116\101\120\116 \102\114\111\109 \097\110 \101\109\112\116\121 \115\116\097\099\107\033")return a.expectationContextStack[#a.expectationContextStack]end function S.shouldSkip(O)if O.hasFocusNodes then for Y=#O.nodeStack,1,-1 do local m=O.nodeStack[Y]if m.planNode.modifier==a.NodeModifier.Skip then return true end if m.planNode.modifier==a.NodeModifier.Focus then return false end end return true else for Y=#O.nodeStack,1,-1 do local m=O.nodeStack[Y]if m.planNode.modifier==a.NodeModifier.Skip then return true end end end return false end function S.setSuccess(O)assert(#O.nodeStack>0,"\065\116\116\101\109\112\116\105\110\103 \116\111 \115\101\116 \115\117\099\099\101\115\115 \115\116\097\116\117\115 \111\110 \101\109\112\116\121 \115\116\097\099\107")O.nodeStack[#O.nodeStack].status=a.TestStatus.Success end function S.setSkipped(O)assert(#O.nodeStack>0,"\065\116\116\101\109\112\116\105\110\103 \116\111 \115\101\116 \115\107\105\112\112\101\100 \115\116\097\116\117\115 \111\110 \101\109\112\116\121 \115\116\097\099\107")O.nodeStack[#O.nodeStack].status=a.TestStatus.Skipped end function S.setError(Y,O)assert(#Y.nodeStack>0,"\065\116\116\101\109\112\116\105\110\103 \116\111 \115\101\116 \101\114\114\111\114 \115\116\097\116\117\115 \111\110 \101\109\112\116\121 \115\116\097\099\107")local m=Y.nodeStack[#Y.nodeStack]m.status=a.TestStatus.Failure table.insert(m.errors,O)end function S.addDummyError(m,O,Y)m:pushNode({type=a.NodeType.It;phrase=O})m:setError(Y)m:popNode()m.nodeStack[#m.nodeStack].status=a.TestStatus.Failure end function S.setStatusFromChildren(O)assert(#O.nodeStack>0,"\065\116\116\101\109\112\116\105\110\103 \116\111 \115\101\116 \115\116\097\116\117\115 \102\114\111\109 \099\104\105\108\100\114\101\110 \111\110 \101\109\112\116\121 \115\116\097\099\107")local Y=O.nodeStack[#O.nodeStack]local m=a.TestStatus.Success local S=true for O,Y in ipairs(Y.children)do if Y.status~=a.TestStatus.Skipped then S=false if Y.status==a.TestStatus.Failure then m=a.TestStatus.Failure end end end if S then m=a.TestStatus.Skipped end Y.status=m end return S
--[[
	Represents the state relevant while executing a test plan.

	Used by TestRunner to produce a TestResults object.

	Uses the same tree building structure as TestPlanBuilder; TestSession keeps
	track of a stack of nodes that represent the current path through the tree.
]]

local TestEnum = require(script.Parent.TestEnum)
local TestResults = require(script.Parent.TestResults)
local Context = require(script.Parent.Context)
local ExpectationContext = require(script.Parent.ExpectationContext)

local TestSession = {}

TestSession.__index = TestSession

--[[
	Create a TestSession related to the given TestPlan.

	The resulting TestResults object will be linked to this TestPlan.
]]
function TestSession.new(plan)
	local self = {
		results = TestResults.new(plan),
		nodeStack = {},
		contextStack = {},
		expectationContextStack = {},
		hasFocusNodes = false
	}

	setmetatable(self, TestSession)

	return self
end

--[[
	Calculate success, failure, and skipped test counts in the tree at the
	current point in the execution.
]]
function TestSession:calculateTotals()
	local results = self.results

	results.successCount = 0
	results.failureCount = 0
	results.skippedCount = 0

	results:visitAllNodes(function(node)
		local status = node.status
		local nodeType = node.planNode.type

		if nodeType == TestEnum.NodeType.It then
			if status == TestEnum.TestStatus.Success then
				results.successCount = results.successCount + 1
			elseif status == TestEnum.TestStatus.Failure then
				results.failureCount = results.failureCount + 1
			elseif status == TestEnum.TestStatus.Skipped then
				results.skippedCount = results.skippedCount + 1
			end
		end
	end)
end

--[[
	Gathers all of the errors reported by tests and puts them at the top level
	of the TestResults object.
]]
function TestSession:gatherErrors()
	local results = self.results

	results.errors = {}

	results:visitAllNodes(function(node)
		if #node.errors > 0 then
			for _, message in ipairs(node.errors) do
				table.insert(results.errors, message)
			end
		end
	end)
end

--[[
	Calculates test totals, verifies the tree is valid, and returns results.
]]
function TestSession:finalize()
	if #self.nodeStack ~= 0 then
		error("Cannot finalize TestResults with nodes still on the stack!", 2)
	end

	self:calculateTotals()
	self:gatherErrors()

	return self.results
end

--[[
	Create a new test result node and push it onto the navigation stack.
]]
function TestSession:pushNode(planNode)
	local node = TestResults.createNode(planNode)
	local lastNode = self.nodeStack[#self.nodeStack] or self.results
	table.insert(lastNode.children, node)
	table.insert(self.nodeStack, node)

	local lastContext = self.contextStack[#self.contextStack]
	local context = Context.new(lastContext)
	table.insert(self.contextStack, context)

	local lastExpectationContext = self.expectationContextStack[#self.expectationContextStack]
	local expectationContext = ExpectationContext.new(lastExpectationContext)
	table.insert(self.expectationContextStack, expectationContext)
end

--[[
	Pops a node off of the navigation stack.
]]
function TestSession:popNode()
	assert(#self.nodeStack > 0, "Tried to pop from an empty node stack!")
	table.remove(self.nodeStack, #self.nodeStack)
	table.remove(self.contextStack, #self.contextStack)
	table.remove(self.expectationContextStack, #self.expectationContextStack)
end

--[[
	Gets the Context object for the current node.
]]
function TestSession:getContext()
	assert(#self.contextStack > 0, "Tried to get context from an empty stack!")
	return self.contextStack[#self.contextStack]
end


function TestSession:getExpectationContext()
	assert(#self.expectationContextStack > 0, "Tried to get expectationContext from an empty stack!")
	return self.expectationContextStack[#self.expectationContextStack]
end

--[[
	Tells whether the current test we're in should be skipped.
]]
function TestSession:shouldSkip()
	-- If our test tree had any exclusive tests, then normal tests are skipped!
	if self.hasFocusNodes then
		for i = #self.nodeStack, 1, -1 do
			local node = self.nodeStack[i]

			-- Skipped tests are still skipped
			if node.planNode.modifier == TestEnum.NodeModifier.Skip then
				return true
			end

			-- Focused tests are the only ones that aren't skipped
			if node.planNode.modifier == TestEnum.NodeModifier.Focus then
				return false
			end
		end

		return true
	else
		for i = #self.nodeStack, 1, -1 do
			local node = self.nodeStack[i]

			if node.planNode.modifier == TestEnum.NodeModifier.Skip then
				return true
			end
		end
	end

	return false
end

--[[
	Set the current node's status to Success.
]]
function TestSession:setSuccess()
	assert(#self.nodeStack > 0, "Attempting to set success status on empty stack")
	self.nodeStack[#self.nodeStack].status = TestEnum.TestStatus.Success
end

--[[
	Set the current node's status to Skipped.
]]
function TestSession:setSkipped()
	assert(#self.nodeStack > 0, "Attempting to set skipped status on empty stack")
	self.nodeStack[#self.nodeStack].status = TestEnum.TestStatus.Skipped
end

--[[
	Set the current node's status to Failure and adds a message to its list of
	errors.
]]
function TestSession:setError(message)
	assert(#self.nodeStack > 0, "Attempting to set error status on empty stack")
	local last = self.nodeStack[#self.nodeStack]
	last.status = TestEnum.TestStatus.Failure
	table.insert(last.errors, message)
end

--[[
	Add a dummy child node to the current node to hold the given error. This
	allows an otherwise empty describe node to report an error in a more natural
	way.
]]
function TestSession:addDummyError(phrase, message)
	self:pushNode({type = TestEnum.NodeType.It, phrase = phrase})
	self:setError(message)
	self:popNode()
	self.nodeStack[#self.nodeStack].status = TestEnum.TestStatus.Failure
end

--[[
	Set the current node's status based on that of its children. If all children
	are skipped, mark it as skipped. If any are fails, mark it as failed.
	Otherwise, mark it as success.
]]
function TestSession:setStatusFromChildren()
	assert(#self.nodeStack > 0, "Attempting to set status from children on empty stack")

	local last = self.nodeStack[#self.nodeStack]
	local status = TestEnum.TestStatus.Success
	local skipped = true

	-- If all children were skipped, then we were skipped
	-- If any child failed, then we failed!
	for _, child in ipairs(last.children) do
		if child.status ~= TestEnum.TestStatus.Skipped then
			skipped = false

			if child.status == TestEnum.TestStatus.Failure then
				status = TestEnum.TestStatus.Failure
			end
		end
	end

	if skipped then
		status = TestEnum.TestStatus.Skipped
	end

	last.status = status
end

return TestSession
