impromptu 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +34 -0
  4. data/README.rdoc +76 -0
  5. data/Rakefile +53 -0
  6. data/VERSION +1 -0
  7. data/impromptu.gemspec +106 -0
  8. data/lib/impromptu/autoload.rb +33 -0
  9. data/lib/impromptu/component.rb +124 -0
  10. data/lib/impromptu/component_set.rb +10 -0
  11. data/lib/impromptu/file.rb +220 -0
  12. data/lib/impromptu/folder.rb +151 -0
  13. data/lib/impromptu/impromptu.rb +81 -0
  14. data/lib/impromptu/ordered_set.rb +49 -0
  15. data/lib/impromptu/resource.rb +195 -0
  16. data/lib/impromptu/symbol.rb +43 -0
  17. data/lib/impromptu.rb +12 -0
  18. data/test/framework/copies/extra_klass2.rb +7 -0
  19. data/test/framework/copies/new_klass.rb +10 -0
  20. data/test/framework/copies/new_unseen.rb +7 -0
  21. data/test/framework/copies/original_klass.rb +10 -0
  22. data/test/framework/ext/extensions/blog.rb +6 -0
  23. data/test/framework/ext/extensions.rb +4 -0
  24. data/test/framework/lib/group/klass2.rb +4 -0
  25. data/test/framework/lib/klass.rb +10 -0
  26. data/test/framework/other/also.rb +8 -0
  27. data/test/framework/other/ignore.rb +2 -0
  28. data/test/framework/other/load.rb +2 -0
  29. data/test/framework/other/two.rb +14 -0
  30. data/test/framework/private/klass.rb +10 -0
  31. data/test/framework/test.components +23 -0
  32. data/test/helper.rb +10 -0
  33. data/test/test_autoload.rb +32 -0
  34. data/test/test_component.rb +133 -0
  35. data/test/test_component_set.rb +20 -0
  36. data/test/test_folder.rb +4 -0
  37. data/test/test_impromptu.rb +43 -0
  38. data/test/test_integration.rb +312 -0
  39. data/test/test_ordered_set.rb +93 -0
  40. data/test/test_resource.rb +186 -0
  41. data/test/test_symbol.rb +99 -0
  42. metadata +139 -0
