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
data/gemfiles/Gemfile.rails80
DELETED
data/test/array_test.rb
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
# This tests our Array extension
|
|
4
|
-
class ArrayTest < Minitest::Test
|
|
5
|
-
test 'Arrays should be extended with #permutations' do
|
|
6
|
-
assert [].respond_to?(:permutations)
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
test 'Array#permutations should calculate permutations correctly' do
|
|
10
|
-
array = [1, 2, 3]
|
|
11
|
-
permutations = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
|
|
12
|
-
assert_equal array.length.factorial, array.permutations.length
|
|
13
|
-
assert_equal permutations.sort, array.permutations.sort
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
test 'Array#permutations should permute duplicates' do
|
|
17
|
-
array = [1, 1]
|
|
18
|
-
assert_equal [[1, 1], [1, 1]], array.permutations
|
|
19
|
-
end
|
|
20
|
-
end
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
# This tests our WorkingDays Time/Date/DateTime extension
|
|
4
|
-
class WorkingDaysTest < Minitest::Test
|
|
5
|
-
def setup
|
|
6
|
-
@normal_date = Date.parse('2015-02-02') # Monday 2nd Feb 2015
|
|
7
|
-
@normal_time = Time.parse('2015-02-02 2pm') # Monday 2nd Feb 2015
|
|
8
|
-
@normal_date_time = DateTime.parse('2015-02-02 11am') # Monday 2nd Feb 2015
|
|
9
|
-
|
|
10
|
-
@easter_date = Date.parse('2015-04-06') # Easter Monday, 2015
|
|
11
|
-
@easter_time = Time.parse('2015-04-06 2pm') # Easter Monday, 2015
|
|
12
|
-
@easter_date_time = DateTime.parse('2015-04-06 11am') # Easter Monday, 2015
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
test 'should identify weekdays' do
|
|
16
|
-
assert Date.parse('2015-12-25').weekday?
|
|
17
|
-
assert Time.parse('2015-12-25 3pm').weekday?
|
|
18
|
-
assert DateTime.parse('2015-12-25 3pm').weekday?
|
|
19
|
-
|
|
20
|
-
refute Date.parse('2015-12-26').weekday?
|
|
21
|
-
refute Time.parse('2015-12-26 3pm').weekday?
|
|
22
|
-
refute DateTime.parse('2015-12-26 3pm').weekday?
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
test 'should identify bank holidays' do
|
|
26
|
-
assert Date.parse('2015-12-25').public_holiday?
|
|
27
|
-
assert Time.parse('2015-12-25 3pm').public_holiday?
|
|
28
|
-
assert DateTime.parse('2015-12-25 3pm').public_holiday?
|
|
29
|
-
|
|
30
|
-
# Boxing Day 2015 is a Saturday; the bank holiday a
|
|
31
|
-
# substitute day, on the following Monday:
|
|
32
|
-
refute Date.parse('2015-12-26').public_holiday?
|
|
33
|
-
refute Time.parse('2015-12-26 3pm').public_holiday?
|
|
34
|
-
refute DateTime.parse('2015-12-26 3pm').public_holiday?
|
|
35
|
-
|
|
36
|
-
assert Date.parse('2015-12-28').public_holiday?
|
|
37
|
-
assert Time.parse('2015-12-28 3pm').public_holiday?
|
|
38
|
-
assert DateTime.parse('2015-12-28 3pm').public_holiday?
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
test 'should allow comparison of Time and DateTime' do
|
|
42
|
-
@normal_time.working_days_until(@normal_date_time)
|
|
43
|
-
@normal_date_time.working_days_until(@normal_time)
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
test 'should allow comparison of DateTime and Date' do
|
|
47
|
-
@normal_date_time.working_days_until(@normal_date)
|
|
48
|
-
@normal_date.working_days_until(@normal_date_time)
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
test 'should be zero working days between same normal day' do
|
|
52
|
-
assert_equal 0, @normal_date.working_days_until(@normal_date)
|
|
53
|
-
assert_equal 0, @normal_time.working_days_until(@normal_time + 6.hours)
|
|
54
|
-
assert_equal 0, @normal_date_time.working_days_until(@normal_date_time + 6.hours)
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
test 'Monday -> Friday should be 4 working days' do
|
|
58
|
-
assert_equal 4, @normal_date.working_days_until(@normal_date + 4.days)
|
|
59
|
-
assert_equal 4, @normal_time.working_days_until(@normal_time + 4.days)
|
|
60
|
-
assert_equal 4, @normal_date_time.working_days_until(@normal_date_time + 4.days)
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
test 'Friday <- Monday should be -4 working days' do
|
|
64
|
-
assert_equal(-4, (@normal_date + 4.days).working_days_until(@normal_date))
|
|
65
|
-
assert_equal(-4, (@normal_time + 4.days).working_days_until(@normal_time))
|
|
66
|
-
assert_equal(-4, (@normal_date_time + 4.days).working_days_until(@normal_date_time))
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
test 'Saturday -> Sunday should be 0 working days' do
|
|
70
|
-
assert_equal 0, (@normal_date - 2.days).working_days_until(@normal_date - 1.day)
|
|
71
|
-
assert_equal 0, (@normal_time - 2.days).working_days_until(@normal_time - 1.day)
|
|
72
|
-
assert_equal 0, (@normal_date_time - 2.days).working_days_until(@normal_date_time - 1.day)
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
test 'Sunday <- Saturday should be 0 working days' do
|
|
76
|
-
assert_equal 0, (@normal_date - 1.day).working_days_until(@normal_date - 2.days)
|
|
77
|
-
assert_equal 0, (@normal_time - 1.day).working_days_until(@normal_time - 2.days)
|
|
78
|
-
assert_equal 0, (@normal_date_time - 1.day).working_days_until(@normal_date_time - 2.days)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
test 'Monday -> next Monday should be 5 working days' do
|
|
82
|
-
assert_equal 5, @normal_date.working_days_until(@normal_date + 7.days)
|
|
83
|
-
assert_equal 5, @normal_time.working_days_until(@normal_time + 7.days)
|
|
84
|
-
assert_equal 5, @normal_date_time.working_days_until(@normal_date_time + 7.days)
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
test 'Monday -> Easter Monday should be 3 working days' do
|
|
88
|
-
assert_equal 3, (@easter_date - 7.days).working_days_until(@easter_date)
|
|
89
|
-
assert_equal 3, (@easter_time - 7.days).working_days_until(@easter_time)
|
|
90
|
-
assert_equal 3, (@easter_date_time - 7.days).working_days_until(@easter_date_time)
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
test 'Tuesday -> Tuesday over Easter should be 3 working days' do
|
|
94
|
-
assert_equal 3, (@easter_date - 6.days).working_days_until(@easter_date + 1.day)
|
|
95
|
-
assert_equal 3, (@easter_time - 6.days).working_days_until(@easter_time + 1.day)
|
|
96
|
-
assert_equal 3, (@easter_date_time - 6.days).working_days_until(@easter_date_time + 1.day)
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
test 'Good Friday -> Friday should be 4 working days' do
|
|
100
|
-
assert_equal 4, (@easter_date - 3.days).working_days_until(@easter_date + 4.day)
|
|
101
|
-
assert_equal 4, (@easter_time - 3.days).working_days_until(@easter_time + 4.day)
|
|
102
|
-
assert_equal 4, (@easter_date_time - 3.days).working_days_until(@easter_date_time + 4.day)
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
test 'Friday -> Monday should be 1 working day' do
|
|
106
|
-
assert_equal 1, (@normal_date - 3.days).working_days_until(@normal_date)
|
|
107
|
-
assert_equal 1, (@normal_time - 3.days).working_days_until(@normal_time)
|
|
108
|
-
assert_equal 1, (@normal_date_time - 3.days).working_days_until(@normal_date_time)
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
test 'A year of week days' do
|
|
112
|
-
assert_equal 261, @normal_date.weekdays_until(@normal_date + 1.year)
|
|
113
|
-
assert_equal 261, @normal_time.weekdays_until(@normal_time + 1.year)
|
|
114
|
-
assert_equal 261, @normal_date_time.weekdays_until(@normal_date_time + 1.year)
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
test 'A year of working days' do
|
|
118
|
-
assert_equal 253, @normal_date.working_days_until(@normal_date + 1.year)
|
|
119
|
-
assert_equal 253, @normal_time.working_days_until(@normal_time + 1.year)
|
|
120
|
-
assert_equal 253, @normal_date_time.working_days_until(@normal_date_time + 1.year)
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
test 'against GOV.UK holidays' do
|
|
124
|
-
require 'net/http'
|
|
125
|
-
require 'json'
|
|
126
|
-
|
|
127
|
-
url = 'https://www.gov.uk/bank-holidays/england-and-wales.json'
|
|
128
|
-
response = Net::HTTP.get(URI(url))
|
|
129
|
-
|
|
130
|
-
events = JSON.parse(response)['events']
|
|
131
|
-
missing_holidays = events.collect do |event|
|
|
132
|
-
event_date = event['date']
|
|
133
|
-
parsed_date = Date.parse(event_date)
|
|
134
|
-
if parsed_date.public_holiday?
|
|
135
|
-
nil
|
|
136
|
-
else
|
|
137
|
-
[event_date, format(" '%<date>s', # %<day>-9s - %<title>s",
|
|
138
|
-
date: event_date, day: parsed_date.strftime('%A'),
|
|
139
|
-
title: event['title'])]
|
|
140
|
-
end
|
|
141
|
-
end.compact
|
|
142
|
-
if missing_holidays.any?
|
|
143
|
-
flunk (["#{missing_holidays.collect(&:first).join(', ')} should be public holidays in " \
|
|
144
|
-
'lib/ndr_support/concerns/working_days.rb:'] +
|
|
145
|
-
missing_holidays.collect(&:last)).join("\n")
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
end
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
# This tests our various date and time class extensions
|
|
4
|
-
# Note that these will probably all need to be changed in a major bump of ndr_support
|
|
5
|
-
# once we've moved everything to Rails 7.
|
|
6
|
-
class DateAndTimeExtensionsTest < Minitest::Test
|
|
7
|
-
def test_date_to_s
|
|
8
|
-
d = Date.new(2000, 2, 1)
|
|
9
|
-
if d.respond_to?(:to_default_s) # Only defined on Rails <= 7.1
|
|
10
|
-
assert_equal '2000-02-01', d.to_default_s, 'Rails 7 default to_s'
|
|
11
|
-
else
|
|
12
|
-
assert_equal '2000-02-01', d.orig_to_s, 'Rails 7 default to_s (via our shim)'
|
|
13
|
-
end
|
|
14
|
-
assert_equal '01.02.2000', d.to_formatted_s(:default), 'Rails 6 default to_s'
|
|
15
|
-
assert_equal d.to_formatted_s(:default), d.to_s,
|
|
16
|
-
'We plan to change default to_s behaviour in a major version bump'
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def test_datetime_to_s
|
|
20
|
-
bst_datetime = DateTime.new(2014, 4, 1, 0, 0, 0, '+1')
|
|
21
|
-
if bst_datetime.respond_to?(:to_default_s) # Only defined on Rails <= 7.1
|
|
22
|
-
assert_equal '2014-04-01T00:00:00+01:00', bst_datetime.to_default_s, 'Rails 7 default to_s'
|
|
23
|
-
else
|
|
24
|
-
assert_equal '2014-04-01T00:00:00+01:00', bst_datetime.orig_to_s, 'Rails 7 default to_s (via our shim)'
|
|
25
|
-
end
|
|
26
|
-
assert_equal '01.04.2014', bst_datetime.to_formatted_s(:default), 'Rails 6 default to_s'
|
|
27
|
-
assert_equal bst_datetime.to_formatted_s(:default), bst_datetime.to_s,
|
|
28
|
-
'We plan to change default to_s behaviour in a major version bump'
|
|
29
|
-
|
|
30
|
-
gmt_datetime = DateTime.new(2014, 3, 1, 0, 0, 0, '+0')
|
|
31
|
-
if gmt_datetime.respond_to?(:to_default_s) # Only defined on Rails <= 7.1
|
|
32
|
-
assert_equal '2014-03-01T00:00:00+00:00', gmt_datetime.to_default_s, 'Rails 7 default to_s'
|
|
33
|
-
else
|
|
34
|
-
assert_equal '2014-03-01T00:00:00+00:00', gmt_datetime.orig_to_s, 'Rails 7 default to_s (via our shim)'
|
|
35
|
-
end
|
|
36
|
-
assert_equal '01.03.2014', gmt_datetime.to_formatted_s(:default), 'Rails 6 default to_s'
|
|
37
|
-
assert_equal gmt_datetime.to_formatted_s(:default), gmt_datetime.to_s,
|
|
38
|
-
'We plan to change default to_s behaviour in a major version bump'
|
|
39
|
-
|
|
40
|
-
datetime_with_hhmmss = DateTime.new(2014, 4, 1, 12, 35, 11, '+0')
|
|
41
|
-
if datetime_with_hhmmss.respond_to?(:to_default_s) # Only defined on Rails <= 7.1
|
|
42
|
-
assert_equal '2014-04-01T12:35:11+00:00', datetime_with_hhmmss.to_default_s,
|
|
43
|
-
'Rails 7 default to_s'
|
|
44
|
-
else
|
|
45
|
-
assert_equal '2014-04-01T12:35:11+00:00', datetime_with_hhmmss.orig_to_s,
|
|
46
|
-
'Rails 7 default to_s (via our shim)'
|
|
47
|
-
end
|
|
48
|
-
assert_equal '01.04.2014 12:35', datetime_with_hhmmss.to_formatted_s(:default),
|
|
49
|
-
'Rails 6 default to_s'
|
|
50
|
-
assert_equal datetime_with_hhmmss.to_formatted_s(:default), datetime_with_hhmmss.to_s,
|
|
51
|
-
'We plan to change default to_s behaviour in a major version bump'
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def test_time_to_s
|
|
55
|
-
time = Time.new(2014, 4, 1, 12, 35, 11.5, '+01:00')
|
|
56
|
-
if time.respond_to?(:to_default_s) # Only defined on Rails <= 7.1
|
|
57
|
-
assert_equal '2014-04-01 12:35:11 +0100', time.to_default_s, 'Rails 7 default to_s'
|
|
58
|
-
else
|
|
59
|
-
assert_equal '2014-04-01 12:35:11 +0100', time.orig_to_s, 'Rails 7 default to_s (via our shim)'
|
|
60
|
-
end
|
|
61
|
-
assert_equal '01.04.2014 12:35', time.to_formatted_s(:default), 'Rails 6 default to_s'
|
|
62
|
-
assert_equal time.to_formatted_s(:default), time.to_s,
|
|
63
|
-
'We plan to change default to_s behaviour in a major version bump'
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def test_time_with_zone_to_s
|
|
67
|
-
time_with_zone = Time.find_zone('Europe/London').local(2014, 4, 1, 12, 35, 11.5)
|
|
68
|
-
assert_equal 'BST', time_with_zone.zone
|
|
69
|
-
# Without ndr_support extensions, we'd expect "2014-04-01 12:35:11 +0100"
|
|
70
|
-
# but we have to trick the database into assuming all times are UTC to retain local time
|
|
71
|
-
if time_with_zone.respond_to?(:to_default_s) # Only defined on Rails <= 7.1
|
|
72
|
-
assert_equal '2014-04-01 12:35:11 UTC', time_with_zone.to_default_s,
|
|
73
|
-
'Rails 7 default to_s with our date and time formatting'
|
|
74
|
-
else
|
|
75
|
-
assert_equal '2014-04-01 12:35:11 +0100', time_with_zone.orig_to_s,
|
|
76
|
-
'Rails 7 default to_s with our date and time formatting (via our shim)'
|
|
77
|
-
end
|
|
78
|
-
assert_equal '01.04.2014 12:35', time_with_zone.to_formatted_s(:default), 'Rails 6 default to_s'
|
|
79
|
-
assert_equal time_with_zone.to_formatted_s(:default), time_with_zone.to_s,
|
|
80
|
-
'We plan to change default to_s behaviour in a major version bump'
|
|
81
|
-
end
|
|
82
|
-
end
|
data/test/daterange_test.rb
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
# This tests our Daterange class
|
|
4
|
-
class DaterangeTest < Minitest::Test
|
|
5
|
-
def test_basic_creation
|
|
6
|
-
dr = Daterange.new
|
|
7
|
-
assert_equal '', dr.to_s
|
|
8
|
-
assert_nil dr.date1
|
|
9
|
-
assert_nil dr.date2
|
|
10
|
-
assert dr.empty?
|
|
11
|
-
assert dr.blank? # delegates to dr.empty?
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def test_creation_from_dates
|
|
15
|
-
d = Date.today
|
|
16
|
-
dr = Daterange.new d, d
|
|
17
|
-
assert_nil dr.source
|
|
18
|
-
refute_nil dr.date1
|
|
19
|
-
refute_nil dr.date2
|
|
20
|
-
refute dr.empty?
|
|
21
|
-
assert_equal d.to_s, dr.to_s # because just one day
|
|
22
|
-
# assert_match(/\d\d\.\d\d\.\d\d\d\d/, dr.to_s) # default format
|
|
23
|
-
# dr = Daterange.new d, d + 1
|
|
24
|
-
# assert_match(/\d\d\.\d\d\.\d\d\d\d to \d\d.\d\d.\d\d\d\d/, dr.to_s)
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def test_date_time_dates_in_reverse_order_with_do_not_sort_dates_false
|
|
28
|
-
d = Time.zone.today
|
|
29
|
-
dr = Daterange.new(d, d + 1)
|
|
30
|
-
dr2 = Daterange.new(d + 1, d)
|
|
31
|
-
assert_equal(dr.to_s, dr2.to_s)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def test_blank_does_not_trigger_error_and_returns_nil_in_dates
|
|
35
|
-
dr = Daterange.new('', do_not_sort_dates: true)
|
|
36
|
-
assert_nil dr.date1
|
|
37
|
-
assert_nil dr.date2
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def test_date_time_dates_in_reverse_order_with_do_not_sort_dates_true
|
|
41
|
-
d = Time.zone.today
|
|
42
|
-
Daterange.new(d, d + 1, do_not_sort_dates: true)
|
|
43
|
-
assert_raises(Daterange::WrongDateOrderError) do
|
|
44
|
-
Daterange.new(d + 1, d, do_not_sort_dates: true)
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def test_string_dates_in_reverse_order_with_do_not_sort_dates_false
|
|
49
|
-
dr = Daterange.new('01/01/2019 to 01/01/2020')
|
|
50
|
-
dr2 = Daterange.new('01/01/2020 to 01/01/2019')
|
|
51
|
-
assert_equal(dr.to_s, dr2.to_s)
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def test_string_dates_in_reverse_order_with_do_not_sort_dates_true
|
|
55
|
-
Daterange.new('01/01/2019 to 01/01/2020', do_not_sort_dates: true)
|
|
56
|
-
assert_raises(Daterange::WrongDateOrderError) do
|
|
57
|
-
Daterange.new('01/01/2020 to 01/01/2019', do_not_sort_dates: true)
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def test_illegal_strings
|
|
62
|
-
dr = Daterange.new('01/o1/2000')
|
|
63
|
-
assert_equal '', dr.to_s
|
|
64
|
-
assert_nil dr.date1
|
|
65
|
-
assert_nil dr.date2
|
|
66
|
-
refute dr.empty? # Illegal dates do not count as empty / blank,
|
|
67
|
-
refute_nil dr.source # but the illegal string is preserved
|
|
68
|
-
|
|
69
|
-
dr = Daterange.new('01_01_2000')
|
|
70
|
-
assert_equal '', dr.to_s
|
|
71
|
-
assert_nil dr.date1
|
|
72
|
-
assert_nil dr.date2
|
|
73
|
-
refute dr.empty? # Illegal dates do not count as empty / blank,
|
|
74
|
-
refute_nil dr.source # but the illegal string is preserved
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
def test_out_of_range
|
|
78
|
-
dr = Daterange.new('31/12/1879')
|
|
79
|
-
assert_equal '', dr.to_s
|
|
80
|
-
assert_nil dr.date1
|
|
81
|
-
assert_nil dr.date2
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def test_year_attributes
|
|
85
|
-
dr = Daterange.new('2000')
|
|
86
|
-
assert_equal '2000', dr.source
|
|
87
|
-
assert_equal '2000', dr.to_s
|
|
88
|
-
assert_equal '01.01.2000', dr.date1.to_s
|
|
89
|
-
assert_equal '31.12.2000', dr.date2.to_s
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
def test_year_range_attributes
|
|
93
|
-
dr = Daterange.new('1880 2020')
|
|
94
|
-
assert_equal '01.01.1880 to 31.12.2020', dr.to_s
|
|
95
|
-
assert_equal '01.01.1880', dr.date1.to_s
|
|
96
|
-
assert_equal '31.12.2020', dr.date2.to_s
|
|
97
|
-
assert_equal '1880 2020', dr.source
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
def test_year_range_future
|
|
101
|
-
s = 2.years.from_now.strftime('%Y')
|
|
102
|
-
dr = Daterange.new(s)
|
|
103
|
-
assert_equal s, dr.to_s, "Daterange should support future years up to #{s}"
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
def test_hyphen_month_input_style
|
|
107
|
-
dr = Daterange.new('2000-05')
|
|
108
|
-
assert_equal '05.2000', dr.to_s
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
def test_dot_month_input_style
|
|
112
|
-
dr = Daterange.new('06.2000')
|
|
113
|
-
assert_equal '01.06.2000', dr.date1.to_s
|
|
114
|
-
assert_equal '30.06.2000', dr.date2.to_s
|
|
115
|
-
assert_equal '06.2000', dr.to_s
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
def test_forwardslash_month_input_style
|
|
119
|
-
dr = Daterange.new('07/2000')
|
|
120
|
-
assert_equal '07.2000', dr.to_s
|
|
121
|
-
assert_equal '01.07.2000', dr.date1.to_s
|
|
122
|
-
assert_equal '31.07.2000', dr.date2.to_s
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
def test_noseparator_month_input_style
|
|
126
|
-
dr = Daterange.new('082000')
|
|
127
|
-
assert_equal '08.2000', dr.to_s
|
|
128
|
-
assert_equal '01.08.2000', dr.date1.to_s
|
|
129
|
-
assert_equal '31.08.2000', dr.date2.to_s
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
def test_date_to_date_input_style
|
|
133
|
-
dr = Daterange.new('01.05.2000 to 31.05.2000')
|
|
134
|
-
assert_equal '05.2000', dr.to_s
|
|
135
|
-
assert_equal '01.05.2000', dr.date1.to_s
|
|
136
|
-
assert_equal '31.05.2000', dr.date2.to_s
|
|
137
|
-
dr = Daterange.new('2000 TO 2001')
|
|
138
|
-
assert_equal '01.01.2000 to 31.12.2001', dr.to_s
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
def test_hyphen_date_input_style
|
|
142
|
-
dr = Daterange.new('2000-09-12')
|
|
143
|
-
assert_equal '12.09.2000', dr.to_s
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
def test_forwardslash_date_input_style
|
|
147
|
-
dr = Daterange.new('13/09/2000')
|
|
148
|
-
assert_equal '13.09.2000', dr.to_s
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
def test_dot_date_input_style
|
|
152
|
-
dr = Daterange.new('14.09.2000')
|
|
153
|
-
assert_equal '14.09.2000', dr.to_s
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
def test_noseparator_date_input_style
|
|
157
|
-
dr = Daterange.new('15092000')
|
|
158
|
-
assert_equal '15.09.2000', dr.to_s
|
|
159
|
-
end
|
|
160
|
-
|
|
161
|
-
def test_spen_daylight_saving
|
|
162
|
-
dr = Daterange.new('03.2010') # Span time zones (daylight saving)
|
|
163
|
-
assert_equal '03.2010', dr.to_s # Ideally '03.2010'
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
def test_date_with_daylight_saving
|
|
167
|
-
dr = Daterange.new(Date.new(2017, 9, 2)) # During daylight saving
|
|
168
|
-
assert_equal '02.09.2017', dr.to_s
|
|
169
|
-
default_timezone = ActiveRecord.default_timezone
|
|
170
|
-
return unless default_timezone == :local
|
|
171
|
-
|
|
172
|
-
assert_equal Date.new(2017, 9, 2).in_time_zone.utc_offset, dr.date1.utc_offset, 'Expect consistent offset'
|
|
173
|
-
assert_equal '2017-09-02'.thetime, dr.date1
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
def test_verbose
|
|
177
|
-
dr = Daterange.new('01.03.2010')
|
|
178
|
-
assert_equal '01 March 2010', dr.verbose
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
def test_verbose_range
|
|
182
|
-
dr = Daterange.new('03.2010 to 2014')
|
|
183
|
-
assert_equal 'The period 01 March 2010 to 31 December 2014 inclusive (1767 days)', dr.verbose
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
def test_year_intersection
|
|
187
|
-
dr1 = Daterange.new('2001')
|
|
188
|
-
dr2 = Daterange.new('2002')
|
|
189
|
-
refute dr1.intersects?(dr2)
|
|
190
|
-
refute dr2.intersects?(dr1)
|
|
191
|
-
|
|
192
|
-
dr1 = Daterange.new('2001')
|
|
193
|
-
dr2 = Daterange.new('2001')
|
|
194
|
-
assert dr1.intersects?(dr2)
|
|
195
|
-
assert dr2.intersects?(dr1)
|
|
196
|
-
end
|
|
197
|
-
|
|
198
|
-
def test_year_range_intersection
|
|
199
|
-
dr1 = Daterange.new('2001 to 2003')
|
|
200
|
-
dr2 = Daterange.new('2002 to 2004')
|
|
201
|
-
assert dr1.intersects?(dr2)
|
|
202
|
-
assert dr2.intersects?(dr1)
|
|
203
|
-
end
|
|
204
|
-
|
|
205
|
-
def test_subset_intersection
|
|
206
|
-
dr1 = Daterange.new('01.05.2000 to 31.05.2000')
|
|
207
|
-
dr2 = Daterange.new('02.05.2000 to 30.05.2000')
|
|
208
|
-
assert dr1.intersects?(dr2)
|
|
209
|
-
assert dr2.intersects?(dr1)
|
|
210
|
-
end
|
|
211
|
-
|
|
212
|
-
def test_smallest_inhabited_intersection
|
|
213
|
-
dr1 = Daterange.new('01.05.2000 to 31.05.2000')
|
|
214
|
-
dr2 = Daterange.new('01.04.2000 to 01.05.2000')
|
|
215
|
-
assert dr1.intersects?(dr2)
|
|
216
|
-
assert dr2.intersects?(dr1)
|
|
217
|
-
|
|
218
|
-
dr1 = Daterange.new('01.05.2000 to 31.05.2000')
|
|
219
|
-
dr2 = Daterange.new('31.05.2000 to 01.06.2000')
|
|
220
|
-
assert dr1.intersects?(dr2)
|
|
221
|
-
assert dr2.intersects?(dr1)
|
|
222
|
-
end
|
|
223
|
-
|
|
224
|
-
def test_disjointed_intersection
|
|
225
|
-
dr1 = Daterange.new('02.05.2000 to 31.05.2000')
|
|
226
|
-
dr2 = Daterange.new('01.04.2000 to 01.05.2000')
|
|
227
|
-
refute dr1.intersects?(dr2)
|
|
228
|
-
refute dr2.intersects?(dr1)
|
|
229
|
-
end
|
|
230
|
-
|
|
231
|
-
def test_real_empty_intersection
|
|
232
|
-
dr1 = Daterange.new
|
|
233
|
-
dr2 = Daterange.new('01.04.2000 to 01.05.2000')
|
|
234
|
-
refute dr1.intersects?(dr2)
|
|
235
|
-
refute dr2.intersects?(dr1)
|
|
236
|
-
|
|
237
|
-
dr1 = Daterange.new('01.04.2000 to 01.05.2000')
|
|
238
|
-
dr2 = Daterange.new
|
|
239
|
-
refute dr1.intersects?(dr2)
|
|
240
|
-
refute dr2.intersects?(dr1)
|
|
241
|
-
end
|
|
242
|
-
|
|
243
|
-
def test_empty_empty_intersection
|
|
244
|
-
dr1 = Daterange.new
|
|
245
|
-
dr2 = Daterange.new
|
|
246
|
-
refute dr1.intersects?(dr2)
|
|
247
|
-
refute dr2.intersects?(dr1)
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
def test_merge
|
|
251
|
-
dr = Daterange.merge('2001, 30.04.2005, 2003,,')
|
|
252
|
-
assert_equal '01.01.2001 to 30.04.2005', dr.to_s
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
def test_comparison
|
|
256
|
-
dr1 = Daterange.new('01.04.2000')
|
|
257
|
-
dr2 = Daterange.new('01.04.2000 to 01.04.2000')
|
|
258
|
-
assert_equal dr1, dr2
|
|
259
|
-
assert_equal dr1, dr1.to_s
|
|
260
|
-
refute_equal nil, dr1
|
|
261
|
-
refute_equal 0, dr1
|
|
262
|
-
refute_equal dr1, nil
|
|
263
|
-
refute_equal dr1, 0
|
|
264
|
-
end
|
|
265
|
-
|
|
266
|
-
def test_three_char_months
|
|
267
|
-
dr1 = Daterange.new('01-APR-2020')
|
|
268
|
-
assert_equal '01.04.2020', dr1.date1.to_s
|
|
269
|
-
assert_equal '01.04.2020', dr1.date2.to_s
|
|
270
|
-
|
|
271
|
-
dr2 = Daterange.new('APR-2020')
|
|
272
|
-
assert_equal '01.04.2020', dr2.date1.to_s
|
|
273
|
-
assert_equal '30.04.2020', dr2.date2.to_s
|
|
274
|
-
|
|
275
|
-
dr3 = Daterange.new('JAN-2020 TO apr-2020')
|
|
276
|
-
assert_equal '01.01.2020', dr3.date1.to_s
|
|
277
|
-
assert_equal '30.04.2020', dr3.date2.to_s
|
|
278
|
-
|
|
279
|
-
dr4 = Daterange.new('20-JAN-2020 TO 12-Apr-2020')
|
|
280
|
-
assert_equal '20.01.2020', dr4.date1.to_s
|
|
281
|
-
assert_equal '12.04.2020', dr4.date2.to_s
|
|
282
|
-
|
|
283
|
-
dr5 = Daterange.new('01-BOB-2020')
|
|
284
|
-
assert_nil dr5.date1
|
|
285
|
-
assert_nil dr5.date2
|
|
286
|
-
|
|
287
|
-
dr6 = Daterange.new('01/APR/2020')
|
|
288
|
-
assert_equal '01.04.2020', dr6.date1.to_s
|
|
289
|
-
assert_equal '01.04.2020', dr6.date2.to_s
|
|
290
|
-
|
|
291
|
-
dr7 = Daterange.new('APR/2020')
|
|
292
|
-
assert_equal '01.04.2020', dr7.date1.to_s
|
|
293
|
-
assert_equal '30.04.2020', dr7.date2.to_s
|
|
294
|
-
|
|
295
|
-
dr8 = Daterange.new('JAN/2020 TO apr.2020')
|
|
296
|
-
assert_equal '01.01.2020', dr8.date1.to_s
|
|
297
|
-
assert_equal '30.04.2020', dr8.date2.to_s
|
|
298
|
-
|
|
299
|
-
dr9 = Daterange.new('20.JAN.2020 TO 12/Apr/2020')
|
|
300
|
-
assert_equal '20.01.2020', dr9.date1.to_s
|
|
301
|
-
assert_equal '12.04.2020', dr9.date2.to_s
|
|
302
|
-
end
|
|
303
|
-
end
|
data/test/hash_test.rb
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
# This tests our Hash extension
|
|
4
|
-
class HashTest < Minitest::Test
|
|
5
|
-
test 'value_by_path' do
|
|
6
|
-
my_hash = { 'one' => '1', 'two' => { 'twopointone' => '2.1', 'twopointtwo' => '2.2' } }
|
|
7
|
-
assert_equal my_hash['one'], my_hash.value_by_path('one')
|
|
8
|
-
assert_equal my_hash['two']['twopointone'], my_hash.value_by_path('two', 'twopointone')
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
test 'intersection' do
|
|
12
|
-
my_hash = { :a => 1, :b => :two, 'c' => '3' }
|
|
13
|
-
assert_equal({ :a => 1, 'c' => '3' }, my_hash & [:a, :c])
|
|
14
|
-
assert_equal({ :b => :two }, my_hash & [:b])
|
|
15
|
-
assert_equal({}, my_hash & [:d, :e])
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
test 'rawtext_merge without prevent_overwrite' do
|
|
19
|
-
first_hash = { :a => 1, :b => :two, :rawtext => { 'x' => 'apples', 'y' => 'pears' } }
|
|
20
|
-
second_hash = { :b => 2, 'c' => '3', :rawtext => { 'y' => 'pears', 'z' => 'oranges' } }
|
|
21
|
-
assert_equal(
|
|
22
|
-
{
|
|
23
|
-
:a => 1, :b => 2, 'c' => '3',
|
|
24
|
-
:rawtext => { 'x' => 'apples', 'y' => 'pears', 'z' => 'oranges' }
|
|
25
|
-
},
|
|
26
|
-
first_hash.rawtext_merge(second_hash, false)
|
|
27
|
-
)
|
|
28
|
-
# Ensure original hashes are preserved
|
|
29
|
-
assert_equal(
|
|
30
|
-
{ :a => 1, :b => :two, :rawtext => { 'x' => 'apples', 'y' => 'pears' } }, first_hash
|
|
31
|
-
)
|
|
32
|
-
assert_equal(
|
|
33
|
-
{ :b => 2, 'c' => '3', :rawtext => { 'y' => 'pears', 'z' => 'oranges' } }, second_hash
|
|
34
|
-
)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
test 'rawtext_merge with prevent_overwrite on rawtext' do
|
|
38
|
-
first_hash = { :a => 1, :b => :two, :rawtext => { 'x' => 'apples', 'y' => 'pears' } }
|
|
39
|
-
second_hash = { 'c' => '3', :rawtext => { 'y' => 'pears', 'z' => 'oranges' } }
|
|
40
|
-
assert_raises RuntimeError do
|
|
41
|
-
first_hash.rawtext_merge(second_hash, true)
|
|
42
|
-
end
|
|
43
|
-
# Ensure original hashes are preserved
|
|
44
|
-
assert_equal(
|
|
45
|
-
{ :a => 1, :b => :two, :rawtext => { 'x' => 'apples', 'y' => 'pears' } }, first_hash
|
|
46
|
-
)
|
|
47
|
-
assert_equal({ 'c' => '3', :rawtext => { 'y' => 'pears', 'z' => 'oranges' } }, second_hash)
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
test 'rawtext_merge with prevent_overwrite on non-rawtext' do
|
|
51
|
-
first_hash = { :a => 1, :b => :two, :rawtext => { 'x' => 'apples' } }
|
|
52
|
-
second_hash = { :b => 2, 'c' => '3', :rawtext => { 'y' => 'pears', 'z' => 'oranges' } }
|
|
53
|
-
assert_raises RuntimeError do
|
|
54
|
-
first_hash.rawtext_merge(second_hash, true)
|
|
55
|
-
end
|
|
56
|
-
# Ensure original hashes are preserved
|
|
57
|
-
assert_equal({ :a => 1, :b => :two, :rawtext => { 'x' => 'apples' } }, first_hash)
|
|
58
|
-
assert_equal(
|
|
59
|
-
{ :b => 2, 'c' => '3', :rawtext => { 'y' => 'pears', 'z' => 'oranges' } }, second_hash
|
|
60
|
-
)
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
test 'rawtext_merge with one rawtext missing' do
|
|
64
|
-
first_hash = { :a => 1, :b => :two }
|
|
65
|
-
second_hash = { 'c' => '3', :rawtext => { 'x' => 'apples' } }
|
|
66
|
-
assert_equal({ :a => 1, :b => :two, 'c' => '3', :rawtext => { 'x' => 'apples' } },
|
|
67
|
-
first_hash.rawtext_merge(second_hash, false))
|
|
68
|
-
assert_equal({ :a => 1, :b => :two, 'c' => '3', :rawtext => { 'x' => 'apples' } },
|
|
69
|
-
second_hash.rawtext_merge(first_hash, false))
|
|
70
|
-
# Ensure original hashes are preserved
|
|
71
|
-
assert_equal({ :a => 1, :b => :two }, first_hash)
|
|
72
|
-
assert_equal({ 'c' => '3', :rawtext => { 'x' => 'apples' } }, second_hash)
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
test 'rawtext_merge with rawtext missing' do
|
|
76
|
-
first_hash = { :a => 1, :b => :two }
|
|
77
|
-
second_hash = { 'c' => '3' }
|
|
78
|
-
assert_equal({ :a => 1, :b => :two, 'c' => '3', :rawtext => {} },
|
|
79
|
-
first_hash.rawtext_merge(second_hash, false))
|
|
80
|
-
# Ensure original hashes are preserved
|
|
81
|
-
assert_equal({ :a => 1, :b => :two }, first_hash)
|
|
82
|
-
assert_equal({ 'c' => '3' }, second_hash)
|
|
83
|
-
end
|
|
84
|
-
end
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
# This tests our Integer calculations extension
|
|
4
|
-
class Integer::CalculationsTest < Minitest::Test
|
|
5
|
-
test 'Integer should be extended with #factorial' do
|
|
6
|
-
assert 1.respond_to?(:factorial)
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
test 'Integer#factorial should behave correctly' do
|
|
10
|
-
assert_equal 1, 0.factorial
|
|
11
|
-
assert_equal 24, 4.factorial
|
|
12
|
-
assert_raises(RuntimeError) { -1.factorial }
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
test 'Integer should be extended with #choose' do
|
|
16
|
-
assert 1.respond_to?(:choose)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
test 'Integer#choose should behave correctly' do
|
|
20
|
-
pascal_row = [1, 5, 10, 10, 5, 1]
|
|
21
|
-
pascal_row.each_with_index do |target, index|
|
|
22
|
-
assert_equal target, 5.choose(index)
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
assert_raises(ArgumentError) { 10.choose(11) }
|
|
26
|
-
assert_raises(ArgumentError) { 10.choose(-1) }
|
|
27
|
-
end
|
|
28
|
-
end
|