idregistry 0.1.0

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.
@@ -0,0 +1,64 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # IDRegistry utilities
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2012 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 IDRegistry
38
+
39
+
40
+ # A set of general utility methods for the implementation.
41
+ # Clients may also have occasion to use these methods.
42
+
43
+ class Utils
44
+
45
+ class << self
46
+
47
+
48
+ # Returns true if the given pattern is matched by the given tuple.
49
+
50
+ def matches?(pattern_, tuple_)
51
+ return false unless pattern_.size == tuple_.size
52
+ tuple_.each_with_index do |tuple_elem_, index_|
53
+ return false unless pattern_[index_] === tuple_elem_
54
+ end
55
+ return true
56
+ end
57
+
58
+
59
+ end
60
+
61
+ end
62
+
63
+
64
+ end
@@ -0,0 +1,53 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # IDRegistry version
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2012 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
+ begin
38
+ require 'versionomy'
39
+ rescue ::LoadError
40
+ end
41
+
42
+
43
+ module IDRegistry
44
+
45
+ # Current version of IDRegistry as a frozen string
46
+ VERSION_STRING = ::File.read(::File.dirname(__FILE__)+'/../../Version').strip.freeze
47
+
48
+ # Current version of IDRegistry as a Versionomy object, if the
49
+ # Versionomy gem is available. Otherwise, the same string value as
50
+ # VERSION_STRING.
51
+ VERSION = defined?(::Versionomy) ? ::Versionomy.parse(VERSION_STRING) : VERSION_STRING
52
+
53
+ end
@@ -0,0 +1,153 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Category tests
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2012 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
+ require 'test/unit'
38
+ require 'idregistry'
39
+ require 'set'
40
+
41
+
42
+ module IDRegistry
43
+ module Tests # :nodoc:
44
+
45
+ class TestCategories < ::Test::Unit::TestCase # :nodoc:
46
+
47
+
48
+ Class1 = ::Struct.new(:value)
49
+ Class2 = ::Struct.new(:value1, :value2)
50
+
51
+
52
+ def setup
53
+ @registry = IDRegistry.create
54
+ @registry.config do
55
+ add_pattern do
56
+ pattern [:hello, ::Integer]
57
+ type :hello_numbers
58
+ to_generate_object{ |tuple_| Class1.new(tuple_[1]) }
59
+ to_generate_tuple{ |obj_| [:hello, obj_.value] }
60
+ end
61
+ add_pattern do
62
+ pattern [:hello, ::Float]
63
+ type :hello_numbers
64
+ to_generate_object{ |tuple_| Class1.new(tuple_[1].to_i) }
65
+ to_generate_tuple{ |obj_| [:hello, obj_.value.to_f] }
66
+ end
67
+ add_pattern do
68
+ pattern [:world, ::Float, ::String]
69
+ type :world_numbers
70
+ to_generate_object{ |tuple_| Class2.new(tuple_[1].to_f, tuple_[2].to_s) }
71
+ to_generate_tuple{ |obj_| [:world, obj_.value1.to_f, obj_.value2.to_s] }
72
+ end
73
+ add_category(:world_float, [:world, ::Float, ::String], [1])
74
+ add_category(:world_string, [:world, ::Float, ::String], [2])
75
+ add_category(:world_both, [:world, ::Float, ::String], [1, 2])
76
+ end
77
+ end
78
+
79
+
80
+ def test_categories_for_simple_object
81
+ obj1_ = @registry.lookup(:world, 1.0, 'hello')
82
+ assert_equal({:world_float => [1.0], :world_string => ['hello'], :world_both => [1.0, 'hello']},
83
+ @registry.categories(obj1_))
84
+ end
85
+
86
+
87
+ def test_objects_in_category_1_index
88
+ obj1_ = @registry.lookup(:world, 1.0, 'hello')
89
+ obj2_ = @registry.lookup(:world, 2.0, 'hello')
90
+ @registry.lookup(:world, 2.0, 'bye')
91
+ @registry.lookup(:hello, 4)
92
+ assert_equal(::Set.new([obj1_, obj2_]),
93
+ ::Set.new(@registry.objects_in_category(:world_string, ['hello'])))
94
+ end
95
+
96
+
97
+ def test_tuples_in_category_1_index
98
+ @registry.lookup(:world, 1.0, 'hello')
99
+ @registry.lookup(:world, 2.0, 'hello')
100
+ @registry.lookup(:world, 2.0, 'bye')
101
+ @registry.lookup(:hello, 4)
102
+ assert_equal(::Set.new([[:world, 1.0, 'hello'], [:world, 2.0, 'hello']]),
103
+ ::Set.new(@registry.tuples_in_category(:world_string, ['hello'])))
104
+ end
105
+
106
+
107
+ def test_objects_in_category_2_index
108
+ @registry.lookup(:world, 1.0, 'hello')
109
+ obj2_ = @registry.lookup(:world, 2.0, 'hello')
110
+ @registry.lookup(:world, 2.0, 'bye')
111
+ @registry.lookup(:hello, 4)
112
+ assert_equal([obj2_],
113
+ @registry.objects_in_category(:world_both, [2.0, 'hello']))
114
+ end
115
+
116
+
117
+ def test_tuples_in_category_2_index
118
+ @registry.lookup(:world, 1.0, 'hello')
119
+ @registry.lookup(:world, 2.0, 'hello')
120
+ @registry.lookup(:world, 2.0, 'bye')
121
+ @registry.lookup(:hello, 4)
122
+ assert_equal([[:world, 2.0, 'hello']],
123
+ @registry.tuples_in_category(:world_both, [2.0, 'hello']))
124
+ end
125
+
126
+
127
+ def test_category_updated_through_rekey
128
+ obj_ = @registry.lookup(:world, 1.0, 'hello')
129
+ obj_.value1 = 2.0
130
+ @registry.rekey(obj_)
131
+ assert_equal({:world_float => [2.0], :world_string => ['hello'], :world_both => [2.0, 'hello']},
132
+ @registry.categories(obj_))
133
+ end
134
+
135
+
136
+ def test_delete_category
137
+ obj1_ = @registry.lookup(:world, 1.0, 'hello')
138
+ obj2_ = @registry.lookup(:world, 2.0, 'hello')
139
+ obj3_ = @registry.lookup(:world, 2.0, 'bye')
140
+ obj4_ = @registry.lookup(:hello, 4)
141
+ @registry.delete_category(:world_string, ['hello'])
142
+ assert_equal(2, @registry.size)
143
+ assert_equal(false, @registry.include?(obj1_))
144
+ assert_equal(false, @registry.include?(obj2_))
145
+ assert_equal(true, @registry.include?(obj3_))
146
+ assert_equal(true, @registry.include?(obj4_))
147
+ end
148
+
149
+
150
+ end
151
+
152
+ end
153
+ end
@@ -0,0 +1,330 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Configuration tests
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2012 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
+ require 'test/unit'
38
+ require 'idregistry'
39
+ require 'set'
40
+
41
+
42
+ module IDRegistry
43
+ module Tests # :nodoc:
44
+
45
+ class TestConfiguration < ::Test::Unit::TestCase # :nodoc:
46
+
47
+
48
+ Class1 = ::Struct.new(:value)
49
+ Class2 = ::Struct.new(:value1, :value2)
50
+
51
+
52
+ def setup
53
+ @config = IDRegistry.create.config
54
+ end
55
+
56
+
57
+ def test_new_config_is_unlocked
58
+ assert_equal(false, @config.locked?)
59
+ end
60
+
61
+
62
+ def test_empty_config
63
+ assert_equal([], @config.all_patterns)
64
+ assert_equal([], @config.all_types)
65
+ assert_equal([], @config.all_categories)
66
+ assert_equal([], @config.all_convenience_methods)
67
+ assert_equal(false, @config.has_pattern?([]))
68
+ assert_equal(false, @config.has_type?(:type))
69
+ assert_equal(false, @config.has_category?(:cat))
70
+ assert_equal(false, @config.has_convenience_method?(:get_hello))
71
+ end
72
+
73
+
74
+ def test_add_pattern_and_list_patterns_and_types
75
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
76
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
77
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
78
+ assert_equal([[:hello, ::Integer]], @config.all_patterns)
79
+ assert_equal([:hello_numbers], @config.all_types)
80
+ assert_equal([], @config.all_categories)
81
+ assert_equal(false, @config.has_pattern?([:bye, ::Integer]))
82
+ assert_equal(true, @config.has_pattern?([:hello, ::Integer]))
83
+ assert_equal(false, @config.has_type?(:hello_letters))
84
+ assert_equal(true, @config.has_type?(:hello_numbers))
85
+ end
86
+
87
+
88
+ def test_type_for_pattern_with_one_pattern
89
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
90
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
91
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
92
+ assert_equal(:hello_numbers, @config.type_for_pattern([:hello, ::Integer]))
93
+ assert_nil(@config.type_for_pattern([:bye, ::Integer]))
94
+ end
95
+
96
+
97
+ def test_patterns_for_type_with_one_pattern
98
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
99
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
100
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
101
+ assert_equal([[:hello, ::Integer]], @config.patterns_for_type(:hello_numbers))
102
+ assert_equal([], @config.patterns_for_type(:hello_letters))
103
+ end
104
+
105
+
106
+ def test_list_patterns_and_types_with_two_patterns_for_the_same_type
107
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
108
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
109
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
110
+ @config.add_pattern([:world, ::Float], :hello_numbers,
111
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
112
+ ::Proc.new{ |obj_| [:world, obj_.value.to_f] })
113
+ assert_equal(::Set.new([[:hello, ::Integer], [:world, ::Float]]), ::Set.new(@config.all_patterns))
114
+ assert_equal([:hello_numbers], @config.all_types)
115
+ assert_equal([], @config.all_categories)
116
+ assert_equal(true, @config.has_pattern?([:hello, ::Integer]))
117
+ assert_equal(false, @config.has_pattern?([:world, ::Integer]))
118
+ assert_equal(true, @config.has_pattern?([:world, ::Float]))
119
+ assert_equal(true, @config.has_type?(:hello_numbers))
120
+ end
121
+
122
+
123
+ def test_type_for_pattern_with_two_patterns_for_the_same_type
124
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
125
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
126
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
127
+ @config.add_pattern([:world, ::Float], :hello_numbers,
128
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
129
+ ::Proc.new{ |obj_| [:world, obj_.value.to_f] })
130
+ assert_equal(:hello_numbers, @config.type_for_pattern([:hello, ::Integer]))
131
+ assert_equal(:hello_numbers, @config.type_for_pattern([:world, ::Float]))
132
+ assert_nil(@config.type_for_pattern([:world, ::Integer]))
133
+ end
134
+
135
+
136
+ def test_patterns_for_type_with_two_patterns_for_the_same_type
137
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
138
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
139
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
140
+ @config.add_pattern([:world, ::Float], :hello_numbers,
141
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
142
+ ::Proc.new{ |obj_| [:world, obj_.value.to_f] })
143
+ assert_equal(::Set.new([[:hello, ::Integer], [:world, ::Float]]), ::Set.new(@config.patterns_for_type(:hello_numbers)))
144
+ assert_equal([], @config.patterns_for_type(:hello_letters))
145
+ end
146
+
147
+
148
+ def test_list_patterns_and_types_with_two_patterns_of_different_types
149
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
150
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
151
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
152
+ @config.add_pattern([:world, ::Float], :world_numbers,
153
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
154
+ ::Proc.new{ |obj_| [:world, obj_.value.to_f] })
155
+ assert_equal(::Set.new([[:hello, ::Integer], [:world, ::Float]]), ::Set.new(@config.all_patterns))
156
+ assert_equal(::Set.new([:hello_numbers, :world_numbers]), ::Set.new(@config.all_types))
157
+ assert_equal([], @config.all_categories)
158
+ assert_equal(true, @config.has_pattern?([:hello, ::Integer]))
159
+ assert_equal(false, @config.has_pattern?([:world, ::Integer]))
160
+ assert_equal(true, @config.has_pattern?([:world, ::Float]))
161
+ assert_equal(true, @config.has_type?(:hello_numbers))
162
+ assert_equal(true, @config.has_type?(:world_numbers))
163
+ end
164
+
165
+
166
+ def test_type_for_pattern_with_two_patterns_of_different_types
167
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
168
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
169
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
170
+ @config.add_pattern([:world, ::Float], :world_numbers,
171
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
172
+ ::Proc.new{ |obj_| [:world, obj_.value.to_f] })
173
+ assert_equal(:hello_numbers, @config.type_for_pattern([:hello, ::Integer]))
174
+ assert_equal(:world_numbers, @config.type_for_pattern([:world, ::Float]))
175
+ assert_nil(@config.type_for_pattern([:world, ::Integer]))
176
+ end
177
+
178
+
179
+ def test_patterns_for_type_with_two_patterns_of_different_types
180
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
181
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
182
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
183
+ @config.add_pattern([:world, ::Float], :world_numbers,
184
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
185
+ ::Proc.new{ |obj_| [:world, obj_.value.to_f] })
186
+ assert_equal([[:hello, ::Integer]], @config.patterns_for_type(:hello_numbers))
187
+ assert_equal([[:world, ::Float]], @config.patterns_for_type(:world_numbers))
188
+ assert_equal([], @config.patterns_for_type(:hello_letters))
189
+ end
190
+
191
+
192
+ def test_add_pattern_dsl
193
+ @config.add_pattern do
194
+ pattern([:hello, ::Integer])
195
+ type(:hello_numbers)
196
+ to_generate_object do |tuple_|
197
+ Class1.new(tuple_[1])
198
+ end
199
+ to_generate_tuple do |obj_|
200
+ [:hello, obj_.value]
201
+ end
202
+ end
203
+ assert_equal([[:hello, ::Integer]], @config.all_patterns)
204
+ assert_equal([:hello_numbers], @config.all_types)
205
+ assert_equal(:hello_numbers, @config.type_for_pattern([:hello, ::Integer]))
206
+ assert_equal([[:hello, ::Integer]], @config.patterns_for_type(:hello_numbers))
207
+ end
208
+
209
+
210
+ def test_delete_pattern
211
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
212
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
213
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
214
+ @config.add_pattern([:hello, ::Float], :hello_numbers,
215
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
216
+ ::Proc.new{ |obj_| [:hello, obj_.value.to_f] })
217
+ @config.add_pattern([:world, ::Float], :world_numbers,
218
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
219
+ ::Proc.new{ |obj_| [:world, obj_.value.to_f] })
220
+ @config.delete_pattern([:hello, ::Integer])
221
+ assert_equal(::Set.new([[:hello, ::Float], [:world, ::Float]]), ::Set.new(@config.all_patterns))
222
+ assert_equal(::Set.new([:hello_numbers, :world_numbers]), ::Set.new(@config.all_types))
223
+ assert_nil(@config.type_for_pattern([:hello, ::Integer]))
224
+ assert_equal([[:hello, ::Float]], @config.patterns_for_type(:hello_numbers))
225
+ end
226
+
227
+
228
+ def test_delete_pattern_which_deletes_type
229
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
230
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
231
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
232
+ @config.add_pattern([:world, ::Float], :world_numbers,
233
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
234
+ ::Proc.new{ |obj_| [:world, obj_.value.to_f] })
235
+ @config.delete_pattern([:hello, ::Integer])
236
+ assert_equal([[:world, ::Float]], @config.all_patterns)
237
+ assert_equal([:world_numbers], @config.all_types)
238
+ assert_nil(@config.type_for_pattern([:hello, ::Integer]))
239
+ assert_equal([], @config.patterns_for_type(:hello_numbers))
240
+ end
241
+
242
+
243
+ def test_delete_type
244
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
245
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
246
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
247
+ @config.add_pattern([:hello, ::Float], :hello_numbers,
248
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
249
+ ::Proc.new{ |obj_| [:hello, obj_.value.to_f] })
250
+ @config.add_pattern([:world, ::Float], :world_numbers,
251
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
252
+ ::Proc.new{ |obj_| [:world, obj_.value.to_f] })
253
+ @config.delete_type(:hello_numbers)
254
+ assert_equal([[:world, ::Float]], @config.all_patterns)
255
+ assert_equal([:world_numbers], @config.all_types)
256
+ assert_nil(@config.type_for_pattern([:hello, ::Integer]))
257
+ assert_equal([], @config.patterns_for_type(:hello_numbers))
258
+ end
259
+
260
+
261
+ def test_cannot_add_to_locked_config
262
+ @config.lock
263
+ assert_raise(ConfigurationLockedError) do
264
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
265
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
266
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
267
+ end
268
+ end
269
+
270
+
271
+ def test_add_category
272
+ @config.add_category(:cat1, [:hello, ::Integer], [1])
273
+ assert_equal([:cat1], @config.all_categories)
274
+ assert_equal(true, @config.has_category?(:cat1))
275
+ assert_equal(false, @config.has_category?(:cat2))
276
+ end
277
+
278
+
279
+ def test_delete_category
280
+ @config.add_category(:cat1, [:hello, ::Integer], [1])
281
+ @config.delete_category(:cat1)
282
+ assert_equal([], @config.all_categories)
283
+ assert_equal(false, @config.has_category?(:cat1))
284
+ end
285
+
286
+
287
+ def test_add_convenience_method
288
+ @config.add_convenience_method(:get_hello, [:hello, ::Integer], [1])
289
+ assert_equal([:get_hello], @config.all_convenience_methods)
290
+ assert_equal(true, @config.has_convenience_method?(:get_hello))
291
+ assert_equal(false, @config.has_convenience_method?(:get_hello2))
292
+ end
293
+
294
+
295
+ def test_delete_convenience_method
296
+ @config.add_convenience_method(:get_hello, [:hello, ::Integer], [1])
297
+ @config.delete_convenience_method(:get_hello)
298
+ assert_equal([], @config.all_convenience_methods)
299
+ assert_equal(false, @config.has_convenience_method?(:get_hello))
300
+ end
301
+
302
+
303
+ def test_clear
304
+ @config.add_pattern([:hello, ::Integer], :hello_numbers,
305
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1]) },
306
+ ::Proc.new{ |obj_| [:hello, obj_.value] })
307
+ @config.add_pattern([:hello, ::Float], :hello_numbers,
308
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
309
+ ::Proc.new{ |obj_| [:hello, obj_.value.to_f] })
310
+ @config.add_pattern([:world, ::Float], :world_numbers,
311
+ ::Proc.new{ |tuple_| Class1.new(tuple_[1].to_i) },
312
+ ::Proc.new{ |obj_| [:world, obj_.value.to_f] })
313
+ @config.add_category(:cat, [:hello, ::Integer], [1])
314
+ @config.add_convenience_method(:get_hello, [:hello, ::Integer], [1])
315
+ @config.clear
316
+ assert_equal([], @config.all_patterns)
317
+ assert_equal([], @config.all_types)
318
+ assert_equal([], @config.all_categories)
319
+ assert_equal([], @config.all_convenience_methods)
320
+ assert_equal(false, @config.has_pattern?([]))
321
+ assert_equal(false, @config.has_type?(:hello_numbers))
322
+ assert_equal(false, @config.has_category?(:cat))
323
+ assert_equal(false, @config.has_convenience_method?(:get_hello))
324
+ end
325
+
326
+
327
+ end
328
+
329
+ end
330
+ end