params 0.0.9 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/params.rb +315 -230
- data/lib/params/version.rb +2 -4
- data/test/params/params_spec.rb +226 -228
- data/test/params/params_test.rb +28 -30
- metadata +4 -4
data/lib/params/version.rb
CHANGED
data/test/params/params_spec.rb
CHANGED
@@ -6,275 +6,273 @@ require 'yaml'
|
|
6
6
|
|
7
7
|
require_relative '../../lib/params.rb'
|
8
8
|
|
9
|
-
module
|
10
|
-
|
11
|
-
|
12
|
-
public_class_method :print_global_parameters, :parse_command_line_arguments, \
|
9
|
+
module Params
|
10
|
+
# make private methods or Params public for testing capability.
|
11
|
+
public_class_method :print_global_parameters, :parse_command_line_arguments, \
|
13
12
|
:raise_error_if_param_exists, :raise_error_if_param_does_not_exist, \
|
14
13
|
:read_yml_params, :override_param
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
15
|
+
module Spec
|
16
|
+
|
17
|
+
describe 'Params::parameter' do
|
18
|
+
|
19
|
+
it 'should define a new parameters' do
|
20
|
+
Params.string 'par_str', 'sss' ,'desc_str'
|
21
|
+
Params.integer 'par_int',1 , 'desc_int'
|
22
|
+
Params.float 'par_float',2.6 , 'desc_float'
|
23
|
+
Params.boolean 'par_true', true, 'desc_true'
|
24
|
+
Params.boolean 'par_false',false , 'desc_false'
|
25
|
+
Params['par_str'].should eq 'sss'
|
26
|
+
Params['par_int'].should eq 1
|
27
|
+
Params['par_float'].should eq 2.6
|
28
|
+
Params['par_true'].should eq true
|
29
|
+
Params['par_false'].should eq false
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should raise an error for wrong parameter type definition.' do
|
33
|
+
expect { Params::Param.new 'bad_type', 5, 'non_existing_type', 'desc_bad_type' }.to raise_error
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should raise an error when trying to define twice the same parameter' do
|
37
|
+
Params.string 'only_once', '1st' ,''
|
38
|
+
expect { Params.string 'only_once', '2nd' ,'' }.to raise_error \
|
40
39
|
"Parameter:'only_once', can only be defined once."
|
41
|
-
end
|
42
40
|
end
|
41
|
+
end
|
43
42
|
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
describe 'Params::read_yml_params' do
|
44
|
+
it 'should raise error when yml parameter is not defined' do
|
45
|
+
expect { Params::read_yml_params StringIO.new 'not_defined: 10' }.to raise_error \
|
47
46
|
"Parameter:'not_defined' has not been defined and can not be overridden. " \
|
48
47
|
"It should first be defined through Param module methods:" \
|
49
|
-
"Params.string, Params.integer, Params.float or Params.boolean."
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
48
|
+
"Params.string, Params.path, Params.integer, Params.float or Params.boolean."
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'Will test yml parameter loading' do
|
52
|
+
# string to other. Will not raise error. Instead a cast is made.
|
53
|
+
Params.string('tmp4str', 'string_value', 'tmp4 def')
|
54
|
+
expect { Params::read_yml_params StringIO.new 'tmp4str: strr' }.to_not raise_error
|
55
|
+
expect { Params::read_yml_params StringIO.new 'tmp4str: 4' }.to_not raise_error
|
56
|
+
expect { Params::read_yml_params StringIO.new 'tmp4str: 4.5' }.to_not raise_error
|
57
|
+
expect { Params::read_yml_params StringIO.new 'tmp4str: true' }.to_not raise_error
|
58
|
+
expect { Params::read_yml_params StringIO.new 'tmp4str: false' }.to_not raise_error
|
59
|
+
|
60
|
+
# override integer with other types.
|
61
|
+
Params.integer('tmp4int', 1, 'tmp4 def')
|
62
|
+
expect { Params::read_yml_params StringIO.new 'tmp4int: strr' }.to raise_error \
|
64
63
|
"Parameter:'tmp4int' type:'Integer' but value type to override " \
|
65
64
|
"is:'String'."
|
66
|
-
|
67
|
-
|
65
|
+
expect { Params::read_yml_params StringIO.new 'tmp4int: 4' }.to_not raise_error
|
66
|
+
expect { Params::read_yml_params StringIO.new 'tmp4int: 4.5' }.to raise_error \
|
68
67
|
"Parameter:'tmp4int' type:'Integer' but value type to override " \
|
69
68
|
"is:'Float'."
|
70
|
-
|
69
|
+
expect { Params::read_yml_params StringIO.new 'tmp4int: true' }.to raise_error \
|
71
70
|
"Parameter:'tmp4int' type:'Integer' but value type to override " \
|
72
71
|
"is:'TrueClass'."
|
73
|
-
|
72
|
+
expect { Params::read_yml_params StringIO.new 'tmp4int: false' }.to raise_error \
|
74
73
|
"Parameter:'tmp4int' type:'Integer' but value type to override " \
|
75
74
|
"is:'FalseClass'."
|
76
75
|
|
77
|
-
|
78
|
-
|
79
|
-
|
76
|
+
# override float with other types.
|
77
|
+
Params.float('tmp4float', 1.1, 'tmp4 def')
|
78
|
+
expect { Params::read_yml_params StringIO.new 'tmp4float: strr' }.to raise_error \
|
80
79
|
"Parameter:'tmp4float' type:'Float' but value type to override " \
|
81
80
|
"is:'String'."
|
82
|
-
|
83
|
-
|
84
|
-
|
81
|
+
expect { Params::read_yml_params StringIO.new 'tmp4float: 4' }.to_not raise_error
|
82
|
+
expect { Params::read_yml_params StringIO.new 'tmp4float: 4.5' }.to_not raise_error
|
83
|
+
expect { Params::read_yml_params StringIO.new 'tmp4float: true' }.to raise_error \
|
85
84
|
"Parameter:'tmp4float' type:'Float' but value type to override " \
|
86
85
|
"is:'TrueClass'."
|
87
|
-
|
86
|
+
expect { Params::read_yml_params StringIO.new 'tmp4float: false' }.to raise_error \
|
88
87
|
"Parameter:'tmp4float' type:'Float' but value type to override " \
|
89
88
|
"is:'FalseClass'."
|
90
|
-
|
91
|
-
|
92
|
-
|
89
|
+
# override boolean with other types.
|
90
|
+
Params.boolean('tmp4true', true, 'tmp4 def')
|
91
|
+
expect { Params::read_yml_params StringIO.new 'tmp4true: strr' }.to raise_error \
|
93
92
|
"Parameter:'tmp4true' type:'Boolean' but value type to override " \
|
94
93
|
"is:'String'."
|
95
|
-
|
94
|
+
expect { Params::read_yml_params StringIO.new 'tmp4true: 4' }.to raise_error \
|
96
95
|
"Parameter:'tmp4true' type:'Boolean' but value type to override " \
|
97
96
|
"is:'Fixnum'."
|
98
|
-
|
97
|
+
expect { Params::read_yml_params StringIO.new 'tmp4true: 4.5' }.to raise_error \
|
99
98
|
"Parameter:'tmp4true' type:'Boolean' but value type to override " \
|
100
99
|
"is:'Float'."
|
101
|
-
|
102
|
-
|
100
|
+
expect { Params::read_yml_params StringIO.new 'tmp4true: true' }.to_not raise_error
|
101
|
+
expect { Params.read_yml_params StringIO.new 'tmp4true: false' }.to_not raise_error
|
103
102
|
|
104
|
-
|
105
|
-
|
103
|
+
Params.boolean('tmp4False', true, 'tmp4 def')
|
104
|
+
expect { Params.read_yml_params StringIO.new 'tmp4False: strr' }.to raise_error \
|
106
105
|
"Parameter:'tmp4False' type:'Boolean' but value type to override " \
|
107
106
|
"is:'String'."
|
108
|
-
|
107
|
+
expect { Params.read_yml_params StringIO.new 'tmp4False: 4' }.to raise_error \
|
109
108
|
"Parameter:'tmp4False' type:'Boolean' but value type to override " \
|
110
109
|
"is:'Fixnum'."
|
111
|
-
|
110
|
+
expect { Params.read_yml_params StringIO.new 'tmp4False: 4.5' }.to raise_error \
|
112
111
|
"Parameter:'tmp4False' type:'Boolean' but value type to override " \
|
113
112
|
"is:'Float'."
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
113
|
+
expect { Params.read_yml_params StringIO.new 'tmp4False: true' }.to_not raise_error
|
114
|
+
expect { Params.read_yml_params StringIO.new 'tmp4False: false' }.to_not raise_error
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should raise error when yml file format is bad' do
|
119
|
+
expect { Params.read_yml_params StringIO.new '"bad yml format' }.to raise_error
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'should override defined values with yml values' do
|
123
|
+
Params.string('tmp5str', 'aaa', 'tmp5 def')
|
124
|
+
Params.integer('tmp5int', 11, 'tmp5 def')
|
125
|
+
Params.float('tmp5float', 11.11, 'tmp5 def')
|
126
|
+
Params.boolean('tmp5true', true, 'tmp5 def')
|
127
|
+
Params.boolean('tmp5false', false, 'tmp5 def')
|
128
|
+
Params.read_yml_params StringIO.new "tmp5str: bbb\ntmp5int: 12\ntmp5float: 12.12\n"
|
129
|
+
Params.read_yml_params StringIO.new "tmp5true: false\ntmp5false: true\n"
|
130
|
+
Params['tmp5str'].should eq 'bbb'
|
131
|
+
Params['tmp5int'].should eq 12
|
132
|
+
Params['tmp5float'].should eq 12.12
|
133
|
+
Params['tmp5true'].should eq false
|
134
|
+
Params['tmp5false'].should eq true
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe 'Params.parse_command_line_arguments' do
|
139
|
+
it 'should raise error when command line parameter is not defined' do
|
140
|
+
expect { Params.parse_command_line_arguments ['--new_param=9]'] }.to raise_error
|
137
141
|
end
|
138
142
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
expect { Params.parse_command_line_arguments ['--tmp9=9'] }.to_not raise_error
|
181
|
-
expect { Params.parse_command_line_arguments ['--tmp9=3.4'] }.to_not raise_error
|
182
|
-
expect { Params.parse_command_line_arguments ['--tmp9=ff'] }.to_not raise_error
|
183
|
-
expect { Params.parse_command_line_arguments ['--tmp9=true'] }.to_not raise_error
|
184
|
-
expect { Params.parse_command_line_arguments ['--tmp9=false'] }.to_not raise_error
|
185
|
-
end
|
143
|
+
it 'should parse parameter from command line.' do
|
144
|
+
# Override string with types.
|
145
|
+
Params.string('tmp6str', 'dummy', 'tmp6str def')
|
146
|
+
expect { Params.parse_command_line_arguments ['--tmp6str=9'] }.to_not raise_error
|
147
|
+
expect { Params.parse_command_line_arguments ['--tmp6str=8.1'] }.to_not raise_error
|
148
|
+
expect { Params.parse_command_line_arguments ['--tmp6str=ff'] }.to_not raise_error
|
149
|
+
expect { Params.parse_command_line_arguments ['--tmp6str=true'] }.to_not raise_error
|
150
|
+
expect { Params.parse_command_line_arguments ['--tmp6str=false'] }.to_not raise_error
|
151
|
+
|
152
|
+
# from fixnum to other types.
|
153
|
+
Params.integer('tmp6', 8, 'tmp6 def')
|
154
|
+
expect { Params.parse_command_line_arguments ['--tmp6=9'] }.to_not raise_error
|
155
|
+
expect { Params.parse_command_line_arguments ['--tmp6=8.1'] }.to raise_error
|
156
|
+
expect { Params.parse_command_line_arguments ['--tmp6=ff'] }.to raise_error
|
157
|
+
expect { Params.parse_command_line_arguments ['--tmp6=true'] }.to raise_error
|
158
|
+
expect { Params.parse_command_line_arguments ['--tmp6=false'] }.to raise_error
|
159
|
+
|
160
|
+
# from float to other types.
|
161
|
+
Params.float('tmp7', 8.9, 'tmp7 def')
|
162
|
+
# Casting fix num to float
|
163
|
+
expect { Params.parse_command_line_arguments ['--tmp7=9'] }.to_not raise_error
|
164
|
+
expect { Params.parse_command_line_arguments ['--tmp7=3.4'] }.to_not raise_error
|
165
|
+
expect { Params.parse_command_line_arguments ['--tmp7=ff'] }.to raise_error
|
166
|
+
expect { Params.parse_command_line_arguments ['--tmp7=true'] }.to raise_error
|
167
|
+
expect { Params.parse_command_line_arguments ['--tmp7=false'] }.to raise_error
|
168
|
+
|
169
|
+
# from TrueClass to other types.
|
170
|
+
Params.boolean('tmp8', true, 'tmp8 def')
|
171
|
+
expect { Params.parse_command_line_arguments ['--tmp8=9'] }.to_not raise_error
|
172
|
+
expect { Params.parse_command_line_arguments ['--tmp8=3.4'] }.to_not raise_error
|
173
|
+
expect { Params.parse_command_line_arguments ['--tmp8=ff'] }.to_not raise_error
|
174
|
+
expect { Params.parse_command_line_arguments ['--tmp8=true'] }.to_not raise_error
|
175
|
+
expect { Params.parse_command_line_arguments ['--tmp8=false'] }.to_not raise_error
|
176
|
+
|
177
|
+
# from FalseClass to other types.
|
178
|
+
Params.boolean('tmp9', false, 'tmp9 def')
|
179
|
+
expect { Params.parse_command_line_arguments ['--tmp9=9'] }.to_not raise_error
|
180
|
+
expect { Params.parse_command_line_arguments ['--tmp9=3.4'] }.to_not raise_error
|
181
|
+
expect { Params.parse_command_line_arguments ['--tmp9=ff'] }.to_not raise_error
|
182
|
+
expect { Params.parse_command_line_arguments ['--tmp9=true'] }.to_not raise_error
|
183
|
+
expect { Params.parse_command_line_arguments ['--tmp9=false'] }.to_not raise_error
|
186
184
|
end
|
185
|
+
end
|
187
186
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
187
|
+
describe 'Params.init' do
|
188
|
+
it 'should override command line arguments correctly.' do
|
189
|
+
File.stub(:exist?).and_return false
|
190
|
+
# Override string with types.
|
191
|
+
Params.string('tmp6str2', 'dummy', 'tmp6str def')
|
192
|
+
expect { Params.init ['--tmp6str2=9'] }.to_not raise_error
|
193
|
+
expect { Params.init ['--tmp6str2=8.1'] }.to_not raise_error
|
194
|
+
expect { Params.init ['--tmp6str2=ff'] }.to_not raise_error
|
195
|
+
expect { Params.init ['--tmp6str2=true'] }.to_not raise_error
|
196
|
+
expect { Params.init ['--tmp6str2=false'] }.to_not raise_error
|
197
|
+
|
198
|
+
# from fixnum to other types.
|
199
|
+
Params.integer('tmp6Fixnum2', 8, 'tmp6 def')
|
200
|
+
expect { Params.init ['--tmp6Fixnum2=9'] }.to_not raise_error
|
201
|
+
expect { Params.init ['--tmp6Fixnum2=8.1'] }.to raise_error
|
202
|
+
expect { Params.init ['--tmp6Fixnum2=ff'] }.to raise_error
|
203
|
+
expect { Params.init ['--tmp6Fixnum2=true'] }.to raise_error
|
204
|
+
expect { Params.init ['--tmp6Fixnum2=false'] }.to raise_error
|
205
|
+
|
206
|
+
# from float to other types.
|
207
|
+
Params.float('tmp7float2', 8.9, 'tmp7 def')
|
208
|
+
# Casting fix num to float
|
209
|
+
expect { Params.init ['--tmp7float2=9'] }.to_not raise_error
|
210
|
+
expect { Params.init ['--tmp7float2=3.4'] }.to_not raise_error
|
211
|
+
expect { Params.init ['--tmp7float2=ff'] }.to raise_error
|
212
|
+
expect { Params.init ['--tmp7float2=true'] }.to raise_error
|
213
|
+
expect { Params.init ['--tmp7float2=false'] }.to raise_error
|
214
|
+
|
215
|
+
# from TrueClass to other types.
|
216
|
+
Params.boolean('tmp8true2', true, 'tmp8 def')
|
217
|
+
expect { Params.init ['--tmp8true2=9'] }.to raise_error
|
218
|
+
expect { Params.init ['--tmp8true2=3.4'] }.to raise_error
|
219
|
+
expect { Params.init ['--tmp8true2=ff'] }.to raise_error
|
220
|
+
expect { Params.init ['--tmp8true2=true'] }.to_not raise_error
|
221
|
+
expect { Params.init ['--tmp8true2=false'] }.to_not raise_error
|
222
|
+
|
223
|
+
# from FalseClass to other types.
|
224
|
+
Params.boolean('tmp9false2', false, 'tmp9 def')
|
225
|
+
expect { Params.init ['--tmp9false2=9'] }.to raise_error
|
226
|
+
expect { Params.init ['--tmp9false2=3.4'] }.to raise_error
|
227
|
+
expect { Params.init ['--tmp9false2=ff'] }.to raise_error
|
228
|
+
expect { Params.init ['--tmp9false2=true'] }.to_not raise_error
|
229
|
+
expect { Params.init ['--tmp9false2=false'] }.to_not raise_error
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'should override defined values with command line values' do
|
233
|
+
File.stub(:exist?).and_return false
|
234
|
+
Params.string('tmp10str', 'aaa', 'tmp10 def')
|
235
|
+
Params.integer('tmp10int', 11, 'tmp10 def')
|
236
|
+
Params.float('tmp10float', 11.11, 'tmp10 def')
|
237
|
+
Params.boolean('tmp10true', true, 'tmp10 def')
|
238
|
+
Params.boolean('tmp10false', false, 'tmp10 def')
|
239
|
+
Params.init ['--tmp10str=bbb', '--tmp10int=12', '--tmp10float=12.12', \
|
241
240
|
'--tmp10true=false', '--tmp10false=true']
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
end
|
241
|
+
Params['tmp10str'].should eq 'bbb'
|
242
|
+
Params['tmp10int'].should eq 12
|
243
|
+
Params['tmp10float'].should eq 12.12
|
244
|
+
Params['tmp10true'].should eq false
|
245
|
+
Params['tmp10false'].should eq true
|
246
|
+
end
|
247
|
+
|
248
|
+
it 'init should override parameters with file and command' do
|
249
|
+
Params.string('init_param', 'code', '')
|
250
|
+
File.stub(:exist?).and_return true
|
251
|
+
File.stub(:open).and_return StringIO.new 'init_param: yml'
|
252
|
+
Params.init ['--conf_file=dummy_file', '--init_param=command-line']
|
253
|
+
Params['init_param'].should eq 'command-line'
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'init should override parameters with file only' do
|
257
|
+
Params.string('init_param2', 'code', '')
|
258
|
+
File.stub(:exist?).and_return true
|
259
|
+
File.stub(:open).and_return StringIO.new 'init_param2: yml'
|
260
|
+
Params.init ['--conf_file=dummy_file']
|
261
|
+
Params['init_param2'].should eq 'yml'
|
262
|
+
end
|
263
|
+
|
264
|
+
it 'init should override parameters with command line only' do
|
265
|
+
Params.string('init_param3', 'code', '')
|
266
|
+
File.stub(:exist?).and_return false
|
267
|
+
Params.init ['--init_param3=command-line']
|
268
|
+
Params['init_param3'].should eq 'command-line'
|
269
|
+
end
|
270
|
+
|
271
|
+
it 'init should not override any parameters' do
|
272
|
+
Params.string('init_param4', 'code', '')
|
273
|
+
File.stub(:exist?).and_return false
|
274
|
+
Params.init []
|
275
|
+
Params['init_param4'].should eq 'code'
|
278
276
|
end
|
279
277
|
end
|
280
278
|
end
|