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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 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.self_test
local WFRxTestCase = require 'WFRxTestCase'

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

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

local controlledWithBuffer = WFRxControlledObservableWithBuffer

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

local WFRxControlledObservableWithBufferTest = {}

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

function WFRxControlledObservableWithBufferTest.rxTestSingleValue( rxTest )

	local bufferSize = 1
	
	local o = Rx.Observable.just( 42 )
		:call( controlledWithBuffer, bufferSize )
		
	rxTest:expectSingleValue( o, 42 )
	
end

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

function WFRxControlledObservableWithBufferTest.rxTestOnlyCompleted( rxTest )

	local bufferSize = 1
	
	local o = Rx.Observable.empty()
		:call( controlledWithBuffer, bufferSize )
		
	rxTest:expectOnlyCompleted( o )
		
end

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

function WFRxControlledObservableWithBufferTest.rxTestNoValue( rxTest )

	local gotValue = false
	local gotError = false
	local gotCompleted = false
	
	local bufferSize = 1
	local initialRequestCount = 0
	
	local o = Rx.Observable.just( 42 )
		:call( controlledWithBuffer, bufferSize, initialRequestCount )
		:tap(
			function()
				gotValue = true
			end,
			function()
				gotError = true
			end,
			function()
				gotCompleted = true
			end
		)
	
	local s = o:subscribe()
	
	rxTest:expectEqual( gotValue, false )
	rxTest:expectEqual( gotError, false )
	rxTest:expectEqual( gotCompleted, false )
	
	s:dispose()
	
end

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

function WFRxControlledObservableWithBufferTest.rxTestSingleBufferedValues( rxTest )

	local bufferSize = 1
	local initialRequestCount = 1
	
	local o = Rx.Observable.range( 1, 3 )
		:call( controlledWithBuffer, bufferSize, initialRequestCount )
		
	local obs = rxTest:observe( o )
	
	obs:expectData( 1 )
	
	o:request( 1 )
	
	obs:expectData( 3 )
	obs:expectCompleted()
	
end

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

function WFRxControlledObservableWithBufferTest.rxTestMultiBuffer( rxTest )

	local bufferSize = 6
	local initialRequestCount = 0
	
	local gotValue = 0
	local gotCompleted = false
	
	local controlledObs = Rx.Observable.range( 1, 6 )
		:call( controlledWithBuffer, bufferSize, initialRequestCount )
		
	local o = controlledObs:tap( function()
		gotValue = gotValue + 1
	end, nil, function()
		gotCompleted = true
	end )
		
	local obs = rxTest:observe( o )
	
	rxTest:expectEqual( gotValue, 0 )
	
	controlledObs:request( 1 )
	obs:expectData( 1 )
	
	rxTest:expectEqual( gotValue, 1 )
	
	controlledObs:request( 1 )
	obs:expectData( 2 )
	rxTest:expectEqual( gotValue, 2 )
	
	controlledObs:request( 1 )
	obs:expectData( 3 )
	rxTest:expectEqual( gotValue, 3 )
	
	controlledObs:request( 1 )
	obs:expectData( 4 )
	rxTest:expectEqual( gotValue, 4 )
	
	controlledObs:request( 1 )
	obs:expectData( 5 )
	rxTest:expectEqual( gotValue, 5 )
	
	controlledObs:request( 1 )
	obs:expectData( 6 )
	rxTest:expectEqual( gotValue, 6 )
	
	controlledObs:request( 1 )
	
	obs:expectCompleted()
	rxTest:expectEqual( gotCompleted, true )
	
end

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

function WFRxControlledObservableWithBufferTest.rxTestBufferHigherRequests( rxTest )

	local bufferSize = 9
	local initialRequestCount = 3
	
	local gotValue = 0
	
	local controlledObs = Rx.Observable.range( 1, 9 )
		:call( controlledWithBuffer, bufferSize, initialRequestCount )
		
	local o = controlledObs
		:tap( function()
			gotValue = gotValue + 1
		end )
	
	local obs = rxTest:observe( o )
	
	obs:expectData( 1 )
	obs:expectData( 2 )
	obs:expectData( 3 )
	
	rxTest:expectEqual( gotValue, 3 )
	
	controlledObs:request( 1 )
	obs:expectData( 4 )
	rxTest:expectEqual( gotValue, 4 )
	
	controlledObs:request( 1 )
	obs:expectData( 5 )
	rxTest:expectEqual( gotValue, 5 )
	
	controlledObs:request( 1 )
	obs:expectData( 6 )
	rxTest:expectEqual( gotValue, 6 )
	
	controlledObs:request( 3 )
	obs:expectData( 7 )
	obs:expectData( 8 )
	obs:expectData( 9 )
	rxTest:expectEqual( gotValue, 9 )
	
	controlledObs:request( 1 )
	
	obs:expectCompleted()
	
end

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

function WFRxControlledObservableWithBufferTest.rxTestBufferError( rxTest )

	local bufferSize = 1
	local initialRequestCount = 0
	
	local o = Rx.Observable.range( 1, 3 )
		:flatMap( function( val )
		
			if val <= 2 then
				return Rx.Observable.just( val )
			end
			
			return Rx.Observable.throw( 'bad value' )
			
		end )
		:call( controlledWithBuffer, bufferSize, initialRequestCount )
		
	local obs = rxTest:observe( o )
	
	o:request( 1 )
	obs:expectData( 2 )
	
	o:request( 1 )
	
	obs:expectError( 'bad value' )
	
end

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

function WFRxControlledObservableWithBufferTest.rxTestBufferComplete( rxTest )

	local bufferSize = 1
	local initialRequestCount = 1
	
	local o = Rx.Observable.range( 1, 2 )
		:call( controlledWithBuffer, bufferSize, initialRequestCount )
		
	local obs = rxTest:observe( o )
	
	obs:expectData( 1 )
	
	o:request( 1 )
	obs:expectData( 2 )
	
	o:request( 1 )
	obs:expectCompleted()
	
end

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

function WFRxControlledObservableWithBufferTest.rxTestInvalidArguments( rxTest )

	rxTest:expectError( function()
	
		local bufferSize = 0
		local initialRequestCount = 1
		
		Rx.Observable.range( 1, 2 )
			:call( controlledWithBuffer, bufferSize, initialRequestCount )
		
	end )
	
	rxTest:expectError( function()
	
		local bufferSize = -1
		local initialRequestCount = 1
		
		Rx.Observable.range( 1, 2 )
			:call( controlledWithBuffer, bufferSize, initialRequestCount )
		
	end )
	
end

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

return WFRxTestCase.wrapSuite( WFRxControlledObservableWithBufferTest )