blockenspiel 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,17 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # Blockenspiel dynamic tests
4
- #
4
+ #
5
5
  # This file contains tests for dynamic DSL generation.
6
- #
6
+ #
7
7
  # -----------------------------------------------------------------------------
8
8
  # Copyright 2008-2011 Daniel Azuma
9
- #
9
+ #
10
10
  # All rights reserved.
11
- #
11
+ #
12
12
  # Redistribution and use in source and binary forms, with or without
13
13
  # modification, are permitted provided that the following conditions are met:
14
- #
14
+ #
15
15
  # * Redistributions of source code must retain the above copyright notice,
16
16
  # this list of conditions and the following disclaimer.
17
17
  # * Redistributions in binary form must reproduce the above copyright notice,
@@ -20,7 +20,7 @@
20
20
  # * Neither the name of the copyright holder, nor the names of any other
21
21
  # contributors to this software, may be used to endorse or promote products
22
22
  # derived from this software without specific prior written permission.
23
- #
23
+ #
24
24
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
25
  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
26
  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -42,14 +42,14 @@ require 'blockenspiel'
42
42
 
43
43
  module Blockenspiel
44
44
  module Tests # :nodoc:
45
-
45
+
46
46
  class TestDynamic < ::Test::Unit::TestCase # :nodoc:
47
-
48
-
47
+
48
+
49
49
  # Test the simple case.
50
- #
50
+ #
51
51
  # * Asserts that the simplest case works.
52
-
52
+
53
53
  def test_simple
54
54
  block_ = ::Proc.new do
55
55
  set_value(:a, 1)
@@ -62,13 +62,13 @@ module Blockenspiel
62
62
  end
63
63
  assert_equal(1, hash_[:a])
64
64
  end
65
-
66
-
65
+
66
+
67
67
  # Test renaming.
68
- #
68
+ #
69
69
  # * Asserts that the method appears renamed in a parameterless block.
70
70
  # * Asserts that the method appears in its original name in a parametered block.
71
-
71
+
72
72
  def test_renaming
73
73
  hash_ = ::Hash.new
74
74
  dsl_definition_ = ::Proc.new do
@@ -91,14 +91,14 @@ module Blockenspiel
91
91
  assert_equal(3, hash_[:c])
92
92
  assert_nil(hash_[:d])
93
93
  end
94
-
95
-
94
+
95
+
96
96
  # Test calls with blocks passed the usual way.
97
97
  # Note: this will fail in MRI < 1.8.7 and JRuby < 1.5.
98
- #
98
+ #
99
99
  # * Asserts that a block passed the usual way works
100
100
  # * Asserts that we can detect when a block has not been passed
101
-
101
+
102
102
  def test_blocks_normal
103
103
  hash_ = ::Hash.new
104
104
  block_ = ::Proc.new do
@@ -118,14 +118,14 @@ module Blockenspiel
118
118
  assert_equal(true, hash_[:b])
119
119
  assert_equal(false, hash_[:c])
120
120
  end
121
-
122
-
121
+
122
+
123
123
  # Test calls with blocks passed as non-block parameters.
124
- #
124
+ #
125
125
  # * Asserts that a block passed "first" works.
126
126
  # * Asserts that a block passed "last" works.
127
127
  # * Asserts that a block passed "true" works.
128
-
128
+
129
129
  def test_blocks_first_and_last
130
130
  hash_ = ::Hash.new
131
131
  block_ = ::Proc.new do
@@ -148,12 +148,12 @@ module Blockenspiel
148
148
  assert_equal(2, hash_[:b])
149
149
  assert_equal(3, hash_[:c])
150
150
  end
151
-
152
-
151
+
152
+
153
153
  # Test calls with blocks not passed.
154
- #
154
+ #
155
155
  # * Asserts that if a block isn't given, it is set to nil.
156
-
156
+
157
157
  def test_blocks_nil
158
158
  hash_ = ::Hash.new
159
159
  block_ = ::Proc.new do
@@ -171,12 +171,12 @@ module Blockenspiel
171
171
  assert_nil(hash_[:a])
172
172
  assert_nil(hash_[:b])
173
173
  end
174
-
175
-
174
+
175
+
176
176
  # Test calls with blocks (legacy api)
177
- #
177
+ #
178
178
  # * Asserts that a block with receive_block works.
179
-
179
+
180
180
  def test_blocks_legacy
181
181
  hash_ = ::Hash.new
182
182
  block_ = ::Proc.new do
@@ -189,12 +189,12 @@ module Blockenspiel
189
189
  end
