require 'test/unit' require 'notifyhub' class NotifyHubTest < Test::Unit::TestCase # Create class that includes interesting events. class Storage # Handle to NotifyHub. attr_accessor :hub def initialize # Create NotifyHub. @hub = NotifyHub.declare( :store, :load, :na ) end # Store data and notify clients. def store( data ) @data = data @hub[ :store ].notify( @data ) end # Load data and notify clients. def load @hub[ :load ].notify( @data ) @data end end def setup @check = {} @n = NotifyHub.auto @n[ :on ].action do |val| @check[ :create_1 ] = val end @n[ :on ].action do |val| @check[ :create_2 ] = val end @n[ :off ].action do |val| @check[ :create_3 ] = val end end def test_integration storage = Storage.new store_data = nil load_data = nil # Setup notify action for store. storage.hub[ :store ].action do |data| @check[:store_data] = data end # Setup notify action for load. storage.hub[ :load ].action do |data| @check[:load_data] = data end storage.store( "my data" ) storage.load assert_equal( @check[:store_data], "my data" ) assert_equal( @check[:load_data], "my data" ) end def test_defining [ :declare, :create, :auto ].each do |type| case type when :declare; n = NotifyHub.declare( :on, :off ) when :create n = NotifyHub.create do declare( :on, :off ) end when :auto; n = NotifyHub.auto end n[ :on ].action do |val| @check[ :create_1 ] = val end n[ :on ].action do |val| @check[ :create_2 ] = val end n[ :off ].action do |val| @check[ :create_3 ] = val end n[ :on ].notify( 1 ) n.notify( :off, 2 ) assert_equal( 1, @check[ :create_1 ] ) assert_equal( 1, @check[ :create_2 ] ) assert_equal( 2, @check[ :create_3 ] ) n = NotifyHub.create do declare( :on, :off ) end end end def test_enable_set @n[ :off ].enable( false ) @n[ :off ].notify( 1 ) assert_equal( nil, @check[ :create_3 ] ) @n[ :off ].enable( true ) @n[ :off ].notify( 1 ) assert_equal( 1, @check[ :create_3 ] ) end def test_enable_one @n[ :on ][ 0 ].enable( false ) @n[ :on ].notify( 1 ) @n[ :off ].notify( 2 ) assert_equal( nil, @check[ :create_1 ] ) assert_equal( 1, @check[ :create_2 ] ) assert_equal( 2, @check[ :create_3 ] ) @n[ :on ].each do |i| i.enable( true ) end @n[ :on ].notify( 1 ) @n[ :off ].notify( 2 ) assert_equal( 1, @check[ :create_1 ] ) assert_equal( 1, @check[ :create_2 ] ) assert_equal( 2, @check[ :create_3 ] ) end def test_remove @n[ :on ].remove( @n[ :on ][ 0 ] ) @n[ :on ].notify( 1 ) @n[ :off ].notify( 2 ) assert_equal( nil, @check[ :create_1 ] ) assert_equal( 1, @check[ :create_2 ] ) assert_equal( 2, @check[ :create_3 ] ) end def test_errors n = NotifyHub.declare( :on, :off ) begin n[ :not ].action do |val| puts "This does not activate..." end rescue NotifyHub::NotFound @check[ :error_action ] = true end assert_equal( true, @check[ :error_action ] ) begin n[ :not ].notify( 2 ) rescue NotifyHub::NotFound @check[ :error_notify ] = true end assert_equal( true, @check[ :error_notify ] ) begin n.declare( :on ) rescue NotifyHub::Redefining @check[ :error_redecl ] = true end assert_equal( true, @check[ :error_redecl ] ) end def test_ids assert_equal( [ :on, :off ], @n.ids ) end end