function TestRunner.runPlanNode(Q,c,k)local function s(c,k)local s=true local i _G[RUNNING_GLOBAL]=true k=k or""local K=getfenv(c)for Q,c in pairs(TestRunner.environment)do K[Q]=c end K.fail=function(Q)if Q==nil then Q="\102\097\105\108\040\041 \119\097\115 \099\097\108\108\101\100\046"end s=false i=k..debug.traceback(tostring(Q),2)end K.expect=wrapExpectContextWithPublicApi(Q:getExpectationContext())local r=Q:getContext()local e,p=xpcall(function()c(r)end,function(Q)return k..debug.traceback(tostring(Q),2)end)if not e then s=false i=p end _G[RUNNING_GLOBAL]=nil return s,i end local function i(Q)for Q,c in ipairs(k:getBeforeEachHooks())do local k,i=s(c,"\098\101\102\111\114\101\069\097\099\104 \104\111\111\107\058 ")if not k then return false,i end end local c,i=s(Q.callback)for Q,k in ipairs(k:getAfterEachHooks())do local K,r=s(k,"\097\102\116\101\114\069\097\099\104 \104\111\111\107\058 ")if not K then if not c then return false,i..("\n\087\104\105\108\101 \099\108\101\097\110\105\110\103 \117\112 \116\104\101 \102\097\105\108\101\100 \116\101\115\116 \097\110\111\116\104\101\114 \101\114\114\111\114 \119\097\115 \102\111\117\110\100\058\n"..r)end return false,r end end if not c then return false,i end return true,nil end k:pushHooksFrom(c)local K=false for c,k in ipairs(k:getBeforeAllHooks())do local i,r=s(k,"\098\101\102\111\114\101\065\108\108 \104\111\111\107\058 ")if not i then Q:addDummyError("\098\101\102\111\114\101\065\108\108",r)K=true end end if not K then for c,s in ipairs(c.children)do if s.type==TestEnum.NodeType.It then Q:pushNode(s)if Q:shouldSkip()then Q:setSkipped()else local c,k=i(s)if c then Q:setSuccess()else Q:setError(k)end end Q:popNode()elseif s.type==TestEnum.NodeType.Describe then Q:pushNode(s)TestRunner.runPlanNode(Q,s,k)if s.loadError then local c="\069\114\114\111\114 \100\117\114\105\110\103 \112\108\097\110\110\105\110\103\058 "..s.loadError Q:setError(c)else Q:setStatusFromChildren()end Q:popNode()end end end for c,k in ipairs(k:getAfterAllHooks())do local i,K=s(k,"\097\102\116\101\114\065\108\108 \104\111\111\107\058 ")if not i then Q:addDummyError("\097\102\116\101\114\065\108\108",K)end end k:popHooks()end return TestRunner
--[[
	Run the given test plan node and its descendants, using the given test
	session to store all of the results.
]]
function TestRunner.runPlanNode(session, planNode, lifecycleHooks)
	local function runCallback(callback, messagePrefix)
		local success = true
		local errorMessage
		-- Any code can check RUNNING_GLOBAL to fork behavior based on
		-- whether a test is running. We use this to avoid accessing
		-- protected APIs; it's a workaround that will go away someday.
		_G[RUNNING_GLOBAL] = true

		messagePrefix = messagePrefix or ""

		local testEnvironment = getfenv(callback)

		for key, value in pairs(TestRunner.environment) do
			testEnvironment[key] = value
		end

		testEnvironment.fail = function(message)
			if message == nil then
				message = "fail() was called."
			end

			success = false
			errorMessage = messagePrefix .. debug.traceback(tostring(message), 2)
		end

		testEnvironment.expect = wrapExpectContextWithPublicApi(session:getExpectationContext())

		local context = session:getContext()

		local nodeSuccess, nodeResult = xpcall(function()
			callback(context)
		end, function(message)
			return messagePrefix .. debug.traceback(tostring(message), 2)
		end)

		-- If a node threw an error, we prefer to use that message over
		-- one created by fail() if it was set.
		if not nodeSuccess then
			success = false
			errorMessage = nodeResult
		end

		_G[RUNNING_GLOBAL] = nil

		return success, errorMessage
	end

	local function runNode(childPlanNode)
		-- Errors can be set either via `error` propagating upwards or
		-- by a test calling fail([message]).

		for _, hook in ipairs(lifecycleHooks:getBeforeEachHooks()) do
			local success, errorMessage = runCallback(hook, "beforeEach hook: ")
			if not success then
				return false, errorMessage
			end
		end

		local testSuccess, testErrorMessage = runCallback(childPlanNode.callback)

		for _, hook in ipairs(lifecycleHooks:getAfterEachHooks()) do
			local success, errorMessage = runCallback(hook, "afterEach hook: ")
			if not success then
				if not testSuccess then
					return false,
						testErrorMessage
							.. "\nWhile cleaning up the failed test another error was found:\n"
							.. errorMessage
				end
				return false, errorMessage
			end
		end

		if not testSuccess then
			return false, testErrorMessage
		end

		return true, nil
	end

	lifecycleHooks:pushHooksFrom(planNode)

	local halt = false
	for _, hook in ipairs(lifecycleHooks:getBeforeAllHooks()) do
		local success, errorMessage = runCallback(hook, "beforeAll hook: ")
		if not success then
			session:addDummyError("beforeAll", errorMessage)
			halt = true
		end
	end

	if not halt then
		for _, childPlanNode in ipairs(planNode.children) do
			if childPlanNode.type == TestEnum.NodeType.It then
				session:pushNode(childPlanNode)
				if session:shouldSkip() then
					session:setSkipped()
				else
					local success, errorMessage = runNode(childPlanNode)

					if success then
						session:setSuccess()
					else
						session:setError(errorMessage)
					end
				end
				session:popNode()
			elseif childPlanNode.type == TestEnum.NodeType.Describe then
				session:pushNode(childPlanNode)
				TestRunner.runPlanNode(session, childPlanNode, lifecycleHooks)

				-- Did we have an error trying build a test plan?
				if childPlanNode.loadError then
					local message = "Error during planning: " .. childPlanNode.loadError
					session:setError(message)
				else
					session:setStatusFromChildren()
				end
				session:popNode()
			end
		end
	end

	for _, hook in ipairs(lifecycleHooks:getAfterAllHooks()) do
		local success, errorMessage = runCallback(hook, "afterAll hook: ")
		if not success then
			session:addDummyError("afterAll", errorMessage)
		end
	end

	lifecycleHooks:popHooks()
end

return TestRunner