@@ -0,0 +1,312 @@
1
+ require 'helper'
2
+ require 'yaml'
3
+
4
+ class TestIntegration < Test::Unit::TestCase
5
+ context "Loading the test.components file" do
6
+ setup do
7
+ Impromptu.reset
8
+ Impromptu.define_components do
9
+ parse_file 'test/framework/test.components'
10
+ end
11
+ end
12
+
13
+ # ----------------------------------------
14
+ # Loading component definitions
15
+ # ----------------------------------------
16
+ should "01 create four components" do
17
+ assert_equal 4, Impromptu.components.size
18
+ end
19
+
20
+ should "02 have a single folder per component" do
21
+ assert_equal 1, Impromptu.components['framework'].folders.size
22
+ assert_equal 1, Impromptu.components['framework.extensions'].folders.size
23
+ assert_equal 1, Impromptu.components['other'].folders.size
24
+ end
25
+
26
+ should "03 have a single require in the framework component" do
27
+ assert_equal 1, Impromptu.components['framework'].requirements.size
28
+ end
29
+
30
+ should "04 have a namespace for the framework components" do
31
+ assert_equal :Framework, Impromptu.components['framework'].namespace
32
+ assert_equal :Framework, Impromptu.components['framework.extensions'].namespace
33
+ assert_equal nil, Impromptu.components['other'].namespace
34
+ end
35
+
36
+ should "05 start tracking 8 files" do
37
+ assert_equal 2, Impromptu.components['framework'].folders.first.files.size
38
+ assert_equal 2, Impromptu.components['framework.extensions'].folders.first.files.size
39
+ assert_equal 3, Impromptu.components['other'].folders.first.files.size
40
+ assert_equal 1, Impromptu.components['private'].folders.first.files.size
41
+ end
42
+
43
+ should "06 load definitions for 9 resources" do
44
+ assert Impromptu.root_resource.child?(:Framework)
45
+ assert Impromptu.root_resource.child(:Framework).child?(:Extensions)
46
+ assert Impromptu.root_resource.child(:Framework).child(:Extensions).child?(:Blog)
47
+ assert Impromptu.root_resource.child(:Framework).child?(:Klass)
48
+ assert Impromptu.root_resource.child(:Framework).child?(:Klass2)
49
+ assert Impromptu.root_resource.child?(:Load)
50
+ assert Impromptu.root_resource.child?(:OtherName)
51
+ assert Impromptu.root_resource.child?(:ModOne)
52
+ assert Impromptu.root_resource.child?(:ModTwo)
53
+ end
54
+
55
+ should "07 correctly mark namespace resources" do
56
+ assert_equal true, Impromptu.root_resource.child(:Framework).namespace?
57
+ assert_equal false, Impromptu.root_resource.child(:Framework).child(:Extensions).namespace?
58
+ assert_equal false, Impromptu.root_resource.child(:Framework).child(:Extensions).child(:Blog).namespace?
59
+ assert_equal false, Impromptu.root_resource.child(:Framework).child(:Klass).namespace?
60
+ assert_equal false, Impromptu.root_resource.child(:Framework).child(:Klass2).namespace?
61
+ assert_equal false, Impromptu.root_resource.child(:Load).namespace?
62
+ assert_equal false, Impromptu.root_resource.child(:OtherName).namespace?
63
+ assert_equal false, Impromptu.root_resource.child(:ModOne).namespace?
64
+ assert_equal false, Impromptu.root_resource.child(:ModTwo).namespace?
65
+ end
66
+
67
+ should "08 keep all resources unloaded to start with" do
68
+ assert_equal false, Impromptu.root_resource.child(:'Framework').loaded?
69
+ assert_equal false, Impromptu.root_resource.child(:'Framework::Extensions').loaded?
70
+ assert_equal false, Impromptu.root_resource.child(:'Framework::Extensions::Blog').loaded?
71
+ assert_equal false, Impromptu.root_resource.child(:'Framework::Klass').loaded?
72
+ assert_equal false, Impromptu.root_resource.child(:'Framework::Klass2').loaded?
73
+ assert_equal false, Impromptu.root_resource.child(:'Load').loaded?
74
+ assert_equal false, Impromptu.root_resource.child(:'OtherName').loaded?
75
+ assert_equal false, Impromptu.root_resource.child(:'ModOne').loaded?
76
+ assert_equal false, Impromptu.root_resource.child(:'ModTwo').loaded?
77
+ end
78
+
79
+ should "09 have all resources specified by the correct number of files" do
80
+ assert_equal 0, Impromptu.root_resource.child(:'Framework').files.size
81
+ assert_equal 1, Impromptu.root_resource.child(:'Framework::Extensions').files.size
82
+ assert_equal 1, Impromptu.root_resource.child(:'Framework::Extensions::Blog').files.size
83
+ assert_equal 2, Impromptu.root_resource.child(:'Framework::Klass').files.size
84
+ assert_equal 1, Impromptu.root_resource.child(:'Framework::Klass2').files.size
85
+ assert_equal 1, Impromptu.root_resource.child(:'Framework::Klass2').files.size
86
+ assert_equal 1, Impromptu.root_resource.child(:'Load').files.size
87
+ assert_equal 2, Impromptu.root_resource.child(:'OtherName').files.size
88
+ assert_equal 1, Impromptu.root_resource.child(:'ModOne').files.size
89
+ assert_equal 1, Impromptu.root_resource.child(:'ModTwo').files.size
90
+ assert_equal true, Impromptu.root_resource.child(:'Framework').implicitly_defined?
91
+ end
92
+
93
+
94
+ # ----------------------------------------
95
+ # Loading/unloading
96
+ # ----------------------------------------
97
+ should "10 allow loading the implicitly defined framework module" do
98
+ assert_equal false, Impromptu.root_resource.child(:'Framework').loaded?
99
+ Impromptu.root_resource.child(:'Framework').reload
100
+ assert_equal true, Impromptu.root_resource.child(:'Framework').loaded?
101
+ assert_nothing_raised do
102
+ Framework
103
+ end
104
+ end
105
+
106
+ should "11 load resources using associated files when required" do
107
+ # ext
108
+ Impromptu.root_resource.child(:'Framework::Extensions::Blog').reload
109
+ assert_equal true, Impromptu.root_resource.child(:'Framework::Extensions').loaded?
110
+ assert_equal true, Impromptu.root_resource.child(:'Framework::Extensions::Blog').loaded?
111
+ assert_nothing_raised do
112
+ Framework::Extensions
113
+ end
114
+ assert_nothing_raised do
115
+ Framework::Extensions::Blog
116
+ end
117
+
118
+ # lib
119
+ Impromptu.root_resource.child(:'Framework::Klass').reload
120
+ Impromptu.root_resource.child(:'Framework::Klass2').reload
121
+ assert_equal true, Impromptu.root_resource.child(:'Framework::Klass').loaded?
122
+ assert_equal true, Impromptu.root_resource.child(:'Framework::Klass2').loaded?
123
+ assert_nothing_raised do
124
+ Framework::Klass
125
+ end
126
+ assert_nothing_raised do
127
+ Framework::Klass2
128
+ end
129
+
130
+ # other
131
+ Impromptu.root_resource.child(:Load).reload
132
+ Impromptu.root_resource.child(:OtherName).reload
133
+ Impromptu.root_resource.child(:ModOne).reload
134
+ Impromptu.root_resource.child(:ModTwo).reload
135
+ assert_equal true, Impromptu.root_resource.child(:Load).loaded?
136
+ assert_equal true, Impromptu.root_resource.child(:OtherName).loaded?
137
+ assert_equal true, Impromptu.root_resource.child(:ModOne).loaded?
138
+ assert_equal true, Impromptu.root_resource.child(:ModTwo).loaded?
139
+ assert_nothing_raised do
140
+ Load
141
+ end
142
+ assert_nothing_raised do
143
+ OtherName
144
+ end
145
+ assert_nothing_raised do
146
+ ModOne
147
+ end
148
+ assert_nothing_raised do
149
+ ModTwo
150
+ end
151
+ end
152
+
153
+ should "12 load multiple files for a resource when required" do
154
+ # Klass
155
+ Impromptu.root_resource.child(:'Framework::Klass').reload
156
+ assert_respond_to Framework::Klass, :standard_method
157
+ assert_respond_to Framework::Klass, :overriden_method
158
+ assert_respond_to Framework::Klass, :extension_method
159
+ assert_equal 2, Framework::Klass.overriden_method
160
+
161
+ # OtherName
162
+ Impromptu.root_resource.child(:OtherName).reload
163
+ assert_respond_to OtherName, :one
164
+ assert_respond_to OtherName, :overriden_method
165
+ assert_respond_to OtherName, :two
166
+ assert_equal 4, OtherName.overriden_method
167
+ end
168
+
169
+ should "13 be able to unload implicit and explicit resources" do
170
+ # explicit
171
+ Impromptu.root_resource.child(:'Framework::Extensions::Blog').reload
172
+ assert_equal true, Impromptu.root_resource.child(:'Framework::Extensions').loaded?
173
+ assert_equal true, Impromptu.root_resource.child(:'Framework::Extensions::Blog').loaded?
174
+ Impromptu.root_resource.child(:'Framework::Extensions').unload
175
+ assert_equal false, Impromptu.root_resource.child(:'Framework::Extensions').loaded?
176
+ assert_equal false, Impromptu.root_resource.child(:'Framework::Extensions::Blog').loaded?
177
+
178
+ # implicit
179
+ Impromptu.root_resource.child(:'Framework').reload
180
+ assert_equal true, Impromptu.root_resource.child(:'Framework').loaded?
181
+ Impromptu.root_resource.child(:'Framework').unload
182
+ assert_equal false, Impromptu.root_resource.child(:'Framework').loaded?
183
+ end
184
+
185
+ should "14 be able to reload previously unloaded resources" do
186
+ # implicit
187
+ Impromptu.root_resource.child(:'Framework').reload
188
+ Impromptu.root_resource.child(:'Framework').unload
189
+ assert_equal false, Impromptu.root_resource.child(:'Framework').loaded?
190
+ Impromptu.root_resource.child(:'Framework').reload
191
+ assert_equal true, Impromptu.root_resource.child(:'Framework').loaded?
192
+
193
+ # explicit
194
+ Impromptu.root_resource.child(:'Framework::Extensions::Blog').reload
195
+ Impromptu.root_resource.child(:'Framework::Extensions').unload
196
+ assert_equal false, Impromptu.root_resource.child(:'Framework::Extensions').loaded?
197
+ assert_equal false, Impromptu.root_resource.child(:'Framework::Extensions::Blog').loaded?
198
+ Impromptu.root_resource.child(:'Framework::Extensions::Blog').reload
199
+ assert_equal true, Impromptu.root_resource.child(:'Framework::Extensions').loaded?
200
+ assert_equal true, Impromptu.root_resource.child(:'Framework::Extensions::Blog').loaded?
201
+ end
202
+
203
+
204
+ # ----------------------------------------
205
+ # Updating files
206
+ # ----------------------------------------
207
+ context "and changing the definition of klass" do
208
+ setup do
209
+ Impromptu.root_resource.child(:'Framework::Klass').reload
210
+ new_klass = File.open('test/framework/copies/new_klass.rb').read
211
+ File.open('test/framework/lib/klass.rb', 'w') do |file|
212
+ file.write new_klass
213
+ end
214
+ end
215
+
216
+ teardown do
217
+ old_klass = File.open('test/framework/copies/original_klass.rb').read
218
+ File.open('test/framework/lib/klass.rb', 'w') do |file|
219
+ file.write old_klass
220
+ end
221
+ end
222
+
223
+ should "reload a class definition correctly when a file is changed" do
224
+ # update impromptu and test the new klass is loaded
225
+ assert_respond_to Framework::Klass, :standard_method
226
+ assert_equal 2, Framework::Klass.overriden_method
227
+ Impromptu.update
228
+ assert_respond_to Framework::Klass, :new_method
229
+ assert_equal false, Framework::Klass.respond_to?(:standard_method)
230
+ assert_equal 2, Framework::Klass.overriden_method
231
+ end
232
+ end
233
+
234
+
235
+ # ----------------------------------------
236
+ # Adding files
237
+ # ----------------------------------------
238
+ context "and adding a new file to a reloadable folder" do
239
+ setup do
240
+ FileUtils.mv 'test/framework/copies/new_unseen.rb', 'test/framework/private/unseen.rb'
241
+ end
242
+
243
+ teardown do
244
+ FileUtils.mv 'test/framework/private/unseen.rb', 'test/framework/copies/new_unseen.rb'
245
+ end
246
+
247
+ should "make the resource from the new file available for loading" do
248
+ assert_equal false, Impromptu.root_resource.child?(:'Framework::Unseen')
249
+ Impromptu.update
250
+ assert_equal true, Impromptu.root_resource.child?(:'Framework::Unseen')
251
+ Impromptu.root_resource.child(:'Framework::Unseen').reload
252
+ assert Framework::Unseen.test_method
253
+ end
254
+ end
255
+
256
+
257
+ # ----------------------------------------
258
+ # Removing files
259
+ # ----------------------------------------
260
+ context "and removing a file from a reloadable folder" do
261
+ teardown do
262
+ FileUtils.mv 'test/framework/copies/klass2.rb', 'test/framework/lib/group/klass2.rb'
263
+ end
264
+
265
+ should "make the resource from the old unavailable since its definition has been removed" do
266
+ # ensure the resource exists and load it
267
+ assert_equal true, Impromptu.root_resource.child?(:'Framework::Klass2')
268
+ Impromptu.root_resource.child(:'Framework::Klass2').reload
269
+ assert_nothing_raised do
270
+ Framework::Klass2
271
+ end
272
+
273
+ # update and see the missing file
274
+ FileUtils.mv 'test/framework/lib/group/klass2.rb', 'test/framework/copies/klass2.rb'
275
+ Impromptu.update
276
+ assert_equal false, Impromptu.root_resource.child?(:'Framework::Klass2')
277
+ assert_raise NameError do
278
+ Framework::Klass2
279
+ end
280
+ end
281
+ end
282
+
283
+
284
+ # ----------------------------------------
285
+ # Adding a file to a previously defined
286
+ # resource
287
+ # ----------------------------------------
288
+ context "and adding a file to a previously defined resource" do
289
+ setup do
290
+ Impromptu.root_resource.child(:'Framework::Klass2').reload
291
+ FileUtils.mv 'test/framework/copies/extra_klass2.rb', 'test/framework/private/klass2.rb'
292
+ end
293
+
294
+ teardown do
295
+ FileUtils.mv 'test/framework/private/klass2.rb', 'test/framework/copies/extra_klass2.rb'
296
+ end
297
+
298
+ should "extend the definition and the list of files implementing the resource" do
299
+ # existing definition
300
+ assert Impromptu.root_resource.child?(:'Framework::Klass2')
301
+ assert_equal 1, Impromptu.root_resource.child(:'Framework::Klass2').files.size
302
+ assert_equal false, Framework::Klass2.respond_to?(:new_method)
303
+
304
+ # extend with new definition
305
+ Impromptu.update
306
+ assert_equal 2, Impromptu.root_resource.child(:'Framework::Klass2').files.size
307
+ assert Framework::Klass2.respond_to?(:new_method)
308
+ assert Framework::Klass2.new_method
309
+ end
310
+ end
311
+ end
312
+ end
@@ -0,0 +1,93 @@
1
+ require 'helper'
2
+
3
+ class TestOrderedSet < Test::Unit::TestCase
4
+ context "An ordered set" do
5
+ setup { @set = Impromptu::OrderedSet.new }
6
+
7
+ # ----------------------------------------
8
+ # API
9
+ # ----------------------------------------
10
+ should "respond to push" do
11
+ assert_respond_to @set, :push
12
+ end
13
+
14
+ should "respond to <<" do
15
+ assert_respond_to @set, :<<
16
+ end
17
+
18
+ should "respond to merge" do
19
+ assert_respond_to @set, :merge
20
+ end
21
+
22
+ should "respond to delete" do
23
+ assert_respond_to @set, :delete
24
+ end
25
+
26
+ should "respond to to_a" do
27
+ assert_respond_to @set, :to_a
28
+ end
29
+
30
+ should "respond to each" do
31
+ assert_respond_to @set, :each
32
+ end
33
+
34
+ should "respond to size" do
35
+ assert_respond_to @set, :size
36
+ end
37
+
38
+ should "respond to empty?" do
39
+ assert_respond_to @set, :empty?
40
+ end
41
+
42
+ # ----------------------------------------
43
+ # Core usage
44
+ # ----------------------------------------
45
+ should "be able to have items added" do
46
+ assert_equal 1, @set << 1
47
+ assert_equal 2, @set.push(2)
48
+ assert_equal 2, @set.size
49
+ end
50
+
51
+ should "only allow an item to be added once" do
52
+ assert_equal 1, @set << 1
53
+ assert_equal 1, @set.push(1)
54
+ assert_equal 1, @set.size
55
+ end
56
+
57
+ should "store items in order" do
58
+ @set << 1
59
+ @set << 2
60
+ assert_equal [1,2], @set.to_a
61
+ end
62
+
63
+ should "be able to delete items" do
64
+ @set << 1
65
+ assert_equal 1, @set.size
66
+ @set.delete(1)
67
+ assert_equal 0, @set.size
68
+ assert_equal true, @set.empty?
69
+ end
70
+
71
+ should "correctly implement empty?" do
72
+ assert_equal true, @set.empty?
73
+ @set << 1
74
+ assert_equal false, @set.empty?
75
+ end
76
+
77
+ should "correctly implement include?" do
78
+ @set << 1
79
+ assert_equal true, @set.include?(1)
80
+ assert_equal false, @set.include?(2)
81
+ end
82
+
83
+ should "be able to be iterated over, in order of insertion" do
84
+ items = []
85
+ @set << 1
86
+ @set << 2
87
+ @set.each do |item|
88
+ items << item
89
+ end
90
+ assert_equal [1,2], items
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,186 @@
1
+ require 'helper'
2
+
3
+ class TestResource < Test::Unit::TestCase
4
+ # ----------------------------------------
5
+ # Root resource and API
6
+ # ----------------------------------------
7
+ context "The root resource" do
8
+ should "exist" do
9
+ assert_not_nil Impromptu.root_resource
10
+ end
11
+
12
+ should "respond to children?" do
13
+ assert_respond_to Impromptu.root_resource, :children?
14
+ end
15
+
16
+ should "respond to child?" do
17
+ assert_respond_to Impromptu.root_resource, :child?
18
+ end
19
+
20
+ should "respond to child" do
21
+ assert_respond_to Impromptu.root_resource, :child
22
+ end
23
+
24
+ should "respond to reference" do
25
+ assert_respond_to Impromptu.root_resource, :reference
26
+ end
27
+
28
+ should "respond to get_or_create_child" do
29
+ assert_respond_to Impromptu.root_resource, :get_or_create_child
30
+ end
31
+
32
+ should "respond to namespace?" do
33
+ assert_respond_to Impromptu.root_resource, :namespace?
34
+ end
35
+
36
+ should "be a root resource" do
37
+ assert Impromptu.root_resource.root?
38
+ end
39
+
40
+ should "reference Object" do
41
+ assert_equal Object, Impromptu.root_resource.reference
42
+ end
43
+ end
44
+
45
+ # ----------------------------------------
46
+ # Hash and equality
47
+ # ----------------------------------------
48
+ context "Two resources" do
49
+ setup { @one = Impromptu::Resource.new(:Object, nil) }
50
+ context "which refer to the same symbol" do
51
+ setup { @two = Impromptu::Resource.new(:Object, nil) }
52
+ should "be equal" do
53
+ assert @one.eql?(@two)
54
+ assert @two.eql?(@one)
55
+ end
56
+
57
+ should "have equal hashes" do
58
+ assert @one.hash == @two.hash
59
+ end
60
+ end
61
+
62
+ context "which refer to different symbols" do
63
+ setup { @two = Impromptu::Resource.new(:Test, @one) }
64
+ should "not be equal" do
65
+ assert_equal false, @one.eql?(@two)
66
+ assert_equal false, @two.eql?(@one)
67
+ end
68
+
69
+ should "have different hashes if their symbols have different hashes" do
70
+ assert_equal false, @one.hash == @two.hash
71
+ end
72
+ end
73
+ end
74
+
75
+ # ----------------------------------------
76
+ # Creating and retrieving resources
77
+ # ----------------------------------------
78
+ context "A root resource" do
79
+ setup { @root = Impromptu::Resource.new(:Object, nil) }
80
+ context "with a child resource" do
81
+ setup { @child = @root.get_or_create_child(:Klass) }
82
+ should "have children" do
83
+ assert @root.children?
84
+ end
85
+
86
+ should "have a child with the name just created" do
87
+ assert @root.child?(:Klass)
88
+ end
89
+
90
+ should "be able to retrieve a reference to the child resource" do
91
+ assert_not_nil @root.child(:Klass)
92
+ assert_instance_of Impromptu::Resource, @root.child(:Klass)
93
+ end
94
+ end
95
+
96
+ context "with a deep chain of child resources" do
97
+ setup { @base = @root.get_or_create_child(:'One::Two::Three::Four') }
98
+ should "have children" do
99
+ assert @root.children?
100
+ end
101
+
102
+ should "be able to retrieve a reference to each resource just created" do
103
+ # One
104
+ assert_not_nil @root.child(:'One')
105
+ assert_instance_of Impromptu::Resource, @root.child(:'One')
106
+
107
+ # One::Two
108
+ assert_not_nil @root.child(:'One::Two')
109
+ assert_instance_of Impromptu::Resource, @root.child(:'One::Two')
110
+
111
+ # One::Two::Three
112
+ assert_not_nil @root.child(:'One::Two::Three')
113
+ assert_instance_of Impromptu::Resource, @root.child(:'One::Two::Three')
114
+
115
+ # One::Two::Three::Four
116
+ assert_not_nil @root.child(:'One::Two::Three::Four')
117
+ assert_instance_of Impromptu::Resource, @root.child(:'One::Two::Three::Four')
118
+ end
119
+ end
120
+ end
121
+
122
+ # ----------------------------------------
123
+ # Adding/removing references
124
+ # ----------------------------------------
125
+ context "A new blank resource" do
126
+ setup { @resource = Impromptu::Resource.new(:Object, nil) }
127
+ should "have no files" do
128
+ assert_equal 0, @resource.files.size
129
+ end
130
+
131
+ should "have no children" do
132
+ assert_equal 0, @resource.children.size
133
+ end
134
+
135
+ should "be implicitly defined" do
136
+ assert @resource.implicitly_defined?
137
+ end
138
+
139
+ context "with an added child" do
140
+ setup do
141
+ @child = Impromptu::Resource.new(:Test, @resource)
142
+ @resource.add_child(@child)
143
+ end
144
+
145
+ should "have an added child" do
146
+ assert_equal 1, @resource.children.size
147
+ end
148
+
149
+ should "be able to remove the child" do
150
+ @resource.remove_child(@child)
151
+ assert_equal 0, @resource.children.size
152
+ end
153
+ end
154
+
155
+ context "with an added file" do
156
+ setup do
157
+ @file = Impromptu::File.new(nil, nil, [:blank])
158
+ @resource.add_file(@file)
159
+ end
160
+
161
+ should "have an added file" do
162
+ assert_equal 1, @resource.files.size
163
+ end
164
+
165
+ should "not be implicitly defined" do
166
+ assert_equal false, @resource.implicitly_defined?
167
+ end
168
+
169
+ should "be able to remove the file" do
170
+ @resource.remove_file(@file)
171
+ assert_equal 0, @resource.files.size
172
+ end
173
+
174
+ should "not be implicitly defined after all files are removed" do
175
+ @resource.remove_file(@file)
176
+ assert_equal false, @resource.implicitly_defined?
177
+ end
178
+
179
+ should "be implicitly defined after all files are removed if it is a namespace" do
180
+ @resource.namespace!
181
+ @resource.remove_file(@file)
182
+ assert_equal true, @resource.implicitly_defined?
183
+ end
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,99 @@
1
+ require 'helper'
2
+
3
+ class TestResource < Test::Unit::TestCase
4
+ context "A symbol" do
5
+ setup { @symbol = :ASymbolWhichDoesntNormallyExist }
6
+
7
+ # ----------------------------------------
8
+ # API
9
+ # ----------------------------------------
10
+ should "respond to nested?" do
11
+ assert_respond_to @symbol, :nested?
12
+ end
13
+
14
+ should "respond to unnested?" do
15
+ assert_respond_to @symbol, :unnested?
16
+ end
17
+
18
+ should "respond to nested_symbols" do
19
+ assert_respond_to @symbol, :nested_symbols
20
+ end
21
+
22
+ should "respond to base_symbol" do
23
+ assert_respond_to @symbol, :base_symbol
24
+ end
25
+
26
+ should "respond to root_symbol" do
27
+ assert_respond_to @symbol, :root_symbol
28
+ end
29
+
30
+ should "respond to each_namespaced_symbol" do
31
+ assert_respond_to @symbol, :each_namespaced_symbol
32
+ end
33
+
34
+ # ----------------------------------------
35
+ # Unnested symbol
36
+ # ----------------------------------------
37
+ context "which isn't nested" do
38
+ should "return false for nested?" do
39
+ assert_equal false, @symbol.nested?
40
+ end
41
+
42
+ should "return true for unnested?" do
43
+ assert @symbol.unnested?
44
+ end
45
+
46
+ should "return an array of a single symbol from nested_symbols" do
47
+ assert_instance_of Array, @symbol.nested_symbols
48
+ assert_equal 1, @symbol.nested_symbols.size
49
+ assert_instance_of Symbol, @symbol.nested_symbols.first
50
+ end
51
+
52
+ should "return itself for base_symbol" do
53
+ assert_equal @symbol, @symbol.base_symbol
54
+ end
55
+
56
+ should "return itself for root_symbol" do
57
+ assert_equal @symbol, @symbol.root_symbol
58
+ end
59
+ end
60
+
61
+ # ----------------------------------------
62
+ # Nested symbol
63
+ # ----------------------------------------
64
+ context "which is nested" do
65
+ setup { @symbol = :'TopLevel::BottomLevel' }
66
+ should "return true for nested?" do
67
+ assert @symbol.nested?
68
+ end
69
+
70
+ should "return false for unnested?" do
71
+ assert_equal false, @symbol.unnested?
72
+ end
73
+
74
+ should "return an array of multiple symbols from nested_symbols" do
75
+ assert_instance_of Array, @symbol.nested_symbols
76
+ assert_equal 2, @symbol.nested_symbols.size
77
+ assert_instance_of Symbol, @symbol.nested_symbols[0]
78
+ assert_instance_of Symbol, @symbol.nested_symbols[1]
79
+ end
80
+
81
+ should "return the first nested name for root_symbol" do
82
+ assert_equal :TopLevel, @symbol.root_symbol
83
+ end
84
+
85
+ should "return the last nested name for base_symbol" do
86
+ assert_equal :BottomLevel, @symbol.base_symbol
87
+ end
88
+
89
+ should "return consecutive namespaced symbols when calling each_namespaced_symbol" do
90
+ symbols = []
91
+ @symbol.each_namespaced_symbol do |symbol|
92
+ symbols << symbol
93
+ end
94
+
95
+ assert_equal [:TopLevel, :'TopLevel::BottomLevel'], symbols
96
+ end
97
+ end
98
+ end
99
+ end