autoloaded 1.6.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +8 -4
  3. data/.rspec +1 -1
  4. data/.travis.yml +1 -14
  5. data/Gemfile +10 -11
  6. data/Guardfile +22 -21
  7. data/History.md +12 -27
  8. data/README.md +48 -58
  9. data/Rakefile +0 -4
  10. data/autoloaded.gemspec +37 -41
  11. data/lib/autoloaded.rb +0 -40
  12. data/lib/autoloaded/autoloader.rb +8 -31
  13. data/lib/autoloaded/deprecation.rb +20 -11
  14. data/lib/autoloaded/inflection.rb +9 -7
  15. data/lib/autoloaded/load_pathed_directory.rb +4 -2
  16. data/lib/autoloaded/specification.rb +0 -2
  17. data/lib/autoloaded/specifications.rb +10 -16
  18. data/lib/autoloaded/version.rb +1 -1
  19. data/lib/autoloaded/warning.rb +44 -26
  20. data/lib/tasks/lib_each.rake +3 -15
  21. data/lib/tasks/spec.rake +6 -3
  22. data/spec/autoloaded/autoloader_spec.rb +469 -0
  23. data/spec/autoloaded/inflection_spec.rb +30 -0
  24. data/spec/autoloaded/load_pathed_directory_spec.rb +120 -0
  25. data/spec/autoloaded/specification_spec.rb +98 -0
  26. data/spec/autoloaded/specifications_spec.rb +191 -0
  27. data/spec/autoloaded/version_spec.rb +3 -0
  28. data/spec/autoloaded/warning_spec.rb +115 -0
  29. data/spec/autoloaded_macro_sharedspec.rb +24 -0
  30. data/spec/autoloaded_spec.rb +173 -0
  31. data/spec/fixtures/autoloaded_with_conventional_filename.rb +12 -0
  32. data/spec/fixtures/autoloaded_with_conventional_filename/N-est-ed.rb +1 -0
  33. data/spec/fixtures/autoloaded_with_conventional_filename/nest_ed.rb +1 -0
  34. data/spec/fixtures/autoloaded_with_conventional_filename/nested.rb +16 -0
  35. data/spec/fixtures/autoloaded_with_conventional_filename/nested/doubly_nested.rb +9 -0
  36. data/spec/fixtures/autoloaded_with_conventional_filename/old_school_autoload.rb +5 -0
  37. data/spec/fixtures/autoloaded_with_unconventional_filename.rb +12 -0
  38. data/spec/fixtures/autoloaded_with_unconventional_filename/N-est-ed.rb +7 -0
  39. data/spec/fixtures/autoloaded_with_unconventional_filename/nest_ed.rb +1 -0
  40. data/spec/fixtures/autoloaded_with_unconventional_filename/old_school_autoload.rb +5 -0
  41. data/spec/fixtures/filenames/AFilename.rb +0 -0
  42. data/spec/fixtures/filenames/a-file-name.rb +0 -0
  43. data/spec/fixtures/filenames/a-filename.rb +0 -0
  44. data/spec/fixtures/filenames/a_file_name.rb +0 -0
  45. data/spec/fixtures/filenames/a_filename.rb +0 -0
  46. data/spec/fixtures/filenames/afile-name.rb +0 -0
  47. data/spec/fixtures/filenames/afile_name.rb +0 -0
  48. data/spec/fixtures/not_autoloaded.rb +5 -0
  49. data/spec/fixtures/not_autoloaded/nested.rb +1 -0
  50. data/spec/fixtures/not_autoloaded/old_school_autoload.rb +5 -0
  51. data/spec/matchers.rb +85 -0
  52. data/spec/spec_helper.rb +91 -0
  53. data/spec/support/util.rb +42 -0
  54. data/spec/support/without_side_effects.rb +37 -0
  55. metadata +86 -22
  56. data/bin/console +0 -10
  57. data/bin/setup +0 -8
  58. data/lib/autoloaded/compatibility/refine_and_using.rb +0 -19
  59. data/lib/autoloaded/constant.rb +0 -94
  60. data/lib/autoloaded/refine.rb +0 -16
  61. data/lib/autoloaded/refine/string.rb +0 -20
  62. data/lib/autoloaded/refine/string/to_source_filename.rb +0 -58