190
190
  assert_equal(1, hash_[:a])
191
191
  end
192
-
193
-
192
+
193
+
194
194
  # Test passing options in.
195
- #
195
+ #
196
196
  # * Asserts that the "parameter" option is recognized
197
-
197
+
198
198
  def test_options_recognized
199
199
  block_ = ::Proc.new do
200
200
  set_value(:a, 1)
@@ -208,9 +208,9 @@ module Blockenspiel
208
208
  end
209
209
  end
210
210
  end
211
-
212
-
211
+
212
+
213
213
  end
214
-
214
+
215
215
  end
216
216
  end
@@ -1,17 +1,17 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # Blockenspiel behavior tests
4
- #
4
+ #
5
5
  # This file contains tests for behavior settings.
6
- #
6
+ #
7
7
  # -----------------------------------------------------------------------------
8
8
  # Copyright 2008-2011 Daniel Azuma
9
- #
9
+ #
10
10
  # All rights reserved.
11
- #
11
+ #
12
12
  # Redistribution and use in source and binary forms, with or without
13
13
  # modification, are permitted provided that the following conditions are met:
14
- #
14
+ #
15
15
  # * Redistributions of source code must retain the above copyright notice,
16
16
  # this list of conditions and the following disclaimer.
17
17
  # * Redistributions in binary form must reproduce the above copyright notice,
@@ -20,7 +20,7 @@
20
20
  # * Neither the name of the copyright holder, nor the names of any other
21
21
  # contributors to this software, may be used to endorse or promote products
22
22
  # derived from this software without specific prior written permission.
23
- #
23
+ #
24
24
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
25
  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
26
  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -42,52 +42,53 @@ require 'blockenspiel'
42
42
 
43
43
  module Blockenspiel
44
44
  module Tests # :nodoc:
45
-
45
+
46
46
  class TestEmbeddedBlock < ::Test::Unit::TestCase # :nodoc:
47
-
48
-
47
+
48
+
49
49
  class Target1 < ::Blockenspiel::Base
50
-
50
+
51
51
  def initialize(value_)
52
52
  @value = value_
53
53
  @block = nil
54
54
  end
55
-
55
+
56
56
  def set_block(&block_)
57
57
  @block = block_
58
58
  end
59
-
59
+
60
60
  def value
61
61
  @value
62
62
  end
63
-
63
+
64
64
  dsl_methods false
65
-
65
+
66
66
  def call_block
67
67
  @block.call
68
68
  end
69
-
69
+
70
70
  end
71
-
72
-
71
+
72
+
73
73
  BLOCK = ::Proc.new do
74
74
  set_block do
75
75
  self.value
76
76
  end
77
77
  end
78
-
79
-
78
+
79
+
80
80
  # Test an embedded block with a proxy.
81
-
81
+
82
82
  def test_proxy_embedded_block
83
- return # TEMP
84
- target_ = Target1.new(23)
85
- ::Blockenspiel.invoke(BLOCK, target_, :parameterless => :proxy)
86
- assert_equal(23, target_.call_block)
83
+ if false # TEMP
84
+ target_ = Target1.new(23)
85
+ ::Blockenspiel.invoke(BLOCK, target_, :parameterless => :proxy)
86
+ assert_equal(23, target_.call_block)
87
+ end
87
88
  end
88
-
89
-
89
+
90
+
90
91
  end
91
-
92
+
92
93
  end
93
94
  end
@@ -1,18 +1,18 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # Blockenspiel mixin tests
4
- #
4
+ #
5
5
  # This file contains tests for various mixin cases,
6
6
  # including nested blocks and multithreading.
7
- #
7
+ #
8
8
  # -----------------------------------------------------------------------------
9
9
  # Copyright 2008-2011 Daniel Azuma
10
- #
10
+ #
11
11
  # All rights reserved.
12
- #
12
+ #
13
13
  # Redistribution and use in source and binary forms, with or without
14
14
  # modification, are permitted provided that the following conditions are met:
15
- #
15
+ #
16
16
  # * Redistributions of source code must retain the above copyright notice,
17
17
  # this list of conditions and the following disclaimer.
18
18
  # * Redistributions in binary form must reproduce the above copyright notice,
@@ -21,7 +21,7 @@
21
21
  # * Neither the name of the copyright holder, nor the names of any other
22
22
  # contributors to this software, may be used to endorse or promote products
23
23
  # derived from this software without specific prior written permission.
24
- #
24
+ #
25
25
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26
26
  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
27
  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -43,86 +43,86 @@ require 'blockenspiel'
