hooked 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,234 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- class ControllerTest < Test::Unit::TestCase
4
- def setup
5
- @containers = [
6
- Class.new do
7
- include Hooked::Container
8
- before :foo, :do_something do |*args|; end
9
- end,
10
- Class.new do
11
- include Hooked::Container
12
- around :foo, :do_something_else do |h, *args|
13
- h.call(*args)
14
- end
15
- end,
16
- Class.new do
17
- include Hooked::Container
18
- hookable :foo do |*args|; end
19
- end
20
- ].map {|c| c.new }
21
- end
22
-
23
- def test_has_containers
24
- controller = Hooked::Controller.new(@containers[0])
25
- controller.add(*@containers[1..2])
26
-
27
- assert_equal @containers, controller.containers
28
- end
29
-
30
- def test_collects_hookables_and_hooks_from_containers
31
- hookable = @containers[2].class.hookables[0]
32
- hooks = [
33
- @containers[0].class.hooks[:foo][0],
34
- @containers[1].class.hooks[:foo][0]
35
- ]
36
- controller = Hooked::Controller.new(*@containers)
37
-
38
- assert_equal hookable, controller.hookables[hookable.name]
39
- assert_equal hooks, controller.hooks[:foo]
40
- end
41
-
42
- def test_sorts_hooks
43
- container = Class.new do
44
- include(Hooked::Container)
45
- before(:foo, :do_something) {}
46
- before(:foo, :do_dishes, :after => :all) {}
47
- around(:foo, :do_something_more, :before => :do_something) {}
48
- after(:foo, :do_something_else, :after => :do_something_more, :before => :do_something) {}
49
- around(:foo, :do_laundry, :before => :all) {}
50
- end
51
- order = [:do_laundry, :do_something_more, :do_something_else, :do_something, :do_dishes]
52
-
53
- controller = Hooked::Controller.new(container.new)
54
- assert_equal order, controller.hooks[:foo].map {|h| h.name }
55
-
56
- controller = Hooked::Controller.new
57
- controller.add(container.new)
58
- assert_not_equal order, controller.hooks[:foo].map {|h| h.name }
59
-
60
- controller.hooks[:foo].shuffle!
61
- controller.refresh
62
- assert_equal order, controller.hooks[:foo].map {|h| h.name }
63
- end
64
-
65
- def test_sets_the_container_on_hookables_and_hooks
66
- controller = Hooked::Controller.new(*@containers[1..2])
67
- assert_equal @containers[1], controller.hooks[:foo][0].container
68
- assert_equal @containers[2], controller.hookables[:foo].container
69
- end
70
-
71
- def test_containers_are_aware_of_their_controller
72
- controller = Hooked::Controller.new(*@containers)
73
- @containers.each do |c|
74
- assert_equal controller, c.hooked
75
- end
76
- end
77
-
78
- def test_invokes_hooks_in_their_containers_scope
79
- scope = nil
80
- container = Class.new do
81
- include(Hooked::Container)
82
- hookable(:something) {}
83
- before(:something, :do_something_else) { scope = self }
84
- end.new
85
-
86
- controller = Hooked::Controller.new(container)
87
- controller.invoke(:something)
88
-
89
- assert_equal(container, scope)
90
- end
91
-
92
- def test_invokes_hookables_in_their_containers_scope
93
- scope = nil
94
- container = Class.new do
95
- include(Hooked::Container)
96
- hookable(:something) { scope = self }
97
- end.new
98
-
99
- controller = Hooked::Controller.new(container)
100
- controller.invoke(:something)
101
-
102
- assert_equal(container, scope)
103
- end
104
-
105
- def test_invokes_dynamic_hookables_in_their_original_scope
106
- controller = Hooked::Controller.new
107
-
108
- scope = nil
109
- controller.invoke(:something) { scope = self }
110
-
111
- assert_equal(self, scope)
112
- end
113
-
114
- def test_passes_arguments_into_the_context
115
- input = "asd"
116
- args = []
117
-
118
- container = Class.new do
119
- include(Hooked::Container)
120
- before(:something, :do_something_else) {|ctx| args << ctx.args}
121
- hookable(:something) {|ctx| args << ctx.args }
122
- end
123
-
124
- controller = Hooked::Controller.new(container.new)
125
- controller.invoke(:something, input)
126
-
127
- assert_equal [input.object_id, input.object_id], args.map {|a| a.object_id }
128
- end
129
-
130
- def test_returns_the_contexts_result
131
- input = ["asd", "def"]
132
-
133
- container = Class.new do
134
- include(Hooked::Container)
135
- hookable(:something) {|ctx| ctx.result = [input[0]] }
136
- after(:something, :do_something_else) {|ctx| ctx.result << input[1] }
137
- end
138
-
139
- controller = Hooked::Controller.new(container.new)
140
- result = controller.invoke(:something)
141
-
142
- assert_equal(input.map {|i| i.object_id }, result.map {|r| r.object_id })
143
- end
144
-
145
- def test_jumps_to_the_next_hook_or_hookable_when_it_catches_next
146
- container = Class.new do
147
- include(Hooked::Container)
148
- before(:something, :asd, :before => :foo) {|ctx| ctx.result = [] }
149
- before(:something, :foo) {|ctx| ctx.next; ctx.result << :foo }
150
- hookable(:something) {|ctx| ctx.result << :something }
151
- after(:something, :bar) {|ctx| ctx.result << :bar }
152
- end
153
-
154
- result = Hooked::Controller.new(container.new).invoke(:something)
155
- assert_equal [:something, :bar], result
156
- end
157
-
158
- def test_next_can_jump_out_of_the_current_invocation
159
- called = []
160
-
161
- container1 = Class.new do
162
- include(Hooked::Container)
163
- hookable(:something) do |ctx|
164
- hookable(:something_else)
165
- called << :something
166
- end
167
- after(:something, :bar) {|ctx| called << :bar }
168
- end
169
- container2 = Class.new do
170
- include(Hooked::Container)
171
- before(:something_else, :foo_else) {|ctx| called << :foo_else }
172
- hookable(:something_else) {|ctx| ctx.next(2); called << :something_else }
173
- end
174
-
175
- Hooked::Controller.new(container1.new, container2.new).invoke(:something)
176
- assert_equal [:foo_else, :bar], called
177
- end
178
-
179
- def test_crashes_if_next_depth_is_bigger_than_invocation_depth
180
- container = Class.new do
181
- include(Hooked::Container)
182
- hookable(:something) {|ctx| ctx.next(2) }
183
- end
184
-
185
- assert_throw(:hooked) do
186
- Hooked::Controller.new(container.new).invoke(:something)
187
- end
188
- end
189
-
190
- def test_returns_from_the_current_invocation_when_it_catches_break
191
- container = Class.new do
192
- include(Hooked::Container)
193
- before(:something, :asd, :before => :foo) {|ctx| ctx.result = [] }
194
- before(:something, :foo) {|ctx| ctx.result << :foo; ctx.break }
195
- hookable(:something) {|ctx| ctx.result << :something }
196
- after(:something, :bar) {|ctx| ctx.result << :bar }
197
- end
198
-
199
- result = Hooked::Controller.new(container.new).invoke(:something)
200
- assert_equal [:foo], result
201
- end
202
-
203
- def test_break_can_jump_out_of_the_current_invocation
204
- called = []
205
-
206
- container1 = Class.new do
207
- include(Hooked::Container)
208
- hookable(:something) do |ctx|
209
- hookable(:something_else)
210
- called << :something
211
- end
212
- after(:something, :bar) {|ctx| called << :bar }
213
- end
214
- container2 = Class.new do
215
- include(Hooked::Container)
216
- before(:something_else, :foo_else) {|ctx| called << :foo_else }
217
- hookable(:something_else) {|ctx| ctx.break(2); called << :something_else }
218
- end
219
-
220
- Hooked::Controller.new(container1.new, container2.new).invoke(:something)
221
- assert_equal [:foo_else], called
222
- end
223
-
224
- def test_crashes_if_break_depth_is_bigger_than_invocation_depth
225
- container = Class.new do
226
- include(Hooked::Container)
227
- hookable(:something) {|ctx| ctx.break(2) }
228
- end
229
-
230
- assert_throw(:hooked) do
231
- Hooked::Controller.new(container.new).invoke(:something)
232
- end
233
- end
234
- end
data/test/helper.rb DELETED
@@ -1,10 +0,0 @@
1
- require "rubygems"
2
- require "bundler"
3
- Bundler.setup(:default, :development)
4
-
5
- require "test/unit"
6
- require "mocha"
7
-
8
- require "awesome_print" rescue nil
9
-
10
- require "hooked"
data/test/hook_test.rb DELETED
@@ -1,90 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- class HookTest < Test::Unit::TestCase
4
- def context
5
- Hooked::Context
6
- end
7
-
8
- def test_has_a_name
9
- name = :foo
10
- hook = Hooked::Hook.new(:before, name) {}
11
-
12
- assert_equal name, hook.name
13
- end
14
-
15
- def test_has_a_type
16
- build_hook = proc do |type|
17
- Hooked::Hook.new(type, :foo) {}
18
- end
19
-
20
- hook = build_hook.call(:before)
21
- assert_equal :before, hook.type
22
-
23
- assert hook.before?
24
- assert build_hook.call(:after).after?
25
- assert build_hook.call(:around).around?
26
-
27
- assert_raise(ArgumentError) do
28
- build_hook.call(:asdas)
29
- end
30
- end
31
-
32
- def test_has_relations
33
- relations = {
34
- :before => 123
35
- }
36
- hook = Hooked::Hook.new(:before, :foo, relations) {}
37
- assert_equal(123, hook.relations[:before])
38
-
39
- hook.relations[:before] = 456
40
- assert_equal(456, hook.relations[:before])
41
-
42
- hook.relations[:after] = "asd"
43
- assert_equal("asd", hook.relations[:after])
44
- end
45
-
46
- def test_has_a_container
47
- container = stub("container")
48
- hook = Hooked::Hook.new(:before, :foo, {}, container) {}
49
- assert_equal container, hook.container
50
-
51
- container2 = stub("container2")
52
- hook.container = container2
53
- assert_equal container2, hook.container
54
- end
55
-
56
- def test_expects_a_block
57
- assert_nothing_raised do
58
- Hooked::Hook.new(:before, :foo) {}
59
- end
60
-
61
- assert_raise(ArgumentError) do
62
- Hooked::Hook.new(:before, :foo)
63
- end
64
- end
65
-
66
- def test_executes_block_in_the_containers_scope
67
- scope = nil
68
- hook = Hooked::Hook.new(:before, :foo) do
69
- scope = self
70
- end
71
-
72
- assert_raise(RuntimeError) { hook.call(context) }
73
-
74
- container = stub("container")
75
- hook.container = container
76
- hook.call(context)
77
- assert_equal(container, scope)
78
- end
79
-
80
- def test_passes_context_to_the_block
81
- ctx = context
82
- ctx2 = nil
83
- hook = Hooked::Hook.new(:before, :foo, {}, stub("container")) do |c|
84
- ctx2 = c
85
- end
86
-
87
- hook.call(ctx)
88
- assert_equal(ctx, ctx2)
89
- end
90
- end
@@ -1,66 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- class HookableTest < Test::Unit::TestCase
4
- def context
5
- Hooked::Context
6
- end
7
-
8
- def test_has_a_name
9
- name = :foo
10
- hookable = Hooked::Hookable.new(name) {}
11
-
12
- assert_equal name, hookable.name
13
- end
14
-
15
- def test_can_have_a_container
16
- container = Object.new
17
- hookable = Hooked::Hookable.new(:foo, container) {}
18
-
19
- assert_equal container, hookable.container
20
-
21
- assert_nil Hooked::Hookable.new(:foo) {}.container
22
- end
23
-
24
- def test_expects_a_block
25
- assert_nothing_raised do
26
- Hooked::Hookable.new(:foo) {}
27
- end
28
-
29
- assert_raise(ArgumentError) do
30
- Hooked::Hookable.new(:foo)
31
- end
32
- end
33
-
34
- def test_executes_block_in_the_containers_scope
35
- container = Object.new
36
-
37
- scope = nil
38
- hookable = Hooked::Hookable.new(:foo, container) do
39
- scope = self
40
- end
41
-
42
- hookable.call(context)
43
- assert_equal container, scope
44
- end
45
-
46
- def test_executes_block_in_its_original_scope_if_no_container_given
47
- scope = nil
48
- hookable = Hooked::Hookable.new(:foo) do
49
- scope = self
50
- end
51
-
52
- hookable.call(context)
53
- assert_equal self, scope
54
- end
55
-
56
- def test_passes_context_to_the_block
57
- ctx = context
58
- ctx2 = nil
59
- hookable = Hooked::Hookable.new(:foo) do |c|
60
- ctx2 = c
61
- end
62
-
63
- hookable.call(ctx)
64
- assert_equal(ctx, ctx2)
65
- end
66
- end