Skip to content

Commit e42c02c

Browse files
committedOct 31, 2014
Add Date#next_day, #prev_day and specs for relevant methods
1 parent 03f31b0 commit e42c02c

File tree

3 files changed

+58
-46
lines changed

3 files changed

+58
-46
lines changed
 

‎spec/opal/core/date_spec.rb

+49-8
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,6 @@
8484
end
8585
end
8686

87-
describe "#prev" do
88-
it "returns the previous date from self" do
89-
Date.new(2013, 3, 5).prev.should == Date.new(2013, 3, 4)
90-
Date.new(2013, 6, 1).prev.should == Date.new(2013, 5, 31)
91-
Date.new(2014, 1, 1).prev.should == Date.new(2013, 12, 31)
92-
end
93-
end
94-
9587
describe "#prev_month" do
9688
it "returns the date with the previous calendar month" do
9789
Date.new(2013, 2, 9).prev_month.should == Date.new(2013, 1, 9)
@@ -100,6 +92,26 @@
10092
end
10193
end
10294

95+
describe '#next_day' do
96+
it 'returns a new date the given number of days after self' do
97+
Date.new(2014, 4, 5).next_day.should == Date.new(2014, 4, 6)
98+
Date.new(2014, 4, 5).next_day(4).should == Date.new(2014, 4, 9)
99+
end
100+
end
101+
102+
describe '#prev_day' do
103+
it 'returns the date the given number of days before self' do
104+
Date.new(2014, 4, 5).prev_day.should == Date.new(2014, 4, 4)
105+
Date.new(2014, 4, 5).prev_day(4).should == Date.new(2014, 4, 1)
106+
end
107+
end
108+
109+
describe '#succ' do
110+
it 'returns the date after the receiver' do
111+
Date.new(1986, 5, 26).succ.should == Date.new(1986, 5, 27)
112+
end
113+
end
114+
103115
describe "#to_s" do
104116
it "returns an ISO 8601 representation" do
105117
Date.new(2013, 10, 15).to_s.should == "2013-10-15"
@@ -119,4 +131,33 @@
119131
Date.new(2013, 2, 9).year.should == 2013
120132
end
121133
end
134+
135+
it 'correctly reports mondays' do
136+
Date.new(2015, 4, 6).monday?.should be_true
137+
end
138+
139+
it 'correctly reports tuesdays' do
140+
Date.new(2015, 4, 7).tuesday?.should be_true
141+
end
142+
143+
it 'correctly reports wednesdays' do
144+
Date.new(2015, 4, 8).wednesday?.should be_true
145+
end
146+
147+
it 'correctly reports thursdays' do
148+
Date.new(2015, 4, 9).thursday?.should be_true
149+
end
150+
151+
it 'correctly reports fridays' do
152+
Date.new(2015, 4, 10).friday?.should be_true
153+
end
154+
155+
it 'correctly reports saturdays' do
156+
Date.new(2015, 4, 11).saturday?.should be_true
157+
end
158+
159+
it 'correctly reports sundays' do
160+
Date.new(2015, 4, 12).sunday?.should be_true
161+
end
162+
122163
end

‎spec/opal/stdlib/date/date_spec.rb

-31
This file was deleted.

‎stdlib/date.rb

+9-7
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ def month
117117
end
118118

119119
def next
120-
res = self.clone
121-
`res.date.setDate(#@date.getDate() + 1)`
122-
res
120+
self + 1
121+
end
122+
123+
def next_day(n=1)
124+
self + n
123125
end
124126

125127
def next_month
@@ -132,10 +134,8 @@ def next_month
132134
}
133135
end
134136

135-
def prev
136-
res = self.clone
137-
`res.date.setDate(#@date.getDate() - 1)`
138-
res
137+
def prev_day(n=1)
138+
self - n
139139
end
140140

141141
def prev_month
@@ -156,6 +156,8 @@ def strftime(format = '')
156156
`#@date.$strftime(#{format})`
157157
end
158158

159+
alias_method :succ, :next
160+
159161
def sunday?
160162
wday == 0
161163
end

0 commit comments

Comments
 (0)
Please sign in to comment.