Skip to content

Commit

Permalink
Merge pull request #1172 from herwinw/date_yday
Browse files Browse the repository at this point in the history
Add specs for Date#yday and DateTime#yday
  • Loading branch information
andrykonchin committed Jun 29, 2024
2 parents 43af972 + 0ba51c7 commit e10c053
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
13 changes: 2 additions & 11 deletions core/time/yday_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../../spec_helper'
require_relative '../../shared/time/yday'

describe "Time#yday" do
it "returns an integer representing the day of the year, 1..366" do
Expand All @@ -7,15 +8,5 @@
end
end

it 'returns the correct value for each day of each month' do
mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

yday = 1
mdays.each_with_index do |days, month|
days.times do |day|
Time.new(2014, month+1, day+1).yday.should == yday
yday += 1
end
end
end
it_behaves_like :time_yday, -> year, month, day { Time.new(year, month, day).yday }
end
3 changes: 2 additions & 1 deletion library/date/yday_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
require_relative '../../shared/time/yday'
require 'date'

describe "Date#yday" do
it "needs to be reviewed for spec completeness"
it_behaves_like :time_yday, -> year, month, day { Date.new(year, month, day).yday }
end
7 changes: 7 additions & 0 deletions library/datetime/yday_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require_relative '../../spec_helper'
require_relative '../../shared/time/yday'
require 'date'

describe "DateTime#yday" do
it_behaves_like :time_yday, -> year, month, day { DateTime.new(year, month, day).yday }
end
18 changes: 18 additions & 0 deletions shared/time/yday.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
describe :time_yday, shared: true do
it 'returns the correct value for each day of each month' do
mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

yday = 1
mdays.each_with_index do |days, month|
days.times do |day|
@method.call(2014, month+1, day+1).should == yday
yday += 1
end
end
end

it 'supports leap years' do
@method.call(2016, 2, 29).should == 31 + 29
@method.call(2016, 3, 1).should == 31 + 29 + 1
end
end

0 comments on commit e10c053

Please sign in to comment.