local C=function(C,j)local Z=C==j if Z then return{message="\099\117\115\116\111\109 \102\097\105\108\117\114\101 \109\101\115\115\097\103\101 \040\110\111\116\041";pass=true}else return{message="\099\117\115\116\111\109 \102\097\105\108\117\114\101 \109\101\115\115\097\103\101";pass=false}end end return function()it("\083\072\079\085\076\068 \104\097\118\101 \099\117\115\116\111\109 \109\097\116\099\104\101\114 \119\104\101\110 \100\101\102\105\110\101\100 \105\110 \096\105\116\096 \098\108\111\099\107",function()expect.extend({customEqual=C})assert((expect()).customEqual,"\099\117\115\116\111\109\069\113\117\097\108 \115\104\111\117\108\100 \101\120\105\115\116")end)describe("\087\072\069\078 \100\101\102\105\110\105\110\103 \099\117\115\116\111\109 \109\097\116\099\104\101\114 \105\110 \100\101\115\099\114\105\098\101 \098\108\111\099\107",function()beforeAll(function()expect.extend({customEqual=C})end)describe("\087\072\069\078 \078\079\084 \105\110\118\101\114\116\105\110\103 \116\104\101 \101\120\112\114\101\115\115\105\111\110",function()it("\083\072\079\085\076\068 \112\097\115\115 \097\115 \101\120\112\101\099\116\101\100",function()(expect("\104\101\108\108\111")).customEqual("\104\101\108\108\111")end)it("\083\072\079\085\076\068 \102\097\105\108 \097\115 \101\120\112\101\099\116\101\100",function()local C=pcall(function()(expect("\104\101\108\108\111")).customEqual("\119\111\114\108\100")end)assert(C==false,"\100\105\100 \110\111\116 \102\097\105\108 \108\105\107\101 \101\120\112\101\099\116\101\100")end)end)describe("\087\072\069\078 \105\110\118\101\114\116\105\110\103 \116\104\101 \101\120\112\114\101\115\115\105\111\110",function()it("\083\072\079\085\076\068 \112\097\115\115 \097\115 \101\120\112\101\099\116\101\100",function()(expect("\104\101\108\108\111")).never.customEqual("\119\111\114\108\100")end)it("\083\072\079\085\076\068 \102\097\105\108 \097\115 \101\120\112\101\099\116\101\100",function()local C=pcall(function()(expect("\104\101\108\108\111")).never.customEqual("\104\101\108\108\111")end)assert(C==false,"\100\105\100 \110\111\116 \102\097\105\108 \108\105\107\101 \101\120\112\101\099\116\101\100")end)end)describe("\087\072\069\078 \099\104\097\105\110 \119\105\116\104\105\110 \111\116\104\101\114 \109\097\116\099\104\101\114\115",function()it("\083\072\079\085\076\068 \119\111\114\107 \097\115 \101\120\112\101\099\116\101\100 \119\104\101\110 \116\104\101 \102\105\114\115\116 \116\111 \101\120\101\099\117\116\101",function()((((expect("\104\101\108\108\111")).to.customEqual("\104\101\108\108\111")).to.equal("\104\101\108\108\111")).to.be.ok()).to.never.equal("\102\111\111\098\097\114")end)it("\083\072\079\085\076\068 \119\111\114\107 \097\115 \101\120\112\101\099\116\101\100 \119\104\101\110 \116\104\101 \108\097\115\116 \116\111 \101\120\101\099\117\116\101",function()((((expect("\104\101\108\108\111")).to.equal("\104\101\108\108\111")).to.be.ok()).to.never.equal("\102\111\111\098\097\114")).to.customEqual("\104\101\108\108\111")end)end)end)end
-- luacheck: globals describe beforeAll expect it

local customEqualMatcher = function(received, expected)
	local pass = received == expected
	if pass then
		return {
			message = "custom failure message (not)",
			pass = true,
		}
	else
		return {
			message = "custom failure message",
			pass = false,
		}
	end
end

return function()
	it("SHOULD have custom matcher when defined in `it` block", function()
		expect.extend({
			customEqual = customEqualMatcher,
		})

		assert(expect().customEqual, "customEqual should exist")
	end)

	describe("WHEN defining custom matcher in describe block", function()
		beforeAll(function()
			expect.extend({
				customEqual = customEqualMatcher,
			})
		end)

		describe("WHEN NOT inverting the expression", function()
			it("SHOULD pass as expected", function()
				expect("hello").customEqual("hello")
			end)

			it("SHOULD fail as expected", function()
				local success = pcall(function()
					expect("hello").customEqual("world")
				end)

				assert(success == false, "did not fail like expected")
			end)
		end)

		describe("WHEN inverting the expression", function()
			it("SHOULD pass as expected", function()
				expect("hello").never.customEqual("world")
			end)

			it("SHOULD fail as expected", function()
				local success = pcall(function()
					expect("hello").never.customEqual("hello")
				end)

				assert(success == false, "did not fail like expected")
			end)
		end)

		describe("WHEN chain within other matchers", function()
			it("SHOULD work as expected when the first to execute", function()
				expect("hello")
					.to.customEqual("hello")
					.to.equal("hello")
					.to.be.ok()
					.to.never.equal("foobar")
			end)

			it("SHOULD work as expected when the last to execute", function()
				expect("hello")
					.to.equal("hello")
					.to.be.ok()
					.to.never.equal("foobar")
					.to.customEqual("hello")
			end)
		end)
	end)
end
