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

WFUnknownTest.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.

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

local WFUnknownTest = { }

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

function WFUnknownTest.testUnknown()

	local WFUnknown = require 'WFUnknown'
	
	local v = WFUnknown.value()
	
	assert( WFUnknown.isUnknown( v ), "The unknown value should be identified as unknown." )
	
	assert( not WFUnknown.isKnown( v ), "The unknown value should not be identified as known." )

end

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

function WFUnknownTest.testUnknownKeyDeref()

	local WFUnknown = require 'WFUnknown'
	
	local v = WFUnknown.value()
	
	local vk = v.k
	
	assert( WFUnknown.isUnknown( vk ), "We should get an unknown value for key dereference." )

end

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

function WFUnknownTest.testUnknownIntDeref()

	local WFUnknown = require 'WFUnknown'
	
	local v = WFUnknown.value()
	
	local v1 = v[ 1 ]
	
	assert( v1 == nil, "unknown[ 1 ] should be nil." )

end

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

function WFUnknownTest.testUnknownKeySet()

	local WFUnknown = require 'WFUnknown'
	
	local v = WFUnknown.value()
	
	assert( not pcall(function() v.k = 3 end ), "setting keys in unknown value isn't allowed" )

end

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

function WFUnknownTest.testUnknownLength()

	local WFUnknown = require 'WFUnknown'
	
	local v = WFUnknown.value()
	
	assert( #v == 0, "unknown should be length zero." )
	
	-- GRUNGE	lua 5.1 reference states the # operator returns length of table even if a __len function is available
	--			however in 5.2 reference says it will call __len first if available
	assert( #v == getmetatable(v).__len() )  -- increase code coverage by directly calling __len

end

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

function WFUnknownTest.testKnownValues()

	local WFUnknown = require 'WFUnknown'
	
	assert( not WFUnknown.isUnknown( 3 ), "Numbers should not be identified as unknown." )
	
	assert( WFUnknown.isKnown( 3 ), "Numbers should be identified as known." )

	assert( not WFUnknown.isUnknown( nil ), "Nil should not be identified as unknown." )
	
	assert( WFUnknown.isKnown( nil ), "Nil should be identified as known." )

end

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

function WFUnknownTest.testMapUnknownToNil()

	local WFUnknown = require 'WFUnknown'
	local KSReactive = require 'KSReactive'
	
	local b = KSReactive.variableB( 42 )
	local mappedB = b:mapB( WFUnknown.mapUnknownToNil )
	
	assert( mappedB:valueNow() == 42 )
	
	b:set( WFUnknown.value() )
	assert( mappedB:valueNow() == nil )
	
	b:set( 53 )
	assert( mappedB:valueNow() == 53 )
	
	b:set( nil )
	assert( mappedB:valueNow() == nil )
	
	local x = {}
	b:set( x )
	assert( mappedB:valueNow() == x )

end

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

return WFUnknownTest
