caricature 0.7.5 → 0.7.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +97 -97
- data/Rakefile +309 -310
- data/caricature.gemspec +123 -110
- data/lib/caricature/bacon/integration.rb +75 -75
- data/lib/caricature/bacon.rb +2 -2
- data/lib/caricature/clr/descriptor.rb +159 -159
- data/lib/caricature/clr/event_verification.rb +56 -56
- data/lib/caricature/clr/expectation.rb +100 -100
- data/lib/caricature/clr/isolation.rb +78 -78
- data/lib/caricature/clr/isolator.rb +252 -252
- data/lib/caricature/clr/messenger.rb +51 -49
- data/lib/caricature/clr/method_call_recorder.rb +96 -96
- data/lib/caricature/expectation.rb +1 -1
- data/lib/caricature/method_call_recorder.rb +3 -3
- data/lib/caricature/rspec/integration.rb +118 -118
- data/lib/caricature/version.rb +5 -5
- data/lib/caricature.rb +25 -25
- data/spec/bacon/integration/callback_spec.rb +156 -156
- data/spec/bacon/integration/clr_to_clr_spec.rb +325 -253
- data/spec/bacon/integration/event_spec.rb +97 -97
- data/spec/bacon/integration/indexer_spec.rb +27 -27
- data/spec/bacon/spec_helper.rb +4 -4
- data/spec/bacon/unit/descriptor_spec.rb +212 -212
- data/spec/bacon/unit/sword_spec.rb +39 -39
- data/spec/bacon/unit/verification_spec.rb +103 -103
- data/spec/bin/ClrModels.dll +0 -0
- data/spec/bin/ClrModels.dll.mdb +0 -0
- data/spec/fixtures/ExplodingCar.cs +56 -0
- data/spec/fixtures/ExposedChangedSubscriber.cs +26 -0
- data/spec/fixtures/ExposingWarrior.cs +58 -0
- data/spec/fixtures/IExplodingWarrior.cs +10 -0
- data/spec/fixtures/IExposing.cs +9 -0
- data/spec/fixtures/IExposingBridge.cs +9 -0
- data/spec/fixtures/IExposingWarrior.cs +8 -0
- data/spec/fixtures/IHaveAnIndexer.cs +8 -0
- data/spec/fixtures/IWarrior.cs +13 -0
- data/spec/fixtures/IWeapon.cs +9 -0
- data/spec/fixtures/IndexerCaller.cs +17 -0
- data/spec/fixtures/IndexerContained.cs +20 -0
- data/spec/fixtures/MyClassWithAStatic.cs +16 -0
- data/spec/fixtures/Ninja.cs +34 -0
- data/spec/fixtures/Samurai.cs +29 -0
- data/spec/fixtures/StaticCaller.cs +12 -0
- data/spec/fixtures/Sword.cs +16 -0
- data/spec/fixtures/SwordWithStatics.cs +19 -0
- data/spec/fixtures/clr_interaction.rb +61 -0
- data/spec/fixtures/dagger.rb +11 -0
- data/spec/fixtures/dagger_with_class_members.rb +11 -0
- data/spec/fixtures/sheath.rb +19 -0
- data/spec/fixtures/soldier.rb +29 -0
- data/spec/fixtures/soldier_with_class_members.rb +7 -0
- data/spec/fixtures/swift_cleanup_crew.rb +21 -0
- data/spec/fixtures/with_class_methods.rb +11 -0
- data/spec/{models → models.notused}/ClrModels.cs +241 -241
- data/spec/{models → models.notused}/ruby_models.rb +150 -150
- data/spec/rspec/integration/callback_spec.rb +156 -156
- data/spec/rspec/integration/clr_to_clr_spec.rb +254 -254
- data/spec/rspec/integration/clr_to_ruby_spec.rb +227 -227
- data/spec/rspec/integration/indexer_spec.rb +27 -27
- data/spec/rspec/integration/ruby_to_ruby_spec.rb +271 -271
- data/spec/rspec/spec_helper.rb +12 -12
- data/spec/rspec/unit/core_ext_spec.rb +87 -87
- data/spec/rspec/unit/descriptor_spec.rb +210 -210
- data/spec/rspec/unit/event_spec.rb +16 -16
- data/spec/rspec/unit/expectation_spec.rb +300 -300
- data/spec/rspec/unit/interop_spec.rb +29 -29
- data/spec/rspec/unit/isolation_spec.rb +86 -86
- data/spec/rspec/unit/isolator_spec.rb +219 -219
- data/spec/rspec/unit/messaging_spec.rb +310 -310
- data/spec/rspec/unit/method_call_spec.rb +342 -342
- data/spec/rspec/unit/sword_spec.rb +39 -39
- data/spec/rspec/unit/verification_spec.rb +103 -103
- data/spec/spec_helper.rb +16 -15
- metadata +42 -11
@@ -1,160 +1,160 @@
|
|
1
|
-
module Caricature
|
2
|
-
|
3
|
-
# Contains the logic to collect members from a CLR type
|
4
|
-
module ClrMemberCollector
|
5
|
-
|
6
|
-
attr_reader :class_events, :events
|
7
|
-
|
8
|
-
private
|
9
|
-
# collects the instance members for a CLR type.
|
10
|
-
# makes sure it can handle indexers for properties etc.
|
11
|
-
def build_member_collections(context={}, instance_member=true)
|
12
|
-
|
13
|
-
build_event_collection(context, instance_member)
|
14
|
-
|
15
|
-
mem = []
|
16
|
-
mem += build_method_collection(context, instance_member)
|
17
|
-
mem += build_property_collection(context, instance_member)
|
18
|
-
mem
|
19
|
-
end
|
20
|
-
|
21
|
-
def build_property_collection(context, instance_member)
|
22
|
-
context[:properties].inject([]) do |res, pi|
|
23
|
-
prop_name = property_name_from(pi)
|
24
|
-
res << MemberDescriptor.new(prop_name, pi.property_type, instance_member)
|
25
|
-
res << MemberDescriptor.new("set_Item", nil, instance_member) if prop_name == "get_Item"
|
26
|
-
res << MemberDescriptor.new("#{prop_name}=", nil, instance_member) if pi.can_write and prop_name != "get_Item"
|
27
|
-
res
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def build_method_collection(context, instance_member)
|
32
|
-
context[:methods].inject([]) do |meths, mi|
|
33
|
-
meths << MemberDescriptor.new(mi.name.underscore, mi.return_type, instance_member) unless event?(mi.name, instance_member)
|
34
|
-
meths
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def build_event_collection(context, instance_member)
|
39
|
-
context[:events].inject(evts=[]) { |evc, ei| evc << ClrEventDescriptor.new(ei.name, instance_member) }
|
40
|
-
(instance_member ? @events = evts : @class_events = evts)
|
41
|
-
end
|
42
|
-
|
43
|
-
# indicates if this member is an event
|
44
|
-
def event?(name, instance_member)
|
45
|
-
((instance_member ? @events : @class_events)||[]).any? { |en| /^(add|remove)_#{en.event_name}/i =~ name }
|
46
|
-
end
|
47
|
-
|
48
|
-
# gets the property name from the +PropertyInfo+
|
49
|
-
# when the property is an indexer it will return +[]+
|
50
|
-
def property_name_from(property_info)
|
51
|
-
return property_info.name.underscore if property_info.get_index_parameters.empty?
|
52
|
-
"get_Item"
|
53
|
-
end
|
54
|
-
|
55
|
-
# the binding flags for instance members of a CLR type
|
56
|
-
def instance_flags
|
57
|
-
System::Reflection::BindingFlags.public | System::Reflection::BindingFlags.instance
|
58
|
-
end
|
59
|
-
|
60
|
-
# the binding flags for class members of a CLR type
|
61
|
-
def class_flags
|
62
|
-
System::Reflection::BindingFlags.public | System::Reflection::BindingFlags.static
|
63
|
-
end
|
64
|
-
|
65
|
-
def event_flags
|
66
|
-
non_public_flag | instance_flags
|
67
|
-
end
|
68
|
-
|
69
|
-
def class_event_flags
|
70
|
-
non_public_flag | class_flags
|
71
|
-
end
|
72
|
-
|
73
|
-
def non_public_flag
|
74
|
-
System::Reflection::BindingFlags.non_public
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
class ClrEventDescriptor
|
80
|
-
|
81
|
-
attr_reader :event_name
|
82
|
-
|
83
|
-
def initialize(event_name, instance_member=true)
|
84
|
-
@event_name, @instance_member = event_name, instance_member
|
85
|
-
end
|
86
|
-
|
87
|
-
def instance_member?
|
88
|
-
@instance_member
|
89
|
-
end
|
90
|
-
|
91
|
-
def add_method_name
|
92
|
-
"add_#{event_name}"
|
93
|
-
end
|
94
|
-
|
95
|
-
def remove_method_name
|
96
|
-
"remove_#{event_name}"
|
97
|
-
end
|
98
|
-
|
99
|
-
def to_s
|
100
|
-
"<#{self.class}:#{object_id} @event_name=\"#{event_name}\">"
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# describes clr interfaces.
|
105
|
-
# Because CLR interfaces can't have static members this descriptor does not collect any class members
|
106
|
-
class ClrInterfaceDescriptor < TypeDescriptor
|
107
|
-
|
108
|
-
include ClrMemberCollector
|
109
|
-
|
110
|
-
# collects instance members on this interface
|
111
|
-
# it will collect properties, methods and property setters
|
112
|
-
def initialize_instance_members_for(klass)
|
113
|
-
clr_type = klass.to_clr_type
|
114
|
-
|
115
|
-
context = {}
|
116
|
-
context[:properties] = clr_type.collect_interface_properties
|
117
|
-
context[:methods] = clr_type.collect_interface_methods
|
118
|
-
context[:events] = clr_type.collect_interface_events
|
119
|
-
|
120
|
-
@instance_members = build_member_collections context
|
121
|
-
end
|
122
|
-
|
123
|
-
# this method is empty because an interface can't have static members
|
124
|
-
def initialize_class_members_for(klass); end
|
125
|
-
|
126
|
-
end
|
127
|
-
|
128
|
-
|
129
|
-
# Describes a CLR class type. it collects the properties and methods on an instance as well as on a static level
|
130
|
-
class ClrClassDescriptor < TypeDescriptor
|
131
|
-
|
132
|
-
include ClrMemberCollector
|
133
|
-
|
134
|
-
# collects all the instance members of the provided CLR class type
|
135
|
-
def initialize_instance_members_for(klass)
|
136
|
-
clr_type = klass.to_clr_type
|
137
|
-
|
138
|
-
context = {}
|
139
|
-
context[:methods] = clr_type.get_methods(instance_flags)
|
140
|
-
context[:properties] = clr_type.get_properties(instance_flags)
|
141
|
-
context[:events] = clr_type.get_events(event_flags)
|
142
|
-
|
143
|
-
@instance_members = build_member_collections context
|
144
|
-
end
|
145
|
-
|
146
|
-
# collects all the static members of the provided CLR class type
|
147
|
-
def initialize_class_members_for(klass)
|
148
|
-
clr_type = klass.to_clr_type
|
149
|
-
|
150
|
-
context = {}
|
151
|
-
context[:methods] = clr_type.get_methods(class_flags)
|
152
|
-
context[:properties] = clr_type.get_properties(class_flags)
|
153
|
-
context[:events] = clr_type.get_events(class_event_flags)
|
154
|
-
|
155
|
-
@class_members = build_member_collections context, false
|
156
|
-
end
|
157
|
-
|
158
|
-
end
|
159
|
-
|
1
|
+
module Caricature
|
2
|
+
|
3
|
+
# Contains the logic to collect members from a CLR type
|
4
|
+
module ClrMemberCollector
|
5
|
+
|
6
|
+
attr_reader :class_events, :events
|
7
|
+
|
8
|
+
private
|
9
|
+
# collects the instance members for a CLR type.
|
10
|
+
# makes sure it can handle indexers for properties etc.
|
11
|
+
def build_member_collections(context={}, instance_member=true)
|
12
|
+
|
13
|
+
build_event_collection(context, instance_member)
|
14
|
+
|
15
|
+
mem = []
|
16
|
+
mem += build_method_collection(context, instance_member)
|
17
|
+
mem += build_property_collection(context, instance_member)
|
18
|
+
mem
|
19
|
+
end
|
20
|
+
|
21
|
+
def build_property_collection(context, instance_member)
|
22
|
+
context[:properties].inject([]) do |res, pi|
|
23
|
+
prop_name = property_name_from(pi)
|
24
|
+
res << MemberDescriptor.new(prop_name, pi.property_type, instance_member)
|
25
|
+
res << MemberDescriptor.new("set_Item", nil, instance_member) if prop_name == "get_Item"
|
26
|
+
res << MemberDescriptor.new("#{prop_name}=", nil, instance_member) if pi.can_write and prop_name != "get_Item"
|
27
|
+
res
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def build_method_collection(context, instance_member)
|
32
|
+
context[:methods].inject([]) do |meths, mi|
|
33
|
+
meths << MemberDescriptor.new(mi.name.underscore, mi.return_type, instance_member) unless event?(mi.name, instance_member)
|
34
|
+
meths
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def build_event_collection(context, instance_member)
|
39
|
+
context[:events].inject(evts=[]) { |evc, ei| evc << ClrEventDescriptor.new(ei.name, instance_member) }
|
40
|
+
(instance_member ? @events = evts : @class_events = evts)
|
41
|
+
end
|
42
|
+
|
43
|
+
# indicates if this member is an event
|
44
|
+
def event?(name, instance_member)
|
45
|
+
((instance_member ? @events : @class_events)||[]).any? { |en| /^(add|remove)_#{en.event_name}/i =~ name }
|
46
|
+
end
|
47
|
+
|
48
|
+
# gets the property name from the +PropertyInfo+
|
49
|
+
# when the property is an indexer it will return +[]+
|
50
|
+
def property_name_from(property_info)
|
51
|
+
return property_info.name.underscore if property_info.get_index_parameters.empty?
|
52
|
+
"get_Item"
|
53
|
+
end
|
54
|
+
|
55
|
+
# the binding flags for instance members of a CLR type
|
56
|
+
def instance_flags
|
57
|
+
System::Reflection::BindingFlags.public | System::Reflection::BindingFlags.instance
|
58
|
+
end
|
59
|
+
|
60
|
+
# the binding flags for class members of a CLR type
|
61
|
+
def class_flags
|
62
|
+
System::Reflection::BindingFlags.public | System::Reflection::BindingFlags.static
|
63
|
+
end
|
64
|
+
|
65
|
+
def event_flags
|
66
|
+
non_public_flag | instance_flags
|
67
|
+
end
|
68
|
+
|
69
|
+
def class_event_flags
|
70
|
+
non_public_flag | class_flags
|
71
|
+
end
|
72
|
+
|
73
|
+
def non_public_flag
|
74
|
+
System::Reflection::BindingFlags.non_public
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
class ClrEventDescriptor
|
80
|
+
|
81
|
+
attr_reader :event_name
|
82
|
+
|
83
|
+
def initialize(event_name, instance_member=true)
|
84
|
+
@event_name, @instance_member = event_name, instance_member
|
85
|
+
end
|
86
|
+
|
87
|
+
def instance_member?
|
88
|
+
@instance_member
|
89
|
+
end
|
90
|
+
|
91
|
+
def add_method_name
|
92
|
+
"add_#{event_name}"
|
93
|
+
end
|
94
|
+
|
95
|
+
def remove_method_name
|
96
|
+
"remove_#{event_name}"
|
97
|
+
end
|
98
|
+
|
99
|
+
def to_s
|
100
|
+
"<#{self.class}:#{object_id} @event_name=\"#{event_name}\">"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# describes clr interfaces.
|
105
|
+
# Because CLR interfaces can't have static members this descriptor does not collect any class members
|
106
|
+
class ClrInterfaceDescriptor < TypeDescriptor
|
107
|
+
|
108
|
+
include ClrMemberCollector
|
109
|
+
|
110
|
+
# collects instance members on this interface
|
111
|
+
# it will collect properties, methods and property setters
|
112
|
+
def initialize_instance_members_for(klass)
|
113
|
+
clr_type = klass.to_clr_type
|
114
|
+
|
115
|
+
context = {}
|
116
|
+
context[:properties] = clr_type.collect_interface_properties
|
117
|
+
context[:methods] = clr_type.collect_interface_methods
|
118
|
+
context[:events] = clr_type.collect_interface_events
|
119
|
+
|
120
|
+
@instance_members = build_member_collections context
|
121
|
+
end
|
122
|
+
|
123
|
+
# this method is empty because an interface can't have static members
|
124
|
+
def initialize_class_members_for(klass); end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
# Describes a CLR class type. it collects the properties and methods on an instance as well as on a static level
|
130
|
+
class ClrClassDescriptor < TypeDescriptor
|
131
|
+
|
132
|
+
include ClrMemberCollector
|
133
|
+
|
134
|
+
# collects all the instance members of the provided CLR class type
|
135
|
+
def initialize_instance_members_for(klass)
|
136
|
+
clr_type = klass.to_clr_type
|
137
|
+
|
138
|
+
context = {}
|
139
|
+
context[:methods] = clr_type.get_methods(instance_flags)
|
140
|
+
context[:properties] = clr_type.get_properties(instance_flags)
|
141
|
+
context[:events] = clr_type.get_events(event_flags)
|
142
|
+
|
143
|
+
@instance_members = build_member_collections context
|
144
|
+
end
|
145
|
+
|
146
|
+
# collects all the static members of the provided CLR class type
|
147
|
+
def initialize_class_members_for(klass)
|
148
|
+
clr_type = klass.to_clr_type
|
149
|
+
|
150
|
+
context = {}
|
151
|
+
context[:methods] = clr_type.get_methods(class_flags)
|
152
|
+
context[:properties] = clr_type.get_properties(class_flags)
|
153
|
+
context[:events] = clr_type.get_events(class_event_flags)
|
154
|
+
|
155
|
+
@class_members = build_member_collections context, false
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
160
|
end
|
@@ -1,57 +1,57 @@
|
|
1
|
-
module Caricature
|
2
|
-
|
3
|
-
# Describes a verification of a method call.
|
4
|
-
# This corresponds kind of to an assertion
|
5
|
-
class EventVerification
|
6
|
-
|
7
|
-
# Initializes a new instance of a +Verification+
|
8
|
-
def initialize(event_name, recorder, mode=:instance)
|
9
|
-
@event_name, @args, @any_args, @recorder, @mode = event_name, [], true, recorder, mode
|
10
|
-
init_plugin
|
11
|
-
end
|
12
|
-
|
13
|
-
def init_plugin
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
# indicates whether this verification can be for any arguments
|
18
|
-
def any_args?
|
19
|
-
@any_args
|
20
|
-
end
|
21
|
-
|
22
|
-
# constrain this verification to the provided arguments
|
23
|
-
def with(*args)
|
24
|
-
@any_args = args.first.is_a?(Symbol) and args.first == :any
|
25
|
-
@args = args
|
26
|
-
# @callback = b if b
|
27
|
-
self
|
28
|
-
end
|
29
|
-
|
30
|
-
# allow any arguments ignore the argument constraint
|
31
|
-
def allow_any_arguments
|
32
|
-
@any_args = true
|
33
|
-
self
|
34
|
-
end
|
35
|
-
|
36
|
-
# figure out if this argument variation matches the provided args.
|
37
|
-
def matches?(event_name, *args)
|
38
|
-
@event_name == event_name and (any_args? or @args == args)
|
39
|
-
end
|
40
|
-
|
41
|
-
def error
|
42
|
-
@recorder.event_error
|
43
|
-
end
|
44
|
-
|
45
|
-
# indicate that this method verification is successful
|
46
|
-
def successful?
|
47
|
-
a = any_args? ? [:any] : @args
|
48
|
-
begin
|
49
|
-
@recorder.event_raised?(@event_name, @mode, *a)
|
50
|
-
rescue ArgumentError
|
51
|
-
false
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
1
|
+
module Caricature
|
2
|
+
|
3
|
+
# Describes a verification of a method call.
|
4
|
+
# This corresponds kind of to an assertion
|
5
|
+
class EventVerification
|
6
|
+
|
7
|
+
# Initializes a new instance of a +Verification+
|
8
|
+
def initialize(event_name, recorder, mode=:instance)
|
9
|
+
@event_name, @args, @any_args, @recorder, @mode = event_name, [], true, recorder, mode
|
10
|
+
init_plugin
|
11
|
+
end
|
12
|
+
|
13
|
+
def init_plugin
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
# indicates whether this verification can be for any arguments
|
18
|
+
def any_args?
|
19
|
+
@any_args
|
20
|
+
end
|
21
|
+
|
22
|
+
# constrain this verification to the provided arguments
|
23
|
+
def with(*args)
|
24
|
+
@any_args = args.first.is_a?(Symbol) and args.first == :any
|
25
|
+
@args = args
|
26
|
+
# @callback = b if b
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
# allow any arguments ignore the argument constraint
|
31
|
+
def allow_any_arguments
|
32
|
+
@any_args = true
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
# figure out if this argument variation matches the provided args.
|
37
|
+
def matches?(event_name, *args)
|
38
|
+
@event_name == event_name and (any_args? or @args == args)
|
39
|
+
end
|
40
|
+
|
41
|
+
def error
|
42
|
+
@recorder.event_error
|
43
|
+
end
|
44
|
+
|
45
|
+
# indicate that this method verification is successful
|
46
|
+
def successful?
|
47
|
+
a = any_args? ? [:any] : @args
|
48
|
+
begin
|
49
|
+
@recorder.event_raised?(@event_name, @mode, *a)
|
50
|
+
rescue ArgumentError
|
51
|
+
false
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
57
|
end
|
@@ -1,101 +1,101 @@
|
|
1
|
-
module Caricature
|
2
|
-
|
3
|
-
# Syntax for the expectation builder and expectation
|
4
|
-
# to define an event on the expectation.
|
5
|
-
module EventExpectationSyntax
|
6
|
-
|
7
|
-
# tell the expectation it needs to raise an event when this method is called
|
8
|
-
def raise_event(name, *ags, &b)
|
9
|
-
collected.merge!(:event_name => name, :event_args => ags, :event_handler => b)
|
10
|
-
self
|
11
|
-
end
|
12
|
-
|
13
|
-
# raises the events that has been registered on the object at runtime
|
14
|
-
def raise_subscriptions
|
15
|
-
collected[:raise_subscriptions] = true
|
16
|
-
self
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
|
-
# Adds event support to the expectation
|
23
|
-
# this is only applicable to clr isolations and not
|
24
|
-
# to ruby isolations
|
25
|
-
class Expectation
|
26
|
-
|
27
|
-
include EventExpectationSyntax
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
# the block that will be used
|
32
|
-
def event_name
|
33
|
-
collected[:event_name]
|
34
|
-
end
|
35
|
-
|
36
|
-
# the arguments that will be used for the event
|
37
|
-
def event_args
|
38
|
-
evt_ags = collected[:event_args]
|
39
|
-
evt_ags.nil? or evt_ags.empty? ? [nil, System::EventArgs.empty] : evt_ags
|
40
|
-
end
|
41
|
-
|
42
|
-
# the block that will be used# the block that will be used
|
43
|
-
def event_handler
|
44
|
-
collected[:event_handler] || lambda { |s,e| }
|
45
|
-
end
|
46
|
-
|
47
|
-
# the events attached to the context
|
48
|
-
def events
|
49
|
-
collected[:events] ||= {}
|
50
|
-
end
|
51
|
-
|
52
|
-
# Set the registered events for this expectation
|
53
|
-
def events=(evts)
|
54
|
-
collected[:events]=evts
|
55
|
-
end
|
56
|
-
|
57
|
-
# indicates whether this expectation has an event that needs to be raised
|
58
|
-
def raises_event?
|
59
|
-
!!collected[:event_name]
|
60
|
-
end
|
61
|
-
|
62
|
-
# indicates whether this expectation has an event handler to be called
|
63
|
-
def has_event_handler?
|
64
|
-
!!collected[:event_handler]
|
65
|
-
end
|
66
|
-
|
67
|
-
# indicates whether to raise the registered event handlers
|
68
|
-
def raises_registered?
|
69
|
-
!!collected[:raise_subscriptions]
|
70
|
-
end
|
71
|
-
|
72
|
-
def event_recorder(&b)
|
73
|
-
@event_recorder = b
|
74
|
-
end
|
75
|
-
|
76
|
-
def to_s #:nodoc:
|
77
|
-
"<Caricature::Expecation, method_name: #{method_name}, args: #{args}, error args: #{error_args}, event: #{event_name}>"
|
78
|
-
end
|
79
|
-
alias :inspect :to_s
|
80
|
-
|
81
|
-
private
|
82
|
-
|
83
|
-
def do_event_raise #:nodoc:
|
84
|
-
return unless @event_recorder
|
85
|
-
@event_recorder.call event_name, event_args, event_handler
|
86
|
-
return unless raises_event?
|
87
|
-
|
88
|
-
ags = event_args
|
89
|
-
event_handler.call *ags if has_event_handler?
|
90
|
-
|
91
|
-
return unless raises_registered? or not has_event_handler?
|
92
|
-
events[event_name].each { |evt| call_handler evt, *ags }
|
93
|
-
end
|
94
|
-
|
95
|
-
def call_handler(evt, *ags)
|
96
|
-
evt.respond_to?(:call) ? evt.call(*ags) : evt.get_invocation_list.each{|ev| ev.invoke(*ags) }
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
ExpectationBuilder.send :include, EventExpectationSyntax
|
1
|
+
module Caricature
|
2
|
+
|
3
|
+
# Syntax for the expectation builder and expectation
|
4
|
+
# to define an event on the expectation.
|
5
|
+
module EventExpectationSyntax
|
6
|
+
|
7
|
+
# tell the expectation it needs to raise an event when this method is called
|
8
|
+
def raise_event(name, *ags, &b)
|
9
|
+
collected.merge!(:event_name => name, :event_args => ags, :event_handler => b)
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
# raises the events that has been registered on the object at runtime
|
14
|
+
def raise_subscriptions
|
15
|
+
collected[:raise_subscriptions] = true
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
# Adds event support to the expectation
|
23
|
+
# this is only applicable to clr isolations and not
|
24
|
+
# to ruby isolations
|
25
|
+
class Expectation
|
26
|
+
|
27
|
+
include EventExpectationSyntax
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
# the block that will be used
|
32
|
+
def event_name
|
33
|
+
collected[:event_name]
|
34
|
+
end
|
35
|
+
|
36
|
+
# the arguments that will be used for the event
|
37
|
+
def event_args
|
38
|
+
evt_ags = collected[:event_args]
|
39
|
+
evt_ags.nil? or evt_ags.empty? ? [nil, System::EventArgs.empty] : evt_ags
|
40
|
+
end
|
41
|
+
|
42
|
+
# the block that will be used# the block that will be used
|
43
|
+
def event_handler
|
44
|
+
collected[:event_handler] || lambda { |s,e| }
|
45
|
+
end
|
46
|
+
|
47
|
+
# the events attached to the context
|
48
|
+
def events
|
49
|
+
collected[:events] ||= {}
|
50
|
+
end
|
51
|
+
|
52
|
+
# Set the registered events for this expectation
|
53
|
+
def events=(evts)
|
54
|
+
collected[:events]=evts
|
55
|
+
end
|
56
|
+
|
57
|
+
# indicates whether this expectation has an event that needs to be raised
|
58
|
+
def raises_event?
|
59
|
+
!!collected[:event_name]
|
60
|
+
end
|
61
|
+
|
62
|
+
# indicates whether this expectation has an event handler to be called
|
63
|
+
def has_event_handler?
|
64
|
+
!!collected[:event_handler]
|
65
|
+
end
|
66
|
+
|
67
|
+
# indicates whether to raise the registered event handlers
|
68
|
+
def raises_registered?
|
69
|
+
!!collected[:raise_subscriptions]
|
70
|
+
end
|
71
|
+
|
72
|
+
def event_recorder(&b)
|
73
|
+
@event_recorder = b
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_s #:nodoc:
|
77
|
+
"<Caricature::Expecation, method_name: #{method_name}, args: #{args}, error args: #{error_args}, event: #{event_name}>"
|
78
|
+
end
|
79
|
+
alias :inspect :to_s
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def do_event_raise #:nodoc:
|
84
|
+
return unless @event_recorder
|
85
|
+
@event_recorder.call event_name, event_args, event_handler
|
86
|
+
return unless raises_event?
|
87
|
+
|
88
|
+
ags = event_args
|
89
|
+
event_handler.call *ags if has_event_handler?
|
90
|
+
|
91
|
+
return unless raises_registered? or not has_event_handler?
|
92
|
+
events[event_name].each { |evt| call_handler evt, *ags }
|
93
|
+
end
|
94
|
+
|
95
|
+
def call_handler(evt, *ags)
|
96
|
+
evt.respond_to?(:call) ? evt.call(*ags) : evt.get_invocation_list.each{|ev| ev.invoke(*ags) }
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
ExpectationBuilder.send :include, EventExpectationSyntax
|
101
101
|
end
|