stephenrichards-holiday_calendar 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,201 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ # require File.dirname(__FILE__) + '/../lib/modified_weekday'
3
+
4
+
5
+
6
+ class ModifiedWeekdayTest < Test::Unit::TestCase
7
+
8
+
9
+ def test_that_two_modified_weekday_objects_created_with_the_same_expression_are_equal
10
+ mw1 = ModifiedWeekday.new(:last_thursday)
11
+ mw2 = ModifiedWeekday.new(:last_thursday)
12
+
13
+ assert_equal mw1, mw2
14
+ end
15
+
16
+
17
+ def test_that_two_modified_weekday_objects_created_with_different_expressions_are_not_equal
18
+ mw1 = ModifiedWeekday.new(:last_thursday)
19
+ mw2 = ModifiedWeekday.new(:first_thursday)
20
+
21
+ assert_not_equal mw1, mw2
22
+ end
23
+
24
+
25
+ def test_that_modified_object_created_with_expressions_and_dates_are_equal
26
+ # Given a ModifiedWeekday object created by an expression
27
+ mwe = ModifiedWeekday.new(:first_thursday)
28
+
29
+ # and a ModifiedWeekday object created by date for a first thursday
30
+ mwd = ModifiedWeekday.new(Date.new(2011,6,2))
31
+
32
+ # when I compare them, they should be equal
33
+ assert mwd == mwe
34
+ end
35
+
36
+
37
+ def test_that_a_modified_weekday_created_with_a_last_friday_expression_matches_one_created_with_a_date
38
+ # Given a ModifiedWeekday object created by an expression
39
+ mwe = ModifiedWeekday.new(:last_friday)
40
+
41
+ # and a ModifiedWeekday object created by date for a first thursday
42
+ mwd = ModifiedWeekday.new(Date.new(2011,6,24))
43
+
44
+ # when I compare them, they should be equal
45
+ assert mwd == mwe
46
+
47
+ end
48
+
49
+
50
+
51
+
52
+
53
+ def test_that_a_modified_weekday_object_with_last_modifier_can_be_properly_created
54
+ mw = ModifiedWeekday.new(:last_thursday)
55
+ assert_instance_of ModifiedWeekday, mw
56
+ assert_equal :last, mw.modifier
57
+ assert_equal :thursday, mw.weekday_name
58
+ assert_equal 4, mw.wday
59
+ assert_equal 0, mw.weekday_occurrance
60
+ assert_equal true, mw.is_last
61
+ end
62
+
63
+
64
+
65
+ def test_that_a_modified_weekday_object_with_first_modifier_can_be_properly_created
66
+ mw = ModifiedWeekday.new(:first_monday)
67
+ assert_equal :first, mw.modifier
68
+ assert_equal :monday, mw.weekday_name
69
+ assert_equal 1, mw.wday
70
+ assert_equal 1, mw.weekday_occurrance
71
+ assert_equal false, mw.is_last
72
+ end
73
+
74
+
75
+
76
+ def test_that_first_wednesday_is_generated_from_wednesday_first_june_2011
77
+ # given a date of 1/6/2011
78
+ date = Date.new(2011, 6, 1)
79
+
80
+ # when I create a modified date from it
81
+ mw = ModifiedWeekday.new(date)
82
+
83
+ # then it should be described as the first monday
84
+ assert_equal :first_wednesday, mw.expression
85
+ assert_equal :wednesday, mw.weekday_name
86
+ assert_equal :first, mw.modifier
87
+ assert_equal 1, mw.weekday_occurrance
88
+ assert_equal false, mw.is_last
89
+ end
90
+
91
+
92
+ def test_that_first_thursday_is_generated_from_thursday_second_june_2011
93
+ # given a date of 2/6/2011
94
+ date = Date.new(2011, 6, 2)
95
+
96
+ # when I create a modified date from it
97
+ mw = ModifiedWeekday.new(date)
98
+
99
+ # then it should be described as the first monday
100
+ assert_equal :first_thursday, mw.expression
101
+ assert_equal :thursday, mw.weekday_name
102
+ assert_equal :first, mw.modifier
103
+ assert_equal 1, mw.weekday_occurrance
104
+ assert_equal false, mw.is_last
105
+ end
106
+
107
+
108
+ def test_that_second_wednesday_is_generated_from_wednesday_eighth_june_2011
109
+ # given a date of 2/6/2011
110
+ date = Date.new(2011, 6, 8)
111
+
112
+ # when I create a modified date from it
113
+ mw = ModifiedWeekday.new(date)
114
+
115
+ # then it should be described as the first monday
116
+ assert_equal :second_wednesday, mw.expression
117
+ assert_equal :wednesday, mw.weekday_name
118
+ assert_equal :second, mw.modifier
119
+ assert_equal 2, mw.weekday_occurrance
120
+ assert_equal false, mw.is_last
121
+ end
122
+
123
+
124
+ def test_that_second_thursday_is_generated_from_thursday_ninth_june_2011
125
+ # given a date of 9/6/2011
126
+ date = Date.new(2011, 6, 9)
127
+
128
+ # when I create a modified date from it
129
+ mw = ModifiedWeekday.new(date)
130
+
131
+ # then it should be described as the first monday
132
+ assert_equal :second_thursday, mw.expression
133
+ assert_equal :thursday, mw.weekday_name
134
+ assert_equal :second, mw.modifier
135
+ assert_equal 2, mw.weekday_occurrance
136
+ assert_equal false, mw.is_last
137
+ end
138
+
139
+
140
+ def test_that_fourth_wednesday_is_generated_from_wednesday_22_june_2011
141
+ # given a date of 22/6/2011
142
+ date = Date.new(2011, 6, 22)
143
+
144
+ # when I create a modified date from it
145
+ mw = ModifiedWeekday.new(date)
146
+
147
+ # then it should be described as the first monday
148
+ assert_equal :fourth_wednesday, mw.expression
149
+ assert_equal :wednesday, mw.weekday_name
150
+ assert_equal :fourth, mw.modifier
151
+ assert_equal 4, mw.weekday_occurrance
152
+ assert_equal false, mw.is_last
153
+ end
154
+
155
+
156
+ def test_that_last_wednesday_is_generated_from_wednesday_29_june_2011
157
+ # given a date of 29/6/2011
158
+ date = Date.new(2011, 6, 29)
159
+
160
+ # when I create a modified weekday from it
161
+ mw = ModifiedWeekday.new(date)
162
+
163
+ # then it should be described as the fifth and last wednesday
164
+ assert_equal :last_wednesday, mw.expression
165
+ assert_equal :wednesday, mw.weekday_name
166
+ assert_equal :last, mw.modifier
167
+ assert_equal 5, mw.weekday_occurrance
168
+ assert_equal true, mw.is_last
169
+ end
170
+
171
+
172
+
173
+
174
+ def test_that_an_invalid_weekday_raises_an_exception
175
+ err = assert_raise ArgumentError do
176
+ mw = ModifiedWeekday.new(:second_wed)
177
+ end
178
+ assert_equal 'Invalid Weekday component passed to ModifiedWeekday.new: second_wed', err.message
179
+ end
180
+
181
+
182
+ def test_that_an_invalid_modifier_raises_an_exception
183
+ err = assert_raise ArgumentError do
184
+ mw = ModifiedWeekday.new(:fifth_tuesday)
185
+ end
186
+ assert_equal 'Invalid weekday modifier passed to ModifiedWeekday.new: fifth_tuesday', err.message
187
+ end
188
+
189
+
190
+ # def test_sort_values_are_generated_correctly
191
+ # assert_equal 11, ModifiedWeekday.new(:first_monday).sort_value
192
+ # assert_equal 12, ModifiedWeekday.new(:first_tuesday).sort_value
193
+ # assert_equal 23, ModifiedWeekday.new(:second_wednesday).sort_value
194
+ # assert_equal 44, ModifiedWeekday.new(:fourth_thursday).sort_value
195
+ # assert_equal 55, ModifiedWeekday.new(:last_friday).sort_value
196
+ # end
197
+
198
+
199
+
200
+
201
+ end
@@ -0,0 +1,254 @@
1
+
2
+ require File.dirname(__FILE__) + '/test_helper'
3
+ require File.dirname(__FILE__) + '/../lib/public_holiday_specification'
4
+ require File.dirname(__FILE__) + '/mocks/mock_public_holiday_specification'
5
+ require File.dirname(__FILE__) + '/../lib/religious_festival'
6
+
7
+
8
+ class PublicHolidaySpecificationTest < Test::Unit::TestCase
9
+
10
+ def test_class_method_generates_valid_public_holiday_specification
11
+ # given a specification that uses a class method
12
+ phs = PublicHolidaySpecification.new(:name => 'Good Friday', :years => :all, :class_method => 'ReligiousFestival.good_friday')
13
+
14
+ assert_instance_of PublicHolidaySpecification, phs
15
+ assert_true phs.uses_class_method?
16
+ assert_equal ReligiousFestival, phs.klass
17
+ assert_equal 'good_friday', phs.method_name
18
+
19
+ end
20
+
21
+
22
+
23
+ def test_instantiating_from_yaml_throws_error_if_yaml_spec_is_not_hash
24
+ # given a yaml_spac that is not a hash
25
+ yaml_spec = [1, 33, 4]
26
+
27
+ # when I attempt to instantiate a PHS from it, I should get an exception
28
+ err = assert_raise ArgumentError do
29
+ phs = PublicHolidaySpecification.instantiate_from_yaml_definition('/path/to/file.yaml', 'Good Friday', yaml_spec)
30
+ end
31
+
32
+ assert_equal "Invalid definition of Good Friday in public_holidays section of /path/to/file.yaml", err.message
33
+ end
34
+
35
+
36
+ def test_instantiating_from_yaml_spec_with_first_monday_works_as_expected
37
+ # given a first_monday yaml spec
38
+ yaml_spec = {'month' => 5, 'day' => 'first_monday', 'years' => 'all'}
39
+
40
+ # when I instantiate a PHS
41
+ phs = PublicHolidaySpecification.instantiate_from_yaml_definition('filename', 'May Day', yaml_spec)
42
+
43
+ # then the object attributes should be as expected
44
+ assert_equal 'May Day', phs.name
45
+ assert_equal 5, phs.month
46
+ assert_equal ModifiedWeekday.new(:first_monday), phs.day
47
+ assert_false phs.uses_class_method
48
+ assert_nil phs.klass
49
+ assert_nil phs.method_name
50
+ end
51
+
52
+
53
+ def test_instantiating_from_yaml_spec_with_class_method_works_as_expected
54
+ # given a first_monday yaml spec
55
+ yaml_spec = {"class_method"=>"ReligiousFestival.good_friday", "years"=>"all"}
56
+
57
+ # when I instantiate a PHS
58
+ phs = PublicHolidaySpecification.instantiate_from_yaml_definition('filename', 'Good Friday', yaml_spec)
59
+
60
+ # then the object attributes should be as expected
61
+ assert_equal 'Good Friday', phs.name
62
+ assert_nil phs.month
63
+ assert_nil phs.day
64
+ assert_true phs.uses_class_method
65
+ assert_equal ReligiousFestival, phs.klass
66
+ assert_equal 'good_friday', phs.method_name
67
+ end
68
+
69
+
70
+
71
+ ###
72
+ ###
73
+ ### tests against mock objects to confirm private attributes are being set up correctly
74
+ ###
75
+ ###
76
+
77
+ def test_mock_object_created_with_single_year_generates_a_range_of_1_year_in_the_years_attribute
78
+ # given an object created with a single year
79
+ phs = MockPublicHolidaySpecification.new(:name => 'test', :years => 2006, :month => 12, :day => 25)
80
+
81
+ # when I test the year, it should be a range 2006..2006
82
+ assert_instance_of Range, phs.years
83
+ assert_equal 2006..2006, phs.years
84
+ end
85
+
86
+
87
+
88
+ def test_mock_object_created_with_all_years_generates_a_range_of_all_years_in_the_years_attribute
89
+ # given an object created with a all years
90
+ phs = MockPublicHolidaySpecification.new(:name => 'test', :years => :all, :month => 12, :day => 25)
91
+
92
+ # when I test the year, it should be a range 2006..2006
93
+ assert_instance_of Range, phs.years
94
+ assert_equal 0..9999, phs.years
95
+ end
96
+
97
+
98
+ def test_mock_object_created_with_range_of_years_generates_the_correct_range_in_the_years_attribute
99
+ # given an object created with a range of years
100
+ phs = MockPublicHolidaySpecification.new(:name => 'test', :years => 1997..2006, :month => 12, :day => 25)
101
+
102
+ # when I test the year, it should be a range 2006..2006
103
+ assert_instance_of Range, phs.years
104
+ assert_equal 1997..2006, phs.years
105
+ end
106
+
107
+ def test_mock_object_created_with_english_month_names_generates_the_correct_month_number_in_the_month_attribute
108
+ # given an object created with a month of May
109
+ phs = MockPublicHolidaySpecification.new(:name => 'test', :years => :all, :month => 'May', :day => :first_monday)
110
+
111
+ # when I test the month, it should be 5
112
+ assert_equal 5, phs.month
113
+ end
114
+
115
+
116
+ def test_mock_object_created_with_fixed_day_number_has_a_fixed_day_number_in_the_day_attribute
117
+ # given an object created with a day number
118
+ phs = MockPublicHolidaySpecification.new(:name => 'test', :years => 1997..2006, :month => 12, :day => 25)
119
+
120
+ # when I test the day, it should be a fixnum 25
121
+ assert_instance_of Fixnum, phs.day
122
+ assert_equal 25, phs.day
123
+ end
124
+
125
+
126
+ def test_mock_object_created_with_first_monday_has_a_modified_weekday_object_as_the_day_attribute
127
+ # given an object created with a phrase like :first_monday
128
+ phs = MockPublicHolidaySpecification.new(:name => 'test', :years => :all, :month => 12, :day => :first_monday)
129
+
130
+ # when I test the day, it should be a fixnum 25
131
+ assert_instance_of ModifiedWeekday, phs.day
132
+ end
133
+
134
+
135
+
136
+
137
+ ###
138
+ ###
139
+ ### tests that the constructor validates incoming parameters properly
140
+ ###
141
+ ###
142
+
143
+ def test_exception_thrown_if_mandatory_paramaters_missing
144
+ err = assert_raise ArgumentError do
145
+ phs = PublicHolidaySpecification.new(:name => 'test')
146
+ end
147
+ assert_equal "Mandatory parameters are missing in a call to PublicHolidaySpecification.new: years, month, day", err.message
148
+ end
149
+
150
+
151
+
152
+ def test_exception_thrown_if_invalid_paramaeter_passed_to_constructor
153
+
154
+ err = assert_raise ArgumentError do
155
+ phs = PublicHolidaySpecification.new(:name => 'Test Holiday', :invalid_param => 'xxxx')
156
+ end
157
+
158
+ assert_equal 'Invalid parameter passed to PublicHolidaySpecification.new: invalid_param => xxxx', err.message
159
+ end
160
+
161
+
162
+ def test_exception_thrown_if_invalid_year_passed_to_constructor
163
+ err = assert_raise ArgumentError do
164
+ phs = PublicHolidaySpecification.new(:name => 'Xmas', :years => :this_year, :month => 12, :day => 25)
165
+ end
166
+ assert_equal 'Invalid value passed as years parameter. Must be a Range, Fixnum or :all', err.message
167
+ end
168
+
169
+
170
+ def test_exception_thrown_if_invalid_month_passed_to_constructor
171
+ err = assert_raise ArgumentError do
172
+ phs = PublicHolidaySpecification.new(:name => 'Xmas', :years => :all, :month => 'décembre', :day => 25)
173
+ end
174
+ assert_equal 'Invalid month passed to PublicHolidaySpecification.new: décembre', err.message
175
+ end
176
+
177
+
178
+ def test_exception_thrown_if_out_of_range_month_passed_to_constructor
179
+ err = assert_raise ArgumentError do
180
+ phs = PublicHolidaySpecification.new(:name => 'Xmas', :years => :all, :month => 33, :day => 25)
181
+ end
182
+ assert_equal 'Invalid month passed to PublicHolidaySpecification.new: 33', err.message
183
+ end
184
+
185
+
186
+ def test_exception_thrown_if_out_of_range_day_passed_to_constructor
187
+ err = assert_raise ArgumentError do
188
+ phs = PublicHolidaySpecification.new(:name => 'Xmas', :years => :all, :month => 12, :day => 125)
189
+ end
190
+ assert_equal 'Invalid value passed as :day parameter to PublicHolidaySpecification.new: 125', err.message
191
+ end
192
+
193
+
194
+
195
+
196
+ def test_exception_thrown_if_obsolete_carry_forward_used
197
+ err = assert_raise ArgumentError do
198
+ phs = PublicHolidaySpecification.new(:name => 'test', :years => :all, :month => 8, :day => 13, :carry_forward => true)
199
+ end
200
+ assert_equal 'Invalid parameter passed to PublicHolidaySpecification.new: carry_forward => true', err.message
201
+ end
202
+
203
+
204
+ def test_default_take_before_is_empty_arry
205
+ # given a public holiday specification without take before specified
206
+ phs = PublicHolidaySpecification.new(:name => 'test', :years => :all, :month => 8, :day => 13)
207
+
208
+ # when I see the take_before attribute, it should be an empty array
209
+ assert_equal Array.new, phs.take_before
210
+ end
211
+
212
+ def test_default_take_after_is_empty_arry
213
+ # given a public holiday specification without take before specified
214
+ phs = PublicHolidaySpecification.new(:name => 'test', :years => :all, :month => 8, :day => 13)
215
+
216
+ # when I see the take_after attribute, it should be an empty array
217
+ assert_equal Array.new, phs.take_after
218
+ end
219
+
220
+ def test_take_before_parameters_are_turned_into_valid_day_numbers
221
+ # given a public holiday specification with a take_before parameter of saturday, sunday
222
+ phs = PublicHolidaySpecification.new(:name => 'test', :years => :all, :month => 8, :day => 13,
223
+ :take_before => ['Saturday', 'Sunday'])
224
+
225
+ # when I see the take_after attribute, it should be an array of 6,0
226
+ assert_equal [6,0], phs.take_before
227
+ end
228
+
229
+
230
+ def test_take_before_parameters_are_turned_into_valid_day_numbers
231
+ # given a public holiday specification with a take_before parameter of saturday, sunday
232
+ phs = PublicHolidaySpecification.new(:name => 'test', :years => :all, :month => 8, :day => 13,
233
+ :take_after => ['Saturday', 'Sunday'])
234
+
235
+ # when I see the take_after attribute, it should be an array of 6,0
236
+ assert_equal [6,0], phs.take_after
237
+ end
238
+
239
+
240
+ def test_take_before_raises_exception_if_given_non_array
241
+ err = assert_raise ArgumentError do
242
+ phs = PublicHolidaySpecification.new(:name => 'test', :years => :all, :month => 8, :day => 13, :take_before => 'Saturday')
243
+ end
244
+ assert_equal 'take_before or take_after parameters must be an array', err.message
245
+ end
246
+
247
+ def test_take_before_raises_exception_if_given_day_numbers_out_of_range
248
+ err = assert_raise ArgumentError do
249
+ phs = PublicHolidaySpecification.new(:name => 'test', :years => :all, :month => 8, :day => 13, :take_before => [0,7])
250
+ end
251
+ assert_equal 'day number passed as take_before and take_after parameters must be in range 0-6', err.message
252
+ end
253
+
254
+ end