local r=require(script.Parent.TestEnum)local f=require(script.Parent.Expectation)local function a(a,m)local g={}if m then if type(m)~="\116\097\098\108\101"then error(("\066\097\100 \097\114\103\117\109\101\110\116 \035\050 \116\111 \110\101\119\069\110\118\105\114\111\110\109\101\110\116\046 \069\120\112\101\099\116\101\100 \116\097\098\108\101\044 \103\111\116 \037\115"):format(typeof(m)),2)end for r,f in pairs(m)do g[r]=f end end local function j(f,m,g,j)local n=a:addChild(f,g,j)n.callback=m if g==r.NodeType.Describe then n:expand()end return n end function g.describeFOCUS(f,a)j(f,a,r.NodeType.Describe,r.NodeModifier.Focus)end function g.describeSKIP(f,a)j(f,a,r.NodeType.Describe,r.NodeModifier.Skip)end function g.describe(f,a,m)j(f,a,r.NodeType.Describe,r.NodeModifier.None)end function g.itFOCUS(f,a)j(f,a,r.NodeType.It,r.NodeModifier.Focus)end function g.itSKIP(f,a)j(f,a,r.NodeType.It,r.NodeModifier.Skip)end function g.itFIXME(f,a)local m=j(f,a,r.NodeType.It,r.NodeModifier.Skip)warn("\070\073\088\077\069\058 \098\114\111\107\101\110 \116\101\115\116",m:getFullName())end function g.it(f,a,m)j(f,a,r.NodeType.It,r.NodeModifier.None)end local n=0 local Y={[r.NodeType.BeforeAll]="\098\101\102\111\114\101\065\108\108",[r.NodeType.AfterAll]="\097\102\116\101\114\065\108\108";[r.NodeType.BeforeEach]="\098\101\102\111\114\101\069\097\099\104",[r.NodeType.AfterEach]="\097\102\116\101\114\069\097\099\104"}for f,a in pairs(Y)do g[a]=function(m)j(a..("\095"..tostring(n)),m,f,r.NodeModifier.None)n=n+1 end end function g.FIXME(f)warn("\070\073\088\077\069\058 \098\114\111\107\101\110 \116\101\115\116",a:getFullName(),f or"")a.modifier=r.NodeModifier.Skip end function g.FOCUS()a.modifier=r.NodeModifier.Focus end function g.SKIP()a.modifier=r.NodeModifier.Skip end function g.HACK_NO_XPCALL()warn("\072\065\067\075\095\078\079\095\088\080\067\065\076\076 \105\115 \100\101\112\114\101\099\097\116\101\100\046 \073\116 \105\115 \110\111\119 \115\097\102\101 \116\111 \121\105\101\108\100 \105\110 \097\110 \120\112\099\097\108\108\044 \115\111 \116\104\105\115 \105\115 \110\111 \108\111\110\103\101\114 \110\101\099\101\115\115\097\114\121\046 \073\116 \099\097\110 \098\101 \115\097\102\101\108\121 \100\101\108\101\116\101\100\046")end g.fit=g.itFOCUS g.xit=g.itSKIP g.fdescribe=g.describeFOCUS g.xdescribe=g.describeSKIP g.expect=setmetatable({extend=function(...)error("\067\097\110\110\111\116 \099\097\108\108 \"\101\120\112\101\099\116\046\101\120\116\101\110\100\" \102\114\111\109 \119\105\116\104\105\110 \097 \"\100\101\115\099\114\105\098\101\" \110\111\100\101\046")end},{__call=function(r,...)return f.new(...)end})return g end local m={}m.__index=m


local TestEnum = require(script.Parent.TestEnum)
local Expectation = require(script.Parent.Expectation)

local function newEnvironment(currentNode, extraEnvironment)
	local env = {}

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

		for key, value in pairs(extraEnvironment) do
			env[key] = value
		end
	end

	local function addChild(phrase, callback, nodeType, nodeModifier)
		local node = currentNode:addChild(phrase, nodeType, nodeModifier)
		node.callback = callback
		if nodeType == TestEnum.NodeType.Describe then
			node:expand()
		end
		return node
	end

	function env.describeFOCUS(phrase, callback)
		addChild(phrase, callback, TestEnum.NodeType.Describe, TestEnum.NodeModifier.Focus)
	end

	function env.describeSKIP(phrase, callback)
		addChild(phrase, callback, TestEnum.NodeType.Describe, TestEnum.NodeModifier.Skip)
	end

	function env.describe(phrase, callback, nodeModifier)
		addChild(phrase, callback, TestEnum.NodeType.Describe, TestEnum.NodeModifier.None)
	end

	function env.itFOCUS(phrase, callback)
		addChild(phrase, callback, TestEnum.NodeType.It, TestEnum.NodeModifier.Focus)
	end

	function env.itSKIP(phrase, callback)
		addChild(phrase, callback, TestEnum.NodeType.It, TestEnum.NodeModifier.Skip)
	end

	function env.itFIXME(phrase, callback)
		local node = addChild(phrase, callback, TestEnum.NodeType.It, TestEnum.NodeModifier.Skip)
		warn("FIXME: broken test", node:getFullName())
	end

	function env.it(phrase, callback, nodeModifier)
		addChild(phrase, callback, TestEnum.NodeType.It, TestEnum.NodeModifier.None)
	end

	-- Incrementing counter used to ensure that beforeAll, afterAll, beforeEach, afterEach have unique phrases
	local lifecyclePhaseId = 0

	local lifecycleHooks = {
		[TestEnum.NodeType.BeforeAll] = "beforeAll",
		[TestEnum.NodeType.AfterAll] = "afterAll",
		[TestEnum.NodeType.BeforeEach] = "beforeEach",
		[TestEnum.NodeType.AfterEach] = "afterEach"
	}

	for nodeType, name in pairs(lifecycleHooks) do
		env[name] = function(callback)
			addChild(name .. "_" .. tostring(lifecyclePhaseId), callback, nodeType, TestEnum.NodeModifier.None)
			lifecyclePhaseId = lifecyclePhaseId + 1
		end
	end

	function env.FIXME(optionalMessage)
		warn("FIXME: broken test", currentNode:getFullName(), optionalMessage or "")

		currentNode.modifier = TestEnum.NodeModifier.Skip
	end

	function env.FOCUS()
		currentNode.modifier = TestEnum.NodeModifier.Focus
	end

	function env.SKIP()
		currentNode.modifier = TestEnum.NodeModifier.Skip
	end

	--[[
		This function is deprecated. Calling it is a no-op beyond generating a
		warning.
	]]
	function env.HACK_NO_XPCALL()
		warn("HACK_NO_XPCALL is deprecated. It is now safe to yield in an " ..
			"xpcall, so this is no longer necessary. It can be safely deleted.")
	end

	env.fit = env.itFOCUS
	env.xit = env.itSKIP
	env.fdescribe = env.describeFOCUS
	env.xdescribe = env.describeSKIP

	env.expect = setmetatable({
		extend = function(...)
			error("Cannot call \"expect.extend\" from within a \"describe\" node.")
		end,
	}, {
		__call = function(_self, ...)
			return Expectation.new(...)
		end,
	})

	return env
end

local TestNode = {}
TestNode.__index = TestNode
