invoca-utils 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/pipeline.yml +20 -0
- data/.gitignore +1 -3
- data/.rspec +3 -0
- data/.ruby-version +1 -1
- data/.tool-versions +1 -0
- data/Appraisals +13 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +9 -16
- data/Gemfile.lock +39 -48
- data/Rakefile +9 -6
- data/gemfiles/activesupport_5.gemfile +12 -0
- data/gemfiles/activesupport_5.gemfile.lock +58 -0
- data/gemfiles/activesupport_6.gemfile +12 -0
- data/gemfiles/activesupport_6.gemfile.lock +59 -0
- data/gemfiles/activesupport_7.gemfile +12 -0
- data/gemfiles/activesupport_7.gemfile.lock +57 -0
- data/invoca-utils.gemspec +18 -6
- data/lib/invoca/utils/version.rb +1 -1
- data/{test → spec}/helpers/constant_overrides.rb +0 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/unit/array_spec.rb +20 -0
- data/spec/unit/enumerable_spec.rb +80 -0
- data/{test/unit/exceptions_test.rb → spec/unit/exceptions_spec.rb} +17 -17
- data/spec/unit/guaranteed_utf8_string_spec.rb +260 -0
- data/spec/unit/hash_spec.rb +81 -0
- data/spec/unit/hash_with_indifferent_access_spec.rb +100 -0
- data/spec/unit/map_compact_spec.rb +25 -0
- data/{test/unit/module_test.rb → spec/unit/module_spec.rb} +4 -4
- data/spec/unit/multi_sender_spec.rb +54 -0
- data/{test/unit/stable_sort_test.rb → spec/unit/stable_sort_spec.rb} +14 -14
- data/spec/unit/time_calculations_spec.rb +39 -0
- data/{test/unit/utils_test.rb → spec/unit/utils_spec.rb} +14 -14
- metadata +59 -37
- data/.jenkins/Jenkinsfile +0 -50
- data/.jenkins/ruby_build_pod.yml +0 -19
- data/test/test_helper.rb +0 -14
- data/test/unit/array_test.rb +0 -20
- data/test/unit/enumerable_test.rb +0 -80
- data/test/unit/guaranteed_utf8_string_test.rb +0 -263
- data/test/unit/hash_test.rb +0 -81
- data/test/unit/hash_with_indifferent_access_test.rb +0 -100
- data/test/unit/map_compact_test.rb +0 -25
- data/test/unit/multi_sender_test.rb +0 -56
- data/test/unit/time_calculations_test.rb +0 -39
@@ -1,263 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../../lib/invoca/utils/guaranteed_utf8_string'
|
4
|
-
require_relative '../test_helper'
|
5
|
-
|
6
|
-
class GuaranteedUTF8StringTest < Minitest::Test
|
7
|
-
class HasNoTo_sMethod
|
8
|
-
undef :to_s
|
9
|
-
end
|
10
|
-
|
11
|
-
class BasicObjectWithKernelMethods
|
12
|
-
end
|
13
|
-
|
14
|
-
class ConvertibleToString
|
15
|
-
attr_reader :to_s
|
16
|
-
|
17
|
-
def initialize(string)
|
18
|
-
@to_s = string
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
context Invoca::Utils::GuaranteedUTF8String do
|
23
|
-
context '.normalize_string' do
|
24
|
-
should 'raise an error if called with an object with no to_s method' do
|
25
|
-
ex = assert_raises ArgumentError do
|
26
|
-
Invoca::Utils::GuaranteedUTF8String.normalize_string(HasNoTo_sMethod.new)
|
27
|
-
end
|
28
|
-
|
29
|
-
assert_match(/must be passed a string or an object with a non-Kernel \.to_s method but instead was GuaranteedUTF8StringTest::HasNoTo_sMethod/, ex.message)
|
30
|
-
end
|
31
|
-
|
32
|
-
should 'raise an error if called with a basic Ruby object' do
|
33
|
-
ex = assert_raises ArgumentError do
|
34
|
-
Invoca::Utils::GuaranteedUTF8String.normalize_string(BasicObjectWithKernelMethods.new)
|
35
|
-
end
|
36
|
-
|
37
|
-
assert_match(/must be passed a string or an object with a non-Kernel \.to_s method but instead was GuaranteedUTF8StringTest::BasicObjectWithKernelMethods/, ex.message)
|
38
|
-
end
|
39
|
-
|
40
|
-
should 'not mutate the original string' do
|
41
|
-
ascii_string = 'new string'.encode('ASCII')
|
42
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(ascii_string)
|
43
|
-
|
44
|
-
assert_equal ascii_string, encoded_string
|
45
|
-
assert_equal Encoding::ASCII, ascii_string.encoding
|
46
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
47
|
-
end
|
48
|
-
|
49
|
-
should 'return UTF-8 encoded string' do
|
50
|
-
original_string = "this,is,a,valid,utf-8,csv,string\none,two,three,four,five,six,seven\n"
|
51
|
-
|
52
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(original_string)
|
53
|
-
|
54
|
-
assert_equal original_string, encoded_string
|
55
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
56
|
-
end
|
57
|
-
|
58
|
-
context "normalize_utf16" do
|
59
|
-
UTF16_LE_BOM = "\xFF\xFE"
|
60
|
-
UTF16_BE_BOM = "\xFE\xFF"
|
61
|
-
UTF16_LE_TEST_STRING = (UTF16_LE_BOM + "v\x00a\x00l\x00i\x00d\x00,\x00u\x00t\x00f\x00-\x001\x006\x00\n\x00s\x00e\x00c\x00o\x00n\x00d\x00").force_encoding('BINARY').freeze
|
62
|
-
UTF16_BE_TEST_STRING = (UTF16_BE_BOM + "\x00v\x00a\x00l\x00i\x00d\x00,\x00u\x00t\x00f\x00-\x001\x006\x00\n\x00s\x00e\x00c\x00o\x00n\x00d").force_encoding('BINARY').freeze
|
63
|
-
|
64
|
-
should 'accept UTF-16LE in BINARY and return UTF-8 encoded string when true' do
|
65
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(UTF16_LE_TEST_STRING, normalize_utf16: true)
|
66
|
-
|
67
|
-
assert_equal "valid,utf-16\nsecond", encoded_string
|
68
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
69
|
-
end
|
70
|
-
|
71
|
-
should 'not check for UTF-16LE in BINARY and return UTF-8 encoded string when false' do
|
72
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(UTF16_LE_TEST_STRING, normalize_utf16: false)
|
73
|
-
expected = "ÿþv\u0000a\u0000l\u0000i\u0000d\u0000,\u0000u\u0000t\u0000f\u0000-\u00001\u00006\u0000\n\u0000s\u0000e\u0000c\u0000o\u0000n\u0000d\u0000"
|
74
|
-
assert_equal expected, encoded_string
|
75
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
76
|
-
end
|
77
|
-
|
78
|
-
should 'accept UTF-16BE in BINARY and return UTF-8 encoded string when true' do
|
79
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(UTF16_BE_TEST_STRING, normalize_utf16: true)
|
80
|
-
|
81
|
-
assert_equal "valid,utf-16\nsecond", encoded_string
|
82
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
83
|
-
end
|
84
|
-
|
85
|
-
should 'not check for UTF-16BE in BINARY and return UTF-8 encoded string when false' do
|
86
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(UTF16_BE_TEST_STRING, normalize_utf16: false)
|
87
|
-
expected = "þÿ\u0000v\u0000a\u0000l\u0000i\u0000d\u0000,\u0000u\u0000t\u0000f\u0000-\u00001\u00006\u0000\n\u0000s\u0000e\u0000c\u0000o\u0000n\u0000d"
|
88
|
-
assert_equal expected, encoded_string
|
89
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
90
|
-
end
|
91
|
-
|
92
|
-
context "containing embedded CP1252" do
|
93
|
-
should 'accept UTF-16LE in BINARY and return UTF-8 encoded string with "private" CP1252 when normalize_utf16: true, normalize_cp1252: false' do
|
94
|
-
original_string = (UTF16_LE_BOM + "\x91\x00s\x00m\x00a\x00r\x00t\x00 \x00q\x00u\x00o\x00t\x00e\x00s\x00\x92\x00\n\x00s\x00e\x00c\x00o\x00n\x00d\x00").force_encoding('BINARY')
|
95
|
-
|
96
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(original_string, normalize_utf16: true, normalize_cp1252: false)
|
97
|
-
|
98
|
-
assert_equal "\u0091smart quotes\u0092\nsecond", encoded_string
|
99
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
100
|
-
end
|
101
|
-
|
102
|
-
should 'accept UTF-16LE in BINARY and return UTF-8 encoded string with normalized CP1252 when normalize_utf16: true, normalize_cp1252: true' do
|
103
|
-
original_string = (UTF16_LE_BOM + "\x91\x00s\x00m\x00a\x00r\x00t\x00 \x00q\x00u\x00o\x00t\x00e\x00s\x00\x92\x00\n\x00s\x00e\x00c\x00o\x00n\x00d\x00").force_encoding('BINARY')
|
104
|
-
|
105
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(original_string, normalize_utf16: true)
|
106
|
-
|
107
|
-
assert_equal "‘smart quotes’\nsecond", encoded_string
|
108
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
109
|
-
end
|
110
|
-
|
111
|
-
should 'accept UTF-16BE in BINARY and return UTF-8 encoded string when normalize_utf16: true, normalize_cp1252: false' do
|
112
|
-
original_string = (UTF16_BE_BOM + "\x00\x91\x00s\x00m\x00a\x00r\x00t\x00 \x00q\x00u\x00o\x00t\x00e\x00s\x00\x92\x00\n\x00s\x00e\x00c\x00o\x00n\x00d").force_encoding('BINARY')
|
113
|
-
|
114
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(original_string, normalize_utf16: true, normalize_cp1252: false)
|
115
|
-
|
116
|
-
assert_equal "\u0091smart quotes\u0092\nsecond", encoded_string
|
117
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
118
|
-
end
|
119
|
-
|
120
|
-
should 'accept UTF-16BE in BINARY and return UTF-8 encoded string when normalize_utf16: true, normalize_cp1252: true' do
|
121
|
-
original_string = (UTF16_BE_BOM + "\x00\x91\x00s\x00m\x00a\x00r\x00t\x00 \x00q\x00u\x00o\x00t\x00e\x00s\x00\x92\x00\n\x00s\x00e\x00c\x00o\x00n\x00d").force_encoding('BINARY')
|
122
|
-
|
123
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(original_string, normalize_utf16: true, normalize_cp1252: true)
|
124
|
-
|
125
|
-
assert_equal "‘smart quotes’\nsecond", encoded_string
|
126
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
context 'normalize_cp1252' do
|
132
|
-
setup do
|
133
|
-
@string = "This,is,NOT,a,valid,utf-8,csv,string\r\none,two,three,four,\x81five,\x91smart quotes\x92,\x93suck!\x94\n"
|
134
|
-
end
|
135
|
-
|
136
|
-
should 'raise ArgumentError when false' do
|
137
|
-
assert_raises(ArgumentError, /xxyy/) do
|
138
|
-
Invoca::Utils::GuaranteedUTF8String.normalize_string(@string, normalize_cp1252: false)
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
should 'return UTF-8 encoded string after falling back to CP1252 encoding when true' do
|
143
|
-
expected_string = "This,is,NOT,a,valid,utf-8,csv,string\none,two,three,four,~five,‘smart quotes’,“suck!”\n"
|
144
|
-
|
145
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(@string)
|
146
|
-
|
147
|
-
assert_equal expected_string, encoded_string
|
148
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
149
|
-
end
|
150
|
-
|
151
|
-
should "encode all 255 UTF-8 characters, returning ~ when the character isn't mapped in CP1252" do
|
152
|
-
all_8_bit_characters = (1..255).map(&:chr).join
|
153
|
-
|
154
|
-
final_utf_8_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(all_8_bit_characters)
|
155
|
-
expected_string = "\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000A\u000B\u000C\u000A\u000E\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007F€~‚ƒ„…†‡ˆ‰Š‹Œ~Ž~~‘’“”•–—˜™š›œ~žŸ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"
|
156
|
-
|
157
|
-
assert_equal expected_string, final_utf_8_string
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
context 'normalize_newlines' do
|
162
|
-
setup do
|
163
|
-
@string = "This string\n\n\n has line feeds\ncarriage\r\r returns\rand Windows\r\n\r\n new line chars\r\nend of \n\r\r\r\nstring"
|
164
|
-
end
|
165
|
-
|
166
|
-
should 'return UTF-8 encoded string without normalized return chars when false' do
|
167
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(@string, normalize_newlines: false)
|
168
|
-
|
169
|
-
assert_equal @string, encoded_string
|
170
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
171
|
-
end
|
172
|
-
|
173
|
-
should 'return UTF-8 encoded string with normalized return chars when true' do
|
174
|
-
expected_string = "This string\n\n\n has line feeds\ncarriage\n\n returns\nand Windows\n\n new line chars\nend of \n\n\n\nstring"
|
175
|
-
|
176
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(@string, normalize_newlines: true)
|
177
|
-
|
178
|
-
assert_equal expected_string, encoded_string
|
179
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
context 'remove_utf8_bom' do
|
184
|
-
setup do
|
185
|
-
@original_string = "\xEF\xBB\xBFthis,is,a,valid,utf-8,csv,string\none,two,three,four,five,six,seven\n"
|
186
|
-
end
|
187
|
-
|
188
|
-
should 'return UTF-8 encoded string with BOM intact when false' do
|
189
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(@original_string, remove_utf8_bom: false)
|
190
|
-
|
191
|
-
assert_equal "\xEF\xBB\xBFthis,is,a,valid,utf-8,csv,string\none,two,three,four,five,six,seven\n", encoded_string
|
192
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
193
|
-
end
|
194
|
-
|
195
|
-
should 'return UTF-8 encoded string without BOM when true' do
|
196
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(@original_string, remove_utf8_bom: true)
|
197
|
-
|
198
|
-
assert_equal "this,is,a,valid,utf-8,csv,string\none,two,three,four,five,six,seven\n", encoded_string
|
199
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
context 'replace_unicode_beyond_ffff' do
|
204
|
-
setup do
|
205
|
-
@string = "This string has some ✓ valid UTF-8 but also some 😹 emoji \xf0\x9f\x98\xb9 that are > U+FFFF"
|
206
|
-
end
|
207
|
-
|
208
|
-
should "consider UTF-8 code points that take > 3 bytes (above U+FFFF) to be invalid (since MySQL can't store them unless column is declared mb4) and encode them as ~ when false" do
|
209
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(@string, replace_unicode_beyond_ffff: false)
|
210
|
-
|
211
|
-
assert_equal @string, encoded_string
|
212
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
213
|
-
end
|
214
|
-
|
215
|
-
should "consider UTF-8 code points that take > 3 bytes (above U+FFFF) to be invalid (since MySQL can't store them unless column is declared mb4) and encode them as ~ when true" do
|
216
|
-
expected_string = 'This string has some ✓ valid UTF-8 but also some ~ emoji ~ that are > U+FFFF'
|
217
|
-
|
218
|
-
encoded_string = Invoca::Utils::GuaranteedUTF8String.normalize_string(@string, replace_unicode_beyond_ffff: true)
|
219
|
-
|
220
|
-
assert_equal expected_string, encoded_string
|
221
|
-
assert_equal Encoding::UTF_8, encoded_string.encoding
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
225
|
-
context ".normalize_strings" do
|
226
|
-
should "walk json doc, replacing strings in: values, inside array elements, and hash keys and values" do
|
227
|
-
json_doc = {
|
228
|
-
'😹' => "\xE2\x9C\x93 laughing cat",
|
229
|
-
['😹'] => ["\xE2", "\xf0\x9f\x98\xb9", { "newline" => "\r\n" }],
|
230
|
-
'cp1252' => "\x91smart quotes\x92"
|
231
|
-
}
|
232
|
-
|
233
|
-
normalized_json = Invoca::Utils::GuaranteedUTF8String.normalize_all_strings(json_doc,
|
234
|
-
normalize_utf16: true,
|
235
|
-
normalize_cp1252: true,
|
236
|
-
normalize_newlines: true,
|
237
|
-
remove_utf8_bom: true,
|
238
|
-
replace_unicode_beyond_ffff: true)
|
239
|
-
|
240
|
-
assert_equal({
|
241
|
-
'~' => "✓ laughing cat",
|
242
|
-
['~'] => ["â", "~", { "newline" => "\n" }],
|
243
|
-
'cp1252' => "‘smart quotes’"
|
244
|
-
}, normalized_json)
|
245
|
-
end
|
246
|
-
end
|
247
|
-
end
|
248
|
-
|
249
|
-
context 'constructor' do
|
250
|
-
should 'call normalize_string with the default conversions' do
|
251
|
-
mock(Invoca::Utils::GuaranteedUTF8String).normalize_string('')
|
252
|
-
|
253
|
-
Invoca::Utils::GuaranteedUTF8String.new('').to_string
|
254
|
-
end
|
255
|
-
|
256
|
-
should 'do the same when using to_s alias' do
|
257
|
-
mock(Invoca::Utils::GuaranteedUTF8String).normalize_string('')
|
258
|
-
|
259
|
-
Invoca::Utils::GuaranteedUTF8String.new('').to_s
|
260
|
-
end
|
261
|
-
end
|
262
|
-
end
|
263
|
-
end
|
data/test/unit/hash_test.rb
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../../lib/invoca/utils/hash.rb'
|
4
|
-
require_relative '../test_helper'
|
5
|
-
|
6
|
-
class HashTest < Minitest::Test
|
7
|
-
|
8
|
-
context 'select_hash' do
|
9
|
-
should 'return a hash containing key/values identified by the block' do
|
10
|
-
assert_equal({ 1 => 2, 3 => 4 }, { 1 => 2, 3 => 4, 6 => 5 }.select_hash { |key, value| key < value })
|
11
|
-
end
|
12
|
-
|
13
|
-
should 'handle blocks that only check values' do
|
14
|
-
assert_equal({ 3 => 4, 6 => 5 }, { 1 => 2, 3 => 4, 6 => 5 }.select_hash { |value| value != 2 })
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'map_hash' do
|
19
|
-
should 'return a hash containing values updated by the block' do
|
20
|
-
assert_equal({ 1 => true, 3 => true, 6 => false }, { 1 => 2, 3 => 4, 6 => 5 }.map_hash { |key, value| key < value })
|
21
|
-
end
|
22
|
-
|
23
|
-
should 'handle blocks that only receive values' do
|
24
|
-
assert_equal({ 1 => 4, 3 => 8, 6 => 10 }, { 1 => 2, 3 => 4, 6 => 5 }.map_hash { |value| value * 2 })
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'partition_hash' do
|
29
|
-
should 'return two hashes, the first contains the pairs with matching keys, the second contains the rest' do
|
30
|
-
assert_equal([{ 1 => 2, 3 => 4 }, { 6 => 5 }], { 1 => 2, 3 => 4, 6 => 5 }.partition_hash([1, 3]))
|
31
|
-
end
|
32
|
-
|
33
|
-
should 'return two hashes, the first contains the pairs with identified by the block, the second contains the rest' do
|
34
|
-
assert_equal([{ 1 => 2, 3 => 4 }, { 6 => 5 }], { 1 => 2, 3 => 4, 6 => 5 }.partition_hash { |key, value| key < value })
|
35
|
-
end
|
36
|
-
|
37
|
-
should 'handle no matches' do
|
38
|
-
assert_equal([{}, { 1 => 2, 3 => 4, 6 => 5 }], { 1 => 2, 3 => 4, 6 => 5 }.partition_hash([100]))
|
39
|
-
end
|
40
|
-
|
41
|
-
should 'handle all matches' do
|
42
|
-
assert_equal([{ 1 => 2, 3 => 4, 6 => 5 }, {}], { 1 => 2, 3 => 4, 6 => 5 }.partition_hash { |_key, _value| true })
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context '- operator' do
|
47
|
-
should 'return a hash with pairs removed that match the keys in rhs array' do
|
48
|
-
assert_equal({ 3 => 4 }, { 1 => 2, 3 => 4, 6 => 5 } - [1, 6])
|
49
|
-
end
|
50
|
-
|
51
|
-
should 'handle empty rhs array' do
|
52
|
-
assert_equal({ 1 => 2, 3 => 4, 6 => 5 }, { 1 => 2, 3 => 4, 6 => 5 } - [])
|
53
|
-
end
|
54
|
-
|
55
|
-
should 'handle no matches in rhs array' do
|
56
|
-
assert_equal({ 1 => 2, 3 => 4, 6 => 5 }, { 1 => 2, 3 => 4, 6 => 5 } - [100, 600])
|
57
|
-
end
|
58
|
-
|
59
|
-
should 'handle all matches in rhs array' do
|
60
|
-
assert_equal({}, { 1 => 2, 3 => 4, 6 => 5 } - [1, 3, 6])
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
context '& operator' do
|
65
|
-
should 'return a hash with pairs removed that do NOT match the keys in rhs array' do
|
66
|
-
assert_equal({ 1 => 2, 6 => 5 }, { 1 => 2, 3 => 4, 6 => 5 } & [1, 6])
|
67
|
-
end
|
68
|
-
|
69
|
-
should 'handle empty rhs array' do
|
70
|
-
assert_equal({}, { 1 => 2, 3 => 4, 6 => 5 } & [])
|
71
|
-
end
|
72
|
-
|
73
|
-
should 'handle no matches in rhs array' do
|
74
|
-
assert_equal({}, { 1 => 2, 3 => 4, 6 => 5 } & [100, 600])
|
75
|
-
end
|
76
|
-
|
77
|
-
should 'handle all matches in rhs array' do
|
78
|
-
assert_equal({ 1 => 2, 3 => 4, 6 => 5 }, { 1 => 2, 3 => 4, 6 => 5 } & [1, 3, 6])
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
@@ -1,100 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'active_support/hash_with_indifferent_access'
|
4
|
-
require_relative '../../lib/invoca/utils/hash_with_indifferent_access.rb'
|
5
|
-
require_relative '../test_helper'
|
6
|
-
|
7
|
-
class HashWithIndifferentAccessTest < Minitest::Test
|
8
|
-
|
9
|
-
context 'partition_hash' do
|
10
|
-
setup do
|
11
|
-
@hash_to_test = HashWithIndifferentAccess.new('one' => 2, :three => 4, 'six' => 5)
|
12
|
-
end
|
13
|
-
|
14
|
-
should 'return two hashes, the first contains the pairs with matching keys, the second contains the rest' do
|
15
|
-
assert_equal([{ 'one' => 2, 'three' => 4 }, { 'six' => 5 }], @hash_to_test.partition_hash(['one', 'three']))
|
16
|
-
end
|
17
|
-
|
18
|
-
should 'return two hashes, the first contains the pairs with identified by the block, the second contains the rest' do
|
19
|
-
assert_equal([{ 'one' => 2, 'three' => 4 }, { 'six' => 5 }], @hash_to_test.partition_hash { |key, _value| ['one', 'three'].include?(key) })
|
20
|
-
end
|
21
|
-
|
22
|
-
should 'handle no matches' do
|
23
|
-
assert_equal([{}, @hash_to_test], @hash_to_test.partition_hash([:not_found]))
|
24
|
-
end
|
25
|
-
|
26
|
-
should 'handle all matches' do
|
27
|
-
assert_equal([@hash_to_test, {}], @hash_to_test.partition_hash { |_key, _value| true })
|
28
|
-
end
|
29
|
-
|
30
|
-
should 'handle symbols for key matching' do
|
31
|
-
assert_equal([{ 'one' => 2, 'three' => 4 }, { 'six' => 5 }], @hash_to_test.partition_hash([:one, :three]))
|
32
|
-
end
|
33
|
-
|
34
|
-
should 'return HashWithIndifferentAccess objects' do
|
35
|
-
matched, unmatched = @hash_to_test.partition_hash([:one, :three])
|
36
|
-
assert(matched.is_a?(HashWithIndifferentAccess))
|
37
|
-
assert(unmatched.is_a?(HashWithIndifferentAccess))
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
context '- operator' do
|
42
|
-
setup do
|
43
|
-
@hash_to_test = HashWithIndifferentAccess.new('one' => 2, :three => 4, 'six' => 5)
|
44
|
-
end
|
45
|
-
|
46
|
-
should 'return a hash with pairs removed that match the keys in rhs array' do
|
47
|
-
assert_equal({ 'three' => 4 }, @hash_to_test - ['one', 'six'])
|
48
|
-
end
|
49
|
-
|
50
|
-
should 'handle empty rhs array' do
|
51
|
-
assert_equal(@hash_to_test, @hash_to_test - [])
|
52
|
-
end
|
53
|
-
|
54
|
-
should 'handle no matches in rhs array' do
|
55
|
-
assert_equal(@hash_to_test, @hash_to_test - ['100', '600'])
|
56
|
-
end
|
57
|
-
|
58
|
-
should 'handle all matches in rhs array' do
|
59
|
-
assert_equal({}, @hash_to_test - ['one', 'three', 'six'])
|
60
|
-
end
|
61
|
-
|
62
|
-
should 'handle symbols for key matching' do
|
63
|
-
assert_equal({ 'six' => 5 }, @hash_to_test - [:one, :three])
|
64
|
-
end
|
65
|
-
|
66
|
-
should 'return HashWithIndifferentAccess object' do
|
67
|
-
assert((@hash_to_test - [:one, :three]).is_a?(HashWithIndifferentAccess))
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
context '& operator' do
|
72
|
-
setup do
|
73
|
-
@hash_to_test = HashWithIndifferentAccess.new('one' => 2, :three => 4, 'six' => 5)
|
74
|
-
end
|
75
|
-
|
76
|
-
should 'return a hash with pairs removed that do NOT match the keys in rhs array' do
|
77
|
-
assert_equal({ 'one' => 2, 'six' => 5 }, @hash_to_test & ['one', 'six'])
|
78
|
-
end
|
79
|
-
|
80
|
-
should 'handle empty rhs array' do
|
81
|
-
assert_equal({}, @hash_to_test & [])
|
82
|
-
end
|
83
|
-
|
84
|
-
should 'handle no matches in rhs array' do
|
85
|
-
assert_equal({}, @hash_to_test & ['100', '600'])
|
86
|
-
end
|
87
|
-
|
88
|
-
should 'handle all matches in rhs array' do
|
89
|
-
assert_equal(@hash_to_test, @hash_to_test & ['one', 'three', 'six'])
|
90
|
-
end
|
91
|
-
|
92
|
-
should 'handle symbols for key matching' do
|
93
|
-
assert_equal({ 'one' => 2, 'three' => 4 }, @hash_to_test & [:one, :three])
|
94
|
-
end
|
95
|
-
|
96
|
-
should 'return HashWithIndifferentAccess object' do
|
97
|
-
assert((@hash_to_test & [:one, :three]).is_a?(HashWithIndifferentAccess))
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../test_helper'
|
4
|
-
|
5
|
-
class MapCompactTest < Minitest::Test
|
6
|
-
should "map_compact" do
|
7
|
-
assert_equal [1, 9], [1, 2, nil, 3, 4].map_compact { |item| item**2 if (nil == item ? nil : item.odd?) }
|
8
|
-
end
|
9
|
-
|
10
|
-
should "map_compact to empty if nothing matches" do
|
11
|
-
assert_equal [], {:a => 'aaa', :b => 'bbb'}.map_compact { |key, value| value if key == :c }
|
12
|
-
end
|
13
|
-
|
14
|
-
should "map_compact a hash" do
|
15
|
-
assert_equal ['bbb'], {:a => 'aaa', :b => 'bbb'}.map_compact { |key, value| value if key == :b }
|
16
|
-
end
|
17
|
-
|
18
|
-
should "map_compact empty collection" do
|
19
|
-
assert_equal [], [].map_compact { |item| true }
|
20
|
-
end
|
21
|
-
|
22
|
-
should "not map_compact false" do
|
23
|
-
assert_equal [false], [nil, false].map_compact { |a| a }
|
24
|
-
end
|
25
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../../lib/invoca/utils/multi_sender.rb'
|
4
|
-
require_relative '../test_helper'
|
5
|
-
|
6
|
-
class MultiSenderTest < Minitest::Test
|
7
|
-
# create enumerable class for testing
|
8
|
-
class LinkedList
|
9
|
-
include Enumerable
|
10
|
-
|
11
|
-
def initialize(head, tail = nil)
|
12
|
-
@head, @tail = head, tail
|
13
|
-
end
|
14
|
-
|
15
|
-
def <<(item)
|
16
|
-
LinkedList.new(item, self)
|
17
|
-
end
|
18
|
-
|
19
|
-
def inspect
|
20
|
-
[@head, @tail].inspect
|
21
|
-
end
|
22
|
-
|
23
|
-
def each(&block)
|
24
|
-
if block_given?
|
25
|
-
block.call(@head)
|
26
|
-
@tail&.each(&block)
|
27
|
-
else
|
28
|
-
to_enum(:each)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
context 'MultiSender' do
|
34
|
-
context 'with custom Enumerable' do
|
35
|
-
setup do
|
36
|
-
linked_list = LinkedList.new('some') << 'short' << 'words'
|
37
|
-
@multi_sender = Invoca::Utils::MultiSender.new(linked_list, :map)
|
38
|
-
end
|
39
|
-
|
40
|
-
should 'call the same method on each item in an Enumerable and return the results as an array' do
|
41
|
-
assert_equal([5, 5, 4], @multi_sender.length)
|
42
|
-
end
|
43
|
-
|
44
|
-
should 'handle methods with arguments' do
|
45
|
-
assert_equal(['or', 'ho', 'om'], @multi_sender.slice(1, 2))
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
context 'with built-in Array' do
|
50
|
-
should 'call the same method on each item in an Array and return the results as an array' do
|
51
|
-
multi_sender = Invoca::Utils::MultiSender.new(['some', 'short', 'words'], :map)
|
52
|
-
assert_equal([4, 5, 5], multi_sender.length)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../test_helper'
|
4
|
-
|
5
|
-
class TimeCalculationsTest < Minitest::Test
|
6
|
-
context "beginning_of_hour" do
|
7
|
-
Time.zone = 'Pacific Time (US & Canada)'
|
8
|
-
[
|
9
|
-
Time.now,
|
10
|
-
Time.zone.now,
|
11
|
-
Time.local(2009),
|
12
|
-
Time.local(2009,3,4,5),
|
13
|
-
Time.local(2001,12,31,23,59),
|
14
|
-
Time.local(1970,1,1)
|
15
|
-
].each_with_index do |time, index|
|
16
|
-
should "give back a time with no minutes, seconds, or msec: #{time} (#{index})" do
|
17
|
-
t = time.beginning_of_hour
|
18
|
-
assert_equal t.year, time.year
|
19
|
-
assert_equal t.month, time.month
|
20
|
-
assert_equal t.day, time.day
|
21
|
-
assert_equal t.hour, time.hour
|
22
|
-
assert_equal 0, t.min
|
23
|
-
assert_equal 0, t.sec
|
24
|
-
assert_equal 0, t.usec
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
context "end_of_day_whole_sec" do
|
30
|
-
should "return the end of day with whole_sec" do
|
31
|
-
t = Time.now
|
32
|
-
end_of_day = t.end_of_day
|
33
|
-
end_whole_sec = t.end_of_day_whole_sec
|
34
|
-
assert_equal 0.0, end_whole_sec.usec
|
35
|
-
assert_equal end_of_day.to_i, end_whole_sec.to_i
|
36
|
-
assert_equal end_of_day.sec, end_whole_sec.sec
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|