@@ -0,0 +1,469 @@
1
+ RSpec.describe Autoloaded::Autoloader do
2
+ subject(:autoloader) { autoloader_class.new host_binding }
3
+
4
+ before :each do
5
+ allow(specifications_class).to receive(:new).and_return(specifications)
6
+ allow(inflector_class).to receive(:to_constant_name).
7
+ and_return(:FromInflection1,
8
+ :FromInflection2,
9
+ :FromInflection3,
10
+ :FromInflection4)
11
+ end
12
+
13
+ let(:autoloader_class) { described_class }
14
+
15
+ let(:host_binding) { self.class.class_eval 'binding', __FILE__, __LINE__ }
16
+
17
+ let(:directory_class) { Autoloaded::LoadPathedDirectory }
18
+
19
+ let(:specifications) { specifications_class.new }
20
+
21
+ let(:specifications_class) { Autoloaded::Specifications }
22
+
23
+ let(:specification_class) { Autoloaded::Specification }
24
+
25
+ let(:inflector_class) { Autoloaded::Inflection }
26
+
27
+ describe '.new' do
28
+ describe 'with nil argument' do
29
+ specify {
30
+ expect { autoloader_class.new nil }.to raise_error(ArgumentError,
31
+ "can't be nil")
32
+ }
33
+ end
34
+ end
35
+
36
+ describe '#from' do
37
+ subject(:from) { autoloader.from }
38
+
39
+ describe 'has expected default value' do
40
+ specify { is_expected.to eq(__FILE__.gsub(/\.rb$/, '')) }
41
+ end
42
+
43
+ describe 'rejects relative path argument' do
44
+ specify {
45
+ expect {
46
+ autoloader.from 'a/relative/path'
47
+ }.to raise_error(ArgumentError, "can't be relative")
48
+ }
49
+ end
50
+
51
+ describe 'operates as attribute reader and writer' do
52
+ specify {
53
+ autoloader.from '/an/absolute/path'
54
+ is_expected.to eq('/an/absolute/path')
55
+ }
56
+ end
57
+ end
58
+
59
+ describe '#except' do
60
+ specify("delegates to #{Autoloaded::Specifications.name}#except") {
61
+ expect(specifications.except).to be_empty
62
+ autoloader.except 'foo'
63
+ expect(specifications.except).to contain_exactly(specification_class.new('foo'))
64
+ }
65
+
66
+ describe 'where #only is specified' do
67
+ before :each do
68
+ autoloader.only :Foo
69
+ end
70
+
71
+ specify {
72
+ expect { autoloader.except :Bar }.to raise_error(RuntimeError,
73
+ "can't specify `except' when `only' is already specified")
74
+ }
75
+ end
76
+ end
77
+
78
+ describe '#only' do
79
+ specify("delegates to #{Autoloaded::Specifications.name}#only") {
80
+ expect(specifications.only).to be_empty
81
+ autoloader.only 'foo'
82
+ expect(specifications.only).to contain_exactly(specification_class.new('foo'))
83
+ }
84
+
85
+ describe 'where #except is specified' do
86
+ before :each do
87
+ autoloader.except :Foo
88
+ end
89
+
90
+ specify {
91
+ expect { autoloader.only :Bar }.to raise_error(RuntimeError,
92
+ "can't specify `only' when `except' is already specified")
93
+ }
94
+ end
95
+ end
96
+
97
+ describe '#with' do
98
+ specify("delegates to #{Autoloaded::Specifications.name}#with") {
99
+ expect(specifications.with).to be_empty
100
+ autoloader.with 'foo'
101
+ expect(specifications.with).to contain_exactly(specification_class.new('foo'))
102
+ }
103
+ end
104
+
105
+ describe '#autoload!' do
106
+ before :each do
107
+ allow_any_instance_of(directory_class).to receive(:closest_ruby_load_path).
108
+ and_return('/foo')
109
+ allow(directory_class).to receive(:new).and_return(directory)
110
+ allow(directory).to receive(:each_source_filename)
111
+ end
112
+
113
+ let(:directory) { directory_class.new '/foo' }
114
+
115
+ describe 'where #from is' do
116
+ describe 'not specified' do
117
+ describe "initializes #{Autoloaded::LoadPathedDirectory.name} with computed value" do
118
+ specify {
119
+ expect(directory_class).to receive(:new).
120
+ with(__FILE__.gsub(/\.rb$/, '')).
121
+ and_return(directory)
122
+ autoloader.autoload!
123
+ }
124
+ end
125
+ end
126
+
127
+ describe 'specified' do
128
+ before :each do
129
+ autoloader.from directory.path
130
+ allow(specifications).to receive(:except).
131
+ and_return(except_specifications)
132
+ allow(specifications).to receive(:only).
133
+ and_return(only_specifications)
134
+ allow(specifications).to receive(:with).
135
+ and_return(with_specifications)
136
+ end
137
+
138
+ let(:except_specifications) { [] }
139
+
140
+ let(:except_specification) { specification_class.new }
141
+
142
+ let(:only_specifications) { [] }
143
+
144
+ let(:only_specification) { specification_class.new }
145
+
146
+ let(:with_specifications) { [] }
147
+
148
+ let(:with_specification) { specification_class.new }
149
+
150
+ describe 'where no source files are found' do
151
+ specify {
152
+ expect(Kernel).not_to receive(:eval)
153
+ autoloader.autoload!
154
+ }
155
+
156
+ specify { expect(autoloader.autoload!).to be_empty }
157
+ end
158
+
159
+ describe 'where a source file is found' do
160
+ before :each do
161
+ allow(directory).to receive(:each_source_filename).
162
+ and_yield('from_each_source_filename')
163
+ end
164
+
165
+ describe 'and no specifications are provided' do
166
+ specify {
167
+ expect(Kernel).to receive(:eval).
168
+ with('autoload? :FromInflection1',
169
+ # host_binding,
170
+ # kind_of(String),
171
+ # kind_of(Numeric)).
172
+ host_binding).
173
+ ordered
174
+ expect(Kernel).to receive(:eval).
175
+ with('constants.include? :FromInflection1',
176
+ # host_binding,
177
+ # kind_of(String),
178
+ # kind_of(Numeric)).
179
+ host_binding).
180
+ ordered
181
+ expect(Kernel).to receive(:eval).
182
+ with('autoload :FromInflection1, "from_each_source_filename"',
183
+ # host_binding,
184
+ # kind_of(String),
185
+ # kind_of(Numeric)).
186
+ host_binding).
187
+ ordered
188
+ autoloader.autoload!
189
+ }
190
+
191
+ specify {
192
+ expect(autoloader.autoload!).to eq([[:FromInflection1,
193
+ 'from_each_source_filename']])
194
+ }
195
+ end
196
+
197
+ describe "and a `with' specification is provided" do
198
+ before :each do
199
+ allow(with_specification).to receive(:match).and_return(:FromWith)
200
+ end
201
+
202
+ let(:with_specifications) { [with_specification] }
203
+
204
+ specify {
205
+ expect(Kernel).to receive(:eval).
206
+ with('autoload? :FromWith',
207
+ # host_binding,
208
+ # kind_of(String),
209
+ # kind_of(Numeric)).
210
+ host_binding).
211
+ ordered
212
+ expect(Kernel).to receive(:eval).
213
+ with('constants.include? :FromWith',
214
+ # host_binding,
215
+ # kind_of(String),
216
+ # kind_of(Numeric)).
217
+ host_binding).
218
+ ordered
219
+ expect(Kernel).to receive(:eval).
220
+ with('autoload :FromWith, "from_each_source_filename"',
221
+ # host_binding,
222
+ # kind_of(String),
223
+ # kind_of(Numeric))
224
+ host_binding).
225
+ ordered
226
+ autoloader.autoload!
227
+ }
228
+
229
+ specify {
230
+ expect(autoloader.autoload!).to eq([[:FromWith,
231
+ 'from_each_source_filename']])
232
+ }
233
+ end
234
+
235
+ describe "and an `only' specification is provided" do
236
+ before :each do
237
+ allow(only_specification).to receive(:match).and_return(:FromOnly)
238
+ end
239
+
240
+ let(:only_specifications) { [only_specification] }
241
+
242
+ specify {
243
+ expect(Kernel).to receive(:eval).
244
+ with('autoload? :FromOnly',
245
+ # host_binding,
246
+ # kind_of(String),
247
+ # kind_of(Numeric)).
248
+ host_binding).
249
+ ordered
250
+ expect(Kernel).to receive(:eval).
251
+ with('constants.include? :FromOnly',
252
+ # host_binding,
253
+ # kind_of(String),
254
+ # kind_of(Numeric)).
255
+ host_binding).
256
+ ordered
257
+ expect(Kernel).to receive(:eval).
258
+ with('autoload :FromOnly, "from_each_source_filename"',
259
+ # host_binding,
260
+ # kind_of(String),
261
+ # kind_of(Numeric)).
262
+ host_binding).
263
+ ordered
264
+ autoloader.autoload!
265
+ }
266
+
267
+ specify { expect(autoloader.autoload!).to eq([[:FromOnly,
268
+ 'from_each_source_filename']]) }
269
+ end
270
+
271
+ describe "and both `with' and `only' specifications are provided" do
272
+ before :each do
273
+ allow(with_specification).to receive(:match).and_return(:FromWith)
274
+ allow(only_specification).to receive(:match).and_return(:FromOnly)
275
+ end
276
+
277
+ let(:with_specifications) { [with_specification] }
278
+
279
+ let(:only_specifications) { [only_specification] }
280
+
281
+ specify {
282
+ expect(Kernel).to receive(:eval).
283
+ with('autoload? :FromWith',
284
+ # host_binding,
285
+ # kind_of(String),
286
+ # kind_of(Numeric)).
287
+ host_binding).
288
+ ordered
289
+ expect(Kernel).to receive(:eval).
290
+ with('constants.include? :FromWith',
291
+ # host_binding,
292
+ # kind_of(String),
293
+ # kind_of(Numeric)).
294
+ host_binding).
295
+ ordered
296
+ expect(Kernel).to receive(:eval).
297
+ with('autoload :FromWith, "from_each_source_filename"',
298
+ # host_binding,
299
+ # kind_of(String),
300
+ # kind_of(Numeric)).
301
+ host_binding).
302
+ ordered
303
+ autoloader.autoload!
304
+ }
305
+
306
+ specify { expect(autoloader.autoload!).to eq([[:FromWith,
307
+ 'from_each_source_filename']]) }
308
+ end
309
+ end
310
+
311
+ describe 'where multiple source files are found' do
312
+ before :each do
313
+ allow(directory).to receive(:each_source_filename).
314
+ and_yield('from_each_source_filename1').
315
+ and_yield('from_each_source_filename2').
316
+ and_yield('from_each_source_filename3')
317
+ end
318
+
319
+ describe 'and no specifications are provided' do
320
+ specify {
321
+ expect(Kernel).to receive(:eval).
322
+ with('autoload? :FromInflection1',
323
+ # host_binding,
324
+ # kind_of(String),
325
+ # kind_of(Numeric)).
326
+ host_binding).
327
+ ordered
328
+ expect(Kernel).to receive(:eval).
329
+ with('constants.include? :FromInflection1',
330
+ # host_binding,
331
+ # kind_of(String),
332
+ # kind_of(Numeric)).
333
+ host_binding).
334
+ ordered
335
+ expect(Kernel).to receive(:eval).
336
+ with('autoload :FromInflection1, "from_each_source_filename1"',
337
+ # host_binding,
338
+ # kind_of(String),
339
+ # kind_of(Numeric)).
340
+ host_binding).
341
+ ordered
342
+ expect(Kernel).to receive(:eval).
343
+ with('autoload? :FromInflection2',
344
+ # host_binding,
345
+ # kind_of(String),
346
+ # kind_of(Numeric)).
347
+ host_binding).
348
+ ordered
349
+ expect(Kernel).to receive(:eval).
350
+ with('constants.include? :FromInflection2',
351
+ # host_binding,
352
+ # kind_of(String),
353
+ # kind_of(Numeric)).
354
+ host_binding).
355
+ ordered
356
+ expect(Kernel).to receive(:eval).
357
+ with('autoload :FromInflection2, "from_each_source_filename2"',
358
+ # host_binding,
359
+ # kind_of(String),
360
+ # kind_of(Numeric)).
361
+ host_binding).
362
+ ordered
363
+ expect(Kernel).to receive(:eval).
364
+ with('autoload? :FromInflection3',
365
+ # host_binding,
366
+ # kind_of(String),
367
+ # kind_of(Numeric)).
368
+ host_binding).
369
+ ordered
370
+ expect(Kernel).to receive(:eval).
371
+ with('constants.include? :FromInflection3',
372
+ # host_binding,
373
+ # kind_of(String),
374
+ # kind_of(Numeric)).
375
+ host_binding).
376
+ ordered
377
+ expect(Kernel).to receive(:eval).
378
+ with('autoload :FromInflection3, "from_each_source_filename3"',
379
+ # host_binding,
380
+ # kind_of(String),
381
+ # kind_of(Numeric)).
382
+ host_binding).
383
+ ordered
384
+ autoloader.autoload!
385
+ }
386
+
387
+ specify {
388
+ expect(autoloader.autoload!).to eq([[:FromInflection1,
389
+ 'from_each_source_filename1'],
390
+ [:FromInflection2,
391
+ 'from_each_source_filename2'],
392
+ [:FromInflection3,
393
+ 'from_each_source_filename3']])
394
+ }
395
+ end
396
+
397
+ describe "and a matching `except' specification is provided" do
398
+ before :each do
399
+ allow(except_specification).to receive(:match).
400
+ with('from_each_source_filename1').
401
+ and_return(nil)
402
+ allow(except_specification).to receive(:match).
403
+ with('from_each_source_filename2').
404
+ and_return(:FromExcept)
405
+ allow(except_specification).to receive(:match).
406
+ with('from_each_source_filename3').
407
+ and_return(nil)
408
+ end
409
+
410
+ let(:except_specifications) { [except_specification] }
411
+
412
+ specify {
413
+ expect(Kernel).to receive(:eval).
414
+ with('autoload? :FromInflection1',
415
+ # host_binding,
416
+ # kind_of(String),
417
+ # kind_of(Numeric)).
418
+ host_binding).
419
+ ordered
420
+ expect(Kernel).to receive(:eval).
421
+ with('constants.include? :FromInflection1',
422
+ # host_binding,
423
+ # kind_of(String),
424
+ # kind_of(Numeric)).
425
+ host_binding).
426
+ ordered
427
+ expect(Kernel).to receive(:eval).
428
+ with('autoload :FromInflection1, "from_each_source_filename1"',
429
+ # host_binding,
430
+ # kind_of(String),
431
+ # kind_of(Numeric)).
432
+ host_binding).
433
+ ordered
434
+ expect(Kernel).to receive(:eval).
435
+ with('autoload? :FromInflection2',
436
+ # host_binding,
437
+ # kind_of(String),
438
+ # kind_of(Numeric)).
439
+ host_binding).
440
+ ordered
441
+ expect(Kernel).to receive(:eval).
442
+ with('constants.include? :FromInflection2',
443
+ # host_binding,
444
+ # kind_of(String),
445
+ # kind_of(Numeric)).
446
+ host_binding).
447
+ ordered
448
+ expect(Kernel).to receive(:eval).
449
+ with('autoload :FromInflection2, "from_each_source_filename3"',
450
+ # host_binding,
451
+ # kind_of(String),
452
+ # kind_of(Numeric)).
453
+ host_binding).
454
+ ordered
455
+ autoloader.autoload!
456
+ }
457
+
458
+ specify {
459
+ expect(autoloader.autoload!).to eq([[:FromInflection1,
460
+ 'from_each_source_filename1'],
461
+ [:FromInflection2,
462
+ 'from_each_source_filename3']])
463
+ }
464
+ end
465
+ end
466
+ end
467
+ end
468
+ end
469
+ end