--[[----------------------------------------------------------------------------

KSLuaUtilsTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2013 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

	-- RxLua
local Rx = require 'Rx'

	-- WF.core.utils
local KSLuaUtils = require 'KSLuaUtils'


--============================================================================--

local KSLuaUtilsTest = {}

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

function KSLuaUtilsTest.testassertf()
	KSLuaUtils.assertf( true, "should %s succeed", "really")
	assert( not pcall(KSLuaUtils.assertf, false, "should %s succeed", "not") , "should fail")
end

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

function KSLuaUtilsTest.testassertft()
	KSLuaUtils.assertft( true, "should %s succeed", "really")
	assert( not pcall(KSLuaUtils.assertft, false, "should %s succeed", "not") , "should fail")
end

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

function KSLuaUtilsTest.testsend()

	local fooClass = Rx.Class 'fooClass' {

		x = 42,

		increment = function( self )
			self.x = self.x + 1
			return self.x
		end,

		decrement = function( self )
			self.x = self.x - 1
			return self.x
		end,

	}

	local foo1 = fooClass()

	assert( KSLuaUtils.send( foo1, 'increment' ) == 43, "result should be 43" )
	assert( KSLuaUtils.send( foo1, 'decrement' ) == 42, "result should be 42" )

end

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

function KSLuaUtilsTest.testdpcall()
	local function test()
		return 42
	end
	
	local success, result = KSLuaUtils.dpcall( 'test', test )
	assert( success and result == 42, "dpcall should return 42" )
	assert( not KSLuaUtils.dpcall( 'test', nil ), "invalid dpcall passing non function")
end

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

function KSLuaUtilsTest.testpsend()

	local fooClass = Rx.Class 'fooClass' {

		x = 42,

		increment = function( self )
			self.x = self.x + 1
			return self.x
		end,

		decrement = function( self )
			self.x = self.x - 1
			return self.x
		end,

		badincrement = function( self )
			self.x = self.x + 1
			error("intentional failure")
			return self.x
		end,

	}

	local foo1 = fooClass()

	local success, curX = KSLuaUtils.psend( foo1, 'increment' )
	assert(success and curX == 43, "curX should be 43" )

	success, curX = KSLuaUtils.psend( foo1, 'decrement' )
	assert( success and curX == 42, "curX should be 42" )

	success, curX = KSLuaUtils.psend( foo1, 'badincrement' )
	assert( not success and foo1.x == 43, "badincrement should fail but still increment" )

end

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

return KSLuaUtilsTest
