ghaki-registry 2011.11.30.1

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,22 @@
1
+ ############################################################################
2
+ require 'ghaki/app/engine'
3
+ require 'ghaki/registry/app'
4
+
5
+ module Ghaki module Registry module AppTesting
6
+ APP_MOD = Ghaki::App::Engine
7
+ ENG_MOD = Ghaki::Registry::Engine
8
+ describe APP_MOD do
9
+
10
+ context 'singleton' do
11
+ subject { APP_MOD.instance }
12
+ it { should respond_to :registry }
13
+ context '#registry' do
14
+ subject { APP_MOD.instance.registry }
15
+ it { should be_an_instance_of(ENG_MOD) }
16
+ it { should equal(ENG_MOD.instance) }
17
+ end
18
+ end
19
+
20
+ end
21
+ end end end
22
+ ############################################################################
@@ -0,0 +1,243 @@
1
+ ############################################################################
2
+ require 'ghaki/registry/engine'
3
+
4
+ ############################################################################
5
+ module Ghaki module Registry module EngineTesting
6
+ describe Ghaki::Registry::Engine do
7
+
8
+ class TestPlugin; end
9
+
10
+ #######################################################################
11
+ subject do Ghaki::Registry::Engine.instance end
12
+
13
+ ########################################################################
14
+ after(:each) do
15
+ subject.clear_features
16
+ end
17
+
18
+ ########################################################################
19
+ context 'singleton' do
20
+ MY_METHODS = [
21
+ :features, :clear_features,
22
+ :reserve_feature, :remove_feature,
23
+ :has_feature?, :assert_feature!, :get_feature,
24
+ :enable_feature, :disable_feature, :enabled_feature?,
25
+ :plugins, :clear_plugins,
26
+ :add_plugin, :remove_plugin,
27
+ :has_plugin?, :assert_plugin!, :get_plugin,
28
+ :enable_plugin, :disable_plugin, :enabled_plugin?,
29
+ :failed_plugin?, :plugin_failure,
30
+ :factory_create, :load_plugin,
31
+ ]
32
+ MY_METHODS.each do |token| it { should respond_to token } end
33
+ specify { should be_an_instance_of Engine }
34
+ end
35
+
36
+ ########################################################################
37
+ context 'initial state' do
38
+ it 'should have no features' do
39
+ subject.features.empty?.should == true
40
+ end
41
+ end
42
+
43
+ ########################################################################
44
+ ########################################################################
45
+ context 'feature normal methods' do
46
+
47
+ describe '#reserve_feature' do
48
+ it 'should reserve names' do
49
+ subject.reserve_feature :my_feat
50
+ subject.features.keys.length.should == 1
51
+ subject.features.has_key?(:my_feat)
52
+ end
53
+ end
54
+
55
+ describe '#has_feature?' do
56
+ it 'should see features' do
57
+ subject.reserve_feature :my_feat
58
+ subject.has_feature?(:my_feat).should == true
59
+ end
60
+ end
61
+
62
+ describe '#assert_feature!' do
63
+ it 'should raise when missing' do
64
+ lambda do
65
+ subject.assert_feature! :mine
66
+ end.should raise_error(Ghaki::FeatureNotFoundError)
67
+ end
68
+ it 'should not raise when present' do
69
+ lambda do
70
+ subject.reserve_feature :mine
71
+ subject.assert_feature! :mine
72
+ end.should_not raise_error(Ghaki::FeatureNotFoundError)
73
+ end
74
+ end
75
+
76
+ describe '#get_feature' do
77
+ it 'should fetch' do
78
+ subject.reserve_feature :my
79
+ subject.get_feature(:my).class.should == Ghaki::Registry::Feature
80
+ subject.get_feature(:my).name.should == :my
81
+ end
82
+ end
83
+
84
+ describe '#clear_features' do
85
+ it 'should remove all' do
86
+ subject.reserve_feature :my_reg_a
87
+ subject.reserve_feature :my_reg_b
88
+ subject.clear_features
89
+ subject.has_feature?( :my_reg_a ).should == false
90
+ subject.features.empty?.should == true
91
+ end
92
+ end
93
+
94
+ describe '#remove_feature' do
95
+ it 'should remove' do
96
+ subject.reserve_feature :my_reg
97
+ subject.remove_feature :my_reg
98
+ subject.has_feature?( :my_reg ).should == false
99
+ end
100
+ end
101
+
102
+ end # feat norm meths
103
+
104
+ ########################################################################
105
+ ########################################################################
106
+ context 'feature delegate methods' do
107
+
108
+ before(:each) do
109
+ subject.reserve_feature :duck
110
+ end
111
+
112
+ describe '#disable_feature' do
113
+ it 'should disable' do
114
+ subject.disable_feature :duck
115
+ subject.enabled_feature?(:duck).should == false
116
+ end
117
+ end
118
+
119
+ describe '#enable_feature' do
120
+ it 'should enable' do
121
+ subject.disable_feature :duck
122
+ subject.enable_feature :duck
123
+ subject.enabled_feature?(:duck).should == true
124
+ end
125
+ end
126
+
127
+ describe '#plugins' do
128
+ it 'should have plugins' do
129
+ subject.plugins(:duck).class.should == Hash
130
+ subject.get_feature(:duck).plugins == subject.plugins(:duck)
131
+ end
132
+ end
133
+
134
+ describe '#get_plugin' do
135
+ it 'should retrieve plugin' do
136
+ subject.add_plugin :my_reg, :my_plugin, TestPlugin
137
+ plug_a = subject.get_feature(:my_reg).get_plugin(:my_plugin)
138
+ plug_b = subject.get_plugin(:my_reg,:my_plugin)
139
+ plug_a.should == plug_b
140
+ end
141
+ end
142
+
143
+ describe '#add_plugin' do
144
+ it 'should register plugin' do
145
+ subject.add_plugin :my_reg, :my_plugin, TestPlugin
146
+ subject.get_feature(:my_reg).has_plugin?(:my_plugin).should == true
147
+ end
148
+ end
149
+
150
+ describe '#has_plugin?' do
151
+ it 'should see plugin' do
152
+ subject.add_plugin :my_reg, :my_plugin, TestPlugin
153
+ subject.plugins(:my_reg).has_key?(:my_plugin).should == true
154
+ end
155
+ end
156
+
157
+ describe '#assert_plugin!' do
158
+ it 'should complain when missing' do
159
+ lambda do
160
+ subject.reserve_feature :my_reg
161
+ subject.assert_plugin! :my_reg, :my_plugin
162
+ end.should raise_error(Ghaki::PluginNotFoundError)
163
+ end
164
+ it 'should not raise when present' do
165
+ lambda do
166
+ subject.add_plugin :my_reg, :my_plugin, TestPlugin
167
+ subject.assert_plugin! :my_reg, :my_plugin
168
+ end.should_not raise_error(Ghaki::PluginNotFoundError)
169
+ end
170
+ end
171
+
172
+ describe '#remove_plugin!' do
173
+ it 'should remove' do
174
+ subject.add_plugin :my_reg, :my_plug, TestPlugin
175
+ subject.remove_plugin :my_reg, :my_plug
176
+ subject.has_plugin?( :my_reg, :my_plug ).should == false
177
+ end
178
+ end
179
+
180
+ describe '#clear_plugins' do
181
+ it 'should remove all plugins' do
182
+ subject.add_plugin :my_reg, :my_plug_a, TestPlugin
183
+ subject.add_plugin :my_reg, :my_plug_b, TestPlugin
184
+ subject.clear_plugins :my_reg
185
+ subject.has_plugin?( :my_reg, :my_plug_a ).should == false
186
+ subject.has_plugin?( :my_reg, :my_plug_b ).should == false
187
+ subject.plugins(:my_reg).empty?
188
+ end
189
+ end
190
+
191
+ end # feat del meth
192
+
193
+ ########################################################################
194
+ ########################################################################
195
+ context 'plugin delegate methods' do
196
+
197
+ before(:each) do
198
+ subject.add_plugin :my_reg, :my_plugin, TestPlugin
199
+ end
200
+
201
+ describe '#enabled_plugin?' do
202
+ it 'should see plugin' do
203
+ subject.enabled_plugin?(:my_reg,:my_plugin).should == true
204
+ end
205
+ end
206
+
207
+ describe '#disable_plugin' do
208
+ it 'should disable' do
209
+ subject.disable_plugin :my_reg, :my_plugin
210
+ subject.get_feature(:my_reg).get_plugin(:my_plugin).enabled?.should == false
211
+ end
212
+ end
213
+
214
+ describe '#enable_plugin' do
215
+ it 'should enable' do
216
+ subject.disable_plugin :my_reg, :my_plugin
217
+ subject.enable_plugin :my_reg, :my_plugin
218
+ subject.get_feature(:my_reg).get_plugin(:my_plugin).enabled?.should == true
219
+ end
220
+ end
221
+
222
+ describe '#factory_create' do
223
+ it 'should create plugin when present' do
224
+ subject.factory_create( :my_reg, :my_plugin ).class.should == TestPlugin
225
+ end
226
+ it 'should complain about missing feature' do
227
+ lambda do
228
+ subject.factory_create( :miss_reg, :my_plugin )
229
+ end.should raise_error(Ghaki::FeatureNotFoundError)
230
+ end
231
+ it 'should complain about missing plugin' do
232
+ lambda do
233
+ subject.factory_create( :my_reg, :miss_plugin )
234
+ end.should raise_error(Ghaki::PluginNotFoundError)
235
+ end
236
+
237
+ end
238
+
239
+ end # plug del meth
240
+
241
+ end
242
+ end end end
243
+ ############################################################################
@@ -0,0 +1,174 @@
1
+ ############################################################################
2
+ require 'ghaki/registry/engine'
3
+ require 'ghaki/registry/factory'
4
+
5
+ ############################################################################
6
+ module Ghaki module Registry module FactoryTesting
7
+ describe Ghaki::Registry::Factory do
8
+
9
+ before(:all) do @reg = Ghaki::Registry::Engine.instance end
10
+ after(:each) do @reg.clear_features end
11
+
12
+ ##########################################################################
13
+ ##########################################################################
14
+ context 'with simple naming' do
15
+
16
+ ########################################################################
17
+ class DogPlugin; end
18
+ class DogFactory
19
+ extend Ghaki::Registry::Factory
20
+ register_feature :dog
21
+ private_class_method :new
22
+ end
23
+
24
+ ########################################################################
25
+ before(:each) do @reg.add_plugin(:dog,:rover,DogPlugin) end
26
+
27
+ ########################################################################
28
+ context 'objects extended by' do
29
+ subject do DogFactory end
30
+ DOG_METHODS = [
31
+ :create, :has_plugin?, :add_plugin,
32
+ :enabled_plugin?, :enable_plugin, :disable_plugin,
33
+ :remove_plugin, :clear_plugins, :plugins, :feature,
34
+ ]
35
+ DOG_METHODS.each do |token| it { should respond_to token } end
36
+ end
37
+
38
+ ########################################################################
39
+ context 'methods' do
40
+
41
+ describe '#create' do
42
+ it 'should make object' do
43
+ DogFactory.create(:rover).class.should == DogPlugin
44
+ end
45
+ end
46
+
47
+ describe '#has_plugin?' do
48
+ it 'should see' do
49
+ DogFactory.has_plugin?(:rover).should == true
50
+ end
51
+ end
52
+
53
+ describe '#plugins' do
54
+ it 'should have plugins' do
55
+ DogFactory.plugins.class.should == Hash
56
+ DogFactory.plugins.keys.length.should == 1
57
+ end
58
+ end
59
+
60
+ describe '#disable_plugins' do
61
+ it 'should disable' do
62
+ DogFactory.disable_plugin(:rover)
63
+ DogFactory.enabled_plugin?(:rover)
64
+ end
65
+ end
66
+
67
+ describe '#enable_plugins' do
68
+ it 'should disable' do
69
+ DogFactory.disable_plugin(:rover)
70
+ DogFactory.enable_plugin(:rover)
71
+ DogFactory.enabled_plugin?(:rover)
72
+ end
73
+ end
74
+
75
+ describe '#remove_plugin' do
76
+ it 'should remove' do
77
+ DogFactory.remove_plugin(:rover)
78
+ DogFactory.has_plugin?(:rover).should == false
79
+ end
80
+ end
81
+
82
+ describe '#feature' do
83
+ it 'should expose feature' do
84
+ DogFactory.feature.class.should == Ghaki::Registry::Feature
85
+ end
86
+ end
87
+
88
+ end # simple methods
89
+
90
+ end # simple
91
+
92
+ ##########################################################################
93
+ ##########################################################################
94
+ context 'complex' do
95
+
96
+ ########################################################################
97
+ class CatPlugin; end
98
+ class CatFactory
99
+ extend Ghaki::Registry::Factory
100
+ register_feature :cat, :simple_naming => false
101
+ private_class_method :new
102
+ end
103
+
104
+ ########################################################################
105
+ before(:each) do @reg.add_plugin(:cat,:felix,CatPlugin) end
106
+
107
+ ########################################################################
108
+ context 'objects extended by' do
109
+ subject do CatFactory end
110
+ CAT_METHODS = [
111
+ :create_cat_plugin, :has_cat_plugin?, :add_cat_plugin,
112
+ :enabled_cat_plugin?, :enable_cat_plugin, :disable_cat_plugin,
113
+ :remove_cat_plugin, :clear_cat_plugins, :cat_plugins, :cat_feature,
114
+ ]
115
+ CAT_METHODS.each do |token| it { should respond_to token } end
116
+ end
117
+
118
+ ########################################################################
119
+ context 'methods' do
120
+
121
+ describe '#create_<name>' do
122
+ it 'should create' do
123
+ CatFactory.create_cat_plugin(:felix).class.should == CatPlugin
124
+ end
125
+ end
126
+
127
+ describe '#has_plugin_<name>?' do
128
+ it 'should see' do
129
+ CatFactory.has_cat_plugin?(:felix).should == true
130
+ end
131
+ end
132
+
133
+ describe '#<name>_plugins' do
134
+ it 'should have plugins' do
135
+ CatFactory.cat_plugins.class.should == Hash
136
+ CatFactory.cat_plugins.keys.length.should == 1
137
+ end
138
+ end
139
+
140
+ describe '#disable_<name>_plugin' do
141
+ it 'should disable' do
142
+ CatFactory.disable_cat_plugin(:felix)
143
+ CatFactory.enabled_cat_plugin?(:felix).should == false
144
+ end
145
+ end
146
+
147
+ describe '#enable_<name>_plugin' do
148
+ it 'should enable' do
149
+ CatFactory.disable_cat_plugin(:felix)
150
+ CatFactory.enable_cat_plugin(:felix)
151
+ CatFactory.enabled_cat_plugin?(:felix).should == true
152
+ end
153
+ end
154
+
155
+ describe '#remove_<name>_plugin' do
156
+ it 'should remove' do
157
+ CatFactory.remove_cat_plugin(:felix)
158
+ CatFactory.has_cat_plugin?(:felix).should == false
159
+ end
160
+ end
161
+
162
+ describe '#<name>_feature' do
163
+ it 'should expose feature' do
164
+ CatFactory.cat_feature.class.should == Ghaki::Registry::Feature
165
+ end
166
+ end
167
+
168
+ end # complex methods
169
+
170
+ end # complex
171
+
172
+ end
173
+ end end end
174
+ ############################################################################
@@ -0,0 +1,209 @@
1
+ ############################################################################
2
+ require 'ghaki/registry/feature'
3
+
4
+ ############################################################################
5
+ module Ghaki module Registry module FeatureTesting
6
+ describe Ghaki::Registry::Feature do
7
+
8
+ class TestPlugin
9
+ end
10
+
11
+ ########################################################################
12
+ before (:each) do
13
+ @feature = Ghaki::Registry::Feature.new(:my_feature)
14
+ end
15
+
16
+ ########################################################################
17
+ subject do @feature end
18
+
19
+ ########################################################################
20
+ context 'object' do
21
+ MY_METHODS = [
22
+ :enabled?, :enable, :disable, :assert_enabled!,
23
+ :add_plugin, :remove_plugin, :has_plugin?, :assert_plugin!,
24
+ :plugins, :clear_plugins, :to_s,
25
+ :get_plugin, :factory_create,
26
+ :enable_plugin, :disable_plugin, :enabled_plugin?,
27
+ :load_plugin, :failed_plugin?, :plugin_failure,
28
+ ]
29
+ MY_METHODS.each do |token| it { should respond_to token } end
30
+ end
31
+
32
+ ########################################################################
33
+ ########################################################################
34
+ context 'initial state' do
35
+ it 'should be enabled' do
36
+ subject.enabled?.should be_true
37
+ end
38
+ it 'should be empty' do
39
+ subject.plugins.empty?.should be_true
40
+ end
41
+ it '#plugins should be hash' do
42
+ subject.plugins.should be_an_instance_of(::Hash)
43
+ end
44
+ end
45
+
46
+ ########################################################################
47
+ ########################################################################
48
+ context 'methods' do
49
+
50
+ ######################################################################
51
+ describe '#disable' do
52
+ it 'should disable' do
53
+ subject.disable
54
+ subject.enabled?.should be_false
55
+ end
56
+ end
57
+
58
+ ######################################################################
59
+ describe '#enable' do
60
+ it 'should enable' do
61
+ subject.disable
62
+ subject.enable
63
+ subject.enabled?.should be_true
64
+ end
65
+ end
66
+
67
+ ######################################################################
68
+ describe '#assert_enabled!' do
69
+ it 'should not complain when enabled' do
70
+ lambda do
71
+ subject.assert_enabled!
72
+ end.should_not raise_error(Ghaki::FeatureDisabledError)
73
+ end
74
+ it 'should complain when disabled' do
75
+ lambda do
76
+ subject.disable
77
+ subject.assert_enabled!
78
+ end.should raise_error(Ghaki::FeatureDisabledError)
79
+ end
80
+ end
81
+
82
+ ######################################################################
83
+ describe '#add_plugin' do
84
+ it 'should register plugin' do
85
+ subject.add_plugin :my_plugin, TestPlugin
86
+ subject.has_plugin?( :my_plugin ).should be_true
87
+ subject.get_plugin(:my_plugin).should_not be_nil
88
+ end
89
+ end
90
+
91
+ ######################################################################
92
+ describe '#assert_plugin!' do
93
+ it 'should complain when missing' do
94
+ lambda do
95
+ subject.assert_plugin! :my_plugin
96
+ end.should raise_error(Ghaki::PluginNotFoundError)
97
+ end
98
+ it 'should not complain when present' do
99
+ lambda do
100
+ subject.add_plugin :my_plugin
101
+ subject.assert_plugin! :my_plugin
102
+ end.should_not raise_error(Ghaki::PluginNotFoundError)
103
+ end
104
+ end
105
+
106
+ ######################################################################
107
+ describe '#remove_plugin' do
108
+ it 'should remove' do
109
+ subject.add_plugin :my_plug_a, TestPlugin
110
+ subject.add_plugin :my_plug_b, TestPlugin
111
+ subject.remove_plugin :my_plug_a
112
+ subject.has_plugin?( :my_plug_a ).should be_false
113
+ subject.has_plugin?( :my_plug_b ).should be_true
114
+ subject.plugins.keys.length.should == 1
115
+ end
116
+ end
117
+
118
+ ######################################################################
119
+ describe '#clear_plugins' do
120
+ it 'should remove all plugins' do
121
+ subject.add_plugin :my_plug_a, TestPlugin
122
+ subject.add_plugin :my_plug_b, TestPlugin
123
+ subject.clear_plugins
124
+ subject.has_plugin?( :my_plug_a ).should be_false
125
+ subject.has_plugin?( :my_plug_b ).should be_false
126
+ subject.plugins.keys.empty?.should be_true
127
+ end
128
+ end
129
+
130
+ ######################################################################
131
+ describe '#load_plugin' do
132
+ it 'should return nil when library load fails' do
133
+ subject.load_plugin( :my_plugin, 'bogus/missing' ).should be_nil
134
+ end
135
+ end
136
+
137
+ ######################################################################
138
+ describe '#plugin_failure' do
139
+ it 'should have error when library load fails' do
140
+ subject.load_plugin( :my_plugin, 'crap/missing' )
141
+ subject.plugin_failure(:my_plugin).should be_an_instance_of(Ghaki::PluginLoadingError)
142
+ end
143
+ end
144
+ describe '#failed_plugin?' do
145
+ it 'should detect library load failure' do
146
+ subject.load_plugin( :my_plug_bad, 'crap/missing' )
147
+ subject.failed_plugin?(:my_plug_bad).should be_true
148
+ end
149
+ it 'should not have a failure if successful' do
150
+ subject.add_plugin( :my_plug_good, TestPlugin )
151
+ subject.failed_plugin?(:my_plug_good).should be_false
152
+ end
153
+ end
154
+
155
+ ######################################################################
156
+ describe '#factory_create' do
157
+ it 'should create when plugin is present' do
158
+ subject.add_plugin :my_plugin, TestPlugin
159
+ subject.factory_create( :my_plugin ).should be_an_instance_of(TestPlugin)
160
+ end
161
+ it 'should complain when plugin is not specified' do
162
+ lambda do
163
+ subject.factory_create( :my_plugin )
164
+ end.should raise_error(Ghaki::PluginNotFoundError)
165
+ end
166
+ it 'should complain when plugin is invalid' do
167
+ subject.load_plugin( :my_plugin, 'junk/missing' )
168
+ lambda do
169
+ subject.factory_create :my_plugin
170
+ end.should raise_error(Ghaki::PluginLoadingError)
171
+ end
172
+ end
173
+
174
+ end # meths
175
+
176
+ ########################################################################
177
+ ########################################################################
178
+ context 'quasi delegated methods' do
179
+
180
+ ######################################################################
181
+ describe '#enabled_plugin?' do
182
+ it 'should see status' do
183
+ subject.add_plugin :my_plugin, TestPlugin
184
+ subject.enabled_plugin?(:my_plugin).should be_true
185
+ end
186
+ end
187
+ ######################################################################
188
+ describe '#disable_plugin' do
189
+ it 'should disable' do
190
+ subject.add_plugin :my_plugin, TestPlugin
191
+ subject.disable_plugin :my_plugin
192
+ subject.enabled_plugin?(:my_plugin).should be_false
193
+ end
194
+ end
195
+ ######################################################################
196
+ describe '#enable_plugin' do
197
+ it 'should enable' do
198
+ subject.add_plugin :my_plugin, TestPlugin
199
+ subject.disable_plugin :my_plugin
200
+ subject.enable_plugin :my_plugin
201
+ subject.enabled_plugin?(:my_plugin).should be_true
202
+ end
203
+ end
204
+
205
+ end # quasi-delegated meths
206
+
207
+ end
208
+ end end end
209
+ ############################################################################