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

WFCapabilitiesTest.lua

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

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

NOTICE: All information contained herein is, and remains 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 are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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

local WFCapabilities = require 'WFCapabilities'

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

local WFCapabilitiesTest = {}

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

function WFCapabilitiesTest.testBasic()
	
	local key = "WFCapabilitiesTest_prop1"
	local value = "test value"
	WFCapabilities.setCapability( key, value )
	
	local result = WFCapabilities.getCapability( key )
	assert( result == value, "test set/get works" )

	assert( pcall( WFCapabilities.getCapability, 12 ) == false, "test get invalid key throws" )
	assert( pcall( WFCapabilities.setCapability, 12, {} ) == false, "test set invalid key throws" )
	
	assert( WFCapabilities.getCapability( "foobar" ) == nil, "test get unused key is nil" )

	WFCapabilities.setCapability( key, nil )
	assert( WFCapabilities.getCapability( key ) == nil, "test removal" )

end

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

local nCalls1 = 0
local nCalls2 = 0

function WFCapabilitiesTest.sampleFunction2( a, b, c )
	nCalls2 = nCalls2 + 1
	return a, b
end

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

function WFCapabilitiesTest.sampleFunction1( a, b, c )
	nCalls1 = nCalls1 + 1
	return 10 + a, b and ( b + 20 ), 40
end

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

function WFCapabilitiesTest.testFilters()

	nCalls1 = 0
	nCalls2 = 0

	local key = "WFCapabilitiesTest_filter"
	WFCapabilities.addFilter( key, "WFCapabilitiesTest_1", "WFCapabilitiesTest.sampleFunction1" )
	WFCapabilities.addFilter( key, "WFCapabilitiesTest_1", "WFCapabilitiesTest.sampleFunction2" )
	WFCapabilities.addFilter( key, "WFCapabilitiesTest_1", "WFCapabilitiesTest.sampleFunction1" )
	WFCapabilities.addFilter( key, "WFCapabilitiesTest_2", "WFCapabilitiesTest.sampleFunction1" )
	WFCapabilities.addFilter( key, "WFCapabilitiesTest_3", "WFCapabilitiesTest.sampleFunction1" )
	WFCapabilities.addFilter( key, "WFCapabilitiesTest_4", "WFCapabilitiesTest.sampleFunction2" )
	
	local A, B, C = WFCapabilities.callFilters( key, 42, nil, true )
	
	assert( nCalls1 == 3, "Filter 1 called incorrect number of times: expected 3, got " .. tostring( nCalls1 ) )
	assert( nCalls2 == 1, "Filter 2 called incorrect number of times: expected 1, got " .. tostring( nCalls2 ) )

	assert( A == 72, "1st result works" )
	assert( B == nil, "2nd result works" )
	assert( C == nil, "3rd result works" )

	assert( pcall( WFCapabilities.callFilters, 12 ) == false, "test call invalid key throws" )
	assert( pcall( WFCapabilities.addFilter, 12, "foo", "bar" ) == false, "test add invalid key throws" )
	assert( pcall( WFCapabilities.addFilter, key, "some+id", "MissingNamespaceKey" ) == false, "test add invalid key throws" )
	assert( pcall( WFCapabilities.addFilter, key, nil, "MissingNamespaceKey" ) == false, "test add invalid clientId throws" )
	
	assert( WFCapabilities.callFilters( "foobar" ) == nil, "test call unused key is nil" )

	WFCapabilities.addFilter( key, "WFCapabilitiesTest_1", nil )
	WFCapabilities.addFilter( key, "WFCapabilitiesTest_2", nil )
	WFCapabilities.addFilter( key, "WFCapabilitiesTest_3", nil )
	WFCapabilities.addFilter( key, "WFCapabilitiesTest_4", nil )

	nCalls1 = 0
	nCalls2 = 0
	
	assert( "testvalue" == WFCapabilities.callFilters( key, "testvalue" ), "test removal" )
		
	assert( nCalls1 == 0, "Filter 1 not called after addFilter called with nil selector for clientId." )
	assert( nCalls2 == 0, "Filter 2 not called after addFilter called with nil selector for clientId." )

end

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

-- Hopefully soon the unit test loader will use require() rather than loadstring() to load
-- unit tests; until such time as it does I will forcibly populate require() with this
-- namespace to avoid the problem that even after this file is loaded by the unit test
-- require 'WFCapabilitiesTest' re-runs it.  (Which means we end up with two copies
-- of the nCalls1 and nCalls2 upvalues, and the above test will fail because of that.)
rawset( debug.getregistry()._LOADED, 'WFCapabilitiesTest', WFCapabilitiesTest )

return WFCapabilitiesTest
