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
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'assert'
|
2
|
+
|
3
|
+
require 'ns-options/assert_macros'
|
4
|
+
require 'test/support/type_class_proxy'
|
5
|
+
|
6
|
+
class TypeClassProxyTests < Assert::Context
|
7
|
+
include NsOptions::AssertMacros
|
8
|
+
|
9
|
+
desc "a proxy using option_type_class"
|
10
|
+
setup do
|
11
|
+
set_proxy_for_tests(TypeClassProxy)
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_proxy_for_tests(proxy_class)
|
15
|
+
@proxy = proxy_class
|
16
|
+
proxy_class.value1 = 10
|
17
|
+
proxy_class.more.more1 = 10
|
18
|
+
proxy_class.strings.string1 = 10
|
19
|
+
proxy_class.objs.obj1 = Object.new
|
20
|
+
end
|
21
|
+
|
22
|
+
should "use the option type class for its options" do
|
23
|
+
assert_kind_of DefaultTypeClass, @proxy.value1
|
24
|
+
assert_equal 10, @proxy.value1.value
|
25
|
+
end
|
26
|
+
|
27
|
+
should "use the option type class for its namespaces" do
|
28
|
+
assert_equal DefaultTypeClass, @proxy.more.option_type_class
|
29
|
+
end
|
30
|
+
|
31
|
+
should "recursively use the option type class" do
|
32
|
+
assert_kind_of DefaultTypeClass, @proxy.more.more1
|
33
|
+
end
|
34
|
+
|
35
|
+
should "allow the recursive option type class to be overridden" do
|
36
|
+
assert_equal String, @proxy.strings.option_type_class
|
37
|
+
assert_kind_of String, @proxy.strings.string1
|
38
|
+
end
|
39
|
+
|
40
|
+
should "allow the recursive option type class to be reset" do
|
41
|
+
assert_equal Object, @proxy.objs.option_type_class
|
42
|
+
assert_kind_of Object, @proxy.objs.obj1
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
class InheritedTypeClassProxyTests < TypeClassProxyTests
|
48
|
+
desc "that inherits from another"
|
49
|
+
setup do
|
50
|
+
set_proxy_for_tests(InheritedTypeClassProxy)
|
51
|
+
end
|
52
|
+
|
53
|
+
should "use the option type class for its options" do
|
54
|
+
assert_kind_of DefaultTypeClass, @proxy.value1
|
55
|
+
assert_equal 10, @proxy.value1.value
|
56
|
+
end
|
57
|
+
|
58
|
+
should "use the option type class for its namespaces" do
|
59
|
+
assert_equal DefaultTypeClass, @proxy.more.option_type_class
|
60
|
+
end
|
61
|
+
|
62
|
+
should "recursively use the option type class" do
|
63
|
+
assert_kind_of DefaultTypeClass, @proxy.more.more1
|
64
|
+
end
|
65
|
+
|
66
|
+
should "allow the recursive option type class to be overridden" do
|
67
|
+
assert_equal String, @proxy.strings.option_type_class
|
68
|
+
assert_kind_of String, @proxy.strings.string1
|
69
|
+
end
|
70
|
+
|
71
|
+
should "allow the recursive option type class to be reset" do
|
72
|
+
assert_equal Object, @proxy.objs.option_type_class
|
73
|
+
assert_kind_of Object, @proxy.objs.obj1
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
class DoubleInheritedTypeClassProxyTests < TypeClassProxyTests
|
79
|
+
desc "that inherits from another that inherits from another"
|
80
|
+
setup do
|
81
|
+
set_proxy_for_tests(DoubleInheritedTypeClassProxy)
|
82
|
+
end
|
83
|
+
|
84
|
+
should "use the option type class for its options" do
|
85
|
+
assert_kind_of DefaultTypeClass, @proxy.value1
|
86
|
+
assert_equal 10, @proxy.value1.value
|
87
|
+
end
|
88
|
+
|
89
|
+
should "use the option type class for its namespaces" do
|
90
|
+
assert_equal DefaultTypeClass, @proxy.more.option_type_class
|
91
|
+
end
|
92
|
+
|
93
|
+
should "recursively use the option type class" do
|
94
|
+
assert_kind_of DefaultTypeClass, @proxy.more.more1
|
95
|
+
end
|
96
|
+
|
97
|
+
should "allow the recursive option type class to be overridden" do
|
98
|
+
assert_equal String, @proxy.strings.option_type_class
|
99
|
+
assert_kind_of String, @proxy.strings.string1
|
100
|
+
end
|
101
|
+
|
102
|
+
should "allow the recursive option type class to be reset" do
|
103
|
+
assert_equal Object, @proxy.objs.option_type_class
|
104
|
+
assert_kind_of Object, @proxy.objs.obj1
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'assert'
|
2
|
+
|
3
|
+
require 'ns-options/assert_macros'
|
4
|
+
require 'test/support/user'
|
5
|
+
|
6
|
+
class User
|
7
|
+
|
8
|
+
class BaseTests < Assert::Context
|
9
|
+
include NsOptions::AssertMacros
|
10
|
+
|
11
|
+
desc "the User class"
|
12
|
+
setup do
|
13
|
+
@class = User
|
14
|
+
end
|
15
|
+
subject{ @class }
|
16
|
+
|
17
|
+
should have_reader :preferences
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
class ClassPreferencesTests < BaseTests
|
22
|
+
desc "preferences"
|
23
|
+
subject{ @class.preferences }
|
24
|
+
|
25
|
+
should have_option :home_url
|
26
|
+
should have_option :show_messages, NsOptions::Boolean, :required => true
|
27
|
+
should have_option :font_size, Integer, :default => 12
|
28
|
+
|
29
|
+
should have_namespace :view do
|
30
|
+
option :color
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
class ClassInheritedPreferencesTests < ClassPreferencesTests
|
36
|
+
desc "on a subclass of User"
|
37
|
+
setup do
|
38
|
+
User.preferences.home_url = "/home"
|
39
|
+
User.preferences.show_messages = false
|
40
|
+
User.preferences.font_size = 15
|
41
|
+
User.preferences.view.color = "green"
|
42
|
+
|
43
|
+
@a_sub_class = Class.new(User)
|
44
|
+
end
|
45
|
+
subject { @a_sub_class.preferences }
|
46
|
+
|
47
|
+
should have_option :home_url
|
48
|
+
should have_option :show_messages, NsOptions::Boolean, :required => true
|
49
|
+
should have_option :font_size, Integer, :default => 12
|
50
|
+
|
51
|
+
should have_namespace :view do
|
52
|
+
option :color
|
53
|
+
end
|
54
|
+
|
55
|
+
should "not have the same preference values as its superclass" do
|
56
|
+
assert_not_equal "/home", subject.home_url
|
57
|
+
assert_not_equal false, subject.show_messages
|
58
|
+
assert_not_equal 15, subject.font_size
|
59
|
+
assert_not_equal "green", subject.view.color
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
class InstanceTests < BaseTests
|
65
|
+
desc "instance"
|
66
|
+
setup do
|
67
|
+
@instance = @class.new
|
68
|
+
@class_preferences = @class.preferences
|
69
|
+
@class_preferences.home_url = "/something"
|
70
|
+
@preferences = @instance.preferences
|
71
|
+
end
|
72
|
+
subject{ @instance }
|
73
|
+
|
74
|
+
should have_instance_methods :preferences
|
75
|
+
|
76
|
+
should "have a new namespace that is a different object than the class namespace" do
|
77
|
+
assert_not_same @class_preferences, @preferences
|
78
|
+
end
|
79
|
+
|
80
|
+
should "have the same options as the class namespace, but different objects" do
|
81
|
+
@class_preferences.__data__.child_options.each do |name, class_opt|
|
82
|
+
inst_opt = @preferences.__data__.child_options[name]
|
83
|
+
|
84
|
+
assert_equal class_opt.name, inst_opt.name
|
85
|
+
assert_equal class_opt.type_class, inst_opt.type_class
|
86
|
+
assert_equal class_opt.rules, inst_opt.rules
|
87
|
+
assert_not_same class_opt, inst_opt
|
88
|
+
end
|
89
|
+
|
90
|
+
assert_not_equal @class_preferences.home_url, @preferences.home_url
|
91
|
+
end
|
92
|
+
|
93
|
+
should "have the same named namespaces as the class, but different objects" do
|
94
|
+
@class_preferences.__data__.child_namespaces.each do |name, class_ns|
|
95
|
+
assert_not_same class_ns, @preferences.__data__.child_namespaces[name]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
class InstancePreferencesTests < InstanceTests
|
102
|
+
desc "preferences"
|
103
|
+
setup do
|
104
|
+
@instance.preferences.home_url = "/home"
|
105
|
+
@instance.preferences.show_messages = false
|
106
|
+
@instance.preferences.font_size = 15
|
107
|
+
@instance.preferences.view.color = "green"
|
108
|
+
end
|
109
|
+
subject{ @instance.preferences }
|
110
|
+
|
111
|
+
should have_option :home_url
|
112
|
+
should have_option :show_messages, NsOptions::Boolean, :required => true
|
113
|
+
should have_option :font_size, Integer, :default => 12
|
114
|
+
|
115
|
+
should have_namespace :view do
|
116
|
+
option :color
|
117
|
+
end
|
118
|
+
|
119
|
+
should "have set the preference values" do
|
120
|
+
assert_equal "/home", subject.home_url
|
121
|
+
assert_equal false, subject.show_messages
|
122
|
+
assert_equal 15, subject.font_size
|
123
|
+
assert_equal "green", subject.view.color
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
class InstanceInheritedPreferencesTests < InstancePreferencesTests
|
129
|
+
desc "on an instance of a subclass of User"
|
130
|
+
setup do
|
131
|
+
@a_sub_class = Class.new(User)
|
132
|
+
@the_sub_class = @a_sub_class.new
|
133
|
+
end
|
134
|
+
subject { @the_sub_class.preferences }
|
135
|
+
|
136
|
+
should have_option :home_url
|
137
|
+
should have_option :show_messages, NsOptions::Boolean, :required => true
|
138
|
+
should have_option :font_size, Integer, :default => 12
|
139
|
+
|
140
|
+
should have_namespace :view do
|
141
|
+
option :color
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'assert'
|
2
|
+
require 'ns-options/boolean'
|
2
3
|
|
3
4
|
class NsOptions::Boolean
|
4
5
|
|
5
|
-
class
|
6
|
+
class BaseTests < Assert::Context
|
6
7
|
desc "NsOptions::Boolean"
|
7
8
|
setup do
|
8
9
|
@boolean = NsOptions::Boolean.new(true)
|
@@ -22,7 +23,7 @@ class NsOptions::Boolean
|
|
22
23
|
|
23
24
|
end
|
24
25
|
|
25
|
-
class
|
26
|
+
class WithTruthyValuesTests < BaseTests
|
26
27
|
desc "with truthy values"
|
27
28
|
setup do
|
28
29
|
@boolean = NsOptions::Boolean.new(nil)
|
@@ -54,7 +55,7 @@ class NsOptions::Boolean
|
|
54
55
|
end
|
55
56
|
end
|
56
57
|
|
57
|
-
class
|
58
|
+
class WithFalsyValuesTests < BaseTests
|
58
59
|
desc "with falsy values"
|
59
60
|
setup do
|
60
61
|
@boolean = NsOptions::Boolean.new(nil)
|
@@ -90,7 +91,7 @@ class NsOptions::Boolean
|
|
90
91
|
end
|
91
92
|
end
|
92
93
|
|
93
|
-
class ComparatorTests <
|
94
|
+
class ComparatorTests < BaseTests
|
94
95
|
desc "when comparing for equality"
|
95
96
|
setup do
|
96
97
|
@true_bool = NsOptions::Boolean.new true
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'ns-options/namespace_advisor'
|
3
|
+
require 'ns-options/namespace_data'
|
4
|
+
require 'ns-options/namespace'
|
5
|
+
|
6
|
+
class NsOptions::NamespaceAdvisor
|
7
|
+
|
8
|
+
class BaseTests < Assert::Context
|
9
|
+
desc "NsOptions::NamespaceAdvisor"
|
10
|
+
setup do
|
11
|
+
@ns = NsOptions::Namespace.new 'test'
|
12
|
+
@ns.option 'opt1'
|
13
|
+
@ns.namespace 'ns1'
|
14
|
+
@ns_data = @ns.__data__
|
15
|
+
@na = ns_advisor_for 'test'
|
16
|
+
end
|
17
|
+
subject { @na }
|
18
|
+
|
19
|
+
def ns_advisor_for(name, kind='an option')
|
20
|
+
NsOptions::NamespaceAdvisor.new(@ns_data, name, kind)
|
21
|
+
end
|
22
|
+
|
23
|
+
should have_imeths :run, :not_recommended?, :duplicate?, :not_recommended_names
|
24
|
+
|
25
|
+
should "know its not recommended names" do
|
26
|
+
exp = NsOptions::Namespace.instance_methods(false).map(&:to_sym)
|
27
|
+
assert_equal exp, subject.not_recommended_names
|
28
|
+
end
|
29
|
+
|
30
|
+
should "know if a name is not recommended" do
|
31
|
+
assert ns_advisor_for('option').not_recommended?
|
32
|
+
assert ns_advisor_for('to_hash').not_recommended?
|
33
|
+
assert_not ns_advisor_for('test').not_recommended?
|
34
|
+
end
|
35
|
+
|
36
|
+
should "know if a name is a duplicate" do
|
37
|
+
assert_not ns_advisor_for('option').duplicate?
|
38
|
+
assert_not ns_advisor_for('to_hash').duplicate?
|
39
|
+
assert_not ns_advisor_for('test').duplicate?
|
40
|
+
assert ns_advisor_for('opt1').duplicate?
|
41
|
+
assert ns_advisor_for('ns1').duplicate?
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
class RunTests < BaseTests
|
47
|
+
desc "run meth"
|
48
|
+
setup do
|
49
|
+
@io = StringIO.new(@out = "")
|
50
|
+
@caller = ["a test caller"]
|
51
|
+
end
|
52
|
+
|
53
|
+
should "write a warning and any caller info on not recommended names" do
|
54
|
+
ns_advisor_for('to_hash').run(@io, @caller)
|
55
|
+
|
56
|
+
assert_match "WARNING: ", @out
|
57
|
+
assert_match @caller.first, @out
|
58
|
+
end
|
59
|
+
|
60
|
+
should "write a warning and any caller info on duplicate names" do
|
61
|
+
ns_advisor_for('opt1').run(@io, @caller)
|
62
|
+
|
63
|
+
assert_match "WARNING: ", @out
|
64
|
+
assert_match @caller.first, @out
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -0,0 +1,336 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'ns-options/namespace_data'
|
3
|
+
require 'ns-options/namespace'
|
4
|
+
|
5
|
+
class NsOptions::NamespaceData
|
6
|
+
|
7
|
+
class BaseTests < Assert::Context
|
8
|
+
desc "NsOptions::NamespaceData"
|
9
|
+
setup do
|
10
|
+
@ns = NsOptions::Namespace.new('thing')
|
11
|
+
@data = @ns.__data__
|
12
|
+
end
|
13
|
+
subject { @data }
|
14
|
+
|
15
|
+
should have_readers :ns, :option_type_class
|
16
|
+
should have_readers :child_options, :child_namespaces
|
17
|
+
should have_imeths :has_option?, :has_namespace?, :required_set?
|
18
|
+
should have_imeths :add_option, :get_option, :set_option
|
19
|
+
should have_imeths :add_namespace, :get_namespace
|
20
|
+
should have_imeths :set_option_type_class
|
21
|
+
should have_imeths :to_hash, :apply, :each, :define, :build_from, :reset
|
22
|
+
|
23
|
+
should "know its namespace" do
|
24
|
+
assert_equal @ns, subject.ns
|
25
|
+
end
|
26
|
+
|
27
|
+
should "know its type class" do
|
28
|
+
assert_equal Object, subject.option_type_class
|
29
|
+
end
|
30
|
+
|
31
|
+
should "know its child options" do
|
32
|
+
assert_kind_of NsOptions::Options, subject.child_options
|
33
|
+
assert_empty subject.child_options
|
34
|
+
end
|
35
|
+
|
36
|
+
should "know its child namespaces" do
|
37
|
+
assert_kind_of NsOptions::Namespaces, subject.child_namespaces
|
38
|
+
assert_empty subject.child_namespaces
|
39
|
+
end
|
40
|
+
|
41
|
+
should "be able to change its option_type_class" do
|
42
|
+
assert_equal Object, subject.option_type_class
|
43
|
+
subject.set_option_type_class(Fixnum)
|
44
|
+
|
45
|
+
assert_equal Fixnum, subject.option_type_class
|
46
|
+
end
|
47
|
+
|
48
|
+
should "add options" do
|
49
|
+
an_option = subject.add_option 'an_option'
|
50
|
+
|
51
|
+
assert_equal an_option, subject.child_options['an_option']
|
52
|
+
assert_equal Object, an_option.type_class
|
53
|
+
end
|
54
|
+
|
55
|
+
should "add options with its type class" do
|
56
|
+
subject.set_option_type_class(Fixnum)
|
57
|
+
an_option = subject.add_option 'an_option'
|
58
|
+
|
59
|
+
assert_equal Fixnum, an_option.type_class
|
60
|
+
end
|
61
|
+
|
62
|
+
should "get option values" do
|
63
|
+
an_option = subject.add_option 'an_option'
|
64
|
+
an_option.value = 123
|
65
|
+
|
66
|
+
assert_equal 123, subject.get_option('an_option')
|
67
|
+
end
|
68
|
+
|
69
|
+
should "set option values" do
|
70
|
+
an_option = subject.add_option 'an_option'
|
71
|
+
an_option.value = 123
|
72
|
+
subject.set_option('an_option', 456)
|
73
|
+
|
74
|
+
assert_equal 456, an_option.value
|
75
|
+
end
|
76
|
+
|
77
|
+
should "add namespaces" do
|
78
|
+
a_namespace = subject.add_namespace 'a_namespace'
|
79
|
+
assert_equal a_namespace, subject.child_namespaces['a_namespace']
|
80
|
+
|
81
|
+
a_ns_data = a_namespace.__data__
|
82
|
+
assert_equal Object, a_ns_data.option_type_class
|
83
|
+
end
|
84
|
+
|
85
|
+
should "add namespaces with its type class" do
|
86
|
+
subject.set_option_type_class(Fixnum)
|
87
|
+
a_namespace = subject.add_namespace 'a_namespace'
|
88
|
+
a_ns_data = subject.child_namespaces['a_namespace'].__data__
|
89
|
+
|
90
|
+
assert_equal Fixnum, a_ns_data.option_type_class
|
91
|
+
end
|
92
|
+
|
93
|
+
should "get namespaces" do
|
94
|
+
a_namespace = subject.add_namespace 'a_namespace'
|
95
|
+
assert_equal a_namespace, subject.get_namespace('a_namespace')
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
class HandlingTests < BaseTests
|
101
|
+
setup do
|
102
|
+
@data.define do
|
103
|
+
option :first; option :second; option :third
|
104
|
+
namespace(:child_a) do
|
105
|
+
option(:fourth); option(:fifth)
|
106
|
+
namespace(:child_b) { option(:sixth) }
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
@named_values = {
|
111
|
+
:first => "1", :second => "2", :third => "3", :twenty_one => "21",
|
112
|
+
:child_a => {
|
113
|
+
:fourth => "4", :fifth => "5",
|
114
|
+
:child_b => { :sixth => "6" }
|
115
|
+
},
|
116
|
+
:child_c => { :what => "?" }
|
117
|
+
}
|
118
|
+
end
|
119
|
+
|
120
|
+
should "know if it has an option" do
|
121
|
+
assert subject.has_option? 'first'
|
122
|
+
assert_not subject.has_option? 'blashalksjdglasjasdga'
|
123
|
+
end
|
124
|
+
|
125
|
+
should "know if it has a namespace" do
|
126
|
+
assert subject.has_namespace? 'child_a'
|
127
|
+
assert_not subject.has_namespace? 'dlakjsglasdjgaklsdgjas'
|
128
|
+
end
|
129
|
+
|
130
|
+
should "return its Hash representation" do
|
131
|
+
exp_hash = {
|
132
|
+
:first => nil, :second => nil, :third => nil,
|
133
|
+
:child_a => {
|
134
|
+
:fourth => nil, :fifth => nil,
|
135
|
+
:child_b => { :sixth => nil }
|
136
|
+
}
|
137
|
+
}
|
138
|
+
assert_equal(exp_hash, subject.to_hash)
|
139
|
+
end
|
140
|
+
|
141
|
+
should "apply a given hash value to itself" do
|
142
|
+
subject.apply(@named_values)
|
143
|
+
assert_equal @named_values, subject.to_hash
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
class EachTests < HandlingTests
|
149
|
+
desc "iterated with the each method"
|
150
|
+
setup do
|
151
|
+
subject.apply(@named_values)
|
152
|
+
@exp = "".tap do |exp|
|
153
|
+
subject.to_hash.each do |k,v|
|
154
|
+
exp << "#{k}=#{v};"
|
155
|
+
end
|
156
|
+
end
|
157
|
+
@act = "".tap do |exp|
|
158
|
+
subject.each do |k,v|
|
159
|
+
exp << "#{k}=#{v};"
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
should "yield k/v pairs by iterating over the #to_hash" do
|
165
|
+
assert_equal @exp, @act
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
class DefineTests < BaseTests
|
171
|
+
desc "define method"
|
172
|
+
|
173
|
+
should "instance eval in the scope of the ns if given a no-arity-block" do
|
174
|
+
scope = nil; subject.define{ scope = self }
|
175
|
+
assert_equal @ns, scope
|
176
|
+
end
|
177
|
+
|
178
|
+
should "yield the ns to the block if given an arity-block" do
|
179
|
+
yielded = nil; subject.define{|ns| yielded = ns}
|
180
|
+
assert_equal @ns, yielded
|
181
|
+
end
|
182
|
+
|
183
|
+
should "return the namespace, if given no block" do
|
184
|
+
assert_equal @ns, subject.define
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
class BuildFromTests < BaseTests
|
190
|
+
desc "build_from method"
|
191
|
+
setup do
|
192
|
+
@from_ns = NsOptions::Namespace.new(:other_thing)
|
193
|
+
@from_child_opt = @from_ns.option :opt1
|
194
|
+
@from_child_ns = @from_ns.namespace(:ns1) { option :sub_opt1 }
|
195
|
+
end
|
196
|
+
|
197
|
+
should "copy the options" do
|
198
|
+
assert_not subject.has_option? :opt1
|
199
|
+
@data.build_from(@from_ns.__data__)
|
200
|
+
|
201
|
+
assert subject.has_option? :opt1
|
202
|
+
assert_not_same @from_child_opt, subject.child_options[:opt1]
|
203
|
+
end
|
204
|
+
|
205
|
+
should "copy the namespaces" do
|
206
|
+
assert_not subject.has_namespace?(:ns1)
|
207
|
+
@data.build_from(@from_ns.__data__)
|
208
|
+
|
209
|
+
assert subject.has_namespace?(:ns1)
|
210
|
+
assert subject.child_namespaces[:ns1].__data__.has_option? :sub_opt1
|
211
|
+
assert_not_same @from_child_ns, subject.child_namespaces[:ns1]
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
class ApplyTests < BaseTests
|
217
|
+
setup do
|
218
|
+
@data.define do
|
219
|
+
option :first; option :second; option :third
|
220
|
+
namespace(:child_a) do
|
221
|
+
option(:fourth); option(:fifth)
|
222
|
+
namespace(:child_b) { option(:sixth) }
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
@named_values = {
|
227
|
+
:first => "1", :second => "2", :third => "3", :twenty_one => "21",
|
228
|
+
:child_a => {
|
229
|
+
:fourth => "4", :fifth => "5",
|
230
|
+
:child_b => { :sixth => "6" }
|
231
|
+
},
|
232
|
+
:child_c => { :what => "?" }
|
233
|
+
}
|
234
|
+
end
|
235
|
+
|
236
|
+
should "apply a given hash value to itself" do
|
237
|
+
subject.apply(@named_values)
|
238
|
+
assert_equal @named_values, subject.to_hash
|
239
|
+
end
|
240
|
+
|
241
|
+
should "ignore applying non-hash values to namespaces" do
|
242
|
+
assert_nil subject.ns.child_a.fourth
|
243
|
+
prev_child_b = subject.ns.child_a.child_b.dup
|
244
|
+
|
245
|
+
subject.apply(:child_a => {
|
246
|
+
:fourth => 4,
|
247
|
+
:child_b => 'something'
|
248
|
+
})
|
249
|
+
assert_equal 4, subject.ns.child_a.fourth
|
250
|
+
assert_not_equal 'something', subject.ns.child_a.child_b
|
251
|
+
assert_equal prev_child_b, subject.ns.child_a.child_b
|
252
|
+
end
|
253
|
+
|
254
|
+
end
|
255
|
+
|
256
|
+
class ResetTests < BaseTests
|
257
|
+
desc "reset method"
|
258
|
+
setup do
|
259
|
+
@data.add_option 'fixnum', Fixnum, :default => 10
|
260
|
+
@data.add_option 'something'
|
261
|
+
@data.add_namespace 'sub' do
|
262
|
+
option 'subopt'
|
263
|
+
end
|
264
|
+
|
265
|
+
@data.set_option 'fixnum', 9999
|
266
|
+
@data.set_option 'something', 'else'
|
267
|
+
@data.get_namespace('sub').subopt = "a subopt"
|
268
|
+
end
|
269
|
+
|
270
|
+
should "set the options back to their default values" do
|
271
|
+
assert_equal 9999, subject.get_option('fixnum')
|
272
|
+
assert_equal 'else', subject.get_option('something')
|
273
|
+
assert_equal 'a subopt', subject.get_namespace('sub').subopt
|
274
|
+
|
275
|
+
subject.reset
|
276
|
+
|
277
|
+
assert_equal 10, subject.get_option('fixnum')
|
278
|
+
assert_nil subject.get_option('something')
|
279
|
+
assert_nil subject.get_namespace('sub').subopt
|
280
|
+
end
|
281
|
+
|
282
|
+
end
|
283
|
+
|
284
|
+
class SameNameTests < BaseTests
|
285
|
+
|
286
|
+
should "replace a same-named option when adding a new option" do
|
287
|
+
subject.add_option :something, String
|
288
|
+
assert subject.has_option? :something
|
289
|
+
|
290
|
+
subject.add_option :something, Fixnum, :default => 10
|
291
|
+
assert subject.has_option? :something
|
292
|
+
|
293
|
+
opt = subject.child_options[:something]
|
294
|
+
assert_equal Fixnum, opt.type_class
|
295
|
+
assert_equal 10, opt.rules[:default]
|
296
|
+
end
|
297
|
+
|
298
|
+
should "replace a same-named option when adding a new namespace" do
|
299
|
+
subject.add_option :something, String
|
300
|
+
assert subject.has_option? :something
|
301
|
+
|
302
|
+
subject.add_namespace :something
|
303
|
+
assert_not subject.has_option? :something
|
304
|
+
assert subject.has_namespace? :something
|
305
|
+
|
306
|
+
assert_equal Hash.new, subject.child_namespaces[:something].to_hash
|
307
|
+
end
|
308
|
+
|
309
|
+
should "replace a same-named namespace when adding a new namespace" do
|
310
|
+
subject.add_namespace :something do
|
311
|
+
option :a, :default => 10
|
312
|
+
end
|
313
|
+
assert subject.has_namespace? :something
|
314
|
+
assert_equal({:a => 10}, subject.child_namespaces[:something].to_hash)
|
315
|
+
|
316
|
+
subject.add_namespace :something
|
317
|
+
assert subject.has_namespace? :something
|
318
|
+
assert_equal Hash.new, subject.child_namespaces[:something].to_hash
|
319
|
+
end
|
320
|
+
|
321
|
+
should "replace a same-named namespace when adding a new option" do
|
322
|
+
subject.add_namespace :something
|
323
|
+
assert subject.has_namespace? :something
|
324
|
+
|
325
|
+
subject.add_option :something, Fixnum, :default => 10
|
326
|
+
assert_not subject.has_namespace? :something
|
327
|
+
assert subject.has_option? :something
|
328
|
+
|
329
|
+
opt = subject.child_options[:something]
|
330
|
+
assert_equal Fixnum, opt.type_class
|
331
|
+
assert_equal 10, opt.rules[:default]
|
332
|
+
end
|
333
|
+
|
334
|
+
end
|
335
|
+
|
336
|
+
end
|