Changeset 271
- Timestamp:
- 06/17/07 11:16:31 (17 months ago)
- Location:
- trunk
- Files:
-
- 2 modified
- 1 moved
-
lib/fm/event.rb (modified) (1 diff)
-
spec/entity_spec.rb (modified) (3 diffs)
-
spec/event_spec.rb (moved) (moved from trunk/spec/event.tests.rb) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/fm/event.rb
r270 r271 110 110 select {|kl| kl.class == Class }. 111 111 collect {|kl| camelcase_to_underbarred(kl.name[/\w+$/]) }. 112 collect {|name| "handle_" + name}112 collect {|name| ("handle_" + name).to_sym } 113 113 end 114 114 -
trunk/spec/entity_spec.rb
r270 r271 18 18 require 'spec/runner' 19 19 require 'fm/entity' 20 require 'fm/events/all'21 20 22 21 23 22 # Event classes for testing event handler. 24 23 class TestingEntity < FaerieMUD::Entity 25 def initialize26 @last_seen_event = nil27 @last_handler = nil28 super29 end30 attr_reader :last_seen_event, :last_handler31 end32 33 class TestingEvent < FaerieMUD::Event34 def initialize; end35 end36 37 class DerivTestingEvent < TestingEvent; end38 class DerivDerivTestingEvent < DerivTestingEvent; end39 class ResultEvent < FaerieMUD::Event40 def initialize; end41 24 end 42 25 … … 46 29 before( :each ) do 47 30 @entity = TestingEntity.new 48 @testingEvent = TestingEvent.new49 @derivTestingEvent = DerivTestingEvent.new50 @derivDerivTestingEvent = DerivDerivTestingEvent.new51 31 end 52 32 … … 58 38 ### Unhandled events should raise an exception 59 39 it "raises an exception for unhandled events" do 40 event = mock( "testing event", :null_object => true ) 41 event.should_receive( :handler_names ). 42 and_return([:handle_testing_event]) 60 43 61 44 # Passing an event without a specific handler should cause an exception 62 45 # to be raised. 63 46 lambda { 64 @entity.handle_events( @testingEvent )47 @entity.handle_events( event ) 65 48 }.should raise_error( FaerieMUD::UnhandledEventError ) 49 end 50 end 51 52 53 describe FaerieMUD::Entity, " with an event handler defined" do 54 before( :each ) do 55 @entity = TestingEntity.new 56 57 # Define an event handler for TestingEvents 58 def @entity.handle_testing_event( ev ) 59 return :result_event 60 end 61 end 62 63 64 it "asks incoming event objects what event handlers should handle them" do 65 event = mock( "event", :null_object => true ) 66 event.should_receive( :handler_names ). 67 and_return([:handle_testing_event]) 68 69 @entity.handle_events( event ).should == [:result_event] 70 end 71 72 it "tries each event handler in turn until it finds an implemented one" do 73 event = mock( "event with one superclass", :null_object => true ) 74 event.should_receive( :handler_names ). 75 and_return([:handle_special_event, :handle_testing_event]) 76 77 @entity.handle_events( event ).should == [:result_event] 66 78 end 67 79 68 80 end 69 81 70 # describe FaerieMUD::Entity, " derivative which defines a fallback handler" do 71 # 72 # it "should handle any event, even ones with no specific handler" do 73 # # Add a fallback handler 74 # def @entity.handle_any_event( ev ) 75 # @last_seen_event = ev 76 # @last_handler = :handle_any_event 77 # end 78 # 79 # lambda { @entity.handle_events(@testingEvent) }.should_not raise_error() 80 # @entity.last_seen_event.should == @testingEvent 81 # @entity.last_handler.should == :handle_any_event 82 # end 83 # 84 # 85 # ### Test fallback event handler. 86 # def test_entities_which_define_fallback_handler_should_handle_any_event 87 # rval = nil 88 # 89 # 90 # end 91 # 92 # 93 # ### Test to be sure handlers for base classes catch derivatives. 94 # def test_entities_should_handle_derivative_events_with_more_general_handlers 95 # rval = nil 96 # 97 # def @entity.handle_testing_event( ev ) 98 # @last_seen_event = ev 99 # @last_handler = :handle_testing_event 100 # end 101 # 102 # assert_nothing_raised { @entity.handle_events(@testingEvent) } 103 # assert_same @testingEvent, @entity.last_seen_event, 104 # "last seen event for TestingEvent" 105 # assert_equal :handle_testing_event, @entity.last_handler, 106 # "last handler for TestingEvent" 107 # 108 # assert_nothing_raised { @entity.handle_events(@derivTestingEvent) } 109 # assert_same @derivTestingEvent, @entity.last_seen_event, 110 # "last seen event for DerivTestingEvent" 111 # assert_equal :handle_testing_event, @entity.last_handler, 112 # "last handler for DerivTestingEvent" 113 # 114 # assert_nothing_raised { @entity.handle_events(@derivDerivTestingEvent) } 115 # assert_same @derivDerivTestingEvent, @entity.last_seen_event, 116 # "last seen event for DerivDerivTestingEvent" 117 # assert_equal :handle_testing_event, @entity.last_handler, 118 # "last handler for DerivDerivTestingEvent" 119 # end 120 # 121 # 122 # def test_event_handler_should_be_able_to_handle_single_event_result 123 # rval = nil 124 # 125 # def @entity.handle_testing_event( ev ) 126 # return ResultEvent.new 127 # end 128 # 129 # assert_nothing_raised { rval = @entity.handle_events(@testingEvent) } 130 # assert_instance_of Array, rval 131 # assert_equal 1, rval.length 132 # assert_instance_of ResultEvent, rval[0] 133 # 134 # assert_nothing_raised { rval = @entity.handle_event(@testingEvent) } 135 # assert_instance_of Array, rval 136 # assert_equal 1, rval.length 137 # assert_instance_of ResultEvent, rval[0] 138 # end 139 # 140 # end 141 # 82 83 describe FaerieMUD::Entity, " which defines a fallback handler" do 84 85 before( :each ) do 86 @entity = TestingEntity.new 87 88 # Add a fallback handler 89 def @entity.handle_any_event( ev ) 90 return :result_event 91 end 92 end 93 94 95 it "should handle any event, even ones with no specific handler" do 96 event = mock( "event", :null_object => true ) 97 event.should_receive( :handler_names ). 98 and_return([:handle_special_event, :handle_testing_event]) 99 100 @entity.handle_events( event ) == [:result_event] 101 end 102 end 103 -
trunk/spec/event_spec.rb
r270 r271 1 1 #!/usr/bin/ruby -w 2 2 # 3 # Unit test for the Eventclass3 # Specification for the FaerieMUD::Entity class 4 4 # $Id$ 5 5 # 6 # Unit test for the FaerieMUD::Logger class 7 # $Id$ 8 # 9 # Copyright (c) 2003-2005 The FaerieMUD Consortium. Most rights reserved. 10 # 11 # This work is licensed under the Creative Commons Attribution-ShareAlike 12 # License. To view a copy of this license, visit 13 # http://creativecommons.org/licenses/by-sa/1.0/ or send a letter to Creative 14 # Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. 15 # 6 # Copyright (c) 2003-2007 The FaerieMUD Consortium. 16 7 # 17 8 … … 21 12 $LOAD_PATH.unshift "#{basedir}/lib" unless 22 13 $LOAD_PATH.include?( "#{basedir}/lib" ) 23 $LOAD_PATH.unshift "#{basedir}/tests/lib" unless 24 $LOAD_PATH.include?( "#{basedir}/tests/lib" ) 25 26 require 'fmtestcase' 14 $LOAD_PATH.unshift "#{basedir}/spec/lib" unless 15 $LOAD_PATH.include?( "#{basedir}/spec/lib" ) 27 16 end 28 17 29 require 'fm/events/all' 30 require 'fm/gameobject' 31 require 'fm/locus' 18 require 'spec/runner' 19 require 'fm/event' 32 20 33 ### Test case for the FaerieMUD::Event class and associated classes/modules.34 class EventTestCase < FaerieMUD::TestCase35 21 36 class TestEntity < FaerieMUD::Entity; end 37 class TestLocus < FaerieMUD::Locus; end 38 class TestVerb < FaerieMUD::Verb; end 39 40 module Complex; end 41 class MyEvent < FaerieMUD::Event; end 42 class IntermediateEvent < MyEvent; end 43 class YourEvent < IntermediateEvent 44 include Complex 22 describe FaerieMUD::Event do 23 24 it "requires an instigator to be created" do 25 lambda { 26 FaerieMUD::Event.new( :origin => :locus, :verb => :run ) 27 }.should raise_error( ArgumentError, /:instigator/ ) 45 28 end 46 29 47 48 # Set up instances of associated classes for testing 49 def setup 50 @entity = TestEntity::new 51 @locus = TestLocus::new 52 @verb = TestVerb::instance 53 super 30 it "requires an origin to be created" do 31 lambda { 32 FaerieMUD::Event.new( :instigator => :character, :verb => :run ) 33 }.should raise_error( ArgumentError, /:origin/ ) 54 34 end 55 35 56 # Discard testing objects 57 def teardown 58 super 59 @entity = @locus = @verb = nil 60 end 61 62 63 64 ################################################################# 65 ### E X A M P L E S 66 ################################################################# 67 68 ### Instance test 69 def test_instantiation_should_require_an_instigator 70 rval = derivClass = derivObject = nil 71 72 # Instantiation requires an :instigator 73 assert_raises( ArgumentError ) do 74 derivClass = Class::new( FaerieMUD::Event ) 75 derivObject = derivClass::new( 76 :origin => @locus, 77 :verb => @verb 78 ) 79 end 80 end 81 82 # Instantiation requires an :origin 83 def test_instantiation_should_require_an_origin 84 rval = derivClass = derivObject = nil 85 86 assert_raises( ArgumentError ) do 87 derivClass = Class::new( FaerieMUD::Event ) 88 derivObject = derivClass::new( 89 :instigator => @entity, 90 :verb => @verb 91 ) 92 end 93 end 94 95 96 # Instantiation requires a :verb 97 def test_instantiation_should_require_a_verb 98 rval = derivClass = derivObject = nil 99 100 assert_raises( ArgumentError ) do 101 derivClass = Class::new( FaerieMUD::Event ) 102 derivObject = derivClass::new( 103 :instigator => @entity, 104 :origin => @locus 105 ) 106 end 107 end 108 109 110 ### Event handler names should use the name of the event class 111 def test_event_handler_names_should_follow_shalow_inheritance 112 rval = ev = nil 113 114 # Simple 1-deep inheritance 115 assert_nothing_raised do 116 ev = MyEvent::new( 117 :instigator => @entity, 118 :origin => @locus, 119 :verb => @verb 120 ) 121 rval = ev.handler_names 122 end 123 124 assert_instance_of Array, rval 125 assert_equal ["handle_my_event"], rval 126 end 127 128 129 #### Test the generation of handler names for deep/complex inheritance 130 def test_event_handler_names_should_follow_deep_inheritance 131 rval = ev = nil 132 133 # Complex 2-deep with mixin 134 assert_nothing_raised do 135 ev = YourEvent::new( 136 :instigator => @entity, 137 :origin => @locus, 138 :verb => @verb 139 ) 140 rval = ev.handler_names 141 end 142 143 # If we do end up implementing mixin-sensitive event handlers, this 144 # assert_equal will fail. 145 assert_instance_of Array, rval 146 assert_equal [ 147 "handle_your_event", "handle_intermediate_event", "handle_my_event" 148 ], rval 149 36 it "requires a verb to be created" do 37 lambda { 38 FaerieMUD::Event.new( :origin => :locus, :instigator => :character ) 39 }.should raise_error( ArgumentError, /:verb/ ) 150 40 end 151 41 152 42 end 153 43 44 45 # The handler-name examples below use a dummy class hierarchy: 46 # 47 # AstrologicalEvent 48 # | 49 # LunarEvent 50 # | ____ Remarkable (mixin) 51 # |/ 52 # |\____ AstrologicallySignificant (mixin) 53 # | 54 # LunarEclipseEvent 55 # 56 class AstronomicalEvent < FaerieMUD::Event; end 57 58 describe "A direct simple subclass of FaerieMUD::Event" do 59 60 before( :each ) do 61 @event = AstronomicalEvent.new( 62 :origin => :a_locus, 63 :verb => :run, 64 :instigator => :the_world 65 ) 66 end 67 68 69 it "suggests a single handler derived from its name" do 70 @event.handler_names.should == [:handle_astronomical_event] 71 end 72 73 end 74 75 76 class LunarEvent < AstronomicalEvent; end 77 78 describe "An indirect simple subclass of FaerieMUD::Event" do 79 before( :each ) do 80 @event = LunarEvent.new( 81 :origin => :a_locus, 82 :verb => :run, 83 :instigator => :the_world 84 ) 85 end 86 87 88 it "suggests handlers derived from the names of its class and parent classes," + 89 " in order from most-specific to least" do 90 @event.handler_names.should == [:handle_lunar_event, :handle_astronomical_event] 91 end 92 end 93 94 95 class LunarEclipseEvent < LunarEvent; end 96 97 describe "An indirect subclass of FaerieMUD::Event that has included mixins" do 98 before( :each ) do 99 @event = LunarEclipseEvent.new( 100 :origin => :a_locus, 101 :verb => :run, 102 :instigator => :the_world 103 ) 104 end 105 106 107 it "suggests handlers derived from the names of its class and parent classes, " + 108 "in order from most-specific to least" do 109 @event.handler_names.should == [ 110 :handle_lunar_eclipse_event, 111 :handle_lunar_event, 112 :handle_astronomical_event 113 ] 114 end 115 end 116
