bahai_date 1.0.0

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.
@@ -0,0 +1,251 @@
1
+ require 'bahai_date/bahai_date'
2
+
3
+ module BahaiDate
4
+
5
+ describe BahaiDate do
6
+
7
+ it "can be created from a year, month and day" do
8
+ bahai_date = BahaiDate.new(year: 1, month: 1, day: 1)
9
+
10
+ expect(bahai_date).to_not be_nil
11
+ end
12
+
13
+ it "can be created from a gregorian Date object" do
14
+ bahai_date = BahaiDate.new(date: Date.new(2010, 1, 1))
15
+
16
+ expect(bahai_date).to_not be_nil
17
+ end
18
+
19
+ it "raises an error if invalid arguments are passed to the initializer" do
20
+ expect { BahaiDate.new }.to raise_error(ArgumentError)
21
+ expect { BahaiDate.new({}) }.to raise_error(ArgumentError)
22
+ expect { BahaiDate.new(day: 1) }.to raise_error(ArgumentError)
23
+ end
24
+
25
+ context "when validating the Baha'i Era date" do
26
+
27
+ context "year" do
28
+ it "raises an error if less than 1" do
29
+ expect { BahaiDate.new(year: 0, month: 1, day: 1)
30
+ }.to raise_error(ArgumentError)
31
+ end
32
+ end
33
+
34
+ context "month" do
35
+ it "raises an error if 0" do
36
+ expect { BahaiDate.new(year: 1, month: 0, day: 1)
37
+ }.to raise_error(ArgumentError)
38
+ end
39
+ it "raises an error if less than -1" do
40
+ expect { BahaiDate.new(year: 1, month: -2, day: 1)
41
+ }.to raise_error(ArgumentError)
42
+ end
43
+ it "raises an error if greater than 19" do
44
+ expect { BahaiDate.new(year: 1, month: 20, day: 1)
45
+ }.to raise_error(ArgumentError)
46
+ end
47
+ end
48
+
49
+ context "day" do
50
+ it "raises an error if less than 1" do
51
+ expect { BahaiDate.new(year: 1, month: 1, day: 0)
52
+ }.to raise_error(ArgumentError)
53
+ end
54
+ it "raises an error if greater than 19" do
55
+ expect { BahaiDate.new(year: 1, month: 1, day: 20)
56
+ }.to raise_error(ArgumentError)
57
+ end
58
+ context "in Ayyam-i-Ha" do
59
+ it "raises an error if greater than 4 on a non-leap year" do
60
+ expect { BahaiDate.new(year: 1, month: -1, day: 5)
61
+ }.to raise_error(ArgumentError)
62
+ end
63
+ it "doesn't raise an error if 5 on a leap year" do
64
+ expect { BahaiDate.new(year: 4, month: -1, day: 5)
65
+ }.not_to raise_error
66
+ end
67
+ it "raises an error if greater than 5 on a leap year" do
68
+ expect { BahaiDate.new(year: 4, month: -1, day: 6)
69
+ }.to raise_error(ArgumentError)
70
+ end
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ it "exposes weekday, day, month, year and gregorian_date" do
77
+ bahai_date = BahaiDate.new(year: 1, month: 1, day: 1)
78
+
79
+ expect(bahai_date.weekday).to be_an_instance_of(Weekday)
80
+ expect(bahai_date.day).to be_an_instance_of(Day)
81
+ expect(bahai_date.month).to be_an_instance_of(Month)
82
+ expect(bahai_date.year).to be_an_instance_of(Year)
83
+ expect(bahai_date.gregorian_date).to be_an_instance_of(Date)
84
+ end
85
+
86
+ context "when converting to a gregorian date" do
87
+
88
+ it "handles the first day of the calendar" do
89
+ bahai_date = BahaiDate.new(year: 1, month: 1, day: 1)
90
+ expect(bahai_date.gregorian_date).to eq(Date.new(1844,3,21))
91
+ end
92
+
93
+ it "handles the last day of the year" do
94
+ bahai_date = BahaiDate.new(year: 1, month: 19, day: 19)
95
+ expect(bahai_date.gregorian_date).to eq(Date.new(1845,3,20))
96
+ end
97
+
98
+ it "handles the year component" do
99
+ bahai_date = BahaiDate.new(year: 2, month: 1, day: 1)
100
+ expect(bahai_date.gregorian_date).to eq(Date.new(1845,3,21))
101
+ end
102
+
103
+ context "in a leap year" do
104
+ it "handles the day before Ayyam-i-Ha" do
105
+ bahai_date = BahaiDate.new(year: 168, month: 18, day: 19)
106
+ expect(bahai_date.gregorian_date).to eq(Date.new(2012,2,25))
107
+ end
108
+
109
+ it "handles the first day of Ayyam-i-Ha" do
110
+ bahai_date = BahaiDate.new(year: 168, month: -1, day: 1)
111
+ expect(bahai_date.gregorian_date).to eq(Date.new(2012,2,26))
112
+ end
113
+
114
+ it "handles the last day of Ayyam-i-Ha" do
115
+ bahai_date = BahaiDate.new(year: 168, month: -1, day: 5)
116
+ expect(bahai_date.gregorian_date).to eq(Date.new(2012,3,1))
117
+ end
118
+
119
+ it "handles the first day of the month after Ayyam-i-Ha" do
120
+ bahai_date = BahaiDate.new(year: 168, month: 19, day: 1)
121
+ expect(bahai_date.gregorian_date).to eq(Date.new(2012,3,2))
122
+ end
123
+ end
124
+
125
+ context "in a non-leap year" do
126
+ it "handles the last day of Ayyam-i-Ha" do
127
+ bahai_date = BahaiDate.new(year: 169, month: -1, day: 4)
128
+ expect(bahai_date.gregorian_date).to eq(Date.new(2013,3,1))
129
+ end
130
+ end
131
+
132
+ end
133
+
134
+
135
+ context "when converting from gregorian date" do
136
+
137
+ it "handles the first day of the calendar" do
138
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,21))
139
+ expect(bahai_date.year.bahai_era).to be 1
140
+ expect(bahai_date.month.number).to be 1
141
+ expect(bahai_date.day.number).to be 1
142
+ end
143
+
144
+ it "handles the last day of the year" do
145
+ bahai_date = BahaiDate.new(date: Date.new(1845,3,20))
146
+ expect(bahai_date.year.bahai_era).to be 1
147
+ expect(bahai_date.month.number).to be 19
148
+ expect(bahai_date.day.number).to be 19
149
+ end
150
+
151
+ it "handles the year component" do
152
+ bahai_date = BahaiDate.new(date: Date.new(1845,3,21))
153
+ expect(bahai_date.year.bahai_era).to be 2
154
+ expect(bahai_date.month.number).to be 1
155
+ expect(bahai_date.day.number).to be 1
156
+ end
157
+
158
+ context "in a leap year" do
159
+ it "handles the day before Ayyam-i-Ha" do
160
+ bahai_date = BahaiDate.new(date: Date.new(2012,2,25))
161
+ expect(bahai_date.year.bahai_era).to be 168
162
+ expect(bahai_date.month.number).to be 18
163
+ expect(bahai_date.day.number).to be 19
164
+ end
165
+
166
+ it "handles the first day of Ayyam-i-Ha" do
167
+ bahai_date = BahaiDate.new(date: Date.new(2012,2,26))
168
+ expect(bahai_date.year.bahai_era).to be 168
169
+ expect(bahai_date.month.number).to be -1
170
+ expect(bahai_date.day.number).to be 1
171
+ end
172
+
173
+ it "handles the last day of Ayyam-i-Ha" do
174
+ bahai_date = BahaiDate.new(date: Date.new(2012,3,1))
175
+ expect(bahai_date.year.bahai_era).to be 168
176
+ expect(bahai_date.month.number).to be -1
177
+ expect(bahai_date.day.number).to be 5
178
+ end
179
+
180
+ it "handles the first day of the month after Ayyam-i-Ha" do
181
+ bahai_date = BahaiDate.new(date: Date.new(2012,3,2))
182
+ expect(bahai_date.year.bahai_era).to be 168
183
+ expect(bahai_date.month.number).to be 19
184
+ expect(bahai_date.day.number).to be 1
185
+ end
186
+ end
187
+
188
+ context "in a non-leap year" do
189
+ it "handles the last day of Ayyam-i-Ha" do
190
+ bahai_date = BahaiDate.new(date: Date.new(2013,3,1))
191
+ expect(bahai_date.year.bahai_era).to be 169
192
+ expect(bahai_date.month.number).to be -1
193
+ expect(bahai_date.day.number).to be 4
194
+ end
195
+ end
196
+
197
+ end
198
+
199
+ it "can get weekday from a gregorian date accurately" do
200
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,21))
201
+ expect(bahai_date.weekday.number).to be 6
202
+
203
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,22))
204
+ expect(bahai_date.weekday.number).to be 7
205
+
206
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,23))
207
+ expect(bahai_date.weekday.number).to be 1
208
+
209
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,24))
210
+ expect(bahai_date.weekday.number).to be 2
211
+
212
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,25))
213
+ expect(bahai_date.weekday.number).to be 3
214
+
215
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,26))
216
+ expect(bahai_date.weekday.number).to be 4
217
+
218
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,27))
219
+ expect(bahai_date.weekday.number).to be 5
220
+
221
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,28))
222
+ expect(bahai_date.weekday.number).to be 6
223
+ end
224
+
225
+ it "can be converted to string" do
226
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,21))
227
+ expect(bahai_date.to_s).to eq "1.1.1"
228
+ end
229
+
230
+ it "can provide the date in long format" do
231
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,21))
232
+ expect(bahai_date.long_format).to eq "Istijlal 1 Baha 1 B.E."
233
+ end
234
+
235
+ it "can provide an array of occasions for a given day" do
236
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,21))
237
+ occasions_array = bahai_date.occasions
238
+ expect(occasions_array.size).to be 2
239
+ expect(occasions_array.first.short_title).to eq "Naw-Ruz"
240
+ expect(occasions_array.last.short_title).to eq "Feast of Baha"
241
+ end
242
+
243
+ it "can advance to the next day" do
244
+ bahai_date = BahaiDate.new(date: Date.new(1844,3,21))
245
+ bahai_date.next_day!
246
+ expect(bahai_date.gregorian_date).to eq Date.new(1844,3,22)
247
+ end
248
+
249
+ end
250
+
251
+ end
@@ -0,0 +1,68 @@
1
+ require 'bahai_date/day'
2
+ require 'bahai_date/weekday'
3
+ require 'bahai_date/occasion_factory'
4
+
5
+ module BahaiDate
6
+
7
+ describe Day do
8
+
9
+ it "can be created given a number from 1 to 19" do
10
+ expect(Day.new(1)).to_not be_nil
11
+ expect(Day.new(19)).to_not be_nil
12
+ end
13
+
14
+ it "can't be created with a number other than 1 to 19" do
15
+ expect { Day.new(0) }.to raise_error(
16
+ ArgumentError, "'0' is not a valid day. Please use 1 to 19.")
17
+
18
+ expect { Day.new(20) }.to raise_error(
19
+ ArgumentError, "'20' is not a valid day. Please use 1 to 19.")
20
+
21
+ expect { Day.new('hello') }.to raise_error(
22
+ ArgumentError, "'0' is not a valid day. Please use 1 to 19.")
23
+ end
24
+
25
+ subject(:day) { Day.new(1) }
26
+
27
+ it "can be converted to string" do
28
+ expect(day.to_s).to eq "Baha"
29
+ end
30
+
31
+ it "provides access to the day number" do
32
+ expect(day.number).to be 1
33
+ end
34
+
35
+ it "provides access to the translated title" do
36
+ expect(day.translation).to eq "Splendour"
37
+ end
38
+
39
+ it "provides access to the title in HTML" do
40
+ expect(day.html).to eq "Bahá"
41
+ end
42
+
43
+ context "working with the weekday" do
44
+ it "is initially nil" do
45
+ expect(day.weekday).to be_nil
46
+ end
47
+
48
+ it "can be set" do
49
+ weekday = day.set_weekday(Weekday.new(1))
50
+ expect(day.weekday).to be weekday
51
+ end
52
+ end
53
+
54
+ context "working with the occasions" do
55
+ it "is initially nil" do
56
+ expect(day.occasions).to be_nil
57
+ end
58
+
59
+ it "can be set" do
60
+ occasions = day.set_occasions(OccasionFactory.new(1, 1, 1).occasions)
61
+ expect(day.occasions).to be occasions
62
+ end
63
+ end
64
+
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,75 @@
1
+ require 'bahai_date/month'
2
+
3
+ module BahaiDate
4
+
5
+ describe Month do
6
+ it "can be created given a number from 1 to 19 or -1" do
7
+ expect(Month.new(1)).to_not be_nil
8
+ expect(Month.new(19)).to_not be_nil
9
+ expect(Month.new(-1)).to_not be_nil
10
+ end
11
+
12
+ it "can't be created with a number other than 1 to 19 or -1" do
13
+ expect { Month.new(0) }.to raise_error(
14
+ ArgumentError, "'0' is not a valid month. Please use 1 to 19 or -1 for Ayyam-i-Ha.")
15
+
16
+ expect { Month.new(-2) }.to raise_error(
17
+ ArgumentError, "'-2' is not a valid month. Please use 1 to 19 or -1 for Ayyam-i-Ha.")
18
+
19
+ expect { Month.new(20) }.to raise_error(
20
+ ArgumentError, "'20' is not a valid month. Please use 1 to 19 or -1 for Ayyam-i-Ha.")
21
+
22
+ expect { Month.new('hello') }.to raise_error(
23
+ ArgumentError, "'0' is not a valid month. Please use 1 to 19 or -1 for Ayyam-i-Ha.")
24
+ end
25
+
26
+ context "when created using -1" do
27
+
28
+ it "has a title of Ayyam-i-Ha" do
29
+ month = Month.new(-1)
30
+
31
+ expect(month.to_s).to eq "Ayyam-i-Ha"
32
+ end
33
+ end
34
+
35
+ subject(:month) { Month.new(1) }
36
+
37
+ it "can be converted to string" do
38
+ expect(month.to_s).to eq "Baha"
39
+ end
40
+
41
+ it "provides access to the month number" do
42
+ expect(month.number).to be 1
43
+ end
44
+
45
+ it "provides access to the translated title" do
46
+ expect(month.translation).to eq "Splendour"
47
+ end
48
+
49
+ it "provides access to the title in HTML" do
50
+ expect(month.html).to eq "Bahá"
51
+ end
52
+
53
+ context "working with a hash of days" do
54
+ it "is readable" do
55
+ month = Month.new(1)
56
+ expect(month.days).to eq({})
57
+ end
58
+
59
+ it "can be added to" do
60
+ month = Month.new(1)
61
+ month.set_day(1)
62
+ expect(month.days[1].number).to be 1
63
+ end
64
+
65
+ it "doesn't create a new Day object if one exists" do
66
+ month = Month.new(1)
67
+ day = month.set_day(1)
68
+ month.set_day(1)
69
+ expect(month.days[1]).to be day
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,92 @@
1
+ require 'bahai_date/occasion_factory'
2
+
3
+ module BahaiDate
4
+
5
+ describe OccasionFactory do
6
+
7
+ it "can be created given a year, month, and day" do
8
+ occasion_factory = OccasionFactory.new(1, 1, 1)
9
+ expect(occasion_factory).not_to be_nil
10
+ end
11
+
12
+ it "provides a valid occasion for feast days" do
13
+ occasions = OccasionFactory.new(1, 2, 1).occasions
14
+ expect(occasions.first.type).to eq :feast
15
+ end
16
+
17
+ it "provides a valid occasion for days of fasting" do
18
+ occasions = OccasionFactory.new(1, 19, 2).occasions
19
+ expect(occasions.first.type).to eq :fasting
20
+ end
21
+
22
+ it "provides a valid occasion for days during ayyam-i-ha" do
23
+ occasions = OccasionFactory.new(1, -1, 1).occasions
24
+ expect(occasions.first.type).to eq :ayyam_i_ha
25
+ end
26
+
27
+ it "provides a valid occasion for days during the festival of ridvan" do
28
+ occasions = OccasionFactory.new(1, 2, 14).occasions
29
+ expect(occasions.first.type).to eq :ridvan
30
+ end
31
+
32
+ it "provides a valid occasion for holy days" do
33
+ occasions = OccasionFactory.new(1, 1, 1).occasions
34
+ expect(occasions.first.type).to eq :holy
35
+ end
36
+
37
+ it "provides valid occasions when more than one occurs on the same day" do
38
+ occasions = OccasionFactory.new(1, 1, 1).occasions
39
+ expect(occasions.size).to be 2
40
+ expect(occasions.first.type).to eq :holy
41
+ expect(occasions.last.type).to eq :feast
42
+ end
43
+
44
+ context "for dates before 172 B.E." do
45
+ it "provides the Declaration of the Bab on the correct day" do
46
+ occasions = OccasionFactory.new(1, 4, 7).occasions
47
+ expect(occasions.first.short_title).to eq "Declaration of the Bab"
48
+
49
+ occasions = OccasionFactory.new(1, 4, 8).occasions
50
+ expect(occasions.size).to be 0
51
+ end
52
+
53
+ it "provides the Martyrdom of the Bab on the correct day" do
54
+ occasions = OccasionFactory.new(1, 6, 16).occasions
55
+ expect(occasions.first.short_title).to eq "Martyrdom of the Bab"
56
+ end
57
+
58
+ it "provides the Birth of the Bab on the correct day" do
59
+ occasions = OccasionFactory.new(1, 12, 5).occasions
60
+ expect(occasions.first.short_title).to eq "Birth of the Bab"
61
+ end
62
+
63
+ it "provides the Birth of Baha'u'llah on the correct day" do
64
+ occasions = OccasionFactory.new(1, 13, 9).occasions
65
+ expect(occasions.first.short_title).to eq "Birth of Baha'u'llah"
66
+ end
67
+ end
68
+
69
+ context "for dates after 172 B.E." do
70
+ it "provides the Declaration of the Bab on the correct day" do
71
+ occasions = OccasionFactory.new(172, 4, 7).occasions
72
+ expect(occasions.size).to be 0
73
+
74
+ occasions = OccasionFactory.new(172, 4, 8).occasions
75
+ expect(occasions.first.short_title).to eq "Declaration of the Bab"
76
+ end
77
+
78
+ it "provides the Martyrdom of the Bab on the correct day" do
79
+ occasions = OccasionFactory.new(172, 6, 17).occasions
80
+ expect(occasions.first.short_title).to eq "Martyrdom of the Bab"
81
+ end
82
+
83
+ it "provides the Birth of the Bab on the correct day" do
84
+ occasions = OccasionFactory.new(172, 13, 10).occasions
85
+ expect(occasions.first.short_title).to eq "Birth of the Bab"
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
92
+ end
@@ -0,0 +1,28 @@
1
+ require 'bahai_date/occasion'
2
+
3
+ module BahaiDate
4
+
5
+ describe Occasion do
6
+
7
+ subject(:occasion) {
8
+ Occasion.new({
9
+ :type => :ayyam_i_ha,
10
+ :work_suspended => false,
11
+ :title => "Test title",
12
+ :short_title => "Test short title",
13
+ :title_html => "Test title html",
14
+ :short_title_html => "Test short title html"
15
+ })
16
+ }
17
+
18
+ it "can be created given an options hash" do
19
+ expect(occasion).not_to be_nil
20
+ end
21
+
22
+ it "exposes a work_suspended? method" do
23
+ expect(occasion.work_suspended?).to be false
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,46 @@
1
+ require 'bahai_date/weekday'
2
+
3
+ module BahaiDate
4
+
5
+ describe Weekday do
6
+ it "can be created given a number from 1 to 7" do
7
+ expect(Weekday.new(1)).to_not be_nil
8
+ expect(Weekday.new(7)).to_not be_nil
9
+ end
10
+
11
+ it "can't be created with a number other than 1 to 7" do
12
+ expect { Weekday.new(0) }.to raise_error(
13
+ ArgumentError, "'0' is not a valid weekday. Please use 1 to 7.")
14
+
15
+ expect { Weekday.new(8) }.to raise_error(
16
+ ArgumentError, "'8' is not a valid weekday. Please use 1 to 7.")
17
+
18
+ expect { Weekday.new('hello') }.to raise_error(
19
+ ArgumentError, "'0' is not a valid weekday. Please use 1 to 7.")
20
+ end
21
+
22
+ subject(:weekday) { Weekday.new(1) }
23
+
24
+ it "can be converted to string" do
25
+ expect(weekday.to_s).to eq "Jalal"
26
+ end
27
+
28
+ it "provides access to the day number" do
29
+ expect(weekday.number).to be 1
30
+ end
31
+
32
+ it "provides access to the translated title" do
33
+ expect(weekday.translation).to eq "Glory"
34
+ end
35
+
36
+ it "provides access to the title in HTML" do
37
+ expect(weekday.html).to eq "Jalál"
38
+ end
39
+
40
+ it "provides access to the English equivalent" do
41
+ expect(weekday.english_equivalent).to eq("Saturday")
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,45 @@
1
+ require 'bahai_date/year_calendar'
2
+
3
+ module BahaiDate
4
+
5
+ describe YearCalendar do
6
+
7
+ subject(:year_calendar) { YearCalendar.new(1) }
8
+
9
+ it "can be created" do
10
+ expect(year_calendar).not_to be_nil
11
+ end
12
+
13
+ it "is an instance of year" do
14
+ expect(year_calendar).to be_a Year
15
+ end
16
+
17
+ it "has months set" do
18
+ months = year_calendar.months
19
+ expect(months.size).to be 20
20
+ expect(months[-1]).not_to be_nil
21
+ expect(months[1]).not_to be_nil
22
+ expect(months[19]).not_to be_nil
23
+ end
24
+
25
+ it "has days set" do
26
+ days = year_calendar.months[1].days
27
+ expect(days.size).to be 19
28
+ expect(days[1]).not_to be_nil
29
+ expect(days[19]).not_to be_nil
30
+ end
31
+
32
+ it "has weekdays set" do
33
+ day = year_calendar.months[1].days[1]
34
+ expect(day.weekday).not_to be_nil
35
+ expect(day.weekday.number).to be 6
36
+ end
37
+
38
+ it "has occasions set" do
39
+ day = year_calendar.months[1].days[1]
40
+ expect(day.occasions.size).to be 2
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,19 @@
1
+ require 'bahai_date/year_data'
2
+
3
+ module BahaiDate
4
+
5
+ describe YearData do
6
+
7
+ it "supplies a Date object for Naw Ruz of a given year" do
8
+ expect(YearData.nawruz_for(1844)).to eq(Date.new(1844,3,21))
9
+ end
10
+
11
+ it "determines whether a year is leap or not" do
12
+ expect(YearData.is_leap?(1)).to eq(false)
13
+ expect(YearData.is_leap?(4)).to eq(true)
14
+ expect(YearData.is_leap?(168)).to eq(true)
15
+ end
16
+
17
+ end
18
+
19
+ end