ndr_support 5.10.3 → 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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -1
  3. data/LICENSE.txt +1 -1
  4. data/README.md +3 -3
  5. data/lib/ndr_support/concerns/working_days.rb +12 -1
  6. data/lib/ndr_support/date_and_time_extensions.rb +12 -11
  7. data/lib/ndr_support/ourdate/build_datetime.rb +1 -5
  8. data/lib/ndr_support/ourdate.rb +1 -5
  9. data/lib/ndr_support/string/clean_methodable.rb +4 -4
  10. data/lib/ndr_support/string/conversions.rb +1 -5
  11. data/lib/ndr_support/version.rb +1 -1
  12. data/ndr_support.gemspec +8 -6
  13. metadata +14 -76
  14. data/.gitignore +0 -19
  15. data/.rubocop.yml +0 -1
  16. data/Gemfile +0 -4
  17. data/Guardfile +0 -24
  18. data/Rakefile +0 -12
  19. data/code_safety.yml +0 -290
  20. data/gemfiles/Gemfile.rails61 +0 -6
  21. data/gemfiles/Gemfile.rails70 +0 -6
  22. data/gemfiles/Gemfile.rails71 +0 -6
  23. data/test/array_test.rb +0 -20
  24. data/test/concerns/working_days_test.rb +0 -148
  25. data/test/date_and_time_extensions_test.rb +0 -56
  26. data/test/daterange_test.rb +0 -307
  27. data/test/hash_test.rb +0 -84
  28. data/test/integer/calculations_test.rb +0 -28
  29. data/test/integer/rounding_test.rb +0 -14
  30. data/test/integer/working_days_test.rb +0 -14
  31. data/test/nil_test.rb +0 -40
  32. data/test/obfuscator_test.rb +0 -26
  33. data/test/ourdate_test.rb +0 -26
  34. data/test/ourtime_test.rb +0 -45
  35. data/test/password_test.rb +0 -129
  36. data/test/regexp_range_test.rb +0 -136
  37. data/test/resources/filesystem_paths.yml +0 -37
  38. data/test/safe_file_test.rb +0 -670
  39. data/test/safe_path_test.rb +0 -168
  40. data/test/string/cleaning_test.rb +0 -247
  41. data/test/string/conversions_test.rb +0 -375
  42. data/test/test_helper.rb +0 -46
  43. data/test/threat_scanner_test.rb +0 -77
  44. data/test/utf8_encoding/control_characters_test.rb +0 -84
  45. data/test/utf8_encoding/force_binary_test.rb +0 -64
  46. data/test/utf8_encoding_test.rb +0 -170
  47. data/test/yaml/serialization_test.rb +0 -200