43
43
 
44
44
  module Blockenspiel
45
45
  module Tests # :nodoc:
46
-
46
+
47
47
  class TestMixins < ::Test::Unit::TestCase # :nodoc:
48
-
49
-
48
+
49
+
50
50
  class Target1 < ::Blockenspiel::Base
51
-
51
+
52
52
  def initialize(hash_)
53
53
  @hash = hash_
54
54
  end
55
-
55
+
56
56
  def set_value(key_, value_)
57
57
  @hash["#{key_}1"] = value_
58
58
  end
59
-
59
+
60
60
  def set_value2(key_)
61
61
  @hash["#{key_}1"] = yield
62
62
  end
63
-
63
+
64
64
  end
65
-
66
-
65
+
66
+
67
67
  class Target2 < ::Blockenspiel::Base
68
-
68
+
69
69
  dsl_methods false
70
-
70
+
71
71
  def initialize(hash_=nil)
72
72
  @hash = hash_ || ::Hash.new
73
73
  end
74
-
74
+
75
75
  def set_value(key_, value_)
76
76
  @hash["#{key_}2"] = value_
77
77
  end
78
78
  dsl_method :set_value
79
-
79
+
80
80
  def set_value2(key_)
81
81
  @hash["#{key_}2"] = yield
82
82
  end
83
83
  dsl_method :set_value2_inmixin, :set_value2
84
-
84
+
85
85
  end
86
-
87
-
86
+
87
+
88
88
  class Target3 < ::Blockenspiel::Base
89
-
89
+
90
90
  def initialize(hash_)
91
91
  @hash = hash_
92
92
  end
93
-
93
+
94
94
  def set_value(key_, value_)
95
95
  @hash[key_] = value_
96
96
  end
97
-
97
+
98
98
  def _helper_method(key_)
99
99
  @hash[key_]
100
100
  end
101
-
101
+
102
102
  def get_value(key_)
103
103
  @hash[key_]
104
104
  end
105
105
  dsl_method :get_value, false
106
-
106
+
107
107
  def get_value2(key_)
108
108
  @hash[key_]
109
109
  end
110
110
  dsl_method :get_value2, false
111
-
111
+
112
112
  end
113
-
114
-
113
+
114
+
115
115
  def get_value(key_)
116
116
  :helper
117
117
  end
118
-
119
-
118
+
119
+
120
120
  # Basic test of mixin mechanism.
121
- #
121
+ #
122
122
  # * Asserts that the mixin methods are added and removed for a single mixin.
123
123
  # * Asserts that the methods properly delegate to the target object.
124
124
  # * Asserts that self doesn't change, and instance variables are preserved.
125
-
125
+
126
126
  def test_basic_mixin
127
127
  hash_ = ::Hash.new
128
128
  saved_object_id_ = self.object_id
@@ -141,13 +141,13 @@ module Blockenspiel
141
141
  assert_equal(1, hash_['a1'])
142
142
  assert_equal(2, hash_['b1'])
143
143
  end
144
-
145
-
144
+
145
+
146
146
  # Test renaming of mixin methods.
147
- #
147
+ #
148
148
  # * Asserts that correctly renamed mixin methods are added and removed.
149
149
  # * Asserts that the methods properly delegate to the target object.
150
-
150
+
151
151
  def test_mixin_with_renaming
152
152
  hash_ = ::Hash.new
153
153
  assert(!self.respond_to?(:set_value))
@@ -165,14 +165,14 @@ module Blockenspiel
165
165
  assert_equal(1, hash_['a2'])
166
166
  assert_equal(2, hash_['b2'])
167
167
  end
168
-
169
-
168
+
169
+
170
170
  # Test of two different nested mixins.
171
- #
171
+ #
172
172
  # * Asserts that the right methods are added and removed at the right time.
173
173
  # * Asserts that the methods delegate to the right target object, even when
174
174
  # multiple mixins add the same method name
175
-
175
+
176
176
  def test_nested_different
177
177
  hash_ = ::Hash.new
178
178
  assert(!self.respond_to?(:set_value))
@@ -200,12 +200,12 @@ module Blockenspiel
200
200
  assert_equal(1, hash_['e1'])
201
201
  assert_equal(2, hash_['f1'])
202
202
  end
203
-
204
-
203
+
204
+
205
205
  # Test of the same mixin nested in itself.
206
- #
206
+ #
207
207
  # * Asserts that the methods are added and removed at the right time.
208
-
208
+
209
209
  def test_nested_same
