xqsr3 0.38.2 → 0.39.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -6
- data/examples/count_word_frequencies.md +12 -12
- data/examples/count_word_frequencies.rb +4 -4
- data/lib/xqsr3/array_utilities/join_with_or.rb +47 -47
- data/lib/xqsr3/array_utilities.rb +2 -2
- data/lib/xqsr3/command_line_utilities/map_option_string.rb +60 -60
- data/lib/xqsr3/command_line_utilities.rb +2 -2
- data/lib/xqsr3/containers/frequency_map.rb +399 -399
- data/lib/xqsr3/containers/multi_map.rb +448 -448
- data/lib/xqsr3/containers.rb +3 -3
- data/lib/xqsr3/conversion/bool_parser.rb +51 -51
- data/lib/xqsr3/conversion/integer_parser.rb +87 -87
- data/lib/xqsr3/conversion.rb +3 -3
- data/lib/xqsr3/diagnostics/exception_utilities.rb +145 -145
- data/lib/xqsr3/diagnostics/exceptions/with_cause.rb +90 -90
- data/lib/xqsr3/diagnostics/inspect_builder.rb +80 -80
- data/lib/xqsr3/diagnostics.rb +3 -3
- data/lib/xqsr3/doc_.rb +130 -130
- data/lib/xqsr3/extensions/array/join_with_or.rb +9 -9
- data/lib/xqsr3/extensions/enumerable/collect_with_index.rb +12 -12
- data/lib/xqsr3/extensions/enumerable/detect_map.rb +35 -35
- data/lib/xqsr3/extensions/enumerable/unique.rb +29 -28
- data/lib/xqsr3/extensions/hash/deep_transform.rb +1 -1
- data/lib/xqsr3/extensions/hash/except.rb +16 -16
- data/lib/xqsr3/extensions/hash/has_match.rb +10 -10
- data/lib/xqsr3/extensions/hash/match.rb +10 -10
- data/lib/xqsr3/extensions/hash/slice.rb +11 -11
- data/lib/xqsr3/extensions/hash.rb +1 -1
- data/lib/xqsr3/extensions/integer/to_s_grp.rb +118 -0
- data/lib/xqsr3/extensions/integer.rb +3 -0
- data/lib/xqsr3/extensions/io/writelines.rb +28 -28
- data/lib/xqsr3/extensions/kernel/integer.rb +20 -20
- data/lib/xqsr3/extensions/kernel/raise_with_options.rb +8 -8
- data/lib/xqsr3/extensions/kernel.rb +1 -0
- data/lib/xqsr3/extensions/string/ends_with.rb +1 -1
- data/lib/xqsr3/extensions/string/map_option_string.rb +4 -4
- data/lib/xqsr3/extensions/string/nil_if_empty.rb +1 -1
- data/lib/xqsr3/extensions/string/nil_if_whitespace.rb +1 -1
- data/lib/xqsr3/extensions/string/quote_if.rb +1 -1
- data/lib/xqsr3/extensions/string/starts_with.rb +1 -1
- data/lib/xqsr3/extensions/string/to_bool.rb +8 -8
- data/lib/xqsr3/extensions/string/to_symbol.rb +1 -1
- data/lib/xqsr3/extensions/string/truncate.rb +1 -1
- data/lib/xqsr3/extensions/test/unit/assert_eql.rb +10 -7
- data/lib/xqsr3/extensions/test/unit/assert_false.rb +9 -6
- data/lib/xqsr3/extensions/test/unit/assert_not.rb +9 -6
- data/lib/xqsr3/extensions/test/unit/assert_not_eql.rb +10 -7
- data/lib/xqsr3/extensions/test/unit/assert_raise_with_message.rb +61 -61
- data/lib/xqsr3/extensions/test/unit/assert_subclass_of.rb +10 -7
- data/lib/xqsr3/extensions/test/unit/assert_superclass_of.rb +10 -7
- data/lib/xqsr3/extensions/test/unit/assert_true.rb +9 -6
- data/lib/xqsr3/extensions/test/unit/assert_type_has_instance_methods.rb +39 -36
- data/lib/xqsr3/extensions.rb +5 -5
- data/lib/xqsr3/hash_utilities/deep_transform.rb +65 -65
- data/lib/xqsr3/hash_utilities/key_matching.rb +77 -77
- data/lib/xqsr3/hash_utilities.rb +3 -3
- data/lib/xqsr3/internal_/test_unit_version_.rb +83 -83
- data/lib/xqsr3/io/writelines.rb +122 -120
- data/lib/xqsr3/quality/parameter_checking.rb +445 -445
- data/lib/xqsr3/quality.rb +2 -2
- data/lib/xqsr3/string_utilities/ends_with.rb +59 -59
- data/lib/xqsr3/string_utilities/nil_if_empty.rb +28 -28
- data/lib/xqsr3/string_utilities/nil_if_whitespace.rb +29 -29
- data/lib/xqsr3/string_utilities/quote_if.rb +50 -50
- data/lib/xqsr3/string_utilities/starts_with.rb +59 -59
- data/lib/xqsr3/string_utilities/to_symbol.rb +75 -75
- data/lib/xqsr3/string_utilities/truncate.rb +55 -55
- data/lib/xqsr3/string_utilities.rb +8 -8
- data/lib/xqsr3/version.rb +12 -12
- data/test/performance/frequency_map.rb +12 -12
- data/test/scratch/test_assert_raise_with_message.rb +11 -11
- data/test/unit/array_utilities/tc_join_with_or.rb +140 -140
- data/test/unit/command_line_utilities/tc_map_option_string.rb +18 -18
- data/test/unit/containers/tc_frequency_map.rb +591 -591
- data/test/unit/containers/tc_multi_map.rb +558 -558
- data/test/unit/conversion/tc_integer_parser.rb +72 -72
- data/test/unit/conversion/tc_to_bool.rb +25 -25
- data/test/unit/diagnostics/exceptions/tc_with_cause.rb +165 -165
- data/test/unit/diagnostics/tc_exception_utilities.rb +156 -156
- data/test/unit/extensions/enumerable/tc_collect_with_index.rb +14 -14
- data/test/unit/extensions/enumerable/tc_detect_map.rb +20 -20
- data/test/unit/extensions/enumerable/tc_unique.rb +34 -34
- data/test/unit/extensions/hash/tc_deep_transform.rb +22 -22
- data/test/unit/extensions/hash/tc_except.rb +28 -28
- data/test/unit/extensions/hash/tc_slice.rb +13 -13
- data/test/unit/extensions/integer/tc_to_s_grp.rb +60 -0
- data/test/unit/extensions/integer/ts_all.rb +12 -0
- data/test/unit/extensions/io/tc_writelines.rb +149 -77
- data/test/unit/extensions/kernel/tc_integer.rb +75 -75
- data/test/unit/extensions/kernel/tc_raise_with_options.rb +155 -155
- data/test/unit/extensions/object/tc_inspect.rb +50 -50
- data/test/unit/extensions/string/tc_bool.tb +24 -24
- data/test/unit/extensions/string/tc_ends_with.rb +53 -53
- data/test/unit/extensions/string/tc_map_option_string.rb +15 -15
- data/test/unit/extensions/string/tc_nil_if_empty.rb +8 -8
- data/test/unit/extensions/string/tc_nil_if_whitespace.rb +8 -8
- data/test/unit/extensions/string/tc_quote_if.rb +18 -18
- data/test/unit/extensions/string/tc_starts_with.rb +53 -53
- data/test/unit/extensions/string/tc_to_symbol.rb +26 -26
- data/test/unit/extensions/string/tc_truncate.rb +18 -18
- data/test/unit/extensions/test/unit/tc_assert_raise_with_message.rb +16 -16
- data/test/unit/extensions/test/unit/tc_assert_subclass_of.rb +9 -9
- data/test/unit/extensions/test/unit/tc_assert_superclass_of.rb +9 -9
- data/test/unit/hash_utilities/tc_has_match.rb +70 -70
- data/test/unit/hash_utilities/tc_match.rb +83 -83
- data/test/unit/io/tc_writelines.rb +166 -106
- data/test/unit/quality/tc_parameter_checking.rb +389 -389
- data/test/unit/string_utilities/tc_truncate.rb +27 -27
- data/test/unit/tc_version.rb +15 -15
- metadata +6 -2
@@ -10,636 +10,636 @@ require 'test/unit'
|
|
10
10
|
|
11
11
|
class Test_check_option_multiple_option_names < Test::Unit::TestCase
|
12
12
|
|
13
|
-
|
13
|
+
include ::Xqsr3::Quality::ParameterChecking
|
14
14
|
|
15
|
-
|
15
|
+
def check_method_stringise_stringize **options
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
check_option options, [ :stringise, :stringize ], nil: true
|
18
|
+
end
|
19
19
|
|
20
|
-
|
20
|
+
def test_1
|
21
21
|
|
22
|
-
|
22
|
+
assert_nil check_method_stringise_stringize()
|
23
23
|
|
24
|
-
|
24
|
+
assert_nil check_method_stringise_stringize(abc: :abc)
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
assert_equal :stringize, check_method_stringise_stringize(stringize: :stringize)
|
27
|
+
assert_equal :stringise, check_method_stringise_stringize(stringise: :stringise)
|
28
|
+
assert_equal :stringise, check_method_stringise_stringize(stringize: :stringize, stringise: :stringise)
|
29
|
+
assert_equal :stringise, check_method_stringise_stringize(stringise: :stringise, stringize: :stringize)
|
30
|
+
end
|
31
31
|
end
|
32
32
|
|
33
33
|
class Test_parameter_checks_as_separate_module < Test::Unit::TestCase
|
34
34
|
|
35
|
-
|
35
|
+
PC = ::Xqsr3::Quality::ParameterChecking
|
36
36
|
|
37
|
-
|
37
|
+
# test 1
|
38
38
|
|
39
|
-
|
39
|
+
def check_method_1 a, **options
|
40
40
|
|
41
|
-
|
42
|
-
|
41
|
+
PC.check_option options, :o1, nil: true
|
42
|
+
PC.check_option options, :o2, nil: false
|
43
43
|
|
44
|
-
|
45
|
-
|
44
|
+
PC.check_param a, 'a'
|
45
|
+
end
|
46
46
|
|
47
|
-
|
47
|
+
def test_1
|
48
48
|
|
49
|
-
|
49
|
+
assert_raise ArgumentError do
|
50
50
|
|
51
|
-
|
52
|
-
|
51
|
+
check_method_1(nil)
|
52
|
+
end
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
54
|
+
assert_equal true, check_method_1(true, o1: true, o2: false)
|
55
|
+
assert_equal '', check_method_1('', o1: true, o2: false)
|
56
|
+
assert_equal [], check_method_1([], o1: true, o2: false)
|
57
|
+
assert_equal Hash.new, check_method_1(Hash.new, o1: true, o2: false)
|
58
|
+
end
|
59
59
|
|
60
60
|
|
61
|
-
|
61
|
+
# test 2
|
62
62
|
end
|
63
63
|
|
64
64
|
class Test_parameter_checks_as_included_module < Test::Unit::TestCase
|
65
65
|
|
66
|
-
|
66
|
+
include ::Xqsr3::Quality::ParameterChecking
|
67
67
|
|
68
|
-
|
68
|
+
# test 0
|
69
69
|
|
70
|
-
|
70
|
+
def test_access_control_instance
|
71
71
|
|
72
|
-
|
73
|
-
|
72
|
+
assert private_methods.include?(:check_param), "check_param() must be a private method of the instance"
|
73
|
+
assert private_methods.include?(:check_parameter), "check_param() must be a private method of the instance"
|
74
74
|
|
75
|
-
|
76
|
-
|
77
|
-
|
75
|
+
assert self.class.private_instance_methods.include?(:check_param), "check_param() must be a private method of the instance"
|
76
|
+
assert self.class.private_instance_methods.include?(:check_parameter), "check_param() must be a private method of the instance"
|
77
|
+
end
|
78
78
|
|
79
|
-
|
79
|
+
def test_access_control_private
|
80
80
|
|
81
|
-
|
82
|
-
|
83
|
-
|
81
|
+
assert self.class.private_methods.include?(:check_param), "check_param() must be a private method of the class"
|
82
|
+
assert self.class.private_methods.include?(:check_parameter), "check_param() must be a private method of the class"
|
83
|
+
end
|
84
84
|
|
85
85
|
|
86
|
-
|
86
|
+
# test 1
|
87
87
|
|
88
|
-
|
88
|
+
def check_method_1 a
|
89
89
|
|
90
|
-
|
91
|
-
|
90
|
+
check_param a, 'a'
|
91
|
+
end
|
92
92
|
|
93
|
-
|
93
|
+
def self.check_method_1_class a
|
94
94
|
|
95
|
-
|
96
|
-
|
95
|
+
check_param a, 'a'
|
96
|
+
end
|
97
97
|
|
98
|
-
|
98
|
+
def test_1
|
99
99
|
|
100
|
-
|
100
|
+
assert_raise ArgumentError do
|
101
101
|
|
102
|
-
|
103
|
-
|
102
|
+
check_method_1(nil)
|
103
|
+
end
|
104
104
|
|
105
|
-
|
105
|
+
assert_raise ArgumentError do
|
106
106
|
|
107
|
-
|
108
|
-
|
107
|
+
self.class.check_method_1_class(nil)
|
108
|
+
end
|
109
109
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
110
|
+
assert_equal true, check_method_1(true)
|
111
|
+
assert_equal '', check_method_1('')
|
112
|
+
assert_equal [], check_method_1([])
|
113
|
+
assert_equal Hash.new, check_method_1(Hash.new)
|
114
114
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
115
|
+
assert_equal true, self.class.check_method_1_class(true)
|
116
|
+
assert_equal '', self.class.check_method_1_class('')
|
117
|
+
assert_equal [], self.class.check_method_1_class([])
|
118
|
+
assert_equal Hash.new, self.class.check_method_1_class(Hash.new)
|
119
|
+
end
|
120
120
|
|
121
121
|
|
122
|
-
|
122
|
+
# test 2
|
123
123
|
|
124
|
-
|
124
|
+
def check_method_2 a, types, options = {}
|
125
125
|
|
126
|
-
|
127
|
-
|
126
|
+
check_param a, 'a', options.merge({ types: types })
|
127
|
+
end
|
128
128
|
|
129
|
-
|
129
|
+
def test_2
|
130
130
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
131
|
+
assert_equal true, check_method_2(true, [ ::TrueClass ])
|
132
|
+
assert_equal true, check_method_2(true, [ :boolean ])
|
133
|
+
assert_equal true, check_method_2(true, [ ::TrueClass, ::String, ::Symbol ])
|
134
|
+
assert_raise TypeError do
|
135
|
+
check_method_2(true, [ ::String, ::Symbol, ::FalseClass ])
|
136
|
+
end
|
137
|
+
assert_equal true, check_method_2(true, [ ::TrueClass ], nothrow: true)
|
138
|
+
assert_nil check_method_2(true, [ ::FalseClass ], nothrow: true)
|
139
|
+
assert_nil check_method_2('abc', [ ::Symbol, ::Regexp ], nothrow: true)
|
140
|
+
assert_nil check_method_2([ 'abc' ], [ [ ::Symbol ], ::Regexp ], nothrow: true)
|
141
|
+
assert_not_nil check_method_2([ 'abc' ], [ [ ::String ], ::Regexp ], nothrow: true)
|
142
|
+
assert_not_nil check_method_2(//, [ [ ::String ], ::Regexp ], nothrow: true)
|
143
|
+
assert_not_nil check_method_2([ [ 'abc', 'def' ], [ 'ghi', 'jkl' ] ], [ [ ::String ], ::Regexp, [ ::Array ] ], nothrow: true)
|
144
144
|
|
145
|
-
|
145
|
+
assert_not_nil check_method_2([ [ 'abc', 'def' ], [ 'ghi', 'jkl' ] ], [ [ ::String ], ::Regexp, [ ::Array ] ], nothrow: true) do |v|
|
146
146
|
|
147
|
-
|
147
|
+
if ::Array === v
|
148
148
|
|
149
|
-
|
149
|
+
na = v.count { |v2| ::Array === v2 }
|
150
150
|
|
151
|
-
|
152
|
-
|
151
|
+
0 == na || v.size == na
|
152
|
+
else
|
153
153
|
|
154
|
-
|
155
|
-
|
156
|
-
|
154
|
+
true
|
155
|
+
end
|
156
|
+
end
|
157
157
|
|
158
|
-
|
158
|
+
assert_nil check_method_2([ [ 'abc', 'def' ], nil, [ 'ghi', 'jkl' ] ], [ [ ::String ], ::Regexp, [ ::Array ] ], nothrow: true) do |v|
|
159
159
|
|
160
|
-
|
160
|
+
if ::Array === v
|
161
161
|
|
162
|
-
|
162
|
+
na = v.count { |v2| ::Array === v2 }
|
163
163
|
|
164
|
-
|
165
|
-
|
164
|
+
0 == na || v.size == na
|
165
|
+
else
|
166
166
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
167
|
+
true
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
171
|
|
172
172
|
|
173
|
-
|
173
|
+
# test 3
|
174
174
|
|
175
|
-
|
175
|
+
def check_method_3 a, types, values, options = {}
|
176
176
|
|
177
|
-
|
178
|
-
|
177
|
+
check_param a, 'a', options.merge({ types: types, values: values })
|
178
|
+
end
|
179
179
|
|
180
|
-
|
180
|
+
def test_3
|
181
181
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
182
|
+
assert_raise RangeError do
|
183
|
+
check_method_3(-1, nil, [ 0..2 ])
|
184
|
+
end
|
185
|
+
assert_equal 0, check_method_3(0, nil, [ 0..2 ])
|
186
|
+
assert_equal 1, check_method_3(1, nil, [ 0..2 ])
|
187
|
+
assert_equal 2, check_method_3(2, nil, [ 0..2 ])
|
188
|
+
assert_raise RangeError do
|
189
|
+
check_method_3(3, nil, [ 0..2 ])
|
190
|
+
end
|
191
|
+
end
|
192
192
|
|
193
193
|
|
194
|
-
|
194
|
+
# test 4
|
195
195
|
|
196
|
-
|
196
|
+
def check_method_4 a, types, values, options = {}, &block
|
197
197
|
|
198
|
-
|
199
|
-
|
198
|
+
check_param a, 'a', options.merge({ types: types, values: values }), &block
|
199
|
+
end
|
200
200
|
|
201
|
-
|
201
|
+
def test_4
|
202
202
|
|
203
|
-
|
204
|
-
|
203
|
+
assert_equal 0, check_method_4(0, nil, nil)
|
204
|
+
assert_equal 0, check_method_4(0, nil, nil) { |n| 0 == n }
|
205
205
|
|
206
|
-
|
207
|
-
|
208
|
-
|
206
|
+
assert_raise RangeError do
|
207
|
+
check_method_4(-1, nil, nil) { |n| 0 == n }
|
208
|
+
end
|
209
209
|
|
210
|
-
|
211
|
-
|
212
|
-
|
210
|
+
assert_raise ArgumentError do
|
211
|
+
check_method_4('-1', nil, nil) { |n| 0 == n }
|
212
|
+
end
|
213
213
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
#
|
222
|
-
#
|
223
|
-
#
|
214
|
+
assert_raise TypeError do
|
215
|
+
check_method_4('-1', [ ::Numeric ], nil)
|
216
|
+
end
|
217
|
+
assert_raise ArgumentError do
|
218
|
+
check_method_4('-1', [ ::String ], [ '-2', '0', '+1', '+2' ])
|
219
|
+
end
|
220
|
+
assert_equal '-1', check_method_4('-1', [ ::String ], [ '-2', '-1', '0', '+1', '+2' ])
|
221
|
+
# assert_raise RangeError do
|
222
|
+
# check_method_4('-1', [ ::String ], [ '-2', '0', '+1', '+2' ]) {
|
223
|
+
# end
|
224
224
|
|
225
|
-
#
|
225
|
+
# check_param(id, 'id', types: ::Integer) { |v| raise ArgumentError, "'id' must be a positive integer" unless v > 0 }
|
226
226
|
|
227
|
-
|
227
|
+
end
|
228
228
|
|
229
229
|
|
230
230
|
|
231
|
-
|
231
|
+
# test 5
|
232
232
|
|
233
|
-
|
233
|
+
def check_method_5 a, options = {}
|
234
234
|
|
235
|
-
|
236
|
-
|
235
|
+
check_param a, 'a', options
|
236
|
+
end
|
237
237
|
|
238
|
-
|
238
|
+
def test_5
|
239
239
|
|
240
|
-
|
241
|
-
|
240
|
+
assert_equal "", check_method_5("", require_empty: true)
|
241
|
+
assert_equal "a", check_method_5("a", reject_empty: true)
|
242
242
|
|
243
|
-
|
244
|
-
|
245
|
-
|
243
|
+
assert_raise ArgumentError do
|
244
|
+
check_method_5("", reject_empty: true)
|
245
|
+
end
|
246
246
|
|
247
247
|
if false
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
248
|
+
assert_raise ArgumentError do
|
249
|
+
check_method_5(nil)
|
250
|
+
end
|
251
|
+
|
252
|
+
assert_equal true, check_method_5(true)
|
253
|
+
assert_equal '', check_method_5('')
|
254
|
+
assert_equal [], check_method_5([])
|
255
|
+
assert_equal Hash.new, check_method_5(Hash.new)
|
256
256
|
end
|
257
|
-
|
257
|
+
end
|
258
258
|
|
259
259
|
|
260
|
-
|
260
|
+
# test 6
|
261
261
|
|
262
|
-
|
262
|
+
def check_method_6 a, types, values, options = {}, &block
|
263
263
|
|
264
|
-
|
265
|
-
|
264
|
+
check_param a, 'a', options.merge({ types: types, values: values }), &block
|
265
|
+
end
|
266
266
|
|
267
|
-
|
267
|
+
def test_6
|
268
268
|
|
269
|
-
|
270
|
-
|
269
|
+
begin
|
270
|
+
check_method_6 '', [ ::Hash ], []
|
271
271
|
|
272
|
-
|
273
|
-
|
272
|
+
assert(false, 'should not get here')
|
273
|
+
rescue TypeError => ax
|
274
274
|
|
275
|
-
|
276
|
-
|
275
|
+
assert_match(/^parameter 'a' \(String\) must be an instance of Hash$/, ax.message)
|
276
|
+
rescue => x
|
277
277
|
|
278
|
-
|
279
|
-
|
278
|
+
assert(false, "wrong exception type #{x.class} (with message '#{x.message}')")
|
279
|
+
end
|
280
280
|
|
281
281
|
|
282
|
-
|
283
|
-
|
282
|
+
begin
|
283
|
+
check_method_6 '', [ ::String ], [ 'b', 'c', 'd' ]
|
284
284
|
|
285
|
-
|
286
|
-
|
285
|
+
assert(false, 'should not get here')
|
286
|
+
rescue ArgumentError => ax
|
287
287
|
|
288
|
-
|
289
|
-
|
288
|
+
assert_match(/^parameter 'a' value '' not found equal\/within any of required values or ranges$/, ax.message)
|
289
|
+
rescue => x
|
290
290
|
|
291
|
-
|
292
|
-
|
293
|
-
|
291
|
+
assert(false, "wrong exception type #{x.class} (with message '#{x.message}')")
|
292
|
+
end
|
293
|
+
end
|
294
294
|
|
295
295
|
|
296
|
-
|
296
|
+
# test 7 - verify that can include an array of types in the array of types
|
297
297
|
|
298
|
-
|
298
|
+
def check_method_7 a, types, values, options = {}, &block
|
299
299
|
|
300
|
-
|
301
|
-
|
300
|
+
check_param a, 'a', options.merge({ types: types, values: values }), &block
|
301
|
+
end
|
302
302
|
|
303
|
-
|
303
|
+
def test_7
|
304
304
|
|
305
|
-
|
305
|
+
assert_equal [], check_method_7([], [ ::Array ], nil)
|
306
306
|
|
307
|
-
|
307
|
+
assert_equal [ 'abc' ], check_method_7([ 'abc' ], [ ::Array ], nil)
|
308
308
|
|
309
|
-
|
309
|
+
assert_equal [ 'abc' ], check_method_7([ 'abc' ], [ [ ::String ] ], nil)
|
310
310
|
|
311
|
-
|
311
|
+
assert_equal [ 'abc' ], check_method_7([ 'abc' ], [ [ ::Regexp, ::String ] ], nil)
|
312
312
|
|
313
|
-
|
313
|
+
assert_equal [ :'abc' ], check_method_7([ :'abc' ], [ [ ::Regexp, ::Symbol ] ], nil)
|
314
314
|
|
315
315
|
|
316
|
-
|
317
|
-
|
316
|
+
begin
|
317
|
+
check_method_7 [ 'abc' ], [ ::Symbol, [ ::Regexp, ::Symbol ], ::Hash ], nil
|
318
318
|
|
319
|
-
|
320
|
-
|
319
|
+
assert(false, 'should not get here')
|
320
|
+
rescue TypeError => ax
|
321
321
|
|
322
|
-
|
323
|
-
|
322
|
+
assert_match(/^parameter 'a' \(Array\) must be an instance of Symbol or Hash, or an array containing instance\(s\) of Regexp or Symbol$/, ax.message)
|
323
|
+
rescue => x
|
324
324
|
|
325
|
-
|
326
|
-
|
325
|
+
assert(false, "wrong exception type #{x.class}) (with message '#{x.message}'")
|
326
|
+
end
|
327
327
|
|
328
328
|
|
329
|
-
|
330
|
-
|
329
|
+
begin
|
330
|
+
check_method_7 [ 'abc' ], [ [ ::Regexp, ::Symbol ] ], nil
|
331
331
|
|
332
|
-
|
333
|
-
|
332
|
+
assert(false, 'should not get here')
|
333
|
+
rescue TypeError => ax
|
334
334
|
|
335
|
-
|
336
|
-
|
335
|
+
assert_match(/^parameter 'a' \(Array\) must be an array containing instance\(s\) of Regexp or Symbol$/, ax.message)
|
336
|
+
rescue => x
|
337
337
|
|
338
|
-
|
339
|
-
|
340
|
-
|
338
|
+
assert(false, "wrong exception type #{x.class}) (with message '#{x.message}'")
|
339
|
+
end
|
340
|
+
end
|
341
341
|
|
342
342
|
|
343
|
-
|
343
|
+
# responds_to
|
344
344
|
|
345
|
-
|
345
|
+
def check_responds_to a, messages, options = {}, &block
|
346
346
|
|
347
|
-
|
348
|
-
|
347
|
+
check_param a, 'a', options.merge({ responds_to: messages }), &block
|
348
|
+
end
|
349
349
|
|
350
|
-
|
350
|
+
def test_responds_to
|
351
351
|
|
352
|
-
|
353
|
-
|
352
|
+
assert check_responds_to Hash.new, [ :[], :map, :to_s ]
|
353
|
+
assert_raise ::TypeError do
|
354
354
|
|
355
|
-
|
356
|
-
|
355
|
+
check_responds_to Hash.new, [ :this_is_not_a_Hash_method ]
|
356
|
+
end
|
357
357
|
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
358
|
+
# check can pass single types
|
359
|
+
assert check_responds_to Hash.new, :[]
|
360
|
+
assert check_responds_to Hash.new, :map
|
361
|
+
assert check_responds_to Hash.new, :to_s
|
362
|
+
end
|
363
363
|
|
364
364
|
|
365
365
|
|
366
|
-
|
366
|
+
# test type:
|
367
367
|
|
368
|
-
|
368
|
+
def check_method_type a, type
|
369
369
|
|
370
|
-
|
371
|
-
|
370
|
+
check_parameter a, 'a', type: type
|
371
|
+
end
|
372
372
|
|
373
|
-
|
373
|
+
def self.check_method_type_class a, type
|
374
374
|
|
375
|
-
|
376
|
-
|
375
|
+
check_parameter a, 'a', type: type
|
376
|
+
end
|
377
377
|
|
378
|
-
|
378
|
+
def test_type
|
379
379
|
|
380
|
-
|
380
|
+
assert_kind_of ::String, check_method_type('', ::String)
|
381
381
|
|
382
|
-
|
382
|
+
assert_raise TypeError do
|
383
383
|
|
384
|
-
|
385
|
-
|
384
|
+
check_method_type :sym, ::String
|
385
|
+
end
|
386
386
|
|
387
|
-
|
387
|
+
assert_kind_of ::String, self.class.check_method_type_class('', ::String)
|
388
388
|
|
389
|
-
|
389
|
+
assert_raise TypeError do
|
390
390
|
|
391
|
-
|
392
|
-
|
391
|
+
self.class.check_method_type_class :sym, ::String
|
392
|
+
end
|
393
393
|
|
394
394
|
|
395
|
-
|
395
|
+
# arrays of strings
|
396
396
|
|
397
|
-
|
398
|
-
|
397
|
+
assert_kind_of ::Array, check_method_type([ 'abc' ], [ ::String ])
|
398
|
+
end
|
399
399
|
|
400
400
|
|
401
|
-
|
401
|
+
# test treat_as_option
|
402
402
|
|
403
|
-
|
403
|
+
def check_method_tao_1 h, o, options = {}, &block
|
404
404
|
|
405
|
-
|
406
|
-
|
405
|
+
check_parameter h[o], o, options.merge({ treat_as_option: true }), &block
|
406
|
+
end
|
407
407
|
|
408
|
-
|
408
|
+
def test_tao_1
|
409
409
|
|
410
|
-
|
411
|
-
|
412
|
-
|
410
|
+
assert_true check_method_tao_1({ thing: true }, :thing)
|
411
|
+
assert_false check_method_tao_1({ thing: false }, :thing)
|
412
|
+
assert_equal [], check_method_tao_1({ thing: [] }, :thing)
|
413
413
|
|
414
|
-
|
415
|
-
|
414
|
+
begin
|
415
|
+
check_method_tao_1({ thing: true }, :thingy)
|
416
416
|
|
417
|
-
|
418
|
-
|
417
|
+
assert(false, 'should not get here')
|
418
|
+
rescue ArgumentError => ax
|
419
419
|
|
420
|
-
|
421
|
-
|
420
|
+
assert_equal "option ':thingy' may not be nil", ax.message
|
421
|
+
rescue => x
|
422
422
|
|
423
|
-
|
424
|
-
|
423
|
+
assert(false, "wrong exception type #{x.class}) (with message '#{x.message}'")
|
424
|
+
end
|
425
425
|
|
426
|
-
|
427
|
-
|
426
|
+
begin
|
427
|
+
check_method_tao_1({ thing: true }, 'thingy')
|
428
428
|
|
429
|
-
|
430
|
-
|
429
|
+
assert(false, 'should not get here')
|
430
|
+
rescue ArgumentError => ax
|
431
431
|
|
432
|
-
|
433
|
-
|
432
|
+
assert_equal "option 'thingy' may not be nil", ax.message
|
433
|
+
rescue => x
|
434
434
|
|
435
|
-
|
436
|
-
|
435
|
+
assert(false, "wrong exception type #{x.class}) (with message '#{x.message}'")
|
436
|
+
end
|
437
437
|
|
438
|
-
|
439
|
-
|
438
|
+
begin
|
439
|
+
check_method_tao_1({ thing: true }, ':thingy')
|
440
440
|
|
441
|
-
|
442
|
-
|
441
|
+
assert(false, 'should not get here')
|
442
|
+
rescue ArgumentError => ax
|
443
443
|
|
444
|
-
|
445
|
-
|
444
|
+
assert_equal "option ':thingy' may not be nil", ax.message
|
445
|
+
rescue => x
|
446
446
|
|
447
|
-
|
448
|
-
|
449
|
-
|
447
|
+
assert(false, "wrong exception type #{x.class}) (with message '#{x.message}'")
|
448
|
+
end
|
449
|
+
end
|
450
450
|
|
451
451
|
|
452
|
-
|
452
|
+
# test treat_as_option
|
453
453
|
|
454
|
-
|
454
|
+
def check_method_tao_2 h, o, options = {}, &block
|
455
455
|
|
456
|
-
|
457
|
-
|
456
|
+
check_option h, o, options.merge({ }), &block
|
457
|
+
end
|
458
458
|
|
459
|
-
|
459
|
+
def check_method_tao_class_2 h, o, options = {}, &block
|
460
460
|
|
461
|
-
|
462
|
-
|
461
|
+
check_option h, o, options.merge({ }), &block
|
462
|
+
end
|
463
463
|
|
464
|
-
|
464
|
+
def test_tao_2
|
465
465
|
|
466
|
-
|
467
|
-
|
468
|
-
|
466
|
+
assert_true check_method_tao_2({ thing: true }, :thing)
|
467
|
+
assert_false check_method_tao_2({ thing: false }, :thing)
|
468
|
+
assert_equal [], check_method_tao_2({ thing: [] }, :thing)
|
469
469
|
|
470
|
-
|
471
|
-
|
472
|
-
|
470
|
+
assert_true check_method_tao_class_2({ thing: true }, :thing)
|
471
|
+
assert_false check_method_tao_class_2({ thing: false }, :thing)
|
472
|
+
assert_equal [], check_method_tao_class_2({ thing: [] }, :thing)
|
473
473
|
|
474
|
-
|
475
|
-
|
474
|
+
begin
|
475
|
+
check_method_tao_2({ thing: true }, :thingy)
|
476
476
|
|
477
|
-
|
478
|
-
|
477
|
+
assert(false, 'should not get here')
|
478
|
+
rescue ArgumentError => ax
|
479
479
|
|
480
|
-
|
481
|
-
|
480
|
+
assert_equal "option ':thingy' may not be nil", ax.message
|
481
|
+
rescue => x
|
482
482
|
|
483
|
-
|
484
|
-
|
483
|
+
assert(false, "wrong exception type #{x.class}) (with message '#{x.message}'")
|
484
|
+
end
|
485
485
|
|
486
|
-
|
487
|
-
|
486
|
+
begin
|
487
|
+
check_method_tao_2({ thing: true }, 'thingy')
|
488
488
|
|
489
|
-
|
490
|
-
|
489
|
+
assert(false, 'should not get here')
|
490
|
+
rescue ArgumentError => ax
|
491
491
|
|
492
|
-
|
493
|
-
|
492
|
+
assert_equal "option 'thingy' may not be nil", ax.message
|
493
|
+
rescue => x
|
494
494
|
|
495
|
-
|
496
|
-
|
495
|
+
assert(false, "wrong exception type #{x.class}) (with message '#{x.message}'")
|
496
|
+
end
|
497
497
|
|
498
|
-
|
499
|
-
|
498
|
+
begin
|
499
|
+
check_method_tao_2({ thing: true }, ':thingy')
|
500
500
|
|
501
|
-
|
502
|
-
|
501
|
+
assert(false, 'should not get here')
|
502
|
+
rescue ArgumentError => ax
|
503
503
|
|
504
|
-
|
505
|
-
|
504
|
+
assert_equal "option ':thingy' may not be nil", ax.message
|
505
|
+
rescue => x
|
506
506
|
|
507
|
-
|
508
|
-
|
509
|
-
|
507
|
+
assert(false, "wrong exception type #{x.class}) (with message '#{x.message}'")
|
508
|
+
end
|
509
|
+
end
|
510
510
|
|
511
|
-
|
511
|
+
# test multiple option names
|
512
512
|
|
513
|
-
|
513
|
+
def check_method_multiple_option_names h, o, options = {}, &block
|
514
514
|
|
515
|
-
|
516
|
-
|
515
|
+
check_option h, o, options.merge({ }), &block
|
516
|
+
end
|
517
517
|
|
518
|
-
|
518
|
+
def test_multiple_option_names
|
519
519
|
|
520
|
-
|
520
|
+
# normal cases
|
521
521
|
|
522
|
-
|
523
|
-
|
522
|
+
thing = check_method_multiple_option_names({ thing: 123 }, :thing)
|
523
|
+
assert_equal 123, thing
|
524
524
|
|
525
525
|
|
526
|
-
|
527
|
-
|
526
|
+
thing = check_method_multiple_option_names({ thing: 123 }, :thingy, allow_nil: true)
|
527
|
+
assert_nil thing
|
528
528
|
|
529
529
|
|
530
|
-
|
531
|
-
|
530
|
+
begin
|
531
|
+
check_method_multiple_option_names({ thing: 123 }, :thingy)
|
532
532
|
|
533
|
-
|
534
|
-
|
533
|
+
assert(false, 'should not get here')
|
534
|
+
rescue ArgumentError => ax
|
535
535
|
|
536
|
-
|
537
|
-
|
536
|
+
assert_equal "option ':thingy' may not be nil", ax.message
|
537
|
+
rescue => x
|
538
538
|
|
539
|
-
|
540
|
-
|
539
|
+
assert(false, "wrong exception type #{x.class}) (with message '#{x.message}'")
|
540
|
+
end
|
541
541
|
|
542
542
|
|
543
|
-
|
543
|
+
# multiple-name cases
|
544
544
|
|
545
545
|
|
546
|
-
|
547
|
-
|
546
|
+
thing = check_method_multiple_option_names({ thing: 123 }, [ :thingy, :thing ])
|
547
|
+
assert_equal 123, thing
|
548
548
|
|
549
549
|
|
550
|
-
|
551
|
-
|
550
|
+
thing = check_method_multiple_option_names({ thing: 123 }, [ :thingy, :Thingy ], allow_nil: true)
|
551
|
+
assert_nil thing
|
552
552
|
|
553
553
|
|
554
|
-
|
555
|
-
|
554
|
+
begin
|
555
|
+
check_method_multiple_option_names({ thing: 123 }, [ :thingy, :Thingy ])
|
556
556
|
|
557
|
-
|
558
|
-
|
557
|
+
assert(false, 'should not get here')
|
558
|
+
rescue ArgumentError => ax
|
559
559
|
|
560
|
-
|
561
|
-
|
560
|
+
assert_equal "option ':thingy' may not be nil", ax.message
|
561
|
+
rescue => x
|
562
562
|
|
563
|
-
|
564
|
-
|
563
|
+
assert(false, "wrong exception type #{x.class}) (with message '#{x.message}'")
|
564
|
+
end
|
565
565
|
|
566
|
-
|
567
|
-
|
566
|
+
assert_raise_with_message(::ArgumentError, "option ':thingy' may not be nil") { check_option({ thing: 123 }, [ :thingy, :Thingy ]) }
|
567
|
+
assert_raise_with_message(::ArgumentError, "option ':Thingy' may not be nil") { check_option({ thing: 123 }, [ :Thingy, :thingy ]) }
|
568
568
|
|
569
|
-
|
569
|
+
# multiple-name cases where several present
|
570
570
|
|
571
|
-
|
572
|
-
|
571
|
+
assert_equal 123, check_method_multiple_option_names({ thing: 123, thingy: 45 }, [ :thing, :thingy ])
|
572
|
+
assert_equal 45, check_method_multiple_option_names({ thing: 123, thingy: 45 }, [ :thingy, :thing ])
|
573
573
|
|
574
|
-
|
575
|
-
|
576
|
-
|
574
|
+
assert_equal 123, check_method_multiple_option_names({ thing: 123, thingy: 45 }, [ :Thingy, :thing, :thingy ])
|
575
|
+
assert_equal 45, check_method_multiple_option_names({ thing: 123, thingy: 45 }, [ :Thingy, :thingy, :thing ])
|
576
|
+
end
|
577
577
|
|
578
|
-
|
578
|
+
# test strip_str_whitespace
|
579
579
|
|
580
|
-
|
580
|
+
def check_method_strip_str_whitespace v, name, options = {}, &block
|
581
581
|
|
582
|
-
|
583
|
-
|
582
|
+
check_parameter v, name, options, &block
|
583
|
+
end
|
584
584
|
|
585
|
-
|
585
|
+
def test_strip_str_whitespace
|
586
586
|
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
587
|
+
assert_equal ' ', check_method_strip_str_whitespace(' ', 's')
|
588
|
+
assert_equal ' ', check_method_strip_str_whitespace(' ', 's', strip_str_whitespace: false)
|
589
|
+
assert_equal ' ', check_method_strip_str_whitespace(' ', 's', reject_empty: true, strip_str_whitespace: false)
|
590
|
+
assert_equal '', check_method_strip_str_whitespace(' ', 's', strip_str_whitespace: true)
|
591
|
+
assert_equal 'abc', check_method_strip_str_whitespace("\tabc ", 's', strip_str_whitespace: true)
|
592
592
|
|
593
|
-
|
594
|
-
|
595
|
-
|
593
|
+
assert_raise_with_message(::ArgumentError, /param.*s.*(?:may|must) not be empty/) { check_method_strip_str_whitespace('', 's', reject_empty: true) }
|
594
|
+
assert_raise_with_message(::ArgumentError, /param.*s.*(?:may|must) not be empty/) { check_method_strip_str_whitespace(' ', 's', reject_empty: true, strip_str_whitespace: true) }
|
595
|
+
end
|
596
596
|
|
597
597
|
|
598
|
-
|
598
|
+
# test_allow_nil
|
599
599
|
|
600
|
-
|
600
|
+
def test_allow_nil
|
601
601
|
|
602
|
-
|
602
|
+
assert_raise_with_message(::ArgumentError, /parameter .*the_param.* may not be nil/) { check_parameter(nil, 'the_param') }
|
603
603
|
|
604
|
-
|
604
|
+
assert_nil(check_parameter(nil, 'the_param', allow_nil: true))
|
605
605
|
|
606
|
-
|
607
|
-
|
606
|
+
assert_nil(check_parameter(nil, 'the_param', nil: true))
|
607
|
+
end
|
608
608
|
|
609
609
|
|
610
|
-
|
610
|
+
# test_ignore_case
|
611
611
|
|
612
|
-
|
612
|
+
def test_ignore_case
|
613
613
|
|
614
|
-
|
614
|
+
assert_not_nil check_parameter('TheString', 'the_param', values: [ 'TheString', 'the-string' ])
|
615
615
|
|
616
|
-
|
616
|
+
assert_raise_with_message(::ArgumentError, /parameter.*the_param.*not found.*values/) { check_parameter('THESTRING', 'the_param', values: [ 'TheString', 'the-string' ]) }
|
617
617
|
|
618
|
-
|
619
|
-
|
618
|
+
assert_not_nil check_parameter('TheString', 'the_param', values: [ 'THESTRING', 'the-string' ], ignore_case: true)
|
619
|
+
end
|
620
620
|
|
621
|
-
|
621
|
+
def test_ignore_case_in_array
|
622
622
|
|
623
|
-
|
623
|
+
assert_not_nil check_parameter([ 'abc', 'def' ], 'the_param', values: [ [ 'ABC', 'DEF' ], [ 'abc', 'def' ] ])
|
624
624
|
|
625
|
-
|
625
|
+
assert_raise_with_message(::ArgumentError, /parameter.*the_param.*not found.*values/) { check_parameter([ 'Abc', 'Def' ], 'the_param', values: [ [ 'ABC', 'DEF' ], [ 'abc', 'def' ] ]) }
|
626
626
|
|
627
|
-
|
627
|
+
assert_not_nil check_parameter([ 'Abc', 'Def' ], 'the_param', values: [ [ 'ABC', 'DEF' ], [ 'abc', 'def' ] ], ignore_case: true)
|
628
628
|
|
629
|
-
|
629
|
+
assert_raise_with_message(::ArgumentError, /parameter.*the_param.*not found.*values/) { check_parameter([ 'Def', 'Abc' ], 'the_param', values: [ [ 'ABC', 'DEF' ], [ 'abc', 'def' ] ], ignore_case: true) }
|
630
630
|
|
631
|
-
|
631
|
+
end
|
632
632
|
|
633
|
-
|
633
|
+
def test_ignore_case_and_order_in_array
|
634
634
|
|
635
|
-
|
636
|
-
|
635
|
+
assert_not_nil check_parameter([ 'Def', 'Abc', 'Ghi' ], 'the_param', values: [ [ 'GHI', 'ABC', 'DEF' ], [ 'ghi', 'abc', 'def' ] ], ignore_case: true, ignore_order: true)
|
636
|
+
end
|
637
637
|
|
638
|
-
|
638
|
+
def test_ignore_order_in_array
|
639
639
|
|
640
|
-
|
640
|
+
assert_raise(::ArgumentError) { check_parameter([ 'abc', 'def', 'ghi' ], 'the_param', values: [ [ 'ghi', 'def', 'abc' ], [ 'ghi', 'abc', 'def' ] ]) }
|
641
641
|
|
642
|
-
|
643
|
-
|
642
|
+
assert_not_nil check_parameter([ 'abc', 'def', 'ghi' ], 'the_param', values: [ [ 'ghi', 'def', 'abc' ], [ 'ghi', 'abc', 'def' ] ], ignore_order: true)
|
643
|
+
end
|
644
644
|
end
|
645
645
|
|