/*
Copyright (C) 2015 Runtime Revolution Ltd.

This file is part of LiveCode.

LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.

LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with LiveCode.  If not see <http://www.gnu.org/licenses/>.  */

/*
This library provides syntax for unit testing LiveCode Builder
programs.  It is used by the LiveCode Builder standard library's
testsuite.

To use this library, write your tests in a Builder source code file.
Each group of tests should be a public handler with a name beginning
with "Test".  If possible, use one test per handler.  Otherwise, add a
"plan N tests" statement at the start of the handler.

Example:

	public handler TestSelf()
		plan 10 tests

		test diagnostic "Normal tests"
		test 2 > 1
		test "Basic test" when true

		test diagnostic "Skipped tests"
		skip test
		skip test "Skipped 2"
		skip test because "Not implemented"
		skip test "Skipped 4" because "Not supported on this platform"

		test diagnostic "Tests which are expected to fail"
		broken test false
		broken test "Failed 2" when false
		broken test false because "broken"
		broken test "Failed 4" when false because "really broken"
	end handler

The test results are output on standard output in TAP (Test Anything
Protocol) format.
*/

module com.livecode.unittest

public foreign handler MCUnitPlan(in pCount as Number) returns nothing binds to "lcb:com.livecode.unittest.__IMPL.MCUnitPlan"
public foreign handler MCUnitDiagnostic(in pMessage as String) returns nothing binds to "lcb:com.livecode.unittest.__IMPL.MCUnitDiagnostic"
foreign handler MCUnitOutputTest(in pCondition as Boolean, in pDescription as String, in pDirective as String, in pReason as String) returns nothing binds to "lcb:com.livecode.unittest.__IMPL.MCUnitOutputTest"

----------------------------------------------------------------
-- Test syntax
----------------------------------------------------------------

syntax UnitPlan is statement
	"plan" <Count: Expression> "tests"
begin
	MCUnitPlan(Count)
end syntax

syntax UnitDiagnostic is statement
	"test" "diagnostic" <Message: Expression>
begin
	MCUnitDiagnostic(Message)
end syntax

----------------------------------------------------------------

syntax UnitTest is statement
	"test" <Condition: Expression>
begin
	MCUnitTest(Condition)
end syntax

public handler MCUnitTest(in pCondition as Boolean)
	MCUnitOutputTest(pCondition, "", "", "")
end handler

syntax UnitTestDescription is statement
	"test" <Description: Expression> "when" <Condition: Expression>
begin
	MCUnitTestDescription(Description, Condition)
end syntax

public handler MCUnitTestDescription(in pDescription as String, in pCondition as Boolean)
	MCUnitOutputTest(pCondition, pDescription, "", "")
end handler

----------------------------------------------------------------

syntax UnitTestSkip is statement
	"skip" "test"
begin
	MCUnitTestSkip()
end syntax

public handler MCUnitTestSkip()
	MCUnitOutputTest(true, "", "SKIP", "")
end handler

syntax UnitTestSkipDescription is statement
	"skip" "test" <Description: Expression>
begin
	MCUnitTestSkipDescription(Description)
end syntax

public handler MCUnitTestSkipDescription(in pDescription as String)
	MCUnitOutputTest(true, pDescription, "SKIP", "")
end handler

syntax UnitTestSkipReason is statement
	"skip" "test" "because" <Reason: Expression>
begin
	MCUnitTestSkipReason(Reason)
end syntax

public handler MCUnitTestSkipReason(in pReason as String)
	MCUnitOutputTest(true, "", "SKIP", pReason)
end handler

syntax UnitTestSkipDescriptionAndReason is statement
	"skip" "test" <Description: Expression> "because" <Reason: Expression>
begin
	MCUnitTestSkipDescriptionAndReason(Description, Reason)
end syntax

public handler MCUnitTestSkipDescriptionAndReason(in pDescription as String, in pReason as String)
	MCUnitOutputTest(true, pDescription, "SKIP", pReason)
end handler

----------------------------------------------------------------

syntax UnitTestFails is statement
	"broken" "test" <Condition: Expression>
begin
	MCUnitTestFails(Condition)
end syntax

public handler MCUnitTestFails(in pCondition as Boolean)
	MCUnitOutputTest(pCondition, "", "TODO", "")
end handler

syntax UnitTestFailsDescription is statement
	"broken" "test" <Description: Expression> "when" <Condition: Expression>
begin
	MCUnitTestFailsDescription(Description, Condition)
end syntax

public handler MCUnitTestFailsDescription(in pDescription as String, in pCondition as Boolean)
	MCUnitOutputTest(pCondition, pDescription, "TODO", "")
end handler

syntax UnitTestFailsReason is statement
	"broken" "test" <Condition: Expression> "because" <Reason: Expression>
begin
	MCUnitTestFailsReason(Condition, Reason)
end syntax

public handler MCUnitTestFailsReason(in pCondition as Boolean, in pReason as String)
	MCUnitOutputTest(pCondition, "", "TODO", pReason)
end handler

syntax UnitTestFailsDescriptionAndReason is statement
	"broken" "test" <Description: Expression> "when" <Condition: Expression> "because" <Reason: Expression>
begin
	MCUnitTestFailsDescriptionAndReason(Description, Condition, Reason)
end syntax

public handler MCUnitTestFailsDescriptionAndReason(in pDescription as String, in pCondition as Boolean, in pReason as String)
	MCUnitOutputTest(pCondition, pDescription, "TODO", pReason)
end handler

end module
