ndr_support 5.10.4 → 5.10.5
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/LICENSE.txt +1 -1
- data/README.md +3 -3
- data/lib/ndr_support/concerns/working_days.rb +12 -1
- data/lib/ndr_support/string/clean_methodable.rb +4 -4
- data/lib/ndr_support/version.rb +1 -1
- data/ndr_support.gemspec +8 -6
- metadata +10 -73
- data/.gitignore +0 -19
- data/.rubocop.yml +0 -1
- data/Gemfile +0 -4
- data/Guardfile +0 -24
- data/Rakefile +0 -12
- data/code_safety.yml +0 -294
- data/gemfiles/Gemfile.rails70 +0 -6
- data/gemfiles/Gemfile.rails71 +0 -6
- data/gemfiles/Gemfile.rails72 +0 -6
- data/gemfiles/Gemfile.rails80 +0 -6
- data/test/array_test.rb +0 -20
- data/test/concerns/working_days_test.rb +0 -148
- data/test/date_and_time_extensions_test.rb +0 -82
- data/test/daterange_test.rb +0 -303
- data/test/hash_test.rb +0 -84
- data/test/integer/calculations_test.rb +0 -28
- data/test/integer/rounding_test.rb +0 -14
- data/test/integer/working_days_test.rb +0 -14
- data/test/nil_test.rb +0 -40
- data/test/obfuscator_test.rb +0 -26
- data/test/ourdate_test.rb +0 -26
- data/test/ourtime_test.rb +0 -45
- data/test/password_test.rb +0 -129
- data/test/regexp_range_test.rb +0 -136
- data/test/resources/filesystem_paths.yml +0 -37
- data/test/safe_file_test.rb +0 -670
- data/test/safe_path_test.rb +0 -168
- data/test/string/cleaning_test.rb +0 -247
- data/test/string/conversions_test.rb +0 -371
- data/test/test_helper.rb +0 -42
- data/test/threat_scanner_test.rb +0 -77
- data/test/utf8_encoding/control_characters_test.rb +0 -84
- data/test/utf8_encoding/force_binary_test.rb +0 -64
- data/test/utf8_encoding_test.rb +0 -170
- data/test/yaml/serialization_test.rb +0 -200
|
@@ -1,371 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
class String::ConversionsTest < Minitest::Test
|
|
4
|
-
test 'soundex' do
|
|
5
|
-
assert_equal 'C460', 'colour'.soundex
|
|
6
|
-
assert_equal 'color'.soundex, 'colour'.soundex
|
|
7
|
-
assert 'color'.sounds_like('colour')
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
test 'date1' do
|
|
11
|
-
assert_equal '01.01.2000', '2000'.date1.to_s
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
test 'date2' do
|
|
15
|
-
assert_equal '31.12.2000', '2000'.date2.to_s
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
test 'thedate' do
|
|
19
|
-
# Treating a String like Ourdate
|
|
20
|
-
d = '01.03.2000'.thedate
|
|
21
|
-
assert_kind_of Date, d
|
|
22
|
-
assert_equal '01.03.2000', d.to_s
|
|
23
|
-
|
|
24
|
-
assert_equal '03.04.2000', '03042000'.thedate.to_s
|
|
25
|
-
assert_nil '01!02'.thedate, 'Expected illegal date format'
|
|
26
|
-
assert_nil '2000'.thedate, 'Date ranges are illegal as single dates'
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
test 'thetime' do
|
|
30
|
-
# Treating a local-format String like Ourtime (without seconds)
|
|
31
|
-
t = '01.02.1993 04:05'.thetime
|
|
32
|
-
assert_kind_of Time, t
|
|
33
|
-
assert_equal '1993-02-01 04:05:00', t.strftime('%Y-%m-%d %H:%M:%S')
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
test 'surname_and_initials' do
|
|
37
|
-
assert_equal 'Smith JD', 'SMITH JD'.surname_and_initials
|
|
38
|
-
assert_equal 'Pencheon JM', 'PENCHEON JM'.surname_and_initials
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
test 'surnameize' do
|
|
42
|
-
assert_equal 'Smith', 'SMITH'.surnameize
|
|
43
|
-
assert_equal 'McKinnon', 'MCKINNON'.surnameize
|
|
44
|
-
assert_equal 'O\'Neil', 'o\'neil'.surnameize
|
|
45
|
-
assert_equal 'X', 'X'.surnameize
|
|
46
|
-
assert_equal '', ''.surnameize
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
test 'nhs_numberize' do
|
|
50
|
-
assert_equal '123 456 7890', '1234567890'.nhs_numberize
|
|
51
|
-
assert_equal '012 345 6789', '0123456789'.nhs_numberize
|
|
52
|
-
assert_equal '012345678', '012345678'.nhs_numberize
|
|
53
|
-
assert_equal '', ''.nhs_numberize
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
test 'should parse dates correctly' do
|
|
57
|
-
assert_ymd [2001, 3, 2], '02.03.2001'.to_date
|
|
58
|
-
assert_ymd [2001, 3, 2], '02/03/2001'.to_date
|
|
59
|
-
assert_ymd [2010, 7, 11], '2010-07-11'.to_date
|
|
60
|
-
|
|
61
|
-
assert_ymd [2001, 3, 2], '2.3.2001'.to_date
|
|
62
|
-
assert_ymd [2001, 3, 2], '2/3/2001'.to_date
|
|
63
|
-
assert_ymd [2010, 7, 11], '2010-7-11'.to_date
|
|
64
|
-
|
|
65
|
-
assert_ymd [2001, 2, 3], '2001/02/03'.to_date('yyyy/mm/dd')
|
|
66
|
-
assert_ymd [2001, 2, 3], '2001/03/02'.to_date('yyyy/dd/mm')
|
|
67
|
-
assert_ymd [2001, 2, 3], '2001-02-03'.to_date('yyyy-mm-dd')
|
|
68
|
-
assert_ymd [2001, 2, 3], '03/02/2001'.to_date('dd/mm/yyyy')
|
|
69
|
-
assert_ymd [2001, 2, 3], '02/03/2001'.to_date('mm/dd/yyyy')
|
|
70
|
-
|
|
71
|
-
assert_ymd [2001, 2, 3], '03-02-2001'.to_date('dd-mm-yyyy')
|
|
72
|
-
assert_ymd [1976, 9, 23], '23-09-1976'.to_date('dd-mm-yyyy')
|
|
73
|
-
|
|
74
|
-
assert_ymd [2001, 2, 3], '20010203'.to_date
|
|
75
|
-
|
|
76
|
-
assert_ymd [1954, 2, 3], '03/02/54'.to_date
|
|
77
|
-
assert_ymd [2014, 2, 3], '03/02/14'.to_date
|
|
78
|
-
assert_ymd [2014, 2, 3], '03/FEB/14'.to_date
|
|
79
|
-
|
|
80
|
-
assert_ymd [2014, 2, 3], '03-02-2014 00:00:00'.to_date
|
|
81
|
-
assert_ymd [2014, 2, 3], '2014-02-03 00:00:00'.to_date
|
|
82
|
-
assert_ymd [2014, 3, 2], '2014/03/02 13:02:01'.to_date
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
test 'should convert strings to DateTime correctly' do
|
|
86
|
-
assert_equal 0, '2018-01-02'.to_datetime.utc_offset
|
|
87
|
-
default_timezone = ActiveRecord.default_timezone
|
|
88
|
-
return unless default_timezone == :local
|
|
89
|
-
|
|
90
|
-
assert_equal Time.new(2017, 9, 2).utc_offset, '2017-09-02'.to_datetime.utc_offset, 'Expect consistent offset'
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
test 'ParseDate should behave consistently' do
|
|
94
|
-
# We use ParseDate (and thus Date._parse) for
|
|
95
|
-
# converting strings to dates. Its behaviour
|
|
96
|
-
# has been known to change...
|
|
97
|
-
#
|
|
98
|
-
# Using '.' as a separator rather than '/' appears
|
|
99
|
-
# to be more reliable across Ruby versions, and
|
|
100
|
-
# as that is what we use internally, that is all
|
|
101
|
-
# that we need to test here:
|
|
102
|
-
|
|
103
|
-
# Behaves as you might hope:
|
|
104
|
-
assert_ymd_parsed [2001, 2, 3], ParseDate.parsedate('20010203')
|
|
105
|
-
assert_ymd_parsed [2001, 2, 3], ParseDate.parsedate('03.02.2001')
|
|
106
|
-
|
|
107
|
-
# Doesn't behave as you might hope, but reliably gets day and year mixed:
|
|
108
|
-
assert_ymd_parsed [3, 2, 14], ParseDate.parsedate('03.02.14')
|
|
109
|
-
assert_ymd_parsed [3, 2, 54], ParseDate.parsedate('03.02.54')
|
|
110
|
-
assert_ymd_parsed [3, 11, 4], ParseDate.parsedate('03.11.04')
|
|
111
|
-
|
|
112
|
-
# Doesn't behave as you might hope, but reliably gets month and year mixed:
|
|
113
|
-
assert_ymd_parsed [14, 2, 3], ParseDate.parsedate('03.FEB.14')
|
|
114
|
-
assert_ymd_parsed [54, 2, 3], ParseDate.parsedate('03.FEB.54')
|
|
115
|
-
assert_ymd_parsed [4, 2, 3], ParseDate.parsedate('03.FEB.04')
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
test 'should calculate date differences / ages correctly' do
|
|
119
|
-
[['03.02.1976', '11.11.2011', 35],
|
|
120
|
-
['29.02.1976', '28.02.2011', 35],
|
|
121
|
-
['28.02.1976', '28.02.2011', 35],
|
|
122
|
-
['28.02.1976', '27.02.2011', 34],
|
|
123
|
-
['01.03.1976', '28.02.2011', 34]
|
|
124
|
-
].each do |date1, date2, expected_diff|
|
|
125
|
-
date1 = date1.to_date if date1.is_a?(String)
|
|
126
|
-
date2 = date2.to_date if date2.is_a?(String)
|
|
127
|
-
assert_equal(expected_diff,
|
|
128
|
-
Ourdate.date_difference_in_years(date2, date1),
|
|
129
|
-
"Expected difference between #{date2} and #{date1} to be #{expected_diff} years"
|
|
130
|
-
)
|
|
131
|
-
end
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
# to_date(pattern = nil)
|
|
135
|
-
test 'dd/mm/yyyy string to_date' do
|
|
136
|
-
# pre_epoch
|
|
137
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13/02/1945'.to_date('dd/mm/yyyy')
|
|
138
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '03/05/1945'.to_date('dd/mm/yyyy')
|
|
139
|
-
# post epoch
|
|
140
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '13/02/1998'.to_date('dd/mm/yyyy')
|
|
141
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '03/05/1998'.to_date('dd/mm/yyyy')
|
|
142
|
-
|
|
143
|
-
# reverse pre_epoch
|
|
144
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13/02/1945'.to_date('yyyy/mm/dd')
|
|
145
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '03/05/1945'.to_date('yyyy/mm/dd')
|
|
146
|
-
# reverse post epoch
|
|
147
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '13/02/1998'.to_date('yyyy/mm/dd')
|
|
148
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '03/05/1998'.to_date('yyyy/mm/dd')
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
test 'yyyy/mm/dd string to_date' do
|
|
152
|
-
# pre_epoch
|
|
153
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '1945/02/13'.to_date('yyyy/mm/dd')
|
|
154
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '1945/05/03'.to_date('yyyy/mm/dd')
|
|
155
|
-
# post epoch
|
|
156
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '1998/02/13'.to_date('yyyy/mm/dd')
|
|
157
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '1998/05/03'.to_date('yyyy/mm/dd')
|
|
158
|
-
|
|
159
|
-
# reverse pre_epoch
|
|
160
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '1945/02/13'.to_date('dd/mm/yyyy')
|
|
161
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '1945/05/03'.to_date('dd/mm/yyyy')
|
|
162
|
-
# reverse post epoch
|
|
163
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '1998/02/13'.to_date('dd/mm/yyyy')
|
|
164
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '1998/05/03'.to_date('dd/mm/yyyy')
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
test 'yyyymmdd string to_date' do
|
|
168
|
-
# pre_epoch
|
|
169
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '19450213'.to_date('yyyymmdd')
|
|
170
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '19450503'.to_date('yyyymmdd')
|
|
171
|
-
# post epoch
|
|
172
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '19980213'.to_date('yyyymmdd')
|
|
173
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '19980503'.to_date('yyyymmdd')
|
|
174
|
-
|
|
175
|
-
# long pre_epoch
|
|
176
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '19450213SOMETHING'.to_date('yyyymmdd')
|
|
177
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '19450503SOMETHING'.to_date('yyyymmdd')
|
|
178
|
-
# long post epoch
|
|
179
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '19980213SOMETHING'.to_date('yyyymmdd')
|
|
180
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '19980503SOMETHING'.to_date('yyyymmdd')
|
|
181
|
-
|
|
182
|
-
# ONS wildcard date formats
|
|
183
|
-
# (cannot convert to a Date, but need to parse into EBaseRecord date fields)
|
|
184
|
-
assert_nil '19450000'.to_date('yyyymmdd')
|
|
185
|
-
assert_nil '19450300'.to_date('yyyymmdd')
|
|
186
|
-
assert_nil '19450013'.to_date('yyyymmdd')
|
|
187
|
-
|
|
188
|
-
# parse our own date format correctly, regardless of format specification
|
|
189
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '13.02.1998'.to_date('yyyymmdd')
|
|
190
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '03.05.1998'.to_date('yyyymmdd')
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
test 'ddmmyyyy string to_date' do
|
|
194
|
-
# pre_epoch
|
|
195
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13021945'.to_date('ddmmyyyy')
|
|
196
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '03051945'.to_date('ddmmyyyy')
|
|
197
|
-
# post epoch
|
|
198
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '13021998'.to_date('ddmmyyyy')
|
|
199
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '03051998'.to_date('ddmmyyyy')
|
|
200
|
-
|
|
201
|
-
# long pre_epoch
|
|
202
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13021945SOMETHING'.to_date('ddmmyyyy')
|
|
203
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '03051945SOMETHING'.to_date('ddmmyyyy')
|
|
204
|
-
# long post epoch
|
|
205
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '13021998SOMETHING'.to_date('ddmmyyyy')
|
|
206
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '03051998SOMETHING'.to_date('ddmmyyyy')
|
|
207
|
-
end
|
|
208
|
-
|
|
209
|
-
test 'mm/dd/yyyy string to_date' do
|
|
210
|
-
# This is currently unsupported, but will be tested if implemented
|
|
211
|
-
begin
|
|
212
|
-
# pre_epoch
|
|
213
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '02/13/1945'.to_date('mm/dd/yyyy')
|
|
214
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '05/03/1945'.to_date('mm/dd/yyyy')
|
|
215
|
-
# post epoch
|
|
216
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '02/13/1998'.to_date('mm/dd/yyyy')
|
|
217
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '05/03/1998'.to_date('mm/dd/yyyy')
|
|
218
|
-
rescue RuntimeError => e
|
|
219
|
-
raise e unless e.message == 'Unsupported date format'
|
|
220
|
-
end
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
test 'yyyy-mm-dd string to_date' do
|
|
224
|
-
# pre_epoch
|
|
225
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '1945-02-13'.to_date('yyyy-mm-dd')
|
|
226
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '1945-05-03'.to_date('yyyy-mm-dd')
|
|
227
|
-
# post epoch
|
|
228
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '1998-02-13'.to_date('yyyy-mm-dd')
|
|
229
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '1998-05-03'.to_date('yyyy-mm-dd')
|
|
230
|
-
end
|
|
231
|
-
|
|
232
|
-
test 'dd-mm-yyyy string to_date' do
|
|
233
|
-
# pre_epoch
|
|
234
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13-02-1945'.to_date('dd-mm-yyyy')
|
|
235
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '03-05-1945'.to_date('dd-mm-yyyy')
|
|
236
|
-
# post epoch
|
|
237
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '13-02-1998'.to_date('dd-mm-yyyy')
|
|
238
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '03-05-1998'.to_date('dd-mm-yyyy')
|
|
239
|
-
end
|
|
240
|
-
|
|
241
|
-
test '%Y-%m-%d string to_date' do
|
|
242
|
-
# pre_epoch
|
|
243
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '1945-02-13'.to_date('%Y-%m-%d')
|
|
244
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '1945-05-03'.to_date('%Y-%m-%d')
|
|
245
|
-
# post epoch
|
|
246
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '1998-02-13'.to_date('%Y-%m-%d')
|
|
247
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '1998-05-03'.to_date('%Y-%m-%d')
|
|
248
|
-
|
|
249
|
-
assert_nil ''.to_date('%Y-%m-%d') # Should behave like Rails-defined to_date
|
|
250
|
-
assert_nil ' '.to_date('%Y-%m-%d')
|
|
251
|
-
end
|
|
252
|
-
|
|
253
|
-
test '%d-%m-%Y string to_date' do
|
|
254
|
-
# pre_epoch
|
|
255
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13-02-1945'.to_date('%d-%m-%Y')
|
|
256
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '03-05-1945'.to_date('%d-%m-%Y')
|
|
257
|
-
# post epoch
|
|
258
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '13-02-1998'.to_date('%d-%m-%Y')
|
|
259
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '03-05-1998'.to_date('%d-%m-%Y')
|
|
260
|
-
end
|
|
261
|
-
|
|
262
|
-
test '%Y %b %d string to_date' do
|
|
263
|
-
# pre_epoch
|
|
264
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '1945 Feb 13'.to_date('%Y %b %d')
|
|
265
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '1945 May 03'.to_date('%Y %b %d')
|
|
266
|
-
# post epoch
|
|
267
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '1998 Feb 13'.to_date('%Y %b %d')
|
|
268
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '1998 May 03'.to_date('%Y %b %d')
|
|
269
|
-
end
|
|
270
|
-
|
|
271
|
-
test 'inferred yyyy-mm-dd string to_date' do
|
|
272
|
-
# pattern pre_epoch
|
|
273
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '1945-02-13'.to_date
|
|
274
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '1945-05-03 00:00:00'.to_date
|
|
275
|
-
# pattern post epoch
|
|
276
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '1998-02-13 00:00:00'.to_date
|
|
277
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '1998-05-03'.to_date
|
|
278
|
-
end
|
|
279
|
-
|
|
280
|
-
test 'inferred dd-mm-yyyy string to_date' do
|
|
281
|
-
# pre_epoch
|
|
282
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13-02-1945'.to_date
|
|
283
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '03-05-1945 00:00:00'.to_date
|
|
284
|
-
# post epoch
|
|
285
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '13-02-1998 00:00:00'.to_date
|
|
286
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '03-05-1998'.to_date
|
|
287
|
-
end
|
|
288
|
-
|
|
289
|
-
test 'inferred dd.mm.yyyy string to_date' do
|
|
290
|
-
# pre_epoch
|
|
291
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13.02.1945'.to_date
|
|
292
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '3.5.1945'.to_date
|
|
293
|
-
# post epoch
|
|
294
|
-
assert_equal Ourdate.build_datetime(1998, 02, 13), '13.2.1998'.to_date
|
|
295
|
-
assert_equal Ourdate.build_datetime(1998, 05, 03), '03.05.1998'.to_date
|
|
296
|
-
end
|
|
297
|
-
|
|
298
|
-
test 'inferred yyyymmdd string to_date' do
|
|
299
|
-
# pre_epoch
|
|
300
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '19450213'.to_date
|
|
301
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '19450503'.to_date
|
|
302
|
-
# post epoch
|
|
303
|
-
assert_equal Ourdate.build_datetime(2008, 02, 13), '20080213'.to_date
|
|
304
|
-
assert_equal Ourdate.build_datetime(2008, 05, 03), '20080503'.to_date
|
|
305
|
-
end
|
|
306
|
-
|
|
307
|
-
test 'inferred dd/mm/yy string to_date' do
|
|
308
|
-
# pre_epoch
|
|
309
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13/02/45'.to_date
|
|
310
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '03/05/45'.to_date
|
|
311
|
-
# post epoch
|
|
312
|
-
assert_equal Ourdate.build_datetime(2008, 02, 13), '13/02/08'.to_date
|
|
313
|
-
assert_equal Ourdate.build_datetime(2008, 05, 03), '03/05/08'.to_date
|
|
314
|
-
end
|
|
315
|
-
|
|
316
|
-
test 'inferred dd/mon/yy string to_date' do
|
|
317
|
-
# pre_epoch
|
|
318
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13/FEB/45'.to_date
|
|
319
|
-
assert_equal Ourdate.build_datetime(1945, 06, 03), '03/JUN/45'.to_date
|
|
320
|
-
# post epoch
|
|
321
|
-
assert_equal Ourdate.build_datetime(2008, 02, 13), '13/FEB/08'.to_date
|
|
322
|
-
assert_equal Ourdate.build_datetime(2008, 06, 03), '03/JUN/08'.to_date
|
|
323
|
-
end
|
|
324
|
-
|
|
325
|
-
test 'inferred dd/mm/yyyy string to_date' do
|
|
326
|
-
# pre_epoch
|
|
327
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13/02/1945'.to_date
|
|
328
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '03/05/1945'.to_date
|
|
329
|
-
# post epoch
|
|
330
|
-
assert_equal Ourdate.build_datetime(2008, 02, 13), '13/02/2008'.to_date
|
|
331
|
-
assert_equal Ourdate.build_datetime(2008, 05, 03), '03/05/2008'.to_date
|
|
332
|
-
end
|
|
333
|
-
|
|
334
|
-
test 'inferred dd/mm/yyyy hh:mm string to_date' do
|
|
335
|
-
# pre_epoch
|
|
336
|
-
assert_equal Ourdate.build_datetime(1945, 02, 13), '13/02/1945 13:38'.to_date
|
|
337
|
-
assert_equal Ourdate.build_datetime(1945, 05, 03), '03/05/1945 13:38'.to_date
|
|
338
|
-
# post epoch
|
|
339
|
-
assert_equal Ourdate.build_datetime(2008, 02, 13), '13/02/2008 13:38'.to_date
|
|
340
|
-
assert_equal Ourdate.build_datetime(2008, 05, 03), '03/05/2008 13:38'.to_date
|
|
341
|
-
end
|
|
342
|
-
|
|
343
|
-
test 'incorrectly formatted string to_date' do
|
|
344
|
-
assert_nil '10-1975'.to_date
|
|
345
|
-
assert_nil '10.1975A'.to_date
|
|
346
|
-
assert_nil '10.1975AA'.to_date
|
|
347
|
-
end
|
|
348
|
-
|
|
349
|
-
test 'to_boolean' do
|
|
350
|
-
assert_equal true, 'true'.to_boolean
|
|
351
|
-
assert_equal true, 'yes'.to_boolean
|
|
352
|
-
assert_equal true, '1'.to_boolean
|
|
353
|
-
assert_equal false, 'false'.to_boolean
|
|
354
|
-
assert_equal false, 'no'.to_boolean
|
|
355
|
-
assert_equal false, '0'.to_boolean
|
|
356
|
-
assert_raises ArgumentError do
|
|
357
|
-
'meaningless'.to_boolean
|
|
358
|
-
end
|
|
359
|
-
end
|
|
360
|
-
|
|
361
|
-
def assert_ymd(ymd, date)
|
|
362
|
-
assert_equal ymd[0], date.year, 'years were not equal'
|
|
363
|
-
assert_equal ymd[1], date.month, 'months were not equal'
|
|
364
|
-
assert_equal ymd[2], date.day, 'days were not equal'
|
|
365
|
-
end
|
|
366
|
-
|
|
367
|
-
def assert_ymd_parsed(ymd, parse_results)
|
|
368
|
-
y, m, d, *_ = parse_results
|
|
369
|
-
assert_equal ymd, [y, m, d]
|
|
370
|
-
end
|
|
371
|
-
end
|
data/test/test_helper.rb
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
require 'simplecov'
|
|
2
|
-
SimpleCov.start
|
|
3
|
-
|
|
4
|
-
require 'minitest/autorun'
|
|
5
|
-
require 'minitest/unit'
|
|
6
|
-
require 'mocha/minitest'
|
|
7
|
-
|
|
8
|
-
require 'active_record'
|
|
9
|
-
require 'active_support/time'
|
|
10
|
-
require 'ndr_support'
|
|
11
|
-
require 'tmpdir'
|
|
12
|
-
|
|
13
|
-
NdrSupport.apply_era_date_formats!
|
|
14
|
-
|
|
15
|
-
# We do not use Rails' preferred time zone support, as this would
|
|
16
|
-
# require all dates to be stored in UTC in the database.
|
|
17
|
-
# Thus a birth date of 1975-06-01 would be stored as 1975-05-31 23.00.00.
|
|
18
|
-
# Instead, we want to store all times in local time.
|
|
19
|
-
ActiveRecord.default_timezone = :local
|
|
20
|
-
ActiveRecord::Base.time_zone_aware_attributes = false
|
|
21
|
-
Time.zone = 'London'
|
|
22
|
-
|
|
23
|
-
SafePath.configure! File.dirname(__FILE__) + '/resources/filesystem_paths.yml'
|
|
24
|
-
|
|
25
|
-
# Borrowed from ActiveSupport::TestCase
|
|
26
|
-
module Minitest
|
|
27
|
-
class Test
|
|
28
|
-
# Allow declarive test syntax:
|
|
29
|
-
def self.test(name, &block)
|
|
30
|
-
test_name = "test_#{name.gsub(/\s+/, '_')}".to_sym
|
|
31
|
-
defined = method_defined? test_name
|
|
32
|
-
fail "#{test_name} is already defined in #{self}" if defined
|
|
33
|
-
if block_given?
|
|
34
|
-
define_method(test_name, &block)
|
|
35
|
-
else
|
|
36
|
-
define_method(test_name) do
|
|
37
|
-
flunk "No implementation provided for #{name}"
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
end
|
data/test/threat_scanner_test.rb
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
# This tests our ThreatScanner extension
|
|
4
|
-
class ThreatScannerTest < Minitest::Test
|
|
5
|
-
def setup
|
|
6
|
-
@tempfile = Tempfile.new
|
|
7
|
-
@scanner = ThreatScanner.new(@tempfile)
|
|
8
|
-
|
|
9
|
-
ThreatScanner.stubs(installed?: true)
|
|
10
|
-
ThreatScanner.any_instance.stubs(:`)
|
|
11
|
-
|
|
12
|
-
`pwd` # Ensure that $CHILD_STATUS is not nil, even if we never run any child processes
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def teardown
|
|
16
|
-
@tempfile.close!
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
test 'can be initialised with a file' do
|
|
20
|
-
assert_equal @tempfile.path, @scanner.path
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
test 'can be initialised with a path' do
|
|
24
|
-
scanner = ThreatScanner.new(@tempfile.path)
|
|
25
|
-
assert_equal @tempfile.path, scanner.path
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
test 'returns true if no threat is detected (when being strict)' do
|
|
29
|
-
Process::Status.any_instance.stubs(exitstatus: 0)
|
|
30
|
-
assert_equal true, @scanner.check!
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
test 'raises if a threat is detected (when being strict)' do
|
|
34
|
-
Process::Status.any_instance.stubs(exitstatus: 1)
|
|
35
|
-
assert_raises(ThreatScanner::ThreatDetectedError) { @scanner.check! }
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
test 'raises if the file does not exist (when being strict)' do
|
|
39
|
-
@tempfile.close!
|
|
40
|
-
assert_raises(ThreatScanner::MissingFileError) { @scanner.check! }
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
test 'raises if ClamAV is not installed (when being strict)' do
|
|
44
|
-
ThreatScanner.stubs(installed?: false)
|
|
45
|
-
assert_raises(ThreatScanner::MissingScannerError) { @scanner.check! }
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
test 'raises if there is an operational error (when being strict)' do
|
|
49
|
-
Process::Status.any_instance.stubs(exitstatus: 2)
|
|
50
|
-
assert_raises(ThreatScanner::ScannerOperationError) { @scanner.check! }
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
test 'returns true if no threat is detected (when being relaxed)' do
|
|
54
|
-
Process::Status.any_instance.stubs(exitstatus: 0)
|
|
55
|
-
assert_equal true, @scanner.check
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
test 'raises if a threat is detected (when being relaxed)' do
|
|
59
|
-
Process::Status.any_instance.stubs(exitstatus: 1)
|
|
60
|
-
assert_raises(ThreatScanner::ThreatDetectedError) { @scanner.check }
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
test 'raises if the file does not exist (when being relaxed)' do
|
|
64
|
-
@tempfile.close!
|
|
65
|
-
assert_raises(ThreatScanner::MissingFileError) { @scanner.check }
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
test 'returns false if ClamAV is not installed (when being relaxed)' do
|
|
69
|
-
ThreatScanner.stubs(installed?: false)
|
|
70
|
-
assert_equal false, @scanner.check
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
test 'returns false if there is an operational error (when being relaxed)' do
|
|
74
|
-
Process::Status.any_instance.stubs(exitstatus: 2)
|
|
75
|
-
assert_equal false, @scanner.check
|
|
76
|
-
end
|
|
77
|
-
end
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
# Tests Utf8Encoding::ControlCharacters module.
|
|
4
|
-
class ControlCharactersTest < Minitest::Test
|
|
5
|
-
include UTF8Encoding
|
|
6
|
-
|
|
7
|
-
test 'control char identification' do
|
|
8
|
-
(0..255).each do |code|
|
|
9
|
-
expected = code == 127 || (code < 32 && [9, 10, 13].exclude?(code)) ? 4 : 1
|
|
10
|
-
actual = escape_control_chars(code.chr).length
|
|
11
|
-
|
|
12
|
-
assert_equal expected, actual, "unexpected escaping for value: #{code} (#{code.chr})"
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
test 'escape_control_chars with harmless string' do
|
|
17
|
-
string = 'null \x00 characters suck'
|
|
18
|
-
expected = 'null \x00 characters suck'
|
|
19
|
-
actual = escape_control_chars(string)
|
|
20
|
-
|
|
21
|
-
assert_equal expected, actual
|
|
22
|
-
refute actual.object_id == string.object_id, 'should not have modified in place'
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
test 'escape_control_chars! with harmless string' do
|
|
26
|
-
string = 'null \x00 characters suck'
|
|
27
|
-
expected = 'null \x00 characters suck'
|
|
28
|
-
actual = escape_control_chars!(string)
|
|
29
|
-
|
|
30
|
-
assert_equal expected, actual
|
|
31
|
-
assert_equal actual.object_id, string.object_id
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
test 'escape_control_chars with unprintable control characters' do
|
|
35
|
-
string = "null \x00 \x7F characters suck"
|
|
36
|
-
expected = 'null 0x00 0x7f characters suck'
|
|
37
|
-
actual = escape_control_chars(string)
|
|
38
|
-
|
|
39
|
-
assert_equal expected, actual
|
|
40
|
-
refute actual.object_id == string.object_id, 'should not have modified in place'
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
test 'escape_control_chars! with unprintable control characters' do
|
|
44
|
-
string = "null \x00 characters suck"
|
|
45
|
-
expected = 'null 0x00 characters suck'
|
|
46
|
-
actual = escape_control_chars!(string)
|
|
47
|
-
|
|
48
|
-
assert_equal expected, actual
|
|
49
|
-
assert_equal string.object_id, actual.object_id
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
test 'escape_control_chars! with printable control characters' do
|
|
53
|
-
string = "null \x00 characters \r\n really \t suck \x07\x07\x07"
|
|
54
|
-
expected = "null 0x00 characters \r\n really \t suck 0x070x070x07" # ring ring ring
|
|
55
|
-
|
|
56
|
-
assert_equal expected, escape_control_chars!(string)
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
test 'escape_control_chars_in_object! with array' do
|
|
60
|
-
array = %W( hello\tcruel \x00 world!\n \x07 )
|
|
61
|
-
expected = %W( hello\tcruel 0x00 world!\n 0x07 )
|
|
62
|
-
actual = escape_control_chars_in_object!(array)
|
|
63
|
-
|
|
64
|
-
assert_equal expected, actual
|
|
65
|
-
assert_equal array.object_id, actual.object_id
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
test 'escape_control_chars_in_object! with hash' do
|
|
69
|
-
hash = { :a => "hello\tcruel", :b => "\x00", :c => "world!\n", :d => "\x07" }
|
|
70
|
-
expected = { :a => "hello\tcruel", :b => '0x00', :c => "world!\n", :d => '0x07' }
|
|
71
|
-
actual = escape_control_chars_in_object!(hash)
|
|
72
|
-
|
|
73
|
-
assert_equal expected, actual
|
|
74
|
-
assert_equal hash.object_id, actual.object_id
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
test 'escape_control_chars_in_object! with PORO' do
|
|
78
|
-
object = Object.new
|
|
79
|
-
escaped = escape_control_chars_in_object!(object)
|
|
80
|
-
|
|
81
|
-
assert_equal object, escaped
|
|
82
|
-
assert_equal object.object_id, escaped.object_id
|
|
83
|
-
end
|
|
84
|
-
end
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
# encoding: UTF-8
|
|
2
|
-
require 'test_helper'
|
|
3
|
-
|
|
4
|
-
# Tests Utf8Encoding::ForceBinary module.
|
|
5
|
-
class ForceBinaryTest < Minitest::Test
|
|
6
|
-
include UTF8Encoding
|
|
7
|
-
|
|
8
|
-
test 'binary_encode_any_high_ascii with low-ascii string' do
|
|
9
|
-
input = 'manana manana'
|
|
10
|
-
|
|
11
|
-
assert_equal 'UTF-8', input.encoding.name
|
|
12
|
-
assert input.valid_encoding?
|
|
13
|
-
|
|
14
|
-
output = binary_encode_any_high_ascii(input)
|
|
15
|
-
|
|
16
|
-
refute_equal input.object_id, output.object_id
|
|
17
|
-
|
|
18
|
-
assert_equal input.bytes.to_a, output.bytes.to_a
|
|
19
|
-
assert_equal 'UTF-8', output.encoding.name
|
|
20
|
-
assert output.valid_encoding?
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
test 'binary_encode_any_high_ascii with high-ascii string' do
|
|
24
|
-
input = 'mañana mañana'
|
|
25
|
-
|
|
26
|
-
assert_equal 'UTF-8', input.encoding.name
|
|
27
|
-
assert input.valid_encoding?
|
|
28
|
-
|
|
29
|
-
output = binary_encode_any_high_ascii(input)
|
|
30
|
-
|
|
31
|
-
refute_equal input.object_id, output.object_id
|
|
32
|
-
|
|
33
|
-
assert_equal input.bytes.to_a, output.bytes.to_a
|
|
34
|
-
assert_equal 'ASCII-8BIT', output.encoding.name
|
|
35
|
-
assert output.valid_encoding?
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
test 'binary_encode_any_high_ascii with array' do
|
|
39
|
-
input = %w(mañana manana)
|
|
40
|
-
output = binary_encode_any_high_ascii(input)
|
|
41
|
-
|
|
42
|
-
refute_equal input.object_id, output.object_id
|
|
43
|
-
|
|
44
|
-
assert_equal %w(UTF-8 UTF-8), input.map { |str| str.encoding.name }
|
|
45
|
-
assert_equal %w(ASCII-8BIT UTF-8), output.map { |str| str.encoding.name }
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
test 'binary_encode_any_high_ascii with hash' do
|
|
49
|
-
input = { :with => 'mañana', :without => 'manana' }
|
|
50
|
-
output = binary_encode_any_high_ascii(input)
|
|
51
|
-
|
|
52
|
-
refute_equal input.object_id, output.object_id
|
|
53
|
-
|
|
54
|
-
assert_equal 'ASCII-8BIT', output[:with].encoding.name
|
|
55
|
-
assert_equal 'UTF-8', output[:without].encoding.name
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
test 'binary_encode_any_high_ascii with other object' do
|
|
59
|
-
input = /mañana mañana/
|
|
60
|
-
output = binary_encode_any_high_ascii(input)
|
|
61
|
-
|
|
62
|
-
assert_equal input.object_id, output.object_id, 'should have returned same object'
|
|
63
|
-
end
|
|
64
|
-
end
|