210
210
  hash_ = ::Hash.new
211
211
  assert(!self.respond_to?(:set_value))
@@ -232,12 +232,12 @@ module Blockenspiel
232
232
  assert_equal(1, hash_['e2'])
233
233
  assert_equal(2, hash_['f2'])
234
234
  end
235
-
236
-
235
+
236
+
237
237
  # Test of two threads mixing the same mixin into the same object
238
- #
238
+ #
239
239
  # * Asserts that the mixin is removed only after the second thread is done.
240
-
240
+
241
241
  def test_threads_same_mixin
242
242
  hash_ = ::Hash.new
243
243
  block1_ = ::Proc.new do
@@ -264,16 +264,51 @@ module Blockenspiel
264
264
  assert_equal(3, hash_['c1'])
265
265
  assert_equal(4, hash_['d1'])
266
266
  end
267
-
268
-
267
+
268
+
269
+ def test_two_threads_different_mixin
270
+ hash_ = {}
271
+ target1_ = Target1.new(hash_)
272
+ target2_ = Target2.new(hash_)
273
+ assert(!self.respond_to?(:set_value))
274
+ assert(!self.respond_to?(:set_value2))
275
+ assert(!self.respond_to?(:set_value2_inmixin))
276
+ t1_ = ::Thread.new do
277
+ ::Blockenspiel.invoke(::Proc.new do
278
+ sleep(0.1)
279
+ set_value('a', 1)
280
+ sleep(0.1)
281
+ set_value('e', 5)
282
+ end, target1_)
283
+ end
284
+ t2_ = ::Thread.new do
285
+ ::Blockenspiel.invoke(::Proc.new do
286
+ sleep(0.1)
287
+ set_value('A', 11)
288
+ sleep(0.1)
289
+ set_value('E', 15)
290
+ end, target2_)
291
+ end
292
+ t1_.join
293
+ t2_.join
294
+ assert(!self.respond_to?(:set_value))
295
+ assert(!self.respond_to?(:set_value2))
296
+ assert(!self.respond_to?(:set_value2_inmixin))
297
+ assert_equal(1, hash_['a1'])
298
+ assert_equal(5, hash_['e1'])
299
+ assert_equal(11, hash_['A2'])
300
+ assert_equal(15, hash_['E2'])
301
+ end
302
+
303
+
269
304
  # A full thread test with the same set of nested mixins done into the same
270
305
  # object twice in two different threads.
271
- #
306
+ #
272
307
  # * Asserts that the right methods are added and removed at the right time.
273
308
  # * Asserts that the methods delegate to the right target object, even when
274
309
  # multiple mixins add the same method name, multiple times from different
275
310
  # threads.
276
-
311
+
277
312
  def test_nested_two_threads
278
313
  hash_ = {}
279
314
  target1_ = Target1.new(hash_)
@@ -329,18 +364,18 @@ module Blockenspiel
329
364
  assert_equal(15, hash_['E2'])
330
365
  assert_equal(16, hash_['F2'])
331
366
  end
332
-
333
-
367
+
368
+
334
369
  # A full fiber test with the same set of nested mixins done into the same
335
370
  # object twice in two different fibers.
336
- #
371
+ #
337
372
  # * Asserts that the right methods are added and removed at the right time.
338
373
  # * Asserts that the methods delegate to the right target object, even when
339
374
  # multiple mixins add the same method name, multiple times from different
340
375
  # fibers.
341
-
376
+
342
377
  if defined?(::Fiber)
343
-
378
+
344
379
  def test_nested_two_fibers
345
380
  hash_ = {}
346
381
  target1_ = Target1.new(hash_)
@@ -402,15 +437,15 @@ module Blockenspiel
402
437
  assert_equal(15, hash_['E2'])
403
438
  assert_equal(16, hash_['F2'])
404
439
  end
405
-
440
+
406
441
  end
407
-
408
-
442
+
443
+
409
444
  # Test mixin omissions.
410
- #
445
+ #
411
446
  # * Asserts that underscore methods are not mixed in.
412
447
  # * Asserts that methods that are turned off after the fact cannot be called.
413
-
448
+
414
449
  def test_omissions
415
450
  hash_ = ::Hash.new
416
451
  block_ = ::Proc.new do
@@ -426,9 +461,9 @@ module Blockenspiel
426
461
  assert(!self.respond_to?(:set_value))
427
462
  assert_equal(1, target_.get_value(:a))
428
463
  end
429
-
430
-
464
+
465
+
431
466
  end
432
-
467
+
433
468
  end
434
469
  end