[Borges-users] some unit tests for Controller

Слепнев Владимир slepnev_v at rambler.ru
Mon May 3 09:21:19 EDT 2004


Here's some unit tests for y'all =)

Vladimir Slepnev



require 'test/unit'

module Borges; end

class Borges::StateHolder # mock

	attr_accessor :contents

	def initialize(*args)
	end

end

require 'Borges/Controller/Controller'
require 'Borges/Utilities/RenderNotification'

class ControllerTest < Test::Unit::TestCase

	# some tests for call/answer/delegation etc.
	# touching only the public interface of Controller

	# those tests rely heavily on 'raise Borges::RenderNotification.new',
	# when we finally do replace it with 'throw :RenderNotification', we 
must
	# remember to change assert_raises for assert_throws everywhere etc.


	# one Controller may 'call' another. this must raise a 
RenderNotification.
	# when the called controller invokes 'answer', the original 'call'
	# must return the value passed to 'answer'.

	def test_call_answer
		c1 = Borges::Controller.new
		c2 = Borges::Controller.new
		moment = 0 

		assert_raises(Borges::RenderNotification) do
			assert_equal "yeah we've done it", c1.call(c2)
			raise Borges::RenderNotification.new if moment==1
		end

		if(moment==0) then 
			moment = 1
			c2.answer "yeah we've done it" 
		end
	end


	# 'on_answer' may be used to override what a controller should do
	# instead of replying to the calling controller

	def test_on_answer
		c1 = Borges::Controller.new
		c2 = Borges::Controller.new
		assert_raises(Borges::RenderNotification) {c1.call(c2)}
		c2.on_answer {return}
		c2.answer; flunk
	end


	# the called controller becomes the 'delegate' of the calling 
controller

	def test_delegate
		c1 = Borges::Controller.new
		c2 = Borges::Controller.new
		assert_raises(Borges::RenderNotification) {c1.call(c2)}
		assert_equal c2,c1.delegate
	end	


	# the current delegate is saved when 'call' is invoked:
	# if c1 calls c2, then calls c3, then c3 answers, delegate should be 
c2 again
	# this test is a bit tricky
	
	def test_save_delegate
		c1 = Borges::Controller.new
		c2 = Borges::Controller.new
		c3 = Borges::Controller.new
		moment = 0

		begin
			c1.call(c2)
			moment = 2
		rescue Borges::RenderNotification
		end

		begin
			if(moment==0) then
				c1.call(c3)
				moment = 1
			end
		rescue Borges::RenderNotification
		end

		c3.answer if moment == 0
		assert_equal(c2, c1.delegate) if moment == 1

		c2.answer if moment == 1
		assert_equal(nil, c1.delegate) if moment == 2
	end


	# 'clear_delegate' should do what it says;
	# however, 'answer' should work even after the delegate has been 
cleared

	def test_clear_delegate
		c1 = Borges::Controller.new
		c2 = Borges::Controller.new
		ret  = ""
		assert_raises(Borges::RenderNotification) do
			ret = c1.call(c2)
			raise Borges::RenderNotification.new
		end
		c1.clear_delegate
		assert_equal nil,c1.delegate
		c2.answer "test" if (ret=="")
		assert_equal "test", ret
	end


	# 'active_controller' should be the controller that is currently 
responsible
	# for UI (i.e. the delegate, or the delegate's delegate, etc.)

	def test_active_controller
		c1 = Borges::Controller.new
		c2 = Borges::Controller.new
		c3 = Borges::Controller.new
		moment = 0

		begin
			assert_equal 2, c1.call(c2)
			moment = 2
		rescue Borges::RenderNotification
		end

		begin
			if(moment==0) then
				assert_equal 3, c2.call(c3)
				moment = 1
			end
		rescue Borges::RenderNotification
		end

		assert_equal c3, c1.active_controller if moment == 0
		c3.answer(3) if moment == 0

		assert_equal c2, c1.active_controller if moment == 1
		c2.answer(2) if moment == 1

		assert_equal c1, c1.active_controller
	end


	# if a controller has will_answer? set to false (this is used for 
optimization),
	# it should not answer to the calling controller
	# (side note: we're not trying to look inside a controller, at its 
@continuation etc.)

	def test_no_answer
		c1 = Borges::Controller.new
		c2 = Borges::Controller.new
		class << c2
			def will_answer?
				false
			end
		end
		assert_raises(Borges::RenderNotification) {c1.call(c2)}

		# should backtrack to the previous line and flunk, if answer 
succeeds
		# TODO: assert_raises with meaningful exception class
		c2.answer
		#assert_raises(Exception) {c2.answer}
	end


	# if a controller has will_call? set to false, it should not be able
	# to call other controllers. currently the test doesn't do anything 
special,
	# because the functionality isn't yet there

	def test_no_call
		c1 = Borges::Controller.new
		class << c1
			def will_call?
				false
			end
		end
		# nothing to test here yet - commented out
		# c1.call(nil)
	end
end


More information about the Borges-users mailing list