@@ -1,375 +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 = if ActiveRecord.respond_to?(:default_timezone)
88
- ActiveRecord.default_timezone
89
- else
90
- ActiveRecord::Base.default_timezone # Rails <= 6.1
91
- end
92
- return unless default_timezone == :local
93
-
94
- assert_equal Time.new(2017, 9, 2).utc_offset, '2017-09-02'.to_datetime.utc_offset, 'Expect consistent offset'
95
- end
96
-
97
- test 'ParseDate should behave consistently' do
98
- # We use ParseDate (and thus Date._parse) for
99
- # converting strings to dates. Its behaviour
100
- # has been known to change...
101
- #
102
- # Using '.' as a separator rather than '/' appears
103
- # to be more reliable across Ruby versions, and
104
- # as that is what we use internally, that is all
105
- # that we need to test here:
106
-
107
- # Behaves as you might hope:
108
- assert_ymd_parsed [2001, 2, 3], ParseDate.parsedate('20010203')
109
- assert_ymd_parsed [2001, 2, 3], ParseDate.parsedate('03.02.2001')
110
-
111
- # Doesn't behave as you might hope, but reliably gets day and year mixed:
112
- assert_ymd_parsed [3, 2, 14], ParseDate.parsedate('03.02.14')
113
- assert_ymd_parsed [3, 2, 54], ParseDate.parsedate('03.02.54')
114
- assert_ymd_parsed [3, 11, 4], ParseDate.parsedate('03.11.04')
115
-
116
- # Doesn't behave as you might hope, but reliably gets month and year mixed:
117
- assert_ymd_parsed [14, 2, 3], ParseDate.parsedate('03.FEB.14')
118
- assert_ymd_parsed [54, 2, 3], ParseDate.parsedate('03.FEB.54')
119
- assert_ymd_parsed [4, 2, 3], ParseDate.parsedate('03.FEB.04')
120
- end
121
-
122
- test 'should calculate date differences / ages correctly' do
123
- [['03.02.1976', '11.11.2011', 35],
124
- ['29.02.1976', '28.02.2011', 35],
125
- ['28.02.1976', '28.02.2011', 35],
126
- ['28.02.1976', '27.02.2011', 34],
127
- ['01.03.1976', '28.02.2011', 34]
128
- ].each do |date1, date2, expected_diff|
129
- date1 = date1.to_date if date1.is_a?(String)
130
- date2 = date2.to_date if date2.is_a?(String)
131
- assert_equal(expected_diff,
132
- Ourdate.date_difference_in_years(date2, date1),
133
- "Expected difference between #{date2} and #{date1} to be #{expected_diff} years"
134
- )
135
- end
136
- end
137
-
138
- # to_date(pattern = nil)
139
- test 'dd/mm/yyyy string to_date' do
140
- # pre_epoch
141
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13/02/1945'.to_date('dd/mm/yyyy')
142
- assert_equal Ourdate.build_datetime(1945, 05, 03), '03/05/1945'.to_date('dd/mm/yyyy')
143
- # post epoch
144
- assert_equal Ourdate.build_datetime(1998, 02, 13), '13/02/1998'.to_date('dd/mm/yyyy')
145
- assert_equal Ourdate.build_datetime(1998, 05, 03), '03/05/1998'.to_date('dd/mm/yyyy')
146
-
147
- # reverse pre_epoch
148
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13/02/1945'.to_date('yyyy/mm/dd')
149
- assert_equal Ourdate.build_datetime(1945, 05, 03), '03/05/1945'.to_date('yyyy/mm/dd')
150
- # reverse post epoch
151
- assert_equal Ourdate.build_datetime(1998, 02, 13), '13/02/1998'.to_date('yyyy/mm/dd')
152
- assert_equal Ourdate.build_datetime(1998, 05, 03), '03/05/1998'.to_date('yyyy/mm/dd')
153
- end
154
-
155
- test 'yyyy/mm/dd string to_date' do
156
- # pre_epoch
157
- assert_equal Ourdate.build_datetime(1945, 02, 13), '1945/02/13'.to_date('yyyy/mm/dd')
158
- assert_equal Ourdate.build_datetime(1945, 05, 03), '1945/05/03'.to_date('yyyy/mm/dd')
159
- # post epoch
160
- assert_equal Ourdate.build_datetime(1998, 02, 13), '1998/02/13'.to_date('yyyy/mm/dd')
161
- assert_equal Ourdate.build_datetime(1998, 05, 03), '1998/05/03'.to_date('yyyy/mm/dd')
162
-
163
- # reverse pre_epoch
164
- assert_equal Ourdate.build_datetime(1945, 02, 13), '1945/02/13'.to_date('dd/mm/yyyy')
165
- assert_equal Ourdate.build_datetime(1945, 05, 03), '1945/05/03'.to_date('dd/mm/yyyy')
166
- # reverse post epoch
167
- assert_equal Ourdate.build_datetime(1998, 02, 13), '1998/02/13'.to_date('dd/mm/yyyy')
168
- assert_equal Ourdate.build_datetime(1998, 05, 03), '1998/05/03'.to_date('dd/mm/yyyy')
169
- end
170
-
171
- test 'yyyymmdd string to_date' do
172
- # pre_epoch
173
- assert_equal Ourdate.build_datetime(1945, 02, 13), '19450213'.to_date('yyyymmdd')
174
- assert_equal Ourdate.build_datetime(1945, 05, 03), '19450503'.to_date('yyyymmdd')
175
- # post epoch
176
- assert_equal Ourdate.build_datetime(1998, 02, 13), '19980213'.to_date('yyyymmdd')
177
- assert_equal Ourdate.build_datetime(1998, 05, 03), '19980503'.to_date('yyyymmdd')
178
-
179
- # long pre_epoch
180
- assert_equal Ourdate.build_datetime(1945, 02, 13), '19450213SOMETHING'.to_date('yyyymmdd')
181
- assert_equal Ourdate.build_datetime(1945, 05, 03), '19450503SOMETHING'.to_date('yyyymmdd')
182
- # long post epoch
183
- assert_equal Ourdate.build_datetime(1998, 02, 13), '19980213SOMETHING'.to_date('yyyymmdd')
184
- assert_equal Ourdate.build_datetime(1998, 05, 03), '19980503SOMETHING'.to_date('yyyymmdd')
185
-
186
- # ONS wildcard date formats
187
- # (cannot convert to a Date, but need to parse into EBaseRecord date fields)
188
- assert_nil '19450000'.to_date('yyyymmdd')
189
- assert_nil '19450300'.to_date('yyyymmdd')
190
- assert_nil '19450013'.to_date('yyyymmdd')
191
-
192
- # parse our own date format correctly, regardless of format specification
193
- assert_equal Ourdate.build_datetime(1998, 02, 13), '13.02.1998'.to_date('yyyymmdd')
194
- assert_equal Ourdate.build_datetime(1998, 05, 03), '03.05.1998'.to_date('yyyymmdd')
195
- end
196
-
197
- test 'ddmmyyyy string to_date' do
198
- # pre_epoch
199
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13021945'.to_date('ddmmyyyy')
200
- assert_equal Ourdate.build_datetime(1945, 05, 03), '03051945'.to_date('ddmmyyyy')
201
- # post epoch
202
- assert_equal Ourdate.build_datetime(1998, 02, 13), '13021998'.to_date('ddmmyyyy')
203
- assert_equal Ourdate.build_datetime(1998, 05, 03), '03051998'.to_date('ddmmyyyy')
204
-
205
- # long pre_epoch
206
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13021945SOMETHING'.to_date('ddmmyyyy')
207
- assert_equal Ourdate.build_datetime(1945, 05, 03), '03051945SOMETHING'.to_date('ddmmyyyy')
208
- # long post epoch
209
- assert_equal Ourdate.build_datetime(1998, 02, 13), '13021998SOMETHING'.to_date('ddmmyyyy')
210
- assert_equal Ourdate.build_datetime(1998, 05, 03), '03051998SOMETHING'.to_date('ddmmyyyy')
211
- end
212
-
213
- test 'mm/dd/yyyy string to_date' do
214
- # This is currently unsupported, but will be tested if implemented
215
- begin
216
- # pre_epoch
217
- assert_equal Ourdate.build_datetime(1945, 02, 13), '02/13/1945'.to_date('mm/dd/yyyy')
218
- assert_equal Ourdate.build_datetime(1945, 05, 03), '05/03/1945'.to_date('mm/dd/yyyy')
219
- # post epoch
220
- assert_equal Ourdate.build_datetime(1998, 02, 13), '02/13/1998'.to_date('mm/dd/yyyy')
221
- assert_equal Ourdate.build_datetime(1998, 05, 03), '05/03/1998'.to_date('mm/dd/yyyy')
222
- rescue RuntimeError => e
223
- raise e unless e.message == 'Unsupported date format'
224
- end
225
- end
226
-
227
- test 'yyyy-mm-dd string to_date' do
228
- # pre_epoch
229
- assert_equal Ourdate.build_datetime(1945, 02, 13), '1945-02-13'.to_date('yyyy-mm-dd')
230
- assert_equal Ourdate.build_datetime(1945, 05, 03), '1945-05-03'.to_date('yyyy-mm-dd')
231
- # post epoch
232
- assert_equal Ourdate.build_datetime(1998, 02, 13), '1998-02-13'.to_date('yyyy-mm-dd')
233
- assert_equal Ourdate.build_datetime(1998, 05, 03), '1998-05-03'.to_date('yyyy-mm-dd')
234
- end
235
-
236
- test 'dd-mm-yyyy string to_date' do
237
- # pre_epoch
238
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13-02-1945'.to_date('dd-mm-yyyy')
239
- assert_equal Ourdate.build_datetime(1945, 05, 03), '03-05-1945'.to_date('dd-mm-yyyy')
240
- # post epoch
241
- assert_equal Ourdate.build_datetime(1998, 02, 13), '13-02-1998'.to_date('dd-mm-yyyy')
242
- assert_equal Ourdate.build_datetime(1998, 05, 03), '03-05-1998'.to_date('dd-mm-yyyy')
243
- end
244
-
245
- test '%Y-%m-%d string to_date' do
246
- # pre_epoch
247
- assert_equal Ourdate.build_datetime(1945, 02, 13), '1945-02-13'.to_date('%Y-%m-%d')
248
- assert_equal Ourdate.build_datetime(1945, 05, 03), '1945-05-03'.to_date('%Y-%m-%d')
249
- # post epoch
250
- assert_equal Ourdate.build_datetime(1998, 02, 13), '1998-02-13'.to_date('%Y-%m-%d')
251
- assert_equal Ourdate.build_datetime(1998, 05, 03), '1998-05-03'.to_date('%Y-%m-%d')
252
-
253
- assert_nil ''.to_date('%Y-%m-%d') # Should behave like Rails-defined to_date
254
- assert_nil ' '.to_date('%Y-%m-%d')
255
- end
256
-
257
- test '%d-%m-%Y string to_date' do
258
- # pre_epoch
259
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13-02-1945'.to_date('%d-%m-%Y')
260
- assert_equal Ourdate.build_datetime(1945, 05, 03), '03-05-1945'.to_date('%d-%m-%Y')
261
- # post epoch
262
- assert_equal Ourdate.build_datetime(1998, 02, 13), '13-02-1998'.to_date('%d-%m-%Y')
263
- assert_equal Ourdate.build_datetime(1998, 05, 03), '03-05-1998'.to_date('%d-%m-%Y')
264
- end
265
-
266
- test '%Y %b %d string to_date' do
267
- # pre_epoch
268
- assert_equal Ourdate.build_datetime(1945, 02, 13), '1945 Feb 13'.to_date('%Y %b %d')
269
- assert_equal Ourdate.build_datetime(1945, 05, 03), '1945 May 03'.to_date('%Y %b %d')
270
- # post epoch
271
- assert_equal Ourdate.build_datetime(1998, 02, 13), '1998 Feb 13'.to_date('%Y %b %d')
272
- assert_equal Ourdate.build_datetime(1998, 05, 03), '1998 May 03'.to_date('%Y %b %d')
273
- end
274
-
275
- test 'inferred yyyy-mm-dd string to_date' do
276
- # pattern pre_epoch
277
- assert_equal Ourdate.build_datetime(1945, 02, 13), '1945-02-13'.to_date
278
- assert_equal Ourdate.build_datetime(1945, 05, 03), '1945-05-03 00:00:00'.to_date
279
- # pattern post epoch
280
- assert_equal Ourdate.build_datetime(1998, 02, 13), '1998-02-13 00:00:00'.to_date
281
- assert_equal Ourdate.build_datetime(1998, 05, 03), '1998-05-03'.to_date
282
- end
283
-
284
- test 'inferred dd-mm-yyyy string to_date' do
285
- # pre_epoch
286
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13-02-1945'.to_date
287
- assert_equal Ourdate.build_datetime(1945, 05, 03), '03-05-1945 00:00:00'.to_date
288
- # post epoch
289
- assert_equal Ourdate.build_datetime(1998, 02, 13), '13-02-1998 00:00:00'.to_date
290
- assert_equal Ourdate.build_datetime(1998, 05, 03), '03-05-1998'.to_date
291
- end
292
-
293
- test 'inferred dd.mm.yyyy string to_date' do
294
- # pre_epoch
295
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13.02.1945'.to_date
296
- assert_equal Ourdate.build_datetime(1945, 05, 03), '3.5.1945'.to_date
297
- # post epoch
298
- assert_equal Ourdate.build_datetime(1998, 02, 13), '13.2.1998'.to_date
299
- assert_equal Ourdate.build_datetime(1998, 05, 03), '03.05.1998'.to_date
300
- end
301
-
302
- test 'inferred yyyymmdd string to_date' do
303
- # pre_epoch
304
- assert_equal Ourdate.build_datetime(1945, 02, 13), '19450213'.to_date
305
- assert_equal Ourdate.build_datetime(1945, 05, 03), '19450503'.to_date
306
- # post epoch
307
- assert_equal Ourdate.build_datetime(2008, 02, 13), '20080213'.to_date
308
- assert_equal Ourdate.build_datetime(2008, 05, 03), '20080503'.to_date
309
- end
310
-
311
- test 'inferred dd/mm/yy string to_date' do
312
- # pre_epoch
313
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13/02/45'.to_date
314
- assert_equal Ourdate.build_datetime(1945, 05, 03), '03/05/45'.to_date
315
- # post epoch
316
- assert_equal Ourdate.build_datetime(2008, 02, 13), '13/02/08'.to_date
317
- assert_equal Ourdate.build_datetime(2008, 05, 03), '03/05/08'.to_date
318
- end
319
-
320
- test 'inferred dd/mon/yy string to_date' do
321
- # pre_epoch
322
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13/FEB/45'.to_date
323
- assert_equal Ourdate.build_datetime(1945, 06, 03), '03/JUN/45'.to_date
324
- # post epoch
325
- assert_equal Ourdate.build_datetime(2008, 02, 13), '13/FEB/08'.to_date
326
- assert_equal Ourdate.build_datetime(2008, 06, 03), '03/JUN/08'.to_date
327
- end
328
-
329
- test 'inferred dd/mm/yyyy string to_date' do
330
- # pre_epoch
331
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13/02/1945'.to_date
332
- assert_equal Ourdate.build_datetime(1945, 05, 03), '03/05/1945'.to_date
333
- # post epoch
334
- assert_equal Ourdate.build_datetime(2008, 02, 13), '13/02/2008'.to_date
335
- assert_equal Ourdate.build_datetime(2008, 05, 03), '03/05/2008'.to_date
336
- end
337
-
338
- test 'inferred dd/mm/yyyy hh:mm string to_date' do
339
- # pre_epoch
340
- assert_equal Ourdate.build_datetime(1945, 02, 13), '13/02/1945 13:38'.to_date
341
- assert_equal Ourdate.build_datetime(1945, 05, 03), '03/05/1945 13:38'.to_date
342
- # post epoch
343
- assert_equal Ourdate.build_datetime(2008, 02, 13), '13/02/2008 13:38'.to_date
344
- assert_equal Ourdate.build_datetime(2008, 05, 03), '03/05/2008 13:38'.to_date
345
- end
346
-
347
- test 'incorrectly formatted string to_date' do
348
- assert_nil '10-1975'.to_date
349
- assert_nil '10.1975A'.to_date
350
- assert_nil '10.1975AA'.to_date
351
- end
352
-
353
- test 'to_boolean' do
354
- assert_equal true, 'true'.to_boolean
355
- assert_equal true, 'yes'.to_boolean
356
- assert_equal true, '1'.to_boolean
357
- assert_equal false, 'false'.to_boolean
358
- assert_equal false, 'no'.to_boolean
359
- assert_equal false, '0'.to_boolean
360
- assert_raises ArgumentError do
361
- 'meaningless'.to_boolean
362
- end
363
- end
364
-
365
- def assert_ymd(ymd, date)
366
- assert_equal ymd[0], date.year, 'years were not equal'
367
- assert_equal ymd[1], date.month, 'months were not equal'
368
- assert_equal ymd[2], date.day, 'days were not equal'
369
- end
370
-
371
- def assert_ymd_parsed(ymd, parse_results)
372
- y, m, d, *_ = parse_results
373
- assert_equal ymd, [y, m, d]
374
- end
375
- end
data/test/test_helper.rb DELETED
@@ -1,46 +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
- if ActiveRecord.respond_to?(:default_timezone=)
20
- ActiveRecord.default_timezone = :local
21
- else
22
- ActiveRecord::Base.default_timezone = :local # Rails <= 6.1
23
- end
24
- ActiveRecord::Base.time_zone_aware_attributes = false
25
- Time.zone = 'London'
26
-
27
- SafePath.configure! File.dirname(__FILE__) + '/resources/filesystem_paths.yml'
28
-
29
- # Borrowed from ActiveSupport::TestCase
30
- module Minitest
31
- class Test
32
- # Allow declarive test syntax:
33
- def self.test(name, &block)
34
- test_name = "test_#{name.gsub(/\s+/, '_')}".to_sym
35
- defined = method_defined? test_name
36
- fail "#{test_name} is already defined in #{self}" if defined
37
- if block_given?
38
- define_method(test_name, &block)
39
- else
40
- define_method(test_name) do
41
- flunk "No implementation provided for #{name}"
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -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