blockenspiel 0.2.0.1-java
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/History.rdoc +46 -0
- data/ImplementingDSLblocks.rdoc +934 -0
- data/README.rdoc +350 -0
- data/Rakefile +218 -0
- data/ext/blockenspiel/BlockenspielUnmixerService.java +140 -0
- data/ext/blockenspiel/extconf.rb +2 -0
- data/ext/blockenspiel/unmixer.c +86 -0
- data/lib/blockenspiel.rb +43 -0
- data/lib/blockenspiel/impl.rb +670 -0
- data/lib/blockenspiel/version.rb +42 -0
- data/lib/blockenspiel_unmixer.jar +0 -0
- data/tests/tc_basic.rb +137 -0
- data/tests/tc_behaviors.rb +191 -0
- data/tests/tc_dsl_methods.rb +324 -0
- data/tests/tc_dynamic.rb +175 -0
- data/tests/tc_mixins.rb +240 -0
- metadata +77 -0
@@ -0,0 +1,42 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Blockenspiel version
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2008-2009 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
module Blockenspiel
|
38
|
+
|
39
|
+
# Current gem version
|
40
|
+
VERSION_STRING = '0.2.0.1'.freeze
|
41
|
+
|
42
|
+
end
|
Binary file
|
data/tests/tc_basic.rb
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Blockenspiel basic tests
|
4
|
+
#
|
5
|
+
# This file contains tests for the simple use cases.
|
6
|
+
#
|
7
|
+
# -----------------------------------------------------------------------------
|
8
|
+
# Copyright 2008-2009 Daniel Azuma
|
9
|
+
#
|
10
|
+
# All rights reserved.
|
11
|
+
#
|
12
|
+
# Redistribution and use in source and binary forms, with or without
|
13
|
+
# modification, are permitted provided that the following conditions are met:
|
14
|
+
#
|
15
|
+
# * Redistributions of source code must retain the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer.
|
17
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
18
|
+
# this list of conditions and the following disclaimer in the documentation
|
19
|
+
# and/or other materials provided with the distribution.
|
20
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
21
|
+
# contributors to this software, may be used to endorse or promote products
|
22
|
+
# derived from this software without specific prior written permission.
|
23
|
+
#
|
24
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
25
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
26
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
27
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
28
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
29
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
30
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
31
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
32
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
33
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
34
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
35
|
+
# -----------------------------------------------------------------------------
|
36
|
+
;
|
37
|
+
|
38
|
+
|
39
|
+
require 'test/unit'
|
40
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../lib/blockenspiel.rb")
|
41
|
+
|
42
|
+
|
43
|
+
module Blockenspiel
|
44
|
+
module Tests # :nodoc:
|
45
|
+
|
46
|
+
class TestBasic < Test::Unit::TestCase # :nodoc:
|
47
|
+
|
48
|
+
|
49
|
+
class SimpleTarget < Blockenspiel::Base
|
50
|
+
|
51
|
+
def initialize
|
52
|
+
@hash = Hash.new
|
53
|
+
end
|
54
|
+
|
55
|
+
def set_value(key_, value_)
|
56
|
+
@hash[key_] = value_
|
57
|
+
end
|
58
|
+
|
59
|
+
def set_value_by_block(key_)
|
60
|
+
@hash[key_] = yield
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_value(key_)
|
64
|
+
@hash[key_]
|
65
|
+
end
|
66
|
+
dsl_method :get_value, false
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
# Test basic usage with a parameter object.
|
72
|
+
#
|
73
|
+
# * Asserts that methods are not mixed in to self.
|
74
|
+
# * Asserts that the specified target object does in fact receive the block messages.
|
75
|
+
|
76
|
+
def test_basic_param
|
77
|
+
block_ = proc do |t_|
|
78
|
+
t_.set_value(:a, 1)
|
79
|
+
t_.set_value_by_block(:b){ 2 }
|
80
|
+
assert(!self.respond_to?(:set_value))
|
81
|
+
assert(!self.respond_to?(:set_value_by_block))
|
82
|
+
end
|
83
|
+
target_ = SimpleTarget.new
|
84
|
+
Blockenspiel.invoke(block_, target_)
|
85
|
+
assert_equal(1, target_.get_value(:a))
|
86
|
+
assert_equal(2, target_.get_value(:b))
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
# Test basic usage with a mixin.
|
91
|
+
#
|
92
|
+
# * Asserts that methods are mixed in to self.
|
93
|
+
# * Asserts that methods are removed from self afterward.
|
94
|
+
# * Asserts that the specified target object still receives the messages.
|
95
|
+
|
96
|
+
def test_basic_mixin
|
97
|
+
block_ = proc do
|
98
|
+
set_value(:a, 1)
|
99
|
+
set_value_by_block(:b){ 2 }
|
100
|
+
end
|
101
|
+
target_ = SimpleTarget.new
|
102
|
+
Blockenspiel.invoke(block_, target_)
|
103
|
+
assert(!self.respond_to?(:set_value))
|
104
|
+
assert(!self.respond_to?(:set_value_by_block))
|
105
|
+
assert_equal(1, target_.get_value(:a))
|
106
|
+
assert_equal(2, target_.get_value(:b))
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
# Test basic usage with a builder.
|
111
|
+
#
|
112
|
+
# * Asserts that the receivers are called.
|
113
|
+
# * Asserts that receivers with blocks are handled properly.
|
114
|
+
|
115
|
+
def test_basic_builder
|
116
|
+
block_ = proc do
|
117
|
+
set_value(:a, 1)
|
118
|
+
set_value_by_block(:b){ 2 }
|
119
|
+
end
|
120
|
+
hash_ = Hash.new
|
121
|
+
Blockenspiel.invoke(block_) do
|
122
|
+
add_method(:set_value) do |key_, value_|
|
123
|
+
hash_[key_] = value_
|
124
|
+
end
|
125
|
+
add_method(:set_value_by_block, :block => true) do |bl_, key_|
|
126
|
+
hash_[key_] = bl_.call
|
127
|
+
end
|
128
|
+
end
|
129
|
+
assert_equal(1, hash_[:a])
|
130
|
+
assert_equal(2, hash_[:b])
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,191 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Blockenspiel behavior tests
|
4
|
+
#
|
5
|
+
# This file contains tests for behavior settings.
|
6
|
+
#
|
7
|
+
# -----------------------------------------------------------------------------
|
8
|
+
# Copyright 2008-2009 Daniel Azuma
|
9
|
+
#
|
10
|
+
# All rights reserved.
|
11
|
+
#
|
12
|
+
# Redistribution and use in source and binary forms, with or without
|
13
|
+
# modification, are permitted provided that the following conditions are met:
|
14
|
+
#
|
15
|
+
# * Redistributions of source code must retain the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer.
|
17
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
18
|
+
# this list of conditions and the following disclaimer in the documentation
|
19
|
+
# and/or other materials provided with the distribution.
|
20
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
21
|
+
# contributors to this software, may be used to endorse or promote products
|
22
|
+
# derived from this software without specific prior written permission.
|
23
|
+
#
|
24
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
25
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
26
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
27
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
28
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
29
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
30
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
31
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
32
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
33
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
34
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
35
|
+
# -----------------------------------------------------------------------------
|
36
|
+
;
|
37
|
+
|
38
|
+
|
39
|
+
require 'test/unit'
|
40
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../lib/blockenspiel.rb")
|
41
|
+
|
42
|
+
|
43
|
+
module Blockenspiel
|
44
|
+
module Tests # :nodoc:
|
45
|
+
|
46
|
+
class TextBehaviors < Test::Unit::TestCase # :nodoc:
|
47
|
+
|
48
|
+
|
49
|
+
class Target1 < Blockenspiel::Base
|
50
|
+
|
51
|
+
dsl_methods false
|
52
|
+
|
53
|
+
def initialize(hash_)
|
54
|
+
@hash = hash_
|
55
|
+
end
|
56
|
+
|
57
|
+
def set_value1(key_, value_)
|
58
|
+
@hash["#{key_}1"] = value_
|
59
|
+
end
|
60
|
+
dsl_method :set_value1
|
61
|
+
|
62
|
+
def set_value2(key_)
|
63
|
+
@hash["#{key_}2"] = yield
|
64
|
+
end
|
65
|
+
dsl_method :set_value2
|
66
|
+
|
67
|
+
def set_value3(key_, value_)
|
68
|
+
@hash["#{key_}3"] = value_
|
69
|
+
end
|
70
|
+
dsl_method :set_value3_dslversion, :set_value3
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
def helper_method
|
76
|
+
true
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
# Test instance_eval behavior.
|
81
|
+
#
|
82
|
+
# * Asserts that self points at the target.
|
83
|
+
# * Asserts that the target methods are available.
|
84
|
+
# * Asserts that the target methods are not renamed by dsl_method directives.
|
85
|
+
# * Asserts that the caller's instance variables are not available.
|
86
|
+
# * Asserts that the caller's helper methods are not available.
|
87
|
+
|
88
|
+
def test_instance_eval_behavior
|
89
|
+
hash_ = Hash.new
|
90
|
+
context_self_ = self
|
91
|
+
@my_instance_variable_test = :hello
|
92
|
+
block_ = proc do
|
93
|
+
set_value1('a', 1)
|
94
|
+
set_value2('b'){ 2 }
|
95
|
+
set_value3('c', 3)
|
96
|
+
context_self_.assert_raise(NoMethodError){ set_value3_dslversion('d', 4) }
|
97
|
+
context_self_.assert_raise(NoMethodError){ helper_method() }
|
98
|
+
context_self_.assert(!instance_variable_defined?(:@my_instance_variable_test))
|
99
|
+
context_self_.assert_instance_of(Blockenspiel::Tests::TextBehaviors::Target1, self)
|
100
|
+
end
|
101
|
+
Blockenspiel.invoke(block_, Target1.new(hash_), :parameterless => :instance)
|
102
|
+
assert_equal(1, hash_['a1'])
|
103
|
+
assert_equal(2, hash_['b2'])
|
104
|
+
assert_equal(3, hash_['c3'])
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
# Test proxy behavior.
|
109
|
+
#
|
110
|
+
# * Asserts that self doesn't point at the Target nor the original context.
|
111
|
+
# * Asserts that the target methods are available in their dsl renamed forms.
|
112
|
+
# * Asserts that the caller's instance variables are not available.
|
113
|
+
# * Asserts that the caller's helper methods *are* available.
|
114
|
+
|
115
|
+
def test_proxy_behavior
|
116
|
+
hash_ = Hash.new
|
117
|
+
context_self_ = self
|
118
|
+
@my_instance_variable_test = :hello
|
119
|
+
block_ = proc do
|
120
|
+
set_value1('a', 1)
|
121
|
+
set_value2('b'){ 2 }
|
122
|
+
set_value3_dslversion('c', 3)
|
123
|
+
context_self_.assert_raise(NoMethodError){ set_value3('d', 4) }
|
124
|
+
context_self_.assert(helper_method())
|
125
|
+
context_self_.assert(!instance_variable_defined?(:@my_instance_variable_test))
|
126
|
+
context_self_.assert(!self.kind_of?(Blockenspiel::Tests::TextBehaviors::Target1))
|
127
|
+
context_self_.assert_not_equal(context_self_, self)
|
128
|
+
end
|
129
|
+
Blockenspiel.invoke(block_, Target1.new(hash_), :parameterless => :proxy)
|
130
|
+
assert_equal(1, hash_['a1'])
|
131
|
+
assert_equal(2, hash_['b2'])
|
132
|
+
assert_equal(3, hash_['c3'])
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
# Test parameterless blocks disabled
|
137
|
+
#
|
138
|
+
# * Asserts that an error is raised if sending a no-parameter block in this case.
|
139
|
+
# * Asserts that sending a one-parameter block still works.
|
140
|
+
|
141
|
+
def test_disable_parameterless
|
142
|
+
hash_ = Hash.new
|
143
|
+
block1_ = proc do ||
|
144
|
+
set_value1('a', 1)
|
145
|
+
end
|
146
|
+
block2_ = proc do |target_|
|
147
|
+
target_.set_value1('b', 2)
|
148
|
+
end
|
149
|
+
block3_ = proc do
|
150
|
+
set_value1('c', 3)
|
151
|
+
end
|
152
|
+
assert_raise(Blockenspiel::BlockParameterError) do
|
153
|
+
Blockenspiel.invoke(block1_, Target1.new(hash_), :parameterless => false)
|
154
|
+
end
|
155
|
+
Blockenspiel.invoke(block2_, Target1.new(hash_), :parameterless => false)
|
156
|
+
assert_raise(Blockenspiel::BlockParameterError) do
|
157
|
+
Blockenspiel.invoke(block3_, Target1.new(hash_), :parameterless => false)
|
158
|
+
end
|
159
|
+
assert_equal(2, hash_['b1'])
|
160
|
+
end
|
161
|
+
|
162
|
+
|
163
|
+
# Test parametered blocks disabled
|
164
|
+
#
|
165
|
+
# * Asserts that an error is raised if sending a one-parameter block in this case.
|
166
|
+
# * Asserts that sending a no-parameter block still works.
|
167
|
+
|
168
|
+
def test_disable_parametered
|
169
|
+
hash_ = Hash.new
|
170
|
+
block1_ = proc do ||
|
171
|
+
set_value1('a', 1)
|
172
|
+
end
|
173
|
+
block2_ = proc do |target_|
|
174
|
+
target_.set_value1('b', 2)
|
175
|
+
end
|
176
|
+
block3_ = proc do
|
177
|
+
set_value1('c', 3)
|
178
|
+
end
|
179
|
+
Blockenspiel.invoke(block1_, Target1.new(hash_), :parameter => false)
|
180
|
+
assert_raise(Blockenspiel::BlockParameterError) do
|
181
|
+
Blockenspiel.invoke(block2_, Target1.new(hash_), :parameter => false)
|
182
|
+
end
|
183
|
+
Blockenspiel.invoke(block3_, Target1.new(hash_), :parameter => false)
|
184
|
+
assert_equal(1, hash_['a1'])
|
185
|
+
assert_equal(3, hash_['c1'])
|
186
|
+
end
|
187
|
+
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
end
|
@@ -0,0 +1,324 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Blockenspiel dsl method tests
|
4
|
+
#
|
5
|
+
# This file contains tests for the dsl method directives.
|
6
|
+
#
|
7
|
+
# -----------------------------------------------------------------------------
|
8
|
+
# Copyright 2008-2009 Daniel Azuma
|
9
|
+
#
|
10
|
+
# All rights reserved.
|
11
|
+
#
|
12
|
+
# Redistribution and use in source and binary forms, with or without
|
13
|
+
# modification, are permitted provided that the following conditions are met:
|
14
|
+
#
|
15
|
+
# * Redistributions of source code must retain the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer.
|
17
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
18
|
+
# this list of conditions and the following disclaimer in the documentation
|
19
|
+
# and/or other materials provided with the distribution.
|
20
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
21
|
+
# contributors to this software, may be used to endorse or promote products
|
22
|
+
# derived from this software without specific prior written permission.
|
23
|
+
#
|
24
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
25
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
26
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
27
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
28
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
29
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
30
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
31
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
32
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
33
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
34
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
35
|
+
# -----------------------------------------------------------------------------
|
36
|
+
;
|
37
|
+
|
38
|
+
|
39
|
+
require 'test/unit'
|
40
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../lib/blockenspiel.rb")
|
41
|
+
|
42
|
+
|
43
|
+
module Blockenspiel
|
44
|
+
module Tests # :nodoc:
|
45
|
+
|
46
|
+
class TestDSLMethods < Test::Unit::TestCase # :nodoc:
|
47
|
+
|
48
|
+
|
49
|
+
class Target1 < Blockenspiel::Base
|
50
|
+
|
51
|
+
def initialize(hash_)
|
52
|
+
@hash = hash_
|
53
|
+
end
|
54
|
+
|
55
|
+
def set_value1(key_, value_)
|
56
|
+
@hash["#{key_}1"] = value_
|
57
|
+
end
|
58
|
+
|
59
|
+
def set_value2(key_)
|
60
|
+
@hash["#{key_}2"] = yield
|
61
|
+
end
|
62
|
+
|
63
|
+
def _set_value3(key_, value_)
|
64
|
+
@hash["#{key_}3"] = value_
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
class Target2 < Blockenspiel::Base
|
71
|
+
|
72
|
+
def initialize(hash_)
|
73
|
+
@hash = hash_
|
74
|
+
end
|
75
|
+
|
76
|
+
dsl_methods false
|
77
|
+
|
78
|
+
def set_value1(key_, value_)
|
79
|
+
@hash["#{key_}1"] = value_
|
80
|
+
end
|
81
|
+
|
82
|
+
dsl_methods true
|
83
|
+
|
84
|
+
def set_value2(key_)
|
85
|
+
@hash["#{key_}2"] = yield
|
86
|
+
end
|
87
|
+
|
88
|
+
def _set_value3(key_, value_)
|
89
|
+
@hash["#{key_}3"] = value_
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
class Target3 < Blockenspiel::Base
|
96
|
+
|
97
|
+
def initialize(hash_)
|
98
|
+
@hash = hash_
|
99
|
+
end
|
100
|
+
|
101
|
+
dsl_methods false
|
102
|
+
|
103
|
+
def set_value1(key_, value_)
|
104
|
+
@hash["#{key_}1"] = value_
|
105
|
+
end
|
106
|
+
dsl_method :set_value1
|
107
|
+
|
108
|
+
def set_value2(key_)
|
109
|
+
@hash["#{key_}2"] = yield
|
110
|
+
end
|
111
|
+
dsl_method :renamed_set_value2, :set_value2
|
112
|
+
dsl_method :another_set_value2, :set_value2
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
class Target4 < Blockenspiel::Base
|
118
|
+
|
119
|
+
def initialize(hash_)
|
120
|
+
@hash = hash_
|
121
|
+
end
|
122
|
+
|
123
|
+
def set_value1(key_, value_)
|
124
|
+
@hash["#{key_}1"] = value_
|
125
|
+
end
|
126
|
+
dsl_method :set_value1, false
|
127
|
+
|
128
|
+
def set_value2(key_)
|
129
|
+
@hash["#{key_}2"] = yield
|
130
|
+
end
|
131
|
+
dsl_method :set_value2, false
|
132
|
+
dsl_method :renamed_set_value2, :set_value2
|
133
|
+
|
134
|
+
end
|
135
|
+
|
136
|
+
|
137
|
+
class Target5a < Blockenspiel::Base
|
138
|
+
|
139
|
+
def initialize(hash_)
|
140
|
+
@hash = hash_
|
141
|
+
end
|
142
|
+
|
143
|
+
def set_value1(key_, value_)
|
144
|
+
@hash["#{key_}1"] = value_
|
145
|
+
end
|
146
|
+
|
147
|
+
def set_value2(key_)
|
148
|
+
@hash["#{key_}2"] = yield
|
149
|
+
end
|
150
|
+
|
151
|
+
def set_value3(key_, value_)
|
152
|
+
@hash["#{key_}3"] = value_
|
153
|
+
end
|
154
|
+
|
155
|
+
def set_value4(key_, value_)
|
156
|
+
@hash["#{key_}4"] = value_
|
157
|
+
end
|
158
|
+
dsl_method :set_value4, false
|
159
|
+
dsl_method :renamed_set_value4, :set_value4
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
class Target5b < Target5a
|
165
|
+
|
166
|
+
def set_value1(key_, value_)
|
167
|
+
@hash["#{key_}1sub"] = value_
|
168
|
+
end
|
169
|
+
|
170
|
+
dsl_method :set_value3, false
|
171
|
+
|
172
|
+
def set_value5(key_, value_)
|
173
|
+
@hash["#{key_}5"] = value_
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
177
|
+
|
178
|
+
|
179
|
+
class Target6 < Blockenspiel::Base
|
180
|
+
|
181
|
+
def initialize(hash_)
|
182
|
+
@hash = hash_
|
183
|
+
end
|
184
|
+
|
185
|
+
dsl_methods false
|
186
|
+
|
187
|
+
def set_value1(key_, value_)
|
188
|
+
@hash["#{key_}1"] = value_
|
189
|
+
end
|
190
|
+
|
191
|
+
def set_value2(key_)
|
192
|
+
@hash["#{key_}2"] = yield
|
193
|
+
end
|
194
|
+
|
195
|
+
dsl_methods :set_value1, :renamed_set_value2 => :set_value2
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
# Test default dsl method setting.
|
201
|
+
#
|
202
|
+
# * Asserts the right dsl methods are added for the default setting.
|
203
|
+
|
204
|
+
def test_default_setting
|
205
|
+
hash_ = Hash.new
|
206
|
+
block_ = proc do
|
207
|
+
set_value1('a', 1)
|
208
|
+
set_value2('b'){ 2 }
|
209
|
+
assert_raise(NoMethodError){ _set_value3('c', 3) }
|
210
|
+
end
|
211
|
+
Blockenspiel.invoke(block_, Target1.new(hash_))
|
212
|
+
assert_equal(1, hash_['a1'])
|
213
|
+
assert_equal(2, hash_['b2'])
|
214
|
+
assert_nil(hash_['c3'])
|
215
|
+
end
|
216
|
+
|
217
|
+
|
218
|
+
# Test dsl_methods true and false switching.
|
219
|
+
#
|
220
|
+
# * Asserts that dsl_methods false turns off automatic method creation.
|
221
|
+
# * Asserts that dsl_methods true turns on automatic method creation.
|
222
|
+
# * Asserts that underscore methods are added in dsl_methods true mode.
|
223
|
+
|
224
|
+
def test_onoff_switching
|
225
|
+
hash_ = Hash.new
|
226
|
+
block_ = proc do
|
227
|
+
assert_raise(NoMethodError){ _set_value1('a', 1) }
|
228
|
+
set_value2('b'){ 2 }
|
229
|
+
_set_value3('c', 3)
|
230
|
+
end
|
231
|
+
Blockenspiel.invoke(block_, Target2.new(hash_))
|
232
|
+
assert_nil(hash_['a1'])
|
233
|
+
assert_equal(2, hash_['b2'])
|
234
|
+
assert_equal(3, hash_['c3'])
|
235
|
+
end
|
236
|
+
|
237
|
+
|
238
|
+
# Test dsl_methods explicit adding.
|
239
|
+
#
|
240
|
+
# * Asserts that adding an explicit dsl method works.
|
241
|
+
# * Asserts that adding an explicit dsl method with a different name works.
|
242
|
+
|
243
|
+
def test_explicit_add
|
244
|
+
hash_ = Hash.new
|
245
|
+
block_ = proc do
|
246
|
+
set_value1('a', 1)
|
247
|
+
assert_raise(NoMethodError){ set_value2('b'){ 2 } }
|
248
|
+
renamed_set_value2('c'){ 3 }
|
249
|
+
another_set_value2('d'){ 4 }
|
250
|
+
end
|
251
|
+
Blockenspiel.invoke(block_, Target3.new(hash_))
|
252
|
+
assert_equal(1, hash_['a1'])
|
253
|
+
assert_nil(hash_['b2'])
|
254
|
+
assert_equal(3, hash_['c2'])
|
255
|
+
assert_equal(4, hash_['d2'])
|
256
|
+
end
|
257
|
+
|
258
|
+
|
259
|
+
# Test dsl_methods explicit removing.
|
260
|
+
#
|
261
|
+
# * Asserts that removing a dsl method works.
|
262
|
+
# * Asserts that re-adding a removed method with a different name works.
|
263
|
+
|
264
|
+
def test_explicit_removing
|
265
|
+
hash_ = Hash.new
|
266
|
+
block_ = proc do
|
267
|
+
assert_raise(NoMethodError){ set_value1('a', 1) }
|
268
|
+
assert_raise(NoMethodError){ set_value2('b'){ 2 } }
|
269
|
+
renamed_set_value2('c'){ 3 }
|
270
|
+
end
|
271
|
+
Blockenspiel.invoke(block_, Target4.new(hash_))
|
272
|
+
assert_nil(hash_['a1'])
|
273
|
+
assert_nil(hash_['b2'])
|
274
|
+
assert_equal(3, hash_['c2'])
|
275
|
+
end
|
276
|
+
|
277
|
+
|
278
|
+
# Test dsl method setting with subclasses
|
279
|
+
#
|
280
|
+
# * Asserts that modules are properly inherited.
|
281
|
+
# * Asserts that method overriding is done correctly.
|
282
|
+
|
283
|
+
def test_subclassing
|
284
|
+
hash_ = Hash.new
|
285
|
+
block_ = proc do
|
286
|
+
set_value1('a', 1)
|
287
|
+
set_value2('b'){ 2 }
|
288
|
+
assert_raise(NoMethodError){ set_value3('c', 3) }
|
289
|
+
assert_raise(NoMethodError){ set_value4('d', 4) }
|
290
|
+
renamed_set_value4('e', 5)
|
291
|
+
set_value5('f', 6)
|
292
|
+
end
|
293
|
+
Blockenspiel.invoke(block_, Target5b.new(hash_))
|
294
|
+
assert_equal(1, hash_['a1sub'])
|
295
|
+
assert_equal(2, hash_['b2'])
|
296
|
+
assert_nil(hash_['c3'])
|
297
|
+
assert_nil(hash_['d4'])
|
298
|
+
assert_equal(5, hash_['e4'])
|
299
|
+
assert_equal(6, hash_['f5'])
|
300
|
+
end
|
301
|
+
|
302
|
+
|
303
|
+
# Test dsl_methods with multiple parameters.
|
304
|
+
#
|
305
|
+
# * Asserts that hash syntax for dsl_methods works.
|
306
|
+
# * Asserts that combined array and hash parameters works.
|
307
|
+
|
308
|
+
def test_multiple_dsl_methods
|
309
|
+
hash_ = Hash.new
|
310
|
+
block_ = proc do
|
311
|
+
set_value1('a', 1)
|
312
|
+
renamed_set_value2('b'){ 2 }
|
313
|
+
assert_raise(NoMethodError){ set_value2('c', 3) }
|
314
|
+
end
|
315
|
+
Blockenspiel.invoke(block_, Target6.new(hash_))
|
316
|
+
assert_equal(1, hash_['a1'])
|
317
|
+
assert_equal(2, hash_['b2'])
|
318
|
+
end
|
319
|
+
|
320
|
+
|
321
|
+
end
|
322
|
+
|
323
|
+
end
|
324
|
+
end
|