ns-options 0.4.1 → 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +2 -1
- data/LICENSE +1 -1
- data/README.md +187 -301
- data/Rakefile +1 -1
- data/lib/ns-options/assert_macros.rb +9 -12
- data/lib/ns-options/boolean.rb +2 -0
- data/lib/ns-options/namespace.rb +34 -134
- data/lib/ns-options/namespace_advisor.rb +35 -0
- data/lib/ns-options/namespace_data.rb +166 -0
- data/lib/ns-options/namespaces.rb +23 -12
- data/lib/ns-options/option.rb +50 -24
- data/lib/ns-options/options.rb +23 -49
- data/lib/ns-options/proxy.rb +40 -53
- data/lib/ns-options/proxy_method.rb +54 -0
- data/lib/ns-options/root_methods.rb +77 -0
- data/lib/ns-options/version.rb +1 -1
- data/lib/ns-options.rb +18 -8
- data/ns-options.gemspec +3 -4
- data/test/helper.rb +3 -10
- data/test/support/app.rb +3 -1
- data/test/support/proxy.rb +4 -0
- data/test/support/type_class_proxy.rb +29 -0
- data/test/support/user.rb +5 -5
- data/test/{integration/app_test.rb → system/app_tests.rb} +8 -6
- data/test/{integration/proxy_test.rb → system/proxy_tests.rb} +12 -0
- data/test/system/type_class_proxy_tests.rb +108 -0
- data/test/system/user_tests.rb +146 -0
- data/test/unit/{ns-options/boolean_test.rb → boolean_tests.rb} +5 -4
- data/test/unit/namespace_advisor_tests.rb +69 -0
- data/test/unit/namespace_data_tests.rb +336 -0
- data/test/unit/namespace_tests.rb +205 -0
- data/test/unit/namespaces_tests.rb +99 -0
- data/test/unit/{ns-options/option_test.rb → option_tests.rb} +155 -93
- data/test/unit/options_tests.rb +152 -0
- data/test/unit/proxy_method_tests.rb +87 -0
- data/test/unit/{ns-options/proxy_test.rb → proxy_tests.rb} +52 -0
- data/test/unit/root_methods_tests.rb +126 -0
- metadata +58 -63
- data/lib/ns-options/errors/invalid_name.rb +0 -15
- data/lib/ns-options/has_options.rb +0 -53
- data/lib/ns-options/helper/advisor.rb +0 -88
- data/lib/ns-options/helper.rb +0 -87
- data/test/integration/user_test.rb +0 -94
- data/test/unit/ns-options/has_options_test.rb +0 -90
- data/test/unit/ns-options/helper/advisor_test.rb +0 -148
- data/test/unit/ns-options/helper_test.rb +0 -56
- data/test/unit/ns-options/namespace_test.rb +0 -432
- data/test/unit/ns-options/namespaces_test.rb +0 -55
- data/test/unit/ns-options/options_test.rb +0 -221
- /data/test/unit/{ns-options/assert_macros_test.rb → assert_macros_tests.rb} +0 -0
@@ -1,96 +1,142 @@
|
|
1
1
|
require 'assert'
|
2
|
+
require 'ns-options/option'
|
3
|
+
require 'ns-options/boolean'
|
2
4
|
|
3
5
|
class NsOptions::Option
|
4
6
|
|
5
|
-
class
|
7
|
+
class BaseTests < Assert::Context
|
6
8
|
desc "NsOptions::Option"
|
7
9
|
setup do
|
8
|
-
@rules
|
9
|
-
@
|
10
|
-
@option = NsOptions::Option.new(*@args)
|
10
|
+
@rules = { :default => "development" }
|
11
|
+
@option = NsOptions::Option.new(:stage, nil, @rules)
|
11
12
|
end
|
12
13
|
subject{ @option }
|
13
14
|
|
14
|
-
should
|
15
|
+
should have_class_methods :rules, :args
|
15
16
|
should have_accessors :name, :value, :type_class, :rules
|
17
|
+
should have_imeths :is_set?, :required?, :reset
|
16
18
|
|
17
|
-
should "
|
18
|
-
assert_equal
|
19
|
+
should "know its name" do
|
20
|
+
assert_equal :stage, subject.name
|
19
21
|
end
|
20
22
|
|
21
|
-
should "
|
22
|
-
assert_equal
|
23
|
+
should "know its type class" do
|
24
|
+
assert_equal Object, subject.type_class
|
23
25
|
end
|
24
26
|
|
25
|
-
should "
|
26
|
-
|
27
|
+
should "know its rules" do
|
28
|
+
exp_rules = {
|
29
|
+
:default => "development",
|
30
|
+
:args => []
|
31
|
+
}
|
32
|
+
assert_equal exp_rules, subject.rules
|
27
33
|
end
|
28
34
|
|
29
|
-
should "
|
30
|
-
assert_equal
|
35
|
+
should "not be required? by default" do
|
36
|
+
assert_equal false, subject.required?
|
31
37
|
end
|
32
38
|
|
33
|
-
|
34
|
-
|
39
|
+
end
|
40
|
+
|
41
|
+
class ParseArgsTests < BaseTests
|
42
|
+
desc "when parsing args"
|
43
|
+
setup do
|
44
|
+
@pname, @ptype_class, @prules = NsOptions::Option.args([:stage, String, @rules])
|
35
45
|
end
|
36
46
|
|
37
|
-
should "
|
38
|
-
|
39
|
-
|
47
|
+
should "parse the name arg and convert to a string" do
|
48
|
+
assert_equal "stage", @pname
|
49
|
+
|
50
|
+
@pname, @ptype_class, @prules = NsOptions::Option.args(['test'])
|
51
|
+
assert_equal 'test', @pname
|
52
|
+
end
|
53
|
+
|
54
|
+
should "parse the type_class arg and default it to Object" do
|
55
|
+
assert_equal String, @ptype_class
|
56
|
+
|
57
|
+
@pname, @ptype_class, @prules = NsOptions::Option.args(['test'])
|
58
|
+
assert_equal Object, @ptype_class
|
59
|
+
end
|
60
|
+
|
61
|
+
should "parse the type_class arg and default it to a given default type class" do
|
62
|
+
assert_equal String, @ptype_class
|
63
|
+
|
64
|
+
@pname, @ptype_class, @prules = NsOptions::Option.args(['test'], Fixnum)
|
65
|
+
assert_equal Fixnum, @ptype_class
|
66
|
+
end
|
67
|
+
|
68
|
+
should "parse option rules arguments, defaulting to {:args => []}" do
|
69
|
+
assert_equal @rules, @prules
|
70
|
+
|
71
|
+
@pname, @ptype_class, @prules = NsOptions::Option.args(['test'])
|
72
|
+
assert_equal({:args => []}, @prules)
|
40
73
|
end
|
74
|
+
|
41
75
|
end
|
42
76
|
|
43
|
-
class
|
44
|
-
desc "
|
77
|
+
class DefaultRuleTests < BaseTests
|
78
|
+
desc "using the :default rule"
|
45
79
|
setup do
|
46
|
-
@
|
47
|
-
subject.class.rules(c)
|
48
|
-
end
|
80
|
+
@option = NsOptions::Option.new(:opt, Object, :default => "something")
|
49
81
|
end
|
50
82
|
|
51
|
-
should "
|
52
|
-
|
83
|
+
should "have defaulted value based on the rule" do
|
84
|
+
assert_equal 'something', subject.value
|
53
85
|
end
|
54
86
|
|
55
|
-
should "
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
87
|
+
should "allow overwriting the default value" do
|
88
|
+
assert_nothing_raised { subject.value = "overwritten" }
|
89
|
+
assert_equal "overwritten", subject.value
|
90
|
+
end
|
91
|
+
|
92
|
+
should "allow setting the value to nil" do
|
93
|
+
assert_nothing_raised { subject.value = nil }
|
94
|
+
assert_nil subject.value
|
95
|
+
end
|
96
|
+
|
97
|
+
should "return the value to its default when `reset` is called" do
|
98
|
+
subject.value = "overwritten"
|
99
|
+
subject.reset
|
100
|
+
|
101
|
+
assert_equal 'something', subject.value
|
60
102
|
end
|
61
103
|
|
62
104
|
end
|
63
105
|
|
64
|
-
class
|
65
|
-
desc "
|
106
|
+
class RequiredRuleTests < BaseTests
|
107
|
+
desc "using the :required rule"
|
66
108
|
setup do
|
67
|
-
@
|
109
|
+
@option = NsOptions::Option.new(:opt, Object, :required => true)
|
68
110
|
end
|
69
111
|
|
70
|
-
should "
|
71
|
-
assert_equal
|
72
|
-
|
73
|
-
@prules, @ptype_class, @pname = subject.class.args('test')
|
74
|
-
assert_equal({:args => []}, @prules)
|
112
|
+
should "return true with a call to #required?" do
|
113
|
+
assert_equal true, subject.required?
|
75
114
|
end
|
76
115
|
|
77
|
-
|
78
|
-
assert_equal "stage", @pname
|
116
|
+
end
|
79
117
|
|
80
|
-
|
81
|
-
|
118
|
+
class ParseRulesTests < BaseTests
|
119
|
+
desc "parsing rules"
|
120
|
+
setup do
|
121
|
+
@cases = [nil, {}, {:args => 'is'}].map do |c|
|
122
|
+
NsOptions::Option.rules(c)
|
123
|
+
end
|
82
124
|
end
|
83
125
|
|
84
|
-
should "
|
85
|
-
|
126
|
+
should "always return them as a Hash" do
|
127
|
+
@cases.each { |c| assert_kind_of Hash, c }
|
128
|
+
end
|
86
129
|
|
87
|
-
|
88
|
-
|
130
|
+
should "always return with an array args rule" do
|
131
|
+
@cases.each do |c|
|
132
|
+
assert c.has_key? :args
|
133
|
+
assert_kind_of Array, c[:args]
|
134
|
+
end
|
89
135
|
end
|
90
136
|
|
91
137
|
end
|
92
138
|
|
93
|
-
class
|
139
|
+
class IsSetTests < BaseTests
|
94
140
|
desc "is_set method"
|
95
141
|
setup do
|
96
142
|
@type_class = Class.new(String) do
|
@@ -101,7 +147,6 @@ class NsOptions::Option
|
|
101
147
|
|
102
148
|
end
|
103
149
|
@special = NsOptions::Option.new(:no_blank, @type_class)
|
104
|
-
|
105
150
|
@boolean = NsOptions::Option.new(:boolean, NsOptions::Boolean)
|
106
151
|
end
|
107
152
|
|
@@ -125,7 +170,7 @@ class NsOptions::Option
|
|
125
170
|
|
126
171
|
end
|
127
172
|
|
128
|
-
class
|
173
|
+
class EqualityOperatorTests < BaseTests
|
129
174
|
desc "== operator"
|
130
175
|
setup do
|
131
176
|
@first = NsOptions::Option.new(:stage, String)
|
@@ -142,9 +187,10 @@ class NsOptions::Option
|
|
142
187
|
@first.value = "staging"
|
143
188
|
assert_not_equal @first, @second
|
144
189
|
end
|
190
|
+
|
145
191
|
end
|
146
192
|
|
147
|
-
class
|
193
|
+
class WithNativeTypeClassTests < BaseTests
|
148
194
|
desc "with a native type class (Float)"
|
149
195
|
setup do
|
150
196
|
@option = NsOptions::Option.new(:something, Float)
|
@@ -168,9 +214,10 @@ class NsOptions::Option
|
|
168
214
|
subject.value = new_value
|
169
215
|
assert_equal new_value.to_f, subject.value
|
170
216
|
end
|
217
|
+
|
171
218
|
end
|
172
219
|
|
173
|
-
class
|
220
|
+
class WithSymbolTypeClasstests < BaseTests
|
174
221
|
desc "with a Symbol as a type class"
|
175
222
|
setup do
|
176
223
|
@option = NsOptions::Option.new(:something, Symbol)
|
@@ -192,13 +239,14 @@ class NsOptions::Option
|
|
192
239
|
end
|
193
240
|
|
194
241
|
should "error on anything that doesn't define #to_sym" do
|
195
|
-
assert_raises(
|
242
|
+
assert_raises(NsOptions::Option::CoerceError) do
|
196
243
|
subject.value = true
|
197
244
|
end
|
198
245
|
end
|
246
|
+
|
199
247
|
end
|
200
248
|
|
201
|
-
class
|
249
|
+
class WithHashTypeClassTests < BaseTests
|
202
250
|
desc "with a Hash as a type class"
|
203
251
|
setup do
|
204
252
|
@option = NsOptions::Option.new(:something, Hash)
|
@@ -210,9 +258,10 @@ class NsOptions::Option
|
|
210
258
|
subject.value = new_value
|
211
259
|
assert_equal new_value, subject.value
|
212
260
|
end
|
261
|
+
|
213
262
|
end
|
214
263
|
|
215
|
-
class
|
264
|
+
class WithArrayTypeClassTests < BaseTests
|
216
265
|
desc "with an Array as a type class"
|
217
266
|
setup do
|
218
267
|
@option = NsOptions::Option.new(:something, Array)
|
@@ -224,21 +273,10 @@ class NsOptions::Option
|
|
224
273
|
subject.value = new_value
|
225
274
|
assert_equal new_value, subject.value
|
226
275
|
end
|
227
|
-
end
|
228
276
|
|
229
|
-
class WithoutTypeClassTest < BaseTest
|
230
|
-
desc "without a type class provided"
|
231
|
-
setup do
|
232
|
-
@option = NsOptions::Option.new(:something, nil)
|
233
|
-
end
|
234
|
-
subject{ @option }
|
235
|
-
|
236
|
-
should "have default it to Object" do
|
237
|
-
assert_equal Object, subject.type_class
|
238
|
-
end
|
239
277
|
end
|
240
278
|
|
241
|
-
class WithTypeClassArgErrorTests <
|
279
|
+
class WithTypeClassArgErrorTests < BaseTests
|
242
280
|
desc "setting a value with arg error"
|
243
281
|
setup do
|
244
282
|
@err = begin
|
@@ -254,18 +292,21 @@ class NsOptions::Option
|
|
254
292
|
@option = NsOptions::Option.new(:something, SuperSuperTestTest)
|
255
293
|
end
|
256
294
|
|
257
|
-
should "reraise the arg error,
|
295
|
+
should "reraise the arg error, with a custom message and backtrace" do
|
258
296
|
err = begin
|
259
297
|
@option.value = "arg error should be raised"
|
260
298
|
rescue Exception => err
|
261
299
|
err
|
262
300
|
end
|
263
|
-
|
264
|
-
|
301
|
+
|
302
|
+
assert_equal NsOptions::Option::CoerceError, err.class
|
303
|
+
assert_included @option.type_class.to_s, err.message
|
304
|
+
assert_included 'test/unit/option_tests.rb:', err.backtrace.first
|
265
305
|
end
|
306
|
+
|
266
307
|
end
|
267
308
|
|
268
|
-
class
|
309
|
+
class WithAValueOfTheSameClassTests < BaseTests
|
269
310
|
desc "with a value of the same class"
|
270
311
|
setup do
|
271
312
|
@class = Class.new
|
@@ -277,9 +318,10 @@ class NsOptions::Option
|
|
277
318
|
@option.value = value
|
278
319
|
assert_same value, @option.value
|
279
320
|
end
|
321
|
+
|
280
322
|
end
|
281
323
|
|
282
|
-
class
|
324
|
+
class WithAValueKindOfTests < BaseTests
|
283
325
|
desc "with a value that is a kind of the class"
|
284
326
|
setup do
|
285
327
|
@class = Class.new
|
@@ -292,9 +334,10 @@ class NsOptions::Option
|
|
292
334
|
@option.value = value
|
293
335
|
assert_same value, @option.value
|
294
336
|
end
|
337
|
+
|
295
338
|
end
|
296
339
|
|
297
|
-
class ProcHandlingTests <
|
340
|
+
class ProcHandlingTests < BaseTests
|
298
341
|
setup do
|
299
342
|
class KindOfProc < Proc; end
|
300
343
|
@a_string = "a string"
|
@@ -342,17 +385,17 @@ class NsOptions::Option
|
|
342
385
|
end
|
343
386
|
end
|
344
387
|
|
345
|
-
class WithReturnValueTests <
|
388
|
+
class WithReturnValueTests < BaseTests
|
346
389
|
setup do
|
347
390
|
# test control values
|
348
|
-
@string = NsOptions::Option.new
|
349
|
-
@symbol = NsOptions::Option.new
|
350
|
-
@integer = NsOptions::Option.new
|
351
|
-
@float = NsOptions::Option.new
|
352
|
-
@hash = NsOptions::Option.new
|
353
|
-
@array = NsOptions::Option.new
|
354
|
-
@proc = NsOptions::Option.new
|
355
|
-
@lazy_proc = NsOptions::Option.new
|
391
|
+
@string = NsOptions::Option.new :string, String
|
392
|
+
@symbol = NsOptions::Option.new :symbol, Symbol
|
393
|
+
@integer = NsOptions::Option.new :integer, Integer
|
394
|
+
@float = NsOptions::Option.new :float, Float
|
395
|
+
@hash = NsOptions::Option.new :hash, Hash
|
396
|
+
@array = NsOptions::Option.new :array, Array
|
397
|
+
@proc = NsOptions::Option.new :proc, Proc
|
398
|
+
@lazy_proc = NsOptions::Option.new :lazy_proc, Object
|
356
399
|
|
357
400
|
# custom return value
|
358
401
|
class HostedAt
|
@@ -402,9 +445,10 @@ class NsOptions::Option
|
|
402
445
|
@hosted_at.value = "path/to/resource/"
|
403
446
|
assert_equal '/path/to/resource', @hosted_at.value
|
404
447
|
end
|
448
|
+
|
405
449
|
end
|
406
450
|
|
407
|
-
class
|
451
|
+
class WithArgsTests < BaseTests
|
408
452
|
desc "with args rule"
|
409
453
|
setup do
|
410
454
|
@class = Class.new do
|
@@ -413,50 +457,68 @@ class NsOptions::Option
|
|
413
457
|
self.args = args
|
414
458
|
end
|
415
459
|
end
|
416
|
-
@value = "amazing"
|
417
460
|
end
|
418
461
|
|
419
|
-
class
|
462
|
+
class AsArrayTests < WithArgsTests
|
420
463
|
desc "as an array"
|
421
464
|
setup do
|
422
465
|
@args = [ true, false, { :hash => "yes" } ]
|
423
466
|
@option = NsOptions::Option.new(:something, @class, { :args => @args })
|
424
|
-
@option.value =
|
467
|
+
@option.value = "amazing"
|
425
468
|
end
|
426
469
|
|
427
470
|
should "pass the args to the type class with the value" do
|
428
|
-
expected =
|
471
|
+
expected = ["amazing", *@args]
|
429
472
|
assert_equal expected, subject.value.args
|
430
473
|
end
|
474
|
+
|
431
475
|
end
|
432
476
|
|
433
|
-
class
|
477
|
+
class AsSingleValueTests < WithArgsTests
|
434
478
|
desc "as a single value"
|
435
479
|
setup do
|
436
480
|
@args = lambda{ "something" }
|
437
481
|
@option = NsOptions::Option.new(:something, @class, { :args => @args })
|
438
|
-
@option.value =
|
482
|
+
@option.value = "amazing"
|
439
483
|
end
|
440
484
|
|
441
485
|
should "pass the single value to the type class with the value" do
|
442
|
-
expected = [*@args]
|
486
|
+
expected = ["amazing", *@args]
|
443
487
|
assert_equal expected, subject.value.args
|
444
488
|
end
|
489
|
+
|
445
490
|
end
|
446
491
|
|
447
|
-
class
|
492
|
+
class AsNilValueTests < WithArgsTests
|
448
493
|
desc "as a nil value"
|
449
494
|
setup do
|
450
495
|
@args = nil
|
451
496
|
@option = NsOptions::Option.new(:something, @class, { :args => @args })
|
452
|
-
@option.value =
|
497
|
+
@option.value = "amazing"
|
498
|
+
end
|
499
|
+
|
500
|
+
should "just pass the value to the type class and that's it" do
|
501
|
+
expected = ["amazing"]
|
502
|
+
assert_equal expected, subject.value.args
|
503
|
+
end
|
504
|
+
|
505
|
+
end
|
506
|
+
|
507
|
+
class AsEmptyArrayValueTests < WithArgsTests
|
508
|
+
desc "as an empty Array value"
|
509
|
+
setup do
|
510
|
+
@args = []
|
511
|
+
@option = NsOptions::Option.new(:something, @class, { :args => @args })
|
512
|
+
@option.value = "amazing"
|
453
513
|
end
|
454
514
|
|
455
|
-
should "just pass the value to the type class" do
|
456
|
-
expected = [
|
515
|
+
should "just pass the value to the type class and that's it" do
|
516
|
+
expected = ["amazing"]
|
457
517
|
assert_equal expected, subject.value.args
|
458
518
|
end
|
519
|
+
|
459
520
|
end
|
521
|
+
|
460
522
|
end
|
461
523
|
|
462
524
|
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'ns-options/options'
|
3
|
+
|
4
|
+
class NsOptions::Options
|
5
|
+
|
6
|
+
class BaseTests < Assert::Context
|
7
|
+
desc "NsOptions::Options"
|
8
|
+
setup do
|
9
|
+
@options = NsOptions::Options.new
|
10
|
+
end
|
11
|
+
subject{ @options }
|
12
|
+
|
13
|
+
should have_accessor :[]
|
14
|
+
should have_imeths :keys, :each, :empty?
|
15
|
+
should have_imeths :add, :rm, :get, :set, :required_set?
|
16
|
+
|
17
|
+
should "only use strings for keys (indifferent access)" do
|
18
|
+
subject['string_key'] = true
|
19
|
+
subject[:symbol_key] = true
|
20
|
+
|
21
|
+
assert_includes 'string_key', subject.keys
|
22
|
+
assert_includes 'symbol_key', subject.keys
|
23
|
+
assert_not_includes :string_key, subject.keys
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
class AddTests < BaseTests
|
29
|
+
desc "add method"
|
30
|
+
|
31
|
+
should "add options" do
|
32
|
+
assert_nil subject[:my_string]
|
33
|
+
subject.add(:my_string, NsOptions::Option.new(:my_string))
|
34
|
+
assert subject[:my_string]
|
35
|
+
end
|
36
|
+
|
37
|
+
should "should work with both string and symbol names" do
|
38
|
+
assert_nil subject[:my_string]
|
39
|
+
subject.add('my_string', NsOptions::Option.new('my_string'))
|
40
|
+
assert subject[:my_string]
|
41
|
+
end
|
42
|
+
|
43
|
+
should "return the option added" do
|
44
|
+
added_opt = subject.add(:something, NsOptions::Option.new(:something))
|
45
|
+
assert_kind_of NsOptions::Option, added_opt
|
46
|
+
end
|
47
|
+
|
48
|
+
should "build options with args when adding" do
|
49
|
+
subject.add(:my_float, NsOptions::Option.new(:my_float, Float, :default => 1.0))
|
50
|
+
|
51
|
+
assert_equal Float, subject[:my_float].type_class
|
52
|
+
assert_equal 1.0, subject[:my_float].rules[:default]
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
class RmTests < BaseTests
|
58
|
+
desc "rm method"
|
59
|
+
setup do
|
60
|
+
@options.add(:my_string, NsOptions::Option.new(:my_string))
|
61
|
+
end
|
62
|
+
|
63
|
+
should "remove the option definition from the collection" do
|
64
|
+
assert subject[:my_string]
|
65
|
+
|
66
|
+
subject.rm(:my_string)
|
67
|
+
assert_nil subject[:my_string]
|
68
|
+
end
|
69
|
+
|
70
|
+
should "should work with both string and symbol names" do
|
71
|
+
assert subject[:my_string]
|
72
|
+
|
73
|
+
subject.rm('my_string')
|
74
|
+
assert_nil subject[:my_string]
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
class GetTests < BaseTests
|
80
|
+
desc "get method"
|
81
|
+
setup do
|
82
|
+
opt = NsOptions::Option.new(:my_string, Object, :default => "something")
|
83
|
+
@options.add(:my_string, opt)
|
84
|
+
end
|
85
|
+
|
86
|
+
should "return the named option value" do
|
87
|
+
assert_equal "something", subject.get(:my_string)
|
88
|
+
end
|
89
|
+
|
90
|
+
should "should work with both string and symbol names" do
|
91
|
+
assert_equal "something", subject.get('my_string')
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
class SetTests < BaseTests
|
97
|
+
desc "set method"
|
98
|
+
setup do
|
99
|
+
@options.add(:my_string, NsOptions::Option.new(:my_string))
|
100
|
+
end
|
101
|
+
|
102
|
+
should "set option values" do
|
103
|
+
assert_nil subject[:my_string].value
|
104
|
+
subject.set(:my_string, "something")
|
105
|
+
|
106
|
+
assert_equal "something", subject.get(:my_string)
|
107
|
+
end
|
108
|
+
|
109
|
+
should "should work with both string and symbol names" do
|
110
|
+
assert_nil subject[:my_string].value
|
111
|
+
subject.set('my_string', "something")
|
112
|
+
|
113
|
+
assert_equal "something", subject.get(:my_string)
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
class RequiredSetTests < BaseTests
|
119
|
+
desc "required_set? method"
|
120
|
+
setup do
|
121
|
+
@options.add(:first, NsOptions::Option.new(:first, String, :required => true))
|
122
|
+
@options.add(:second, NsOptions::Option.new(:second, String, :required => true))
|
123
|
+
@options.add(:third, NsOptions::Option.new(:third, String))
|
124
|
+
end
|
125
|
+
|
126
|
+
should "return true when all required options are set" do
|
127
|
+
subject.set(:first, "first")
|
128
|
+
subject.set(:second, "second")
|
129
|
+
|
130
|
+
assert_equal true, subject.required_set?
|
131
|
+
end
|
132
|
+
|
133
|
+
should "return false if one required option is not set" do
|
134
|
+
subject.set(:first, "first")
|
135
|
+
subject.set(:third, "third")
|
136
|
+
|
137
|
+
assert_equal false, subject.required_set?
|
138
|
+
end
|
139
|
+
|
140
|
+
should "not change because of options that aren't required" do
|
141
|
+
subject.set(:first, "first")
|
142
|
+
subject.set(:second, "second")
|
143
|
+
subject.set(:third, "third")
|
144
|
+
assert_equal true, subject.required_set?
|
145
|
+
|
146
|
+
subject.set(:third, nil)
|
147
|
+
assert_equal true, subject.required_set?
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'ns-options/proxy_method'
|
3
|
+
|
4
|
+
class NsOptions::RootMethods
|
5
|
+
|
6
|
+
class BaseTests < Assert::Context
|
7
|
+
desc "NsOptions::RootMethods"
|
8
|
+
setup do
|
9
|
+
@pm = NsOptions::ProxyMethod.new(Module.new, 'whatever', 'a thing')
|
10
|
+
end
|
11
|
+
subject { @pm }
|
12
|
+
|
13
|
+
should have_imeths :define_on_class?, :define, :validate
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
class ValidateTests < BaseTests
|
18
|
+
desc "validate meth"
|
19
|
+
setup do
|
20
|
+
@io = StringIO.new(@out = "")
|
21
|
+
@caller = ["a test caller"]
|
22
|
+
end
|
23
|
+
|
24
|
+
should "return false for not recommended methods" do
|
25
|
+
pm = NsOptions::ProxyMethod.new(Module.new, :option, 'a thing')
|
26
|
+
assert_equal false, pm.validate(@io, @caller)
|
27
|
+
|
28
|
+
pm = NsOptions::ProxyMethod.new(Module.new, "anything_else", 'a thing')
|
29
|
+
assert_equal true, pm.validate(@io, @caller)
|
30
|
+
end
|
31
|
+
|
32
|
+
should "write a warning and any caller info" do
|
33
|
+
NsOptions::ProxyMethod.new(Module.new, :ns, 'a thing').validate(@io, @caller)
|
34
|
+
|
35
|
+
assert_match "WARNING: ", @out
|
36
|
+
assert_match @caller.first, @out
|
37
|
+
end
|
38
|
+
|
39
|
+
should "be called when calling `define'" do
|
40
|
+
NsOptions::ProxyMethod.new(Module.new, :ns, 'a thing').define(@io, @caller)
|
41
|
+
|
42
|
+
assert_match "WARNING: ", @out
|
43
|
+
assert_match @caller.first, @out
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
class ModuleTests < BaseTests
|
49
|
+
desc "defined on a module"
|
50
|
+
setup do
|
51
|
+
@pm = NsOptions::ProxyMethod.new(@the_module = Module.new, 'on_module', 'a thing')
|
52
|
+
@pm.define
|
53
|
+
end
|
54
|
+
|
55
|
+
should "know its not defining on a class" do
|
56
|
+
assert_not subject.define_on_class?
|
57
|
+
end
|
58
|
+
|
59
|
+
should "define a singleton method that builds a ns" do
|
60
|
+
assert_responds_to 'on_module', @the_module
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
class ClassTests < BaseTests
|
66
|
+
desc "defined on a class"
|
67
|
+
setup do
|
68
|
+
@pm = NsOptions::ProxyMethod.new(@the_class = Class.new, 'on_class', 'a thing')
|
69
|
+
@pm.define
|
70
|
+
end
|
71
|
+
|
72
|
+
should "know its defining on a class" do
|
73
|
+
assert subject.define_on_class?
|
74
|
+
end
|
75
|
+
|
76
|
+
should "define a singleton method that builds a ns" do
|
77
|
+
assert_responds_to 'on_class', @the_class
|
78
|
+
end
|
79
|
+
|
80
|
+
should "define an instance method that builds a ns from its singleton" do
|
81
|
+
a_class = @the_class.new
|
82
|
+
assert_responds_to 'on_class', a_class
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|