function TestRunner.runPlanNode(K,h,R)local function V(h,R)local V=true local m _G[RUNNING_GLOBAL]=true R=R or""local B=getfenv(h)for K,h in pairs(TestRunner.environment)do B[K]=h end B.fail=function(K)if K==nil then K="\102\097\105\108\040\041 \119\097\115 \099\097\108\108\101\100\046"end V=false m=R..debug.traceback(tostring(K),2)end B.expect=wrapExpectContextWithPublicApi(K:getExpectationContext())local d=K:getContext()local p,Q=xpcall(function()h(d)end,function(K)return R..debug.traceback(tostring(K),2)end)if not p then V=false m=Q end _G[RUNNING_GLOBAL]=nil return V,m end local function m(K)for K,h in ipairs(R:getBeforeEachHooks())do local R,m=V(h,"\098\101\102\111\114\101\069\097\099\104 \104\111\111\107\058 ")if not R then return false,m end end local h,m=V(K.callback)for K,R in ipairs(R:getAfterEachHooks())do local B,d=V(R,"\097\102\116\101\114\069\097\099\104 \104\111\111\107\058 ")if not B then if not h then return false,m..("\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"..d)end return false,d end end if not h then return false,m end return true,nil end R:pushHooksFrom(h)local B=false for h,R in ipairs(R:getBeforeAllHooks())do local m,d=V(R,"\098\101\102\111\114\101\065\108\108 \104\111\111\107\058 ")if not m then K:addDummyError("\098\101\102\111\114\101\065\108\108",d)B=true end end if not B then for h,V in ipairs(h.children)do if V.type==TestEnum.NodeType.It then K:pushNode(V)if K:shouldSkip()then K:setSkipped()else local h,R=m(V)if h then K:setSuccess()else K:setError(R)end end K:popNode()elseif V.type==TestEnum.NodeType.Describe then K:pushNode(V)TestRunner.runPlanNode(K,V,R)if V.loadError then local h="\069\114\114\111\114 \100\117\114\105\110\103 \112\108\097\110\110\105\110\103\058 "..V.loadError K:setError(h)else K:setStatusFromChildren()end K:popNode()end end end for h,R in ipairs(R:getAfterAllHooks())do local m,B=V(R,"\097\102\116\101\114\065\108\108 \104\111\111\107\058 ")if not m then K:addDummyError("\097\102\116\101\114\065\108\108",B)end end R:popHooks()end return TestRunner

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
