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,191 @@
1
+ require 'assert'
2
+
3
+ class NsOptions::Option
4
+
5
+ class BaseTest < Assert::Context
6
+ desc "NsOptions::Option"
7
+ setup do
8
+ @rules = { :default => "development", :require => true }
9
+ @option = NsOptions::Option.new(:stage, String, @rules)
10
+ end
11
+ subject{ @option }
12
+
13
+ should have_accessors :name, :value, :type_class, :rules
14
+
15
+ should "have set the name" do
16
+ assert_equal "stage", subject.name
17
+ end
18
+ should "have set the type class" do
19
+ assert_equal String, subject.type_class
20
+ end
21
+ should "have set the rules" do
22
+ assert_equal(@rules, subject.rules)
23
+ end
24
+ should "have defaulted value based on the rules" do
25
+ assert_equal subject.rules[:default], subject.value
26
+ end
27
+ should "return true with a call to #required?" do
28
+ assert_equal true, subject.required?
29
+ end
30
+ end
31
+
32
+ class IsSetTest < BaseTest
33
+ desc "is_set method"
34
+ setup do
35
+ @type_class = Class.new(String) do
36
+
37
+ def is_set?
38
+ self.gsub(/\s+/, '').size != 0
39
+ end
40
+
41
+ end
42
+ @special = NsOptions::Option.new(:no_blank, @type_class)
43
+ end
44
+
45
+ should "return true/false appropriately for a boolean option with true or false" do
46
+ @option.value = true
47
+ assert_equal true, @option.is_set?
48
+ @option.value = false
49
+ assert_equal true, @option.is_set?
50
+ @option.value = nil
51
+ assert_equal false, @option.is_set?
52
+ end
53
+ should "return true/false based on type class's is_set method" do
54
+ @special.value = "not blank"
55
+ assert_equal true, @special.is_set?
56
+ @special.value = " "
57
+ assert_equal false, @special.is_set?
58
+ end
59
+ end
60
+
61
+ class EqualityOperatorTest < BaseTest
62
+ desc "== operator"
63
+ setup do
64
+ @first = NsOptions::Option.new(:stage, String)
65
+ @first.value = "test"
66
+ @second = NsOptions::Option.new(:stage, String)
67
+ @second.value = "test"
68
+ end
69
+
70
+ should "return true if their attributes are equal" do
71
+ [ :name, :type_class, :value, :rules ].each do |attribute|
72
+ assert_equal @first.send(attribute), @second.send(attribute)
73
+ end
74
+ assert_equal @first, @second
75
+ @first.value = "staging"
76
+ assert_not_equal @first, @second
77
+ end
78
+ end
79
+
80
+ class WithNativeTypeClassTest < BaseTest
81
+ desc "with a native type class (Float)"
82
+ setup do
83
+ @option = NsOptions::Option.new(:something, Float)
84
+ end
85
+ subject{ @option }
86
+
87
+ should "allow setting it's value with a float" do
88
+ new_value = 12.5
89
+ subject.value = new_value
90
+ assert_equal new_value, subject.value
91
+ end
92
+ should "allow setting it's value with a string and convert it" do
93
+ new_value = "13.4"
94
+ subject.value = new_value
95
+ assert_equal new_value.to_f, subject.value
96
+ end
97
+ should "allow setting it's value with an integer and convert it" do
98
+ new_value = 1
99
+ subject.value = new_value
100
+ assert_equal new_value.to_f, subject.value
101
+ end
102
+ end
103
+
104
+ class WithTypeClassFixnumTest < BaseTest
105
+ desc "with the Fixnum as a type class (happens through dynamic writers)"
106
+ setup do
107
+ @option = NsOptions::Option.new(:something, Fixnum)
108
+ end
109
+ subject{ @option }
110
+
111
+ should "have used Integer for it's type class" do
112
+ assert_equal Integer, subject.type_class
113
+ end
114
+ should "allow setting it's value with an integer" do
115
+ new_value = 1
116
+ subject.value = new_value
117
+ assert_equal new_value, subject.value
118
+ end
119
+ should "allow setting it's value with a float and convert it" do
120
+ new_value = 12.5
121
+ subject.value = new_value
122
+ assert_equal new_value.to_i, subject.value
123
+ end
124
+ should "allow setting it's value with a string and convert it" do
125
+ new_value = "13"
126
+ subject.value = new_value
127
+ assert_equal new_value.to_i, subject.value
128
+ end
129
+ end
130
+
131
+ class WithHashTypeClassTest < BaseTest
132
+ desc "with a Hash as a type class"
133
+ setup do
134
+ @option = NsOptions::Option.new(:something, Hash)
135
+ end
136
+ subject{ @option }
137
+
138
+ should "allow setting it with a hash" do
139
+ new_value = { :another => true }
140
+ subject.value = new_value
141
+ assert_equal new_value, subject.value
142
+ end
143
+ end
144
+
145
+ class WithArrayTypeClassTest < BaseTest
146
+ desc "with an Array as a type class"
147
+ setup do
148
+ @option = NsOptions::Option.new(:something, Array)
149
+ end
150
+ subject{ @option }
151
+
152
+ should "allow setting it with a array" do
153
+ new_value = [ :something, :else, :another ]
154
+ subject.value = new_value
155
+ assert_equal new_value, subject.value
156
+ end
157
+ end
158
+
159
+ class WithTrueFalseClassTest < BaseTest
160
+ desc "with a TrueClass/FalseClass as a type class (happens with dynamic writers)"
161
+ setup do
162
+ @true_option = NsOptions::Option.new(:something, TrueClass)
163
+ @true_option.value = true
164
+ @false_option = NsOptions::Option.new(:else, FalseClass)
165
+ @false_option.value = false
166
+ end
167
+ subject{ @true_option }
168
+
169
+ should "have used NsOptions::Option::Boolean for their type class" do
170
+ assert_equal NsOptions::Option::Boolean, @true_option.type_class
171
+ assert_equal NsOptions::Option::Boolean, @false_option.type_class
172
+ end
173
+ should "return the 'true' or 'false' value instead of the NsOptions::Option::Boolean object" do
174
+ assert_equal true, @true_option.value
175
+ assert_equal false, @false_option.value
176
+ end
177
+ end
178
+
179
+ class WithoutTypeClassTest < BaseTest
180
+ desc "without a type class provided"
181
+ setup do
182
+ @option = NsOptions::Option.new(:something, nil)
183
+ end
184
+ subject{ @option }
185
+
186
+ should "have default it to String" do
187
+ assert_equal String, subject.type_class
188
+ end
189
+ end
190
+
191
+ end
@@ -0,0 +1,227 @@
1
+ require 'assert'
2
+
3
+ class NsOptions::Options
4
+
5
+ class BaseTest < Assert::Context
6
+ desc "NsOptions::Options"
7
+ setup do
8
+ @options = NsOptions::Options.new(:something)
9
+ end
10
+ subject{ @options }
11
+
12
+ should have_accessors :key, :parent, :children
13
+ should have_instance_method :namespaces, :add, :del, :remove, :get, :set, :fetch, :is_defined?,
14
+ :parent_options
15
+
16
+ should "be a kind of Hash" do
17
+ assert_kind_of Hash, subject
18
+ end
19
+ should "only use symbols for keys" do
20
+ subject["string_key"] = true
21
+ subject[:symbol_key] = true
22
+
23
+ assert_includes :string_key, subject.keys
24
+ assert_includes :symbol_key, subject.keys
25
+ assert_not_includes "string_key", subject.keys
26
+ end
27
+ should "have set the key" do
28
+ assert_equal "something", subject.key
29
+ end
30
+ should "have set the parent to nil" do
31
+ assert_nil subject.parent
32
+ end
33
+ should "return a kind of NsOption::Namespaces with a call to #children" do
34
+ assert_kind_of NsOptions::Namespaces, subject.children
35
+ end
36
+ end
37
+
38
+ class AddTest < BaseTest
39
+ desc "add method"
40
+ setup do
41
+ @options.add(:my_string)
42
+ @options.add(:my_integer, Integer)
43
+ @options.add(:my_float, Float, { :default => 1.0 })
44
+ end
45
+ subject{ @options }
46
+
47
+ should "have added a string option on itself when adding :my_string" do
48
+ assert(option = subject[:my_string])
49
+ assert_equal String, option.type_class
50
+ assert_equal({}, option.rules)
51
+ end
52
+ should "have added an integer option on itself when adding :my_integer" do
53
+ assert(option = subject[:my_integer])
54
+ assert_equal Integer, option.type_class
55
+ assert_equal({}, option.rules)
56
+ end
57
+ should "have added a float option on itself when adding :my_float" do
58
+ assert(option = subject[:my_float])
59
+ assert_equal Float, option.type_class
60
+ assert_equal({ :default => 1.0 }, option.rules)
61
+ end
62
+ end
63
+
64
+ class DelTest < BaseTest
65
+ desc "add method"
66
+ setup do
67
+ @options.add(:my_string)
68
+ @options.del(:my_string)
69
+ end
70
+ subject{ @options }
71
+
72
+ should "remove the option definition from the collection" do
73
+ assert_nil subject[:my_string]
74
+ end
75
+ end
76
+
77
+ class GetTest < BaseTest
78
+ desc "get method"
79
+ setup do
80
+ option = @options.add(:my_string)
81
+ option.value = @value = "something"
82
+ @result = @options.get(:my_string)
83
+ end
84
+ subject{ @result }
85
+
86
+ should "have returned the option's value" do
87
+ assert_equal @value, subject
88
+ end
89
+
90
+ class WithParentTest < GetTest
91
+ desc "with a parent"
92
+ setup do
93
+ @parent = NsOptions::Namespace.new(:child)
94
+ @parent.options = @options
95
+ @options = NsOptions::Options.new(:child, @parent)
96
+ @result = @options.get(:my_string)
97
+ end
98
+ subject{ @result }
99
+
100
+ should "have returned the parent's option's value" do
101
+ assert_equal @value, subject
102
+ end
103
+ end
104
+ end
105
+
106
+ class SetTest < BaseTest
107
+ desc "set method"
108
+ setup do
109
+ option = @options.add(:my_string)
110
+ @options.set(:my_string, "something")
111
+ end
112
+ subject{ @options }
113
+
114
+ should "have set the option's value" do
115
+ assert_equal "something", subject.get(:my_string)
116
+ end
117
+ end
118
+
119
+ class FetchTest < BaseTest
120
+ desc "fetch method"
121
+ setup do
122
+ option = @options.add(:my_string)
123
+ @result = @options.fetch(:my_string)
124
+ end
125
+ subject{ @result }
126
+
127
+ should "return the option definition for my_string" do
128
+ assert_kind_of NsOptions::Option, subject
129
+ assert_equal "my_string", subject.name
130
+ end
131
+
132
+ class WithAParentTest < FetchTest
133
+ desc "with a parent"
134
+ setup do
135
+ @parent = NsOptions::Namespace.new(:child)
136
+ @parent.options = @options
137
+ @options = NsOptions::Options.new(:child, @parent)
138
+ @result = @options.fetch(:my_string)
139
+ end
140
+ subject{ @result }
141
+
142
+ should "return the option definition for my_string from it's parent" do
143
+ assert_kind_of NsOptions::Option, subject
144
+ assert_equal "my_string", subject.name
145
+ end
146
+ end
147
+ end
148
+
149
+ class IsDefinedTest < BaseTest
150
+ desc "fetch method"
151
+ setup do
152
+ option = @options.add(:my_string)
153
+ end
154
+ subject{ @options }
155
+
156
+ should "return true for a defined option" do
157
+ assert_equal true, subject.is_defined?(:my_string)
158
+ end
159
+ should "return false for an undefined option" do
160
+ assert_equal false, subject.is_defined?(:undefined)
161
+ end
162
+
163
+ class WithAParentTest < IsDefinedTest
164
+ desc "with a parent"
165
+ setup do
166
+ @parent = NsOptions::Namespace.new(:child)
167
+ @parent.options = @options
168
+ @options = NsOptions::Options.new(:child, @parent)
169
+ end
170
+
171
+ should "return true for an option defined on it's parent" do
172
+ assert_equal true, subject.is_defined?(:my_string)
173
+ end
174
+ end
175
+ end
176
+
177
+ class ParentOptionsTest < BaseTest
178
+ desc "parent_options method"
179
+ subject{ @options }
180
+
181
+ should "return nil" do
182
+ assert_nil subject.parent_options
183
+ end
184
+
185
+ class WithAParentTest < ParentOptionsTest
186
+ desc "with a parent"
187
+ setup do
188
+ @parent = NsOptions::Namespace.new(:child)
189
+ @parent.options = @options
190
+ @options = NsOptions::Options.new(:child, @parent)
191
+ end
192
+
193
+ should "return it's parent's options" do
194
+ assert_equal @parent.options, subject.parent_options
195
+ end
196
+ end
197
+ end
198
+
199
+ class RequiredSetTest < BaseTest
200
+ desc "required_set? method"
201
+ setup do
202
+ @options.add(:first, String, { :require => true })
203
+ @options.add(:second, String, { :required => true })
204
+ @options.add(:third, String)
205
+ end
206
+
207
+ should "return true when all required options are set" do
208
+ @options.set(:first, "first")
209
+ @options.set(:second, "second")
210
+ assert_equal true, subject.required_set?
211
+ end
212
+ should "return false if one required option is not set" do
213
+ @options.set(:first, "first")
214
+ @options.set(:third, "third")
215
+ assert_equal false, subject.required_set?
216
+ end
217
+ should "not change because of options that aren't required" do
218
+ @options.set(:first, "first")
219
+ @options.set(:second, "second")
220
+ @options.set(:third, "third")
221
+ assert_equal true, subject.required_set?
222
+ @options.set(:third, nil)
223
+ assert_equal true, subject.required_set?
224
+ end
225
+ end
226
+
227
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ns-options
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Collin Redding
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-30 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :development
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 0
31
+ - 6
32
+ - 0
33
+ version: 0.6.0
34
+ version_requirements: *id001
35
+ name: assert
36
+ description: Define and use namespaced options with a clean interface.
37
+ email:
38
+ - collin.redding@reelfx.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - README.markdown
49
+ - Rakefile
50
+ - lib/ns-options.rb
51
+ - lib/ns-options/has_options.rb
52
+ - lib/ns-options/helper.rb
53
+ - lib/ns-options/namespace.rb
54
+ - lib/ns-options/namespaces.rb
55
+ - lib/ns-options/option.rb
56
+ - lib/ns-options/option/boolean.rb
57
+ - lib/ns-options/options.rb
58
+ - lib/ns-options/version.rb
59
+ - ns-options.gemspec
60
+ - test/helper.rb
61
+ - test/integration/app_test.rb
62
+ - test/integration/user_test.rb
63
+ - test/support/app.rb
64
+ - test/support/user.rb
65
+ - test/unit/ns-options/has_options_test.rb
66
+ - test/unit/ns-options/helper_test.rb
67
+ - test/unit/ns-options/namespace_test.rb
68
+ - test/unit/ns-options/namespaces_test.rb
69
+ - test/unit/ns-options/option/boolean_test.rb
70
+ - test/unit/ns-options/option_test.rb
71
+ - test/unit/ns-options/options_test.rb
72
+ homepage:
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options: []
77
+
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.10
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Define and use namespaced options with a clean interface.
105
+ test_files:
106
+ - test/helper.rb
107
+ - test/integration/app_test.rb
108
+ - test/integration/user_test.rb
109
+ - test/support/app.rb
110
+ - test/support/user.rb
111
+ - test/unit/ns-options/has_options_test.rb
112
+ - test/unit/ns-options/helper_test.rb
113
+ - test/unit/ns-options/namespace_test.rb
114
+ - test/unit/ns-options/namespaces_test.rb
115
+ - test/unit/ns-options/option/boolean_test.rb
116
+ - test/unit/ns-options/option_test.rb
117
+ - test/unit/ns-options/options_test.rb