idregistry 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,139 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Tests for Rack middleware
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
+
40
+
41
+ module IDRegistry
42
+ module Tests # :nodoc:
43
+
44
+ class TestMiddleware < ::Test::Unit::TestCase # :nodoc:
45
+
46
+
47
+ Class1 = ::Struct.new(:value)
48
+
49
+
50
+ def setup
51
+ @registry = IDRegistry.create
52
+ @registry.config do
53
+ add_pattern do
54
+ pattern [:hello, ::Integer]
55
+ type :hello_numbers
56
+ to_generate_object{ |tuple_| Class1.new(tuple_[1]) }
57
+ to_generate_tuple{ |obj_| [:hello, obj_.value] }
58
+ end
59
+ add_pattern do
60
+ pattern [:hello, ::Float]
61
+ type :hello_numbers
62
+ to_generate_object{ |tuple_| Class1.new(tuple_[1].to_i) }
63
+ to_generate_tuple{ |obj_| [:hello, obj_.value.to_f] }
64
+ end
65
+ add_pattern do
66
+ pattern [:world, ::Float]
67
+ type :world_numbers
68
+ to_generate_object{ |tuple_| Class1.new(tuple_[1].to_i) }
69
+ to_generate_tuple{ |obj_| [:world, obj_.value.to_f] }
70
+ end
71
+ end
72
+ end
73
+
74
+
75
+ def test_clear_registry_task_without_condition
76
+ task_ = RegistryMiddleware::ClearRegistry.new(@registry)
77
+ @registry.lookup(:hello, 1)
78
+ assert_equal(1, @registry.size)
79
+ task_.pre({})
80
+ task_.post({})
81
+ assert_equal(0, @registry.size)
82
+ end
83
+
84
+
85
+ def test_clear_registry_task_with_condition
86
+ task_ = RegistryMiddleware::ClearRegistry.new(@registry) do |env_|
87
+ env_[:foo] == :bar
88
+ end
89
+ @registry.lookup(:hello, 1)
90
+ assert_equal(1, @registry.size)
91
+ task_.pre({:foo => :baz})
92
+ task_.post({:foo => :baz})
93
+ assert_equal(1, @registry.size)
94
+ task_.pre({:foo => :bar})
95
+ task_.post({:foo => :bar})
96
+ assert_equal(0, @registry.size)
97
+ end
98
+
99
+
100
+ def test_spawn_registry_task
101
+ task_ = RegistryMiddleware::SpawnRegistry.new(@registry.config.lock, :reg)
102
+ obj1_ = @registry.lookup(:hello, 1)
103
+ assert_equal(1, @registry.size)
104
+ env_ = {}
105
+ task_.pre(env_)
106
+ nreg_ = env_[:reg]
107
+ assert_equal(0, nreg_.size)
108
+ obj2_ = nreg_.lookup(:hello, 1)
109
+ assert_not_nil(obj2_)
110
+ assert_not_equal(obj1_.object_id, obj2_.object_id)
111
+ task_.post(env_)
112
+ assert_nil(env_[:reg])
113
+ assert_equal(0, nreg_.size)
114
+ assert_equal(1, @registry.size)
115
+ end
116
+
117
+
118
+ def test_full_middleware
119
+ called_ = false
120
+ task_ = RegistryMiddleware::ClearRegistry.new(@registry)
121
+ app_ = ::Proc.new do |env_|
122
+ @registry.lookup(:hello, 2)
123
+ assert_equal(2, @registry.size)
124
+ called_ = true
125
+ nil
126
+ end
127
+ middleware_ = RegistryMiddleware.new(app_, [task_])
128
+ @registry.lookup(:hello, 1)
129
+ assert_equal(1, @registry.size)
130
+ middleware_.call({})
131
+ assert_equal(true, called_)
132
+ assert_equal(0, @registry.size)
133
+ end
134
+
135
+
136
+ end
137
+
138
+ end
139
+ end
data/test/tc_misc.rb ADDED
@@ -0,0 +1,111 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Tests for pattern matcher
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
+
40
+
41
+ module IDRegistry
42
+ module Tests # :nodoc:
43
+
44
+ class TestMisc < ::Test::Unit::TestCase # :nodoc:
45
+
46
+
47
+ def test_match_basic
48
+ assert_equal(true, Utils.matches?([:foo, ::Integer], [:foo, 1]))
49
+ end
50
+
51
+
52
+ def test_match_wrong_class
53
+ assert_equal(false, Utils.matches?([:foo, ::Integer], [:foo, 'bar']))
54
+ end
55
+
56
+
57
+ def test_match_extra_items
58
+ assert_equal(false, Utils.matches?([:foo, ::Integer], [:foo, 1, 2]))
59
+ end
60
+
61
+
62
+ def test_match_missing_items
63
+ assert_equal(false, Utils.matches?([:foo, ::Integer], [:foo]))
64
+ end
65
+
66
+
67
+ def test_match_multiple_classes
68
+ assert_equal(true, Utils.matches?([:foo, ::Integer, ::Integer], [:foo, 3, 4]))
69
+ end
70
+
71
+
72
+ def test_match_multiple_classes_missing_items
73
+ assert_equal(false, Utils.matches?([:foo, ::Integer, ::Integer], [:foo, 1]))
74
+ end
75
+
76
+
77
+ def test_match_multiple_classes_extra_items
78
+ assert_equal(false, Utils.matches?([:foo, ::Integer, ::Integer], [:foo, 1, 2, 3]))
79
+ end
80
+
81
+
82
+ def test_match_wrong_constant
83
+ assert_equal(false, Utils.matches?([:foo, ::Integer, ::Integer], [:bar, 2, 3]))
84
+ end
85
+
86
+
87
+ def test_match_multiple_different_classes
88
+ assert_equal(true, Utils.matches?([:foo, ::Integer, ::String], [:foo, 2, 'bar']))
89
+ end
90
+
91
+
92
+ def test_match_multiple_constants
93
+ assert_equal(true, Utils.matches?([:foo, :bar, ::String], [:foo, :bar, 'bar']))
94
+ end
95
+
96
+
97
+ def test_match_multiple_constants_wrong_constant
98
+ assert_equal(false, Utils.matches?([:foo, :bar, ::String], [:foo, :baz, 'bar']))
99
+ end
100
+
101
+
102
+ def test_match_symbols_and_strings
103
+ assert_equal(true, Utils.matches?([::Symbol, 1, 3, ::String], [:foo, 1, 3, 'bar']))
104
+ assert_equal(false, Utils.matches?([::Symbol, 1, 3, ::String], ['foo', 1, 3, 'bar']))
105
+ end
106
+
107
+
108
+ end
109
+
110
+ end
111
+ end
@@ -0,0 +1,221 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Basic end-to-end 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 TestSimplePatterns < ::Test::Unit::TestCase # :nodoc:
46
+
47
+
48
+ Class1 = ::Struct.new(:value)
49
+
50
+
51
+ def setup
52
+ @registry = IDRegistry.create
53
+ @registry.config do
54
+ add_pattern do
55
+ pattern [:hello, ::Integer]
56
+ type :hello_numbers
57
+ to_generate_object{ |tuple_| Class1.new(tuple_[1]) }
58
+ to_generate_tuple{ |obj_| [:hello, obj_.value] }
59
+ end
60
+ add_pattern do
61
+ pattern [:hello, ::Float]
62
+ type :hello_numbers
63
+ to_generate_object{ |tuple_| Class1.new(tuple_[1].to_i) }
64
+ to_generate_tuple{ |obj_| [:hello, obj_.value.to_f] }
65
+ end
66
+ add_pattern do
67
+ pattern [:world, ::Float]
68
+ type :world_numbers
69
+ to_generate_object{ |tuple_| Class1.new(tuple_[1].to_i) }
70
+ to_generate_tuple{ |obj_| [:world, obj_.value.to_f] }
71
+ end
72
+ add_pattern([:anon, ::Integer]) do |tuple_|
73
+ Class1.new(tuple_[1] * 2)
74
+ end
75
+ end
76
+ end
77
+
78
+
79
+ def test_simple_lookup_object
80
+ obj_ = @registry.lookup([:hello, 1])
81
+ assert_equal(Class1.new(1), obj_)
82
+ end
83
+
84
+
85
+ def test_lookup_with_bad_pattern
86
+ assert_nil(@registry.lookup([:yoyo, 1]))
87
+ end
88
+
89
+
90
+ def test_simple_lookup_size
91
+ assert_equal(0, @registry.size)
92
+ @registry.lookup([:hello, 1])
93
+ assert_equal(1, @registry.size)
94
+ end
95
+
96
+
97
+ def test_simple_lookup_and_get
98
+ assert_nil(@registry.get([:hello, 1]))
99
+ obj_ = @registry.lookup([:hello, 1])
100
+ assert_equal(obj_.object_id, @registry.get([:hello, 1]).object_id)
101
+ end
102
+
103
+
104
+ def test_simple_lookup_and_tuples_for
105
+ obj_ = @registry.lookup([:hello, 1])
106
+ assert_nil(@registry.tuples_for(Class1.new(1)))
107
+ assert_equal(::Set.new([[:hello, 1], [:hello, 1.0]]), ::Set.new(@registry.tuples_for(obj_)))
108
+ end
109
+
110
+
111
+ def test_simple_lookup_and_include
112
+ obj_ = @registry.lookup([:hello, 1])
113
+ assert_equal(false, @registry.include?(Class1.new(1)))
114
+ assert_equal(true, @registry.include?(obj_))
115
+ assert_equal(true, @registry.include?([:hello, 1]))
116
+ assert_equal(true, @registry.include?([:hello, 1.0]))
117
+ assert_equal(false, @registry.include?([:world, 1.0]))
118
+ end
119
+
120
+
121
+ def test_simple_lookup_returns_same_object
122
+ obj1_ = @registry.lookup([:hello, 1])
123
+ obj2_ = @registry.lookup([:hello, 1.0])
124
+ assert_equal(obj1_.object_id, obj2_.object_id)
125
+ assert_equal(1, @registry.size)
126
+ end
127
+
128
+
129
+ def test_multiple_lookups
130
+ obj1_ = @registry.lookup([:hello, 1])
131
+ obj2_ = @registry.lookup([:hello, 2.0])
132
+ assert_not_equal(obj1_.object_id, obj2_.object_id)
133
+ assert_equal(2, @registry.size)
134
+ end
135
+
136
+
137
+ def test_add_for_one_tuple
138
+ obj_ = Class1.new(1)
139
+ @registry.add(:world_numbers, obj_)
140
+ assert_equal(1, @registry.size)
141
+ assert_equal(obj_.object_id, @registry.get([:world, 1.0]).object_id)
142
+ assert_equal([[:world, 1.0]], @registry.tuples_for(obj_))
143
+ end
144
+
145
+
146
+ def test_add_for_multi_tuples
147
+ obj_ = Class1.new(1)
148
+ @registry.add(:hello_numbers, obj_)
149
+ assert_equal(1, @registry.size)
150
+ assert_equal(obj_.object_id, @registry.get([:hello, 1.0]).object_id)
151
+ assert_equal(::Set.new([[:hello, 1], [:hello, 1.0]]), ::Set.new(@registry.tuples_for(obj_)))
152
+ end
153
+
154
+
155
+ def test_delete_object
156
+ obj_ = @registry.lookup([:hello, 1])
157
+ @registry.delete(obj_)
158
+ assert_nil(@registry.get([:hello, 1]))
159
+ assert_equal(0, @registry.size)
160
+ end
161
+
162
+
163
+ def test_delete_tuple
164
+ @registry.lookup([:hello, 1])
165
+ @registry.delete([:hello, 1.0])
166
+ assert_nil(@registry.get([:hello, 1]))
167
+ assert_equal(0, @registry.size)
168
+ end
169
+
170
+
171
+ def test_rekey_single_tuple_by_object
172
+ obj_ = @registry.lookup([:world, 1.0])
173
+ obj_.value = 2
174
+ @registry.rekey(obj_)
175
+ assert_nil(@registry.get([:world, 1.0]))
176
+ assert_equal(obj_.object_id, @registry.get([:world, 2.0]).object_id)
177
+ assert_equal([[:world, 2.0]], @registry.tuples_for(obj_))
178
+ end
179
+
180
+
181
+ def test_rekey_single_tuple_by_tuple
182
+ obj_ = @registry.lookup([:world, 1.0])
183
+ obj_.value = 2
184
+ @registry.rekey([:world, 1.0])
185
+ assert_nil(@registry.get([:world, 1.0]))
186
+ assert_equal(obj_.object_id, @registry.get([:world, 2.0]).object_id)
187
+ assert_equal([[:world, 2.0]], @registry.tuples_for(obj_))
188
+ end
189
+
190
+
191
+ def test_rekey_multi_tuple_by_object
192
+ obj_ = @registry.lookup([:hello, 1.0])
193
+ obj_.value = 2
194
+ @registry.rekey(obj_)
195
+ assert_nil(@registry.get([:hello, 1.0]))
196
+ assert_equal(obj_.object_id, @registry.get([:hello, 2]).object_id)
197
+ assert_equal(::Set.new([[:hello, 2], [:hello, 2.0]]), ::Set.new(@registry.tuples_for(obj_)))
198
+ end
199
+
200
+
201
+ def test_rekey_multi_tuple_by_tuple
202
+ obj_ = @registry.lookup([:hello, 1.0])
203
+ obj_.value = 2
204
+ @registry.rekey([:hello, 1])
205
+ assert_nil(@registry.get([:hello, 1.0]))
206
+ assert_equal(obj_.object_id, @registry.get([:hello, 2]).object_id)
207
+ assert_equal(::Set.new([[:hello, 2], [:hello, 2.0]]), ::Set.new(@registry.tuples_for(obj_)))
208
+ end
209
+
210
+
211
+ def test_lookup_with_no_tuple_generator
212
+ obj1_ = @registry.lookup([:anon, 1])
213
+ obj2_ = @registry.lookup([:anon, 1])
214
+ assert_equal(obj1_.object_id, obj2_.object_id)
215
+ end
216
+
217
+
218
+ end
219
+
220
+ end
221
+ end