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
@@ -8,218 +8,218 @@ require 'test/unit'
|
|
8
8
|
|
9
9
|
class Test_ExceptionUtilities_raise_with_options < Test::Unit::TestCase
|
10
10
|
|
11
|
-
|
11
|
+
include ::Xqsr3::Diagnostics
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
OS_IS_WINDOWS = (RUBY_PLATFORM =~ /(mswin|mingw|bccwin|wince)/i) ? true : false
|
14
|
+
LINE_NUM_RE = OS_IS_WINDOWS ? /.+:(\d+):/ : /^[^:]+:(\d+)/
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
class ArgumentErrorWithOptions < ArgumentError
|
17
|
+
def initialize(message = nil, **options)
|
18
|
+
super(message)
|
19
|
+
options ||= {}
|
20
|
+
@options = options
|
21
|
+
end
|
22
|
+
attr_reader :options
|
23
23
|
|
24
|
-
|
24
|
+
def exception(msg = nil, options = nil)
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
return self if msg.nil?
|
27
|
+
return self if msg.object_id == self.object_id
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
29
|
+
msg ||= self.message
|
30
|
+
options ||= {}
|
31
|
+
r = self.class.new msg, **self.options.merge(options)
|
32
|
+
r
|
33
|
+
end
|
34
|
+
end
|
35
35
|
|
36
|
-
|
36
|
+
def execute(&block)
|
37
37
|
|
38
|
-
|
39
|
-
|
38
|
+
block.call
|
39
|
+
end
|
40
40
|
|
41
|
-
|
41
|
+
def test_0
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
# standard raise: message-only
|
44
|
+
begin
|
45
|
+
raise "abc"
|
46
46
|
|
47
|
-
|
48
|
-
|
47
|
+
assert false, "should not get here"
|
48
|
+
rescue => x
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
assert_kind_of(RuntimeError, x)
|
51
|
+
assert_equal "abc", x.message
|
52
|
+
end
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
# RWO: message-only
|
55
|
+
begin
|
56
|
+
ExceptionUtilities.raise_with_options "abc"
|
57
57
|
|
58
|
-
|
59
|
-
|
58
|
+
assert false, "should not get here"
|
59
|
+
rescue => x
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
61
|
+
assert_kind_of(RuntimeError, x)
|
62
|
+
assert_equal "abc", x.message
|
63
|
+
end
|
64
|
+
end
|
65
65
|
|
66
|
-
|
66
|
+
def test_1
|
67
67
|
|
68
|
-
|
69
|
-
|
70
|
-
|
68
|
+
# standard raise: class
|
69
|
+
begin
|
70
|
+
raise ArgumentError
|
71
71
|
|
72
|
-
|
73
|
-
|
72
|
+
assert false, "should not get here"
|
73
|
+
rescue => x
|
74
74
|
|
75
|
-
|
76
|
-
|
77
|
-
|
75
|
+
assert_kind_of(ArgumentError, x)
|
76
|
+
assert_equal "ArgumentError", x.message
|
77
|
+
end
|
78
78
|
|
79
|
-
|
80
|
-
|
81
|
-
|
79
|
+
# RWO: class + message
|
80
|
+
begin
|
81
|
+
ExceptionUtilities.raise_with_options ArgumentError
|
82
82
|
|
83
|
-
|
84
|
-
|
83
|
+
assert false, "should not get here"
|
84
|
+
rescue => x
|
85
85
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
86
|
+
assert_kind_of(ArgumentError, x)
|
87
|
+
assert_equal "ArgumentError", x.message
|
88
|
+
end
|
89
|
+
end
|
90
90
|
|
91
|
-
|
91
|
+
def test_2
|
92
92
|
|
93
|
-
|
94
|
-
|
95
|
-
|
93
|
+
# standard raise: class + message
|
94
|
+
begin
|
95
|
+
raise ArgumentError, "abc"
|
96
96
|
|
97
|
-
|
98
|
-
|
97
|
+
assert false, "should not get here"
|
98
|
+
rescue => x
|
99
99
|
|
100
|
-
|
101
|
-
|
102
|
-
|
100
|
+
assert_kind_of(ArgumentError, x)
|
101
|
+
assert_equal "abc", x.message
|
102
|
+
end
|
103
103
|
|
104
|
-
|
105
|
-
|
106
|
-
|
104
|
+
# RWO: class + message
|
105
|
+
begin
|
106
|
+
ExceptionUtilities.raise_with_options ArgumentError, "abc"
|
107
107
|
|
108
|
-
|
109
|
-
|
108
|
+
assert false, "should not get here"
|
109
|
+
rescue => x
|
110
110
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
111
|
+
assert_kind_of(ArgumentError, x)
|
112
|
+
assert_equal "abc", x.message
|
113
|
+
end
|
114
|
+
end
|
115
115
|
|
116
|
-
|
116
|
+
def test_2_b_no_options
|
117
117
|
|
118
|
-
|
119
|
-
|
120
|
-
|
118
|
+
# standard raise: class + message
|
119
|
+
begin
|
120
|
+
raise ArgumentErrorWithOptions, "abc"
|
121
121
|
|
122
|
-
|
123
|
-
|
122
|
+
assert false, "should not get here"
|
123
|
+
rescue => x
|
124
124
|
|
125
|
-
|
126
|
-
|
127
|
-
|
125
|
+
assert_kind_of(ArgumentError, x)
|
126
|
+
assert_equal "abc", x.message
|
127
|
+
end
|
128
128
|
|
129
|
-
|
130
|
-
|
131
|
-
|
129
|
+
# RWO: class + message
|
130
|
+
begin
|
131
|
+
ExceptionUtilities.raise_with_options ArgumentErrorWithOptions, "abc"
|
132
132
|
|
133
|
-
|
134
|
-
|
133
|
+
assert false, "should not get here"
|
134
|
+
rescue => x
|
135
135
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
136
|
+
assert_kind_of(ArgumentError, x)
|
137
|
+
assert_equal "abc", x.message
|
138
|
+
end
|
139
|
+
end
|
140
140
|
|
141
|
-
|
141
|
+
def test_2_b_with_options
|
142
142
|
|
143
|
-
|
144
|
-
|
145
|
-
|
143
|
+
# standard raise: class + message + options
|
144
|
+
begin
|
145
|
+
raise ArgumentErrorWithOptions.new("abc", blah: :blech, id: 23)
|
146
146
|
|
147
|
-
|
148
|
-
|
147
|
+
assert false, "should not get here"
|
148
|
+
rescue => x
|
149
149
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
150
|
+
assert_kind_of(ArgumentError, x)
|
151
|
+
assert_equal "abc", x.message
|
152
|
+
assert_equal ({ :blah => :blech, :id => 23 }), x.options
|
153
|
+
end
|
154
154
|
|
155
|
-
|
156
|
-
|
157
|
-
|
155
|
+
# RWO: class + message + options (raise-way)
|
156
|
+
begin
|
157
|
+
ExceptionUtilities.raise_with_options ArgumentErrorWithOptions.new("abc", blah: :blech, id: 23)
|
158
158
|
|
159
|
-
|
160
|
-
|
159
|
+
assert false, "should not get here"
|
160
|
+
rescue => x
|
161
161
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
162
|
+
assert_kind_of(ArgumentError, x)
|
163
|
+
assert_equal "abc", x.message
|
164
|
+
assert_equal ({ :blah => :blech, :id => 23 }), x.options
|
165
|
+
end
|
166
166
|
|
167
|
-
|
168
|
-
|
169
|
-
|
167
|
+
# RWO: class + message + options (raise_with_options-way)
|
168
|
+
begin
|
169
|
+
ExceptionUtilities.raise_with_options ArgumentErrorWithOptions, "abc", blah: :blech, id: 23
|
170
170
|
|
171
|
-
|
172
|
-
|
171
|
+
assert false, "should not get here"
|
172
|
+
rescue => x
|
173
173
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
174
|
+
assert_kind_of(ArgumentError, x)
|
175
|
+
assert_equal "abc", x.message
|
176
|
+
assert_equal ({ :blah => :blech, :id => 23 }), x.options
|
177
|
+
end
|
178
|
+
end
|
179
179
|
|
180
180
|
|
181
|
-
|
182
|
-
|
181
|
+
def test_with_backtrace
|
182
|
+
line_number = nil
|
183
183
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
184
|
+
# standard raise: class + message + stack trace
|
185
|
+
begin
|
186
|
+
begin
|
187
|
+
execute { line_number = __LINE__; raise ArgumentError }
|
188
|
+
rescue => x
|
189
|
+
raise x, "abc", x.backtrace
|
190
|
+
end
|
191
191
|
|
192
|
-
|
193
|
-
|
192
|
+
assert false, "should not get here"
|
193
|
+
rescue => x
|
194
194
|
|
195
|
-
|
196
|
-
|
195
|
+
assert_kind_of(ArgumentError, x)
|
196
|
+
assert_equal "abc", x.message
|
197
197
|
|
198
198
|
|
199
|
-
|
200
|
-
|
201
|
-
|
199
|
+
assert $@[0] =~ LINE_NUM_RE
|
200
|
+
assert_equal line_number.to_s, $1.to_s
|
201
|
+
end
|
202
202
|
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
203
|
+
# RWO: class + message + stack trace
|
204
|
+
begin
|
205
|
+
begin
|
206
|
+
execute { line_number = __LINE__; ExceptionUtilities.raise_with_options ArgumentErrorWithOptions, blah: :blech, id: 23 }
|
207
207
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
208
|
+
assert false, "should not get here"
|
209
|
+
rescue => x
|
210
|
+
raise x, "abc", x.backtrace
|
211
|
+
end
|
212
212
|
|
213
|
-
|
214
|
-
|
213
|
+
assert false, "should not get here"
|
214
|
+
rescue => x
|
215
215
|
|
216
|
-
|
217
|
-
|
218
|
-
|
216
|
+
assert_kind_of(ArgumentError, x)
|
217
|
+
assert_equal "abc", x.message
|
218
|
+
assert_equal ({ :blah => :blech, :id => 23 }), x.options
|
219
219
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
220
|
+
assert $@[0] =~ LINE_NUM_RE
|
221
|
+
assert_equal line_number.to_s, $1.to_s
|
222
|
+
end
|
223
|
+
end
|
224
224
|
end
|
225
225
|
|
@@ -8,29 +8,29 @@ require 'test/unit'
|
|
8
8
|
|
9
9
|
class Test_Enumerable_collect_with_index < Test::Unit::TestCase
|
10
10
|
|
11
|
-
|
11
|
+
def test_simple
|
12
12
|
|
13
|
-
|
13
|
+
ar = [ 1, -2, 3, -4, 5, -6 ]
|
14
14
|
|
15
|
-
|
15
|
+
r = ar.collect_with_index do |v, index0|
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
index0 * v.abs
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
assert_equal [ 0, 2, 6, 12, 20, 30 ], r
|
21
|
+
end
|
22
22
|
|
23
|
-
|
23
|
+
def test_simple_with_offset
|
24
24
|
|
25
|
-
|
25
|
+
ar = [ 1, -2, 3, -4, 5, -6 ]
|
26
26
|
|
27
|
-
|
27
|
+
r = ar.collect_with_index(1) do |v, index0|
|
28
28
|
|
29
|
-
|
30
|
-
|
29
|
+
index0 * v.abs
|
30
|
+
end
|
31
31
|
|
32
|
-
|
33
|
-
|
32
|
+
assert_equal [ 1, 4, 9, 16, 25, 36 ], r
|
33
|
+
end
|
34
34
|
end
|
35
35
|
|
36
36
|
|
@@ -10,34 +10,34 @@ require 'test/unit'
|
|
10
10
|
|
11
11
|
class Test_Enumerable_detect_map < Test::Unit::TestCase
|
12
12
|
|
13
|
-
|
13
|
+
def test_with_array
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
assert_nil [].detect_map { |v| nil }
|
16
|
+
assert_nil [ 1, 2, 3, 4, 5 ].detect_map { |v| nil }
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
assert_nil [].detect_map { |v| :nil }
|
19
|
+
assert_not_nil [ 1, 2, 3, 4, 5 ].detect_map { |v| :nil }
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
assert_nil [].detect_map { |v| return -2 * v }
|
22
|
+
assert_equal(-2, [ 1, 2, 3, 4, 5 ].detect_map { |v| -2 * v })
|
23
|
+
assert_equal(-4, [ 1, 2, 3, 4, 5 ].detect_map { |v| -2 * v if 2 == v })
|
24
|
+
end
|
25
25
|
|
26
|
-
|
26
|
+
def test_with_hash
|
27
27
|
|
28
|
-
|
29
|
-
|
28
|
+
h1 = {}
|
29
|
+
h2 = { :abc => 'def', :ghi => 'jkl' }
|
30
30
|
|
31
|
-
|
32
|
-
|
31
|
+
assert_nil h1.detect_map { |k, v| nil }
|
32
|
+
assert_nil h2.detect_map { |k, v| nil }
|
33
33
|
|
34
|
-
|
35
|
-
|
34
|
+
assert_nil h1.detect_map { |k, v| :nil }
|
35
|
+
assert_not_nil h2.detect_map { |k, v| :nil }
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
assert_nil h1.detect_map { |k, v| v.upcase }
|
38
|
+
assert_equal 'DEF', h2.detect_map { |k, v| v.upcase }
|
39
|
+
assert_equal 'JKL', h2.detect_map { |k, v| v.upcase if :ghi == k }
|
40
|
+
end
|
41
41
|
end
|
42
42
|
|
43
43
|
|
@@ -8,64 +8,64 @@ require 'test/unit'
|
|
8
8
|
|
9
9
|
class Test_Enumerable_unique_test < Test::Unit::TestCase
|
10
10
|
|
11
|
-
|
11
|
+
def test_empty
|
12
12
|
|
13
|
-
|
13
|
+
src = []
|
14
14
|
|
15
|
-
|
15
|
+
dest = src.unique
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
assert dest.empty?
|
18
|
+
end
|
19
19
|
|
20
|
-
|
20
|
+
def test_already_unique
|
21
21
|
|
22
|
-
|
22
|
+
src = [ 1, 2, 3, 4 ]
|
23
23
|
|
24
|
-
|
24
|
+
dest = src.unique
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
assert_equal src, dest
|
27
|
+
end
|
28
28
|
|
29
|
-
|
29
|
+
def test_unique_contiguous
|
30
30
|
|
31
|
-
|
31
|
+
src = [ 1, 2, 3, 3, 4 ]
|
32
32
|
|
33
|
-
|
33
|
+
dest = src.unique
|
34
34
|
|
35
|
-
|
36
|
-
|
35
|
+
assert_equal [ 1, 2, 3, 4], dest
|
36
|
+
end
|
37
37
|
|
38
|
-
|
38
|
+
def test_unique_noncontiguous
|
39
39
|
|
40
|
-
|
40
|
+
src = [ 1, 2, 3, 4, 3 ]
|
41
41
|
|
42
|
-
|
42
|
+
dest = src.unique
|
43
43
|
|
44
|
-
|
45
|
-
|
44
|
+
assert_equal [ 1, 2, 3, 4], dest
|
45
|
+
end
|
46
46
|
|
47
|
-
|
47
|
+
def test_unique_very_large_sorted
|
48
48
|
|
49
|
-
|
49
|
+
max = 100000
|
50
50
|
|
51
|
-
|
52
|
-
|
51
|
+
src = (0...max).to_a * 2
|
52
|
+
exp = (0...max).to_a
|
53
53
|
|
54
|
-
|
54
|
+
dest = src.unique
|
55
55
|
|
56
|
-
|
57
|
-
|
56
|
+
assert_equal exp, dest
|
57
|
+
end
|
58
58
|
|
59
|
-
|
59
|
+
def test_unique_very_large_unsorted
|
60
60
|
|
61
|
-
|
61
|
+
max = 100000
|
62
62
|
|
63
|
-
|
64
|
-
|
63
|
+
src = ((0...max).to_a * 2).sort
|
64
|
+
exp = (0...max).to_a
|
65
65
|
|
66
|
-
|
66
|
+
dest = src.unique
|
67
67
|
|
68
|
-
|
69
|
-
|
68
|
+
assert_equal exp, dest
|
69
|
+
end
|
70
70
|
end
|
71
71
|
|
@@ -11,38 +11,38 @@ require 'stringio'
|
|
11
11
|
|
12
12
|
class Test_Hash_deep_transform < Test::Unit::TestCase
|
13
13
|
|
14
|
-
|
14
|
+
def test_Hash_has_method
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
assert Hash.new.respond_to? :deep_transform
|
17
|
+
end
|
18
18
|
|
19
|
-
|
19
|
+
def test_with_empty
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
assert_equal Hash.new, Hash.new.deep_transform { |k| k }
|
22
|
+
assert_equal Hash.new, Hash.new.deep_transform { |k, v| [ k, v ] }
|
23
|
+
end
|
24
24
|
|
25
|
-
|
25
|
+
def test_transform_to_same
|
26
26
|
|
27
|
-
|
28
|
-
|
27
|
+
jkl = 12
|
28
|
+
h = { 'abc' => 'def', ghi: jkl, mno: { p: 'q', 'r' => 'st' } }
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
assert_equal h, h.deep_transform { |k| k }
|
31
|
+
assert_equal h, h.deep_transform { |k, v| [ k, v ] }
|
32
|
+
end
|
33
33
|
|
34
|
-
|
34
|
+
def test_transform_to_symbolise_keys
|
35
35
|
|
36
|
-
|
37
|
-
|
36
|
+
jkl = 12
|
37
|
+
h = { 'abc' => 'def', ghi: jkl, mno: { p: 'q', 'r' => 'st' } }
|
38
38
|
|
39
|
-
|
40
|
-
|
39
|
+
h_1 = { abc: 'def', ghi: jkl, mno: { p: 'q', r: 'st' } }
|
40
|
+
h_2 = { abc: :def, ghi: jkl, mno: { p: :q, r: :st } }
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
assert_equal h_1, h.deep_transform { |k| k.respond_to?(:to_sym) ? k.to_sym : k }
|
43
|
+
assert_equal h_1, h.deep_transform { |k, v| [ k.respond_to?(:to_sym) ? k.to_sym : k, v ] }
|
44
|
+
assert_equal h_2, h.deep_transform { |k, v| [ k.respond_to?(:to_sym) ? k.to_sym : k, v.respond_to?(:to_sym) ? v.to_sym : v ] }
|
45
|
+
end
|
46
46
|
end
|
47
47
|
|
48
48
|
|