ns-options 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ require 'assert'
2
+
3
+ class User
4
+
5
+ class BaseTest < Assert::Context
6
+ desc "the User class"
7
+ setup do
8
+ @class = User
9
+ end
10
+ subject{ @class }
11
+
12
+ should have_instance_methods :options, :preferences
13
+ end
14
+
15
+ class ClassPreferencesTest < BaseTest
16
+ desc "preferences"
17
+ subject{ @class.preferences }
18
+
19
+ should have_instance_methods :namespace, :option, :define, :options, :metaclass
20
+ should have_accessors :home_url, :show_messages, :font_size
21
+ end
22
+
23
+ class InstanceTest < BaseTest
24
+ desc "instance"
25
+ setup do
26
+ @instance = @class.new
27
+ end
28
+ subject{ @instance }
29
+
30
+ should have_instance_methods :preferences
31
+ end
32
+
33
+ class PreferencesTest < InstanceTest
34
+ desc "preferences"
35
+ setup do
36
+ @preferences = @instance.preferences
37
+ @preferences.home_url = "/home"
38
+ @preferences.show_messages = false
39
+ @preferences.font_size = 15
40
+ end
41
+ subject{ @preferences }
42
+
43
+ should have_instance_methods :namespace, :option, :define, :options, :metaclass
44
+ should have_accessors :home_url, :show_messages, :font_size
45
+
46
+ should "have set the home_url" do
47
+ assert_equal "/home", subject.home_url
48
+ end
49
+ should "have set show_messages" do
50
+ assert_equal false, subject.show_messages
51
+ end
52
+ should "have set the font_size" do
53
+ assert_equal 15, subject.font_size
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,12 @@
1
+ module App
2
+ include NsOptions::HasOptions
3
+ options(:settings, "settings:app") do
4
+ option :root, Pathname
5
+ option :stage
6
+ option :logger, Logger
7
+
8
+ namespace :sub do
9
+ option :run_commands, NsOptions::Option::Boolean
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ class User
2
+ include NsOptions::HasOptions
3
+ options(:preferences, 'user-preferences') do
4
+ option :home_url
5
+ option :show_messages, NsOptions::Option::Boolean, :require => true
6
+ option :font_size, Integer, :default => 12
7
+ end
8
+
9
+ def preferences_key
10
+ "user_#{self.object_id}"
11
+ end
12
+
13
+ end
@@ -0,0 +1,67 @@
1
+ require 'assert'
2
+
3
+ module NsOptions::HasOptions
4
+
5
+ class BaseTest < Assert::Context
6
+ desc "NsOptions::HasOptions"
7
+ setup do
8
+ @class = Class.new do
9
+ include NsOptions::HasOptions
10
+
11
+ def configs_key
12
+ "random_class_#{self.object_id}"
13
+ end
14
+ end
15
+ @instance = @class.new
16
+ end
17
+ subject{ @instance }
18
+
19
+ should have_class_methods :options
20
+ end
21
+
22
+ class OptionsTest < BaseTest
23
+ desc "options method"
24
+
25
+ class WithAKeyTest < OptionsTest
26
+ desc "with a key"
27
+ setup do
28
+ @key = "configs-key"
29
+ @class.options(:configs, @key) do
30
+ option :something
31
+ end
32
+ @instance = @class.new
33
+ end
34
+ subject{ @instance }
35
+
36
+ should have_class_method :configs
37
+ should have_instance_method :configs
38
+
39
+ should "have used the provided key when creating the namespace" do
40
+ assert_kind_of NsOptions::Namespace, subject.class.configs
41
+ assert_kind_of NsOptions::Namespace, subject.configs
42
+ assert_equal @key, subject.class.configs.options.key
43
+ assert_match @key, subject.configs.options.key
44
+ end
45
+ should "have used the provided block to define the namespace" do
46
+ assert_respond_to :something, subject.configs
47
+ assert_respond_to :something=, subject.configs
48
+ assert subject.configs.options.fetch(:something)
49
+ end
50
+ end
51
+ class WithoutAKeyTest < OptionsTest
52
+ desc "without a key"
53
+ setup do
54
+ @name = "configs"
55
+ @class.options(@name.to_sym)
56
+ @instance = @class.new
57
+ end
58
+ subject{ @instance }
59
+
60
+ should "have used the name for the key when creating the namespace" do
61
+ assert_equal @name, subject.class.configs.options.key
62
+ assert_match @name, subject.configs.options.key
63
+ end
64
+ end
65
+ end
66
+
67
+ end
@@ -0,0 +1,63 @@
1
+ require 'assert'
2
+
3
+ module NsOptions::Helper
4
+
5
+ class BaseTest < Assert::Context
6
+ desc "NsOptions::Helper"
7
+ setup do
8
+ @module = NsOptions::Helper
9
+ end
10
+ subject{ @module }
11
+
12
+ should have_instance_methods :new_namespace, :new_child_namespace, :fetch_and_define_option
13
+
14
+ end
15
+
16
+ class NewNamespaceTest < BaseTest
17
+ desc "new_namespace method"
18
+ setup do
19
+ @parent = @module.new_namespace("parent")
20
+ @child = @module.new_namespace("child", @parent) do
21
+ option :something
22
+ end
23
+ end
24
+
25
+ should "have created a parent namespace" do
26
+ assert_equal "parent", @parent.options.key
27
+ end
28
+ should "have created a child namespace" do
29
+ assert_equal "child", @child.options.key
30
+ assert_equal @parent, @child.options.parent
31
+ assert @child.options[:something]
32
+ end
33
+ end
34
+
35
+ class NewChildNamespaceTest < BaseTest
36
+ desc "new_child_namespace method"
37
+ setup do
38
+ @parent = @module.new_namespace("parent")
39
+ @mock_class = mock()
40
+ @mock_class.stubs(:super_settings).returns(@parent)
41
+ @first_owner = mock()
42
+ @first_owner.stubs({ :class => @mock_class })
43
+ @first = @module.new_child_namespace(@first_owner, "super_settings") do
44
+ option :something
45
+ end
46
+ @second_owner = User.new
47
+ @second = @module.new_child_namespace(@second_owner, "preferences")
48
+ end
49
+
50
+ should "have created a child namespace" do
51
+ class_name = @mock_class.to_s.split('::').last.downcase
52
+ key = "#{@parent.options.key}:#{class_name}_#{@first_owner.object_id}"
53
+ assert_equal key, @first.options.key
54
+ assert_equal @parent, @first.options.parent
55
+ assert @first.options[:something]
56
+ end
57
+ should "have created a second child namespace" do
58
+ key = "#{@second.options.parent.options.key}:#{@second_owner.preferences_key}"
59
+ assert_equal key, @second.options.key
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,232 @@
1
+ require 'assert'
2
+
3
+ class NsOptions::Namespace
4
+
5
+ class BaseTest < Assert::Context
6
+ desc "NsOptions::Namespace"
7
+ setup do
8
+ @key = "options"
9
+ @namespace = NsOptions::Namespace.new(@key)
10
+ end
11
+ subject{ @namespace }
12
+
13
+ should have_accessors :metaclass, :options
14
+ should have_instance_methods :option, :namespace, :required_set?, :define
15
+
16
+ should "have set it's metaclass accessor" do
17
+ assert subject.metaclass
18
+ end
19
+ should "have created a new options collection and set it's options accessor" do
20
+ assert subject.options
21
+ assert_kind_of NsOptions::Options, subject.options
22
+ assert_equal @key, subject.options.key
23
+ assert_nil subject.options.parent
24
+ end
25
+ end
26
+
27
+ class OptionTest < BaseTest
28
+ desc "option method"
29
+ setup do
30
+ @name = :something
31
+ @type = NsOptions::Option::Boolean
32
+ @rules = { :default => true }
33
+ @namespace.option(@name, @type, @rules)
34
+ end
35
+ subject{ @namespace }
36
+
37
+ should "have added the option to the namespace's options collection" do
38
+ assert(option = subject.options[@name])
39
+ assert_equal @name.to_s, option.name
40
+ assert_equal @type, option.type_class
41
+ assert_equal @rules, option.rules
42
+ end
43
+ end
44
+
45
+ class OptionWithNoTypeTest < BaseTest
46
+ desc "option method with no type specified"
47
+ setup do
48
+ @name = :something
49
+ @namespace.option(@name)
50
+ end
51
+ subject{ @namespace }
52
+
53
+ should "default the type to String" do
54
+ assert(option = subject.options[@name])
55
+ assert_equal String, option.type_class
56
+ end
57
+ end
58
+
59
+ class OptionMethodsTest < OptionTest
60
+ desc "defined methods"
61
+
62
+ should have_instance_methods :something, :something=
63
+
64
+ should "be writable through the defined writer" do
65
+ assert_nothing_raised{ subject.something = false }
66
+ assert_equal false, subject.something
67
+ end
68
+ should "be writable through the reader with args" do
69
+ assert_nothing_raised{ subject.something true }
70
+ assert_equal true, subject.something
71
+ end
72
+ end
73
+
74
+ class NamespaceTest < BaseTest
75
+ desc "namespace method"
76
+ setup do
77
+ @namespace.namespace(:something) do
78
+ option :something_else
79
+ end
80
+ @namespace.namespace(:another, "special_key")
81
+ end
82
+ subject{ @namespace }
83
+
84
+ should "have added a namespace to the namespace's options collection" do
85
+ assert(namespace = subject.options.namespaces[:something])
86
+ assert_equal "#{subject.options.key}:something", namespace.options.key
87
+ assert_equal subject, namespace.options.parent
88
+ assert namespace.options[:something_else]
89
+ end
90
+ should "allow passing a special key to the namespace" do
91
+ assert(namespace = subject.options.namespaces[:another])
92
+ assert_equal "#{subject.options.key}:special_key", namespace.options.key
93
+ end
94
+ end
95
+
96
+ class NamespaceMethodsTest < NamespaceTest
97
+ desc "defined methods"
98
+
99
+ should have_instance_methods :something
100
+
101
+ should "be return the namespace using the reader" do
102
+ assert_equal subject.options.namespaces[:something], subject.something
103
+ end
104
+ should "define the namespace with the reader and a block" do
105
+ subject.something do
106
+ option :another
107
+ end
108
+ assert subject.something.options[:another]
109
+ end
110
+ end
111
+
112
+ class DefineTest < BaseTest
113
+ desc "define method"
114
+
115
+ class BlockWithoutArityTest < BaseTest
116
+ desc "with a block with no arity"
117
+
118
+ should "instance eval the block in the scope of the namespace" do
119
+ scope = nil
120
+ subject.define do
121
+ scope = self
122
+ end
123
+ assert_equal subject, scope
124
+ end
125
+ end
126
+ class BlockWithArityTest < BaseTest
127
+ desc "with a block with arity"
128
+
129
+ should "yield the namespace to the block" do
130
+ yielded = nil
131
+ subject.define do |namespace|
132
+ yielded = namespace
133
+ end
134
+ assert_equal subject, yielded
135
+ end
136
+ end
137
+ class NoBlockTest < BaseTest
138
+ desc "with no block"
139
+
140
+ should "return the namespace" do
141
+ assert_equal subject, subject.define
142
+ end
143
+ end
144
+ end
145
+
146
+ class MethodMissingTest < BaseTest
147
+ desc "method missing"
148
+ setup do
149
+ @parent = @namespace
150
+ @parent.options.add(:something)
151
+ @parent.options.set(:something, "amazing")
152
+ @namespace = NsOptions::Namespace.new("child", @parent)
153
+ end
154
+
155
+ class ReaderForAKnownOptionTest < MethodMissingTest
156
+ desc "as a reader for a known option"
157
+ setup do
158
+ @result = @namespace.something
159
+ end
160
+
161
+ should "have defined the option on the namespace" do
162
+ defined_methods = subject.metaclass.public_instance_methods(false).map(&:to_sym)
163
+ assert_includes :something, defined_methods
164
+ assert_includes :something=, defined_methods
165
+ assert_equal "amazing", @result
166
+ end
167
+ end
168
+ class WriterForAKnownOptionTest < MethodMissingTest
169
+ desc "as a reader for a known option"
170
+ setup do
171
+ @namespace.something = "even more amazing"
172
+ end
173
+
174
+ should "have defined the option on the namespace" do
175
+ defined_methods = subject.metaclass.public_instance_methods(false).map(&:to_sym)
176
+ assert_includes :something, defined_methods
177
+ assert_includes :something=, defined_methods
178
+ assert_equal "even more amazing", subject.something
179
+ end
180
+ end
181
+ class DynamicWriterTest < MethodMissingTest
182
+ desc "with a writer for an unknown option"
183
+ setup do
184
+ @namespace.something_not_defined = "you know it"
185
+ @namespace.another_not_defined = true
186
+ @namespace.even_more_not_defined = 12
187
+ end
188
+
189
+ should "have defined the accessors and added the option" do
190
+ defined_methods = subject.metaclass.public_instance_methods(false).map(&:to_sym)
191
+ assert subject.options[:something_not_defined]
192
+ assert_includes :something_not_defined, defined_methods
193
+ assert_includes :something_not_defined=, defined_methods
194
+ assert_equal "you know it", subject.something_not_defined
195
+ end
196
+ should "use the class of the value for the option's type class" do
197
+ assert_equal String, subject.options[:something_not_defined].type_class
198
+ assert_equal Integer, subject.options[:even_more_not_defined].type_class
199
+ assert_equal NsOptions::Option::Boolean, subject.options[:another_not_defined].type_class
200
+ end
201
+ end
202
+ end
203
+
204
+ class RespondToTest < BaseTest
205
+ desc "respond to"
206
+ setup do
207
+ @namespace.options.add(:something)
208
+ end
209
+
210
+ should "return true when the reader of an option is requested without the reader defined" do
211
+ assert_not_includes :something, subject.class.public_instance_methods(false).map(&:to_sym)
212
+ assert_not_includes :something, subject.metaclass.public_instance_methods(false).map(&:to_sym)
213
+ assert_equal true, subject.respond_to?(:something)
214
+ end
215
+ end
216
+
217
+ class WithAParentTest < BaseTest
218
+ desc "with a parent"
219
+ setup do
220
+ @key = "options"
221
+ @parent = @namespace
222
+ @namespace = NsOptions::Namespace.new(@key, @parent)
223
+ end
224
+ subject{ @namespace }
225
+
226
+ should "have set it's options accessor and stored it's parent on it" do
227
+ assert subject.options
228
+ assert_equal @parent, subject.options.parent
229
+ end
230
+ end
231
+
232
+ end
@@ -0,0 +1,55 @@
1
+ require 'assert'
2
+
3
+ class NsOptions::Namespaces
4
+
5
+ class BaseTest < Assert::Context
6
+ desc "NsOptions::Namespaces"
7
+ setup do
8
+ @namespaces = NsOptions::Namespaces.new
9
+ end
10
+ subject{ @namespaces }
11
+
12
+ should have_instance_methods :add, :get
13
+
14
+ should "be a kind of a hash" do
15
+ assert_kind_of Hash, subject
16
+ end
17
+ should "only use symbols for keys" 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
+ end
26
+
27
+ class AddTest < BaseTest
28
+ desc "add method"
29
+ setup do
30
+ @parent = NsOptions::Namespace.new(:parent)
31
+ @namespaces.add(:a_name, "a-key", @parent) do
32
+ option :an_option
33
+ end
34
+ end
35
+ subject{ @namespaces }
36
+
37
+ should "have created a new namespace and added it to itself" do
38
+ assert(namespace = subject[:a_name])
39
+ assert_kind_of NsOptions::Namespace, namespace
40
+ assert_equal "a-key", namespace.options.key
41
+ assert_equal @parent, namespace.options.parent
42
+ assert namespace.options[:an_option]
43
+ end
44
+ end
45
+
46
+ class GetTest < AddTest
47
+ desc "get method"
48
+
49
+ should "return the namespace matching the name" do
50
+ assert(namespace = subject.get("a_name"))
51
+ assert_equal subject[:a_name], namespace
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,67 @@
1
+ require 'assert'
2
+
3
+ class NsOptions::Option::Boolean
4
+
5
+ class BaseTest < Assert::Context
6
+ desc "NsOptions::Option::Boolean"
7
+ setup do
8
+ @boolean = NsOptions::Option::Boolean.new(true)
9
+ end
10
+ subject{ @boolean }
11
+
12
+ should have_accessors :actual
13
+ end
14
+
15
+ class WithTruthyValuesTest < BaseTest
16
+ desc "with truthy values"
17
+ setup do
18
+ @boolean = NsOptions::Option::Boolean.new(false)
19
+ end
20
+
21
+ should "have set actual to true with true" do
22
+ subject.actual = true
23
+ assert_equal true, subject.actual
24
+ end
25
+ should "have set actual to true with 'true'" do
26
+ subject.actual = 'true'
27
+ assert_equal true, subject.actual
28
+ end
29
+ should "have set actual to true with 1" do
30
+ subject.actual = 1
31
+ assert_equal true, subject.actual
32
+ end
33
+ should "have set actual to true with '1'" do
34
+ subject.actual = '1'
35
+ assert_equal true, subject.actual
36
+ end
37
+ end
38
+
39
+ class WithFalsyValuesTest < BaseTest
40
+ desc "with falsy values"
41
+ setup do
42
+ @boolean = NsOptions::Option::Boolean.new(true)
43
+ end
44
+
45
+ should "have set actual to false with false" do
46
+ subject.actual = false
47
+ assert_equal false, subject.actual
48
+ end
49
+ should "have set actual to false with 'false'" do
50
+ subject.actual = 'false'
51
+ assert_equal false, subject.actual
52
+ end
53
+ should "have set actual to false with 0" do
54
+ subject.actual = 0
55
+ assert_equal false, subject.actual
56
+ end
57
+ should "have set actual to false with '0'" do
58
+ subject.actual = '0'
59
+ assert_equal false, subject.actual
60
+ end
61
+ should "have set actual to false with nil" do
62
+ subject.actual = nil
63
+ assert_equal false, subject.actual
64
+ end
65
+ end
66
+
67
+ end