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

WFRxDynamicArraySliceTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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.

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

	-- Rx
local Rx = require 'Rx'
local Subject = Rx.Subject

	-- WF.core.rxlua
local WFRxDynamicArraySlice = require 'WFRxDynamicArraySlice'

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'


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

local WFRxDynamicArraySliceTest = {}

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

function WFRxDynamicArraySliceTest.rxTestBasic( rxTest )

	local source = Subject()
	local bounds = Subject()

	local slice = rxTest:observe( source:combineLatest( WFRxDynamicArraySlice, bounds ) )

	source:onNext {}
		-- No bounds yet. Nothing should happen.

	source:onNext { 1, 3, 5, 7 }
		-- No bounds yet. Nothing should happen.

	bounds:onNext( 1, 2 )
	slice:expectData { 1, 3 }

	bounds:onNext( 2, 3 )
	slice:expectData { 3, 5 }

	source:onNext { 1, 4, 7, 11 }
	slice:expectData { 4, 7 }

	-- Out of bounds indices should be "corrected" to nearest reasonable value.

	bounds:onNext( -42, 2 )
	slice:expectData { 1, 4 }

	bounds:onNext( 2.5, 3 ) -- round down (start)
	slice:expectData { 4, 7 }

	bounds:onNext( 3, 3.5 ) -- round down (end)
	slice:expectData { 7 }

	bounds:onNext( 3, 4000 )
	slice:expectData { 7, 11 }

	bounds:onNext( 3, 2 )
	slice:expectData {}

	source:onCompleted()

	bounds:onNext( 1, 2 ) -- can still update bounds after source completes
	slice:expectData { 1, 4 }

	bounds:onCompleted() -- combineLatest closes when both sources close
	slice:expectCompleted()

end

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

function WFRxDynamicArraySliceTest.rxTestErrorNotTable( rxTest )

	local source = Subject()
	local bounds = Subject()

	local slice = rxTest:observe( source:combineLatest( WFRxDynamicArraySlice, bounds ) )

	source:onNext( 42 )
	bounds:onNext( 1, 1 )

	slice:expectError( ':ignore:' )

end

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

function WFRxDynamicArraySliceTest.rxTestErrorStartIndexNotNumber( rxTest )

	local source = Subject()
	local bounds = Subject()

	local slice = rxTest:observe( source:combineLatest( WFRxDynamicArraySlice, bounds ) )

	source:onNext { 42 }
	bounds:onNext( 'bogus', 1 )

	slice:expectError( ':ignore:' )

end

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

function WFRxDynamicArraySliceTest.rxTestErrorEndIndexNotNumber( rxTest )

	local source = Subject()
	local bounds = Subject()

	local slice = rxTest:observe( source:combineLatest( WFRxDynamicArraySlice, bounds ) )

	source:onNext { 42 }
	bounds:onNext( 1, 'bogus' )

	slice:expectError( ':ignore:' )

end

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

function WFRxDynamicArraySliceTest.rxTestRelaysSourceOnError( rxTest )

	local source = Subject()
	local bounds = Subject()

	local slice = rxTest:observe( source:combineLatest( WFRxDynamicArraySlice, bounds ) )

	source:onNext { 1, 3, 5, 7 }
	bounds:onNext( 1, 2 )

	slice:expectData { 1, 3 }

	source:onError( "something's not right" )
	slice:expectError( "something's not right" )

end

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

function WFRxDynamicArraySliceTest.rxTestRelaysBoundsOnError( rxTest )

	local source = Subject()
	local bounds = Subject()

	local slice = rxTest:observe( source:combineLatest( WFRxDynamicArraySlice, bounds ) )

	source:onNext { 1, 3, 5, 7 }
	bounds:onNext( 1, 2 )

	slice:expectData { 1, 3 }

	bounds:onError( "something's not right" )
	slice:expectError( "something's not right" )

end

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

return WFRxTestCase.wrapSuite( WFRxDynamicArraySliceTest )
