weekling 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,378 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2012
4
+
5
+ This file is part of Weekling.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'spec_helper'
21
+ require 'aef/weekling/year'
22
+ require 'ostruct'
23
+
24
+ describe Aef::Weekling::Year do
25
+ [:today, :now].each do |method|
26
+ context ".#{method}" do
27
+ it "should find the current year" do
28
+ year = described_class.method(method).call
29
+ today = Date.today
30
+
31
+ year.index.should eql today.year
32
+ end
33
+ end
34
+ end
35
+
36
+ context ".parse" do
37
+ it "should recognize an ancient year" do
38
+ year = described_class.parse('-1503')
39
+
40
+ year.index.should eql -1503
41
+ end
42
+
43
+ it "should recognize a normal year" do
44
+ year = described_class.parse('2011')
45
+
46
+ year.index.should eql 2011
47
+ end
48
+
49
+ it "should recognize a post apocalyptic year" do
50
+ year = described_class.parse('50023')
51
+
52
+ year.index.should eql 50023
53
+ end
54
+
55
+ it "should report being unable to parse the given String" do
56
+ lambda{
57
+ described_class.parse('no year!')
58
+ }.should raise_error(ArgumentError, 'No year found for parsing')
59
+ end
60
+ end
61
+
62
+ context ".new" do
63
+ it "should complain about a param of invalid type" do
64
+ lambda {
65
+ described_class.new(/abc/)
66
+ }.should raise_error(ArgumentError, 'A single argument must either respond to #to_date or to #to_i')
67
+ end
68
+
69
+ it "should be able to initialize an ancient year by index" do
70
+ year = described_class.new(-1691)
71
+
72
+ year.index.should eql -1691
73
+ end
74
+
75
+ it "should be able to initialize a normal year by index" do
76
+ year = described_class.new(2012)
77
+
78
+ year.index.should eql 2012
79
+ end
80
+
81
+ it "should be able to initialize a post apocalyptic year by index" do
82
+ year = described_class.new(23017)
83
+
84
+ year.index.should eql 23017
85
+ end
86
+
87
+ it "should be able to initialize a year by a given Year object" do
88
+ old_year = described_class.new(2011)
89
+ year = described_class.new(old_year)
90
+
91
+ year.index.should eql old_year.index
92
+ end
93
+
94
+ it "should be able to initialize a year by a given Date object" do
95
+ date = Date.today
96
+ year = described_class.new(date)
97
+
98
+ year.index.should eql date.year
99
+ end
100
+
101
+ it "should be able to initialize a year by a given DateTime object" do
102
+ date = DateTime.now
103
+ year = described_class.new(date)
104
+
105
+ year.index.should eql date.year
106
+ end
107
+
108
+ it "should be able to initialize a year by a given Time object" do
109
+ time = Time.now
110
+ year = described_class.new(time)
111
+
112
+ date = time.to_date
113
+
114
+ year.index.should eql date.year
115
+ end
116
+ end
117
+
118
+ context "#== (type independent equality)" do
119
+ it "should be true if index matches" do
120
+ year = described_class.new(2012)
121
+ other = described_class.new(2012)
122
+
123
+ year.should == other
124
+ end
125
+
126
+ it "should be true if index matches, independent of the other object's type" do
127
+ year = described_class.new(2012)
128
+
129
+ other = OpenStruct.new(to_i: 2012)
130
+
131
+ year.should == other
132
+ end
133
+
134
+ it "should be false if index doesn't match" do
135
+ year = described_class.new(2012)
136
+ other = described_class.new(2013)
137
+
138
+ year.should_not == other
139
+ end
140
+
141
+ it "should be false if index doesn't match, independent of the other object's type" do
142
+ year = described_class.new(2012)
143
+
144
+ other = OpenStruct.new(to_i: 2013)
145
+
146
+ year.should_not == other
147
+ end
148
+ end
149
+
150
+ context "#eql? (type dependant equality)" do
151
+ it "should be true if index matches and other is of same class" do
152
+ year = described_class.new(2012)
153
+ other = described_class.new(2012)
154
+
155
+ year.should eql other
156
+ end
157
+
158
+ it "should be true if index matches and other is of descendent class" do
159
+ year = described_class.new(2012)
160
+
161
+ inheriting_class = Class.new(described_class)
162
+
163
+ other = inheriting_class.new(2012)
164
+
165
+ year.should eql other
166
+ end
167
+
168
+ it "should be false if index matches but type differs" do
169
+ year = described_class.new(2012)
170
+
171
+ other = OpenStruct.new(to_i: 2012)
172
+
173
+ year.should_not eql other
174
+ end
175
+
176
+ it "should be false if index doesn't match" do
177
+ year = described_class.new(2012)
178
+ other = described_class.new(2005)
179
+
180
+ year.should_not eql other
181
+ end
182
+ end
183
+
184
+ context "#hash" do
185
+ it "should return Integers" do
186
+ a_year = described_class.new(2012)
187
+ another_year = described_class.new(2013)
188
+
189
+ a_year.hash.should be_a(Integer)
190
+ another_year.hash.should be_a(Integer)
191
+ end
192
+
193
+ it "should discriminate a year from another one" do
194
+ a_year = described_class.new(2012)
195
+ another_year = described_class.new(2013)
196
+
197
+ a_year.hash.should_not == another_year.hash
198
+ end
199
+ end
200
+
201
+ context "#<=>" do
202
+ it "should correctly determine the order of years" do
203
+ lower_year = described_class.new(2011)
204
+ higher_year = described_class.new(2012)
205
+
206
+ lower_year.should < higher_year
207
+ end
208
+
209
+ it "should correctly determine the order of years, independent of type" do
210
+ lower_year = described_class.new(2011)
211
+
212
+ higher_year = OpenStruct.new(to_year: OpenStruct.new(index: 2012))
213
+
214
+ lower_year.should < higher_year
215
+ end
216
+ end
217
+
218
+ context "#to_s" do
219
+ it "should be able to display an ancient year" do
220
+ year = described_class.new(-1503)
221
+
222
+ year.to_s.should eql '-1503'
223
+ end
224
+
225
+ it "should be able to display a normal year" do
226
+ year = described_class.new(2011)
227
+
228
+ year.to_s.should eql '2011'
229
+ end
230
+
231
+ it "should be able to display a post apocalyptic year" do
232
+ year = described_class.new(50023)
233
+
234
+ year.to_s.should eql '50023'
235
+ end
236
+ end
237
+
238
+ context "#inspect" do
239
+ it "should be able to display an ancient year" do
240
+ year = described_class.new(-1503)
241
+
242
+ year.inspect.should eql '#<Aef::Weekling::Year: -1503>'
243
+ end
244
+
245
+ it "should be able to display a normal year" do
246
+ year = described_class.new(2011)
247
+
248
+ year.inspect.should eql '#<Aef::Weekling::Year: 2011>'
249
+ end
250
+
251
+ it "should be able to display a post apocalyptic year" do
252
+ year = described_class.new(50023)
253
+
254
+ year.inspect.should eql '#<Aef::Weekling::Year: 50023>'
255
+ end
256
+ end
257
+
258
+ context "#to_year" do
259
+ it "should return itself" do
260
+ year = described_class.new(2011)
261
+ year.to_year.should equal(year)
262
+ end
263
+ end
264
+
265
+ [:next, :succ].each do |method|
266
+ context "##{method}" do
267
+ it "should return the next year" do
268
+ described_class.new(2011).method(method).call.should eql described_class.new(2012)
269
+ end
270
+ end
271
+ end
272
+
273
+ [:previous, :pred].each do |method|
274
+ context "##{method}" do
275
+ it "should return the previous year" do
276
+ described_class.new(2011).method(method).call.should eql described_class.new(2010)
277
+ end
278
+ end
279
+ end
280
+
281
+ context "#+" do
282
+ it "should be able to add a positive amount of years" do
283
+ (described_class.new(1998) + 15).should eql described_class.new(2013)
284
+ end
285
+
286
+ it "should be able to add a negative amount of years" do
287
+ (described_class.new(1998) + -12).should eql described_class.new(1986)
288
+ end
289
+
290
+ it "should be able to add zero years" do
291
+ (described_class.new(1998) + 0).should eql described_class.new(1998)
292
+ end
293
+ end
294
+
295
+ context "#-" do
296
+ it "should be able to subtract a positive amount of years" do
297
+ (described_class.new(1998) - 15).should eql described_class.new(1983)
298
+ end
299
+
300
+ it "should be able to subtract a negative amount of years" do
301
+ (described_class.new(1998) - -12).should eql described_class.new(2010)
302
+ end
303
+
304
+ it "should be able to add zero years" do
305
+ (described_class.new(1998) - 0).should eql described_class.new(1998)
306
+ end
307
+ end
308
+
309
+ context "#even?" do
310
+ it "should be true if the index is even" do
311
+ described_class.new(420).should be_even
312
+ described_class.new(2012).should be_even
313
+ described_class.new(25046).should be_even
314
+ end
315
+
316
+ it "should be false if the index is odd" do
317
+ described_class.new(419).should_not be_even
318
+ described_class.new(2011).should_not be_even
319
+ described_class.new(25043).should_not be_even
320
+ end
321
+ end
322
+
323
+ context "#leap?" do
324
+ it "should be true if year is a leap year" do
325
+ Aef::Weekling::Year.new(2004).should be_leap
326
+ Aef::Weekling::Year.new(1804).should be_leap
327
+ Aef::Weekling::Year.new(2016).should be_leap
328
+ end
329
+
330
+ it "should be false if year is not a leap year" do
331
+ Aef::Weekling::Year.new(1998).should_not be_leap
332
+ Aef::Weekling::Year.new(2100).should_not be_leap
333
+ Aef::Weekling::Year.new(2003).should_not be_leap
334
+ end
335
+ end
336
+
337
+ context "#odd?" do
338
+ it "should be true if the index is odd" do
339
+ described_class.new(419).should be_odd
340
+ described_class.new(2011).should be_odd
341
+ described_class.new(25043).should be_odd
342
+ end
343
+
344
+ it "should be false if the index is even" do
345
+ described_class.new(420).should_not be_odd
346
+ described_class.new(2012).should_not be_odd
347
+ described_class.new(25046).should_not be_odd
348
+ end
349
+ end
350
+
351
+ context "#week_count" do
352
+ it "should return the correct amount of weeks for years with 52 weeks" do
353
+ described_class.new(1985).week_count.should eql 52
354
+ described_class.new(2002).week_count.should eql 52
355
+ described_class.new(2024).week_count.should eql 52
356
+ end
357
+
358
+ it "should return the correct amount of weeks for years with 53 weeks" do
359
+ described_class.new(1981).week_count.should eql 53
360
+ described_class.new(2020).week_count.should eql 53
361
+ described_class.new(2026).week_count.should eql 53
362
+ end
363
+ end
364
+
365
+ context "#weeks" do
366
+ it "should return a range of weeks" do
367
+ described_class.new(2011).weeks.should eql(
368
+ (Aef::Weekling::Week.new(2011, 1)..Aef::Weekling::Week.new(2011, 52)))
369
+ end
370
+ end
371
+
372
+ context "#week" do
373
+ it "should return a week by index" do
374
+ described_class.new(2011).week(17).should eql Aef::Weekling::Week.new(2011, 17)
375
+ end
376
+ end
377
+
378
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2012
4
+
5
+ This file is part of Weekling.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
12
+ TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
14
+ CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
15
+ DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16
+ ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17
+ SOFTWARE.
18
+ =end
19
+
20
+ unless defined?(Rubinius)
21
+ require 'simplecov'
22
+ SimpleCov.start
23
+ end
24
+
25
+ require 'rspec'
26
+ require 'pry'
@@ -0,0 +1,55 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ This file is part of Weekling.
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
16
+ =end
17
+
18
+ $LOAD_PATH << 'lib'
19
+
20
+ require 'weekling/bare'
21
+
22
+ Gem::Specification.new do |s|
23
+ s.name = 'weekling'
24
+ s.version = Aef::Weekling::VERSION.dup
25
+ s.authors = ['Alexander E. Fischer']
26
+ s.email = ['aef@raxys.net']
27
+ s.homepage = 'http://github.com/aef/weekling'
28
+ s.license = 'ISC'
29
+ s.summary = 'Easy ISO 8601 year, week and week-day helper classes'
30
+ s.description = <<-DESCRIPTION
31
+ Weekling is a Ruby library which provides class representations to make date
32
+ calculations using years, weeks and week-days much easier. Years, weeks and
33
+ week-days are interpreted as in ISO 8601.
34
+ DESCRIPTION
35
+
36
+ s.rubyforge_project = nil
37
+ s.has_rdoc = 'yard'
38
+ s.extra_rdoc_files = ['HISTORY.md', 'LICENSE.md']
39
+
40
+ s.files = `git ls-files`.lines.map(&:chomp)
41
+ s.test_files = `git ls-files -- {test,spec,features}/*`.lines.map(&:chomp)
42
+ s.executables = `git ls-files -- bin/*`.lines.map{|f| File.basename(f.chomp) }
43
+ s.require_paths = ["lib"]
44
+
45
+ s.add_development_dependency('bundler', '~> 1.0.21')
46
+ s.add_development_dependency('rake', '~> 0.9.2')
47
+ s.add_development_dependency('rspec', '~> 2.6.0')
48
+ s.add_development_dependency('simplecov', '~> 0.5.4')
49
+ s.add_development_dependency('pry', '~> 0.9.8')
50
+ s.add_development_dependency('yard', '~> 0.7.5')
51
+ s.add_development_dependency('maruku', '~> 0.6.0')
52
+
53
+ s.cert_chain = "#{ENV['GEM_CERT_CHAIN']}".split(':')
54
+ s.signing_key = ENV['GEM_SIGNING_KEY']
55
+ end