Skip to content

Commit

Permalink
Add custom specs for Date (for missing rubyspec tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Jan 14, 2014
1 parent 41c13fd commit 9ba58b6
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 8 deletions.
115 changes: 115 additions & 0 deletions spec/opal/core/date_spec.rb
@@ -0,0 +1,115 @@
require 'spec_helper'
require 'date'

# rubyspec does not have specs for these listed methods
describe Date do
describe "#<" do
it "is true when self is before other" do
(Date.new(2013, 2, 4) < Date.new(2013, 2, 5)).should == true
(Date.new(2013, 2, 4) < Date.new(2014, 7, 6)).should == true
end

it "is false when self is not before other" do
(Date.new(2013, 2, 4) < Date.new(2013, 2, 4)).should == false
(Date.new(2014, 2, 4) < Date.new(2013, 7, 6)).should == false
end
end

describe '#<=' do
it "is true when self is before or the same day as other" do
(Date.new(2013, 4, 5) <= Date.new(2013, 4, 5)).should == true
(Date.new(2013, 4, 5) <= Date.new(2013, 4, 9)).should == true
end

it "is false when self is after other" do
(Date.new(2013, 4, 5) <= Date.new(2013, 4, 2)).should == false
(Date.new(2013, 4, 5) <= Date.new(2013, 2, 5)).should == false
end
end

describe "#==" do
it "returns true if self is equal to other date" do
(Date.new(2013, 9, 13) == Date.new(2013, 9, 13)).should == true
end

it "returns false if self is not equal to other date" do
(Date.new(2013, 10, 2) == Date.new(2013, 10, 11)).should == false
end
end

describe "#clone" do
it "creates a copy of the current date" do
orig = Date.new(2013, 10, 15)
copy = orig.clone

orig.should == copy
orig.object_id.should_not == copy.object_id
end
end

describe "#day" do
it "returns the day of the date" do
Date.new(2013, 2, 10).day.should == 10
Date.new(2013, 2, 1).day.should == 1
end
end

describe "#month" do
it "returns the month of the date" do
Date.new(2013, 1, 23).month.should == 1
Date.new(2013, 12, 2).month.should == 12
end
end

describe "#next" do
it "returns the next date from self" do
Date.new(2013, 4, 6).next.should == Date.new(2013, 4, 7)
Date.new(2013, 6, 30).next.should == Date.new(2013, 7, 1)
Date.new(2013, 12, 31).next.should == Date.new(2014, 1, 1)
end
end

describe "#next_month" do
it "returns the date with the next calendar month to self" do
Date.new(2013, 2, 5).next_month.should == Date.new(2013, 3, 5)
Date.new(2013, 5, 31).next_month.should == Date.new(2013, 6, 30)
Date.new(2013, 12, 5).next_month.should == Date.new(2014, 1, 5)
end
end

describe "#prev" do
it "returns the previous date from self" do
Date.new(2013, 3, 5).prev.should == Date.new(2013, 3, 4)
Date.new(2013, 6, 1).prev.should == Date.new(2013, 5, 31)
Date.new(2014, 1, 1).prev.should == Date.new(2013, 12, 31)
end
end

describe "#prev_month" do
it "returns the date with the previous calendar month" do
Date.new(2013, 2, 9).prev_month.should == Date.new(2013, 1, 9)
Date.new(2013, 7, 31).prev_month.should == Date.new(2013, 6, 30)
Date.new(2013, 1, 3).prev_month.should == Date.new(2012, 12, 3)
end
end

describe "#to_s" do
it "returns an ISO 8601 representation" do
Date.new(2013, 10, 15).to_s.should == "2013-10-15"
Date.new(2013, 4, 9).to_s.should == "2013-04-09"
end
end

describe "#wday" do
it "returns the day of the week" do
Date.new(2001, 2, 3).wday.should == 6
Date.new(2001, 2, 4).wday.should == 0
end
end

describe "#year" do
it "returns the year as an integer" do
Date.new(2013, 2, 9).year.should == 2013
end
end
end
33 changes: 25 additions & 8 deletions stdlib/date.rb
Expand Up @@ -114,9 +114,13 @@ def next
end

def next_month
res = self.clone
`res.date.add({months: 1})`
res
%x{
var result = #{clone}, date = result.date, cur = date.getDate();
date.setDate(1);
date.setMonth(date.getMonth() + 1);
date.setDate(Math.min(cur, days_in_month(date.getFullYear(), date.getMonth())));
return result;
}
end

def prev
Expand All @@ -126,9 +130,13 @@ def prev
end

def prev_month
res = self.clone
`res.date.add({months: -1})`
res
%x{
var result = #{clone}, date = result.date, cur = date.getDate();
date.setDate(1);
date.setMonth(date.getMonth() - 1);
date.setDate(Math.min(cur, days_in_month(date.getFullYear(), date.getMonth())));
return result;
}
end

def strftime(format = '')
Expand All @@ -137,8 +145,10 @@ def strftime(format = '')

def to_s
%x{
var date = #@date;
return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
var d = #@date, year = d.getFullYear(), month = d.getMonth() + 1, day = d.getDate();
if (month < 10) { month = '0' + month; }
if (day < 10) { day = '0' + day; }
return year + '-' + month + '-' + day;
}
end

Expand All @@ -157,4 +167,11 @@ def wday
def year
`#@date.getFullYear()`
end

%x{
function days_in_month(year, month) {
var leap = ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0);
return [31, (leap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
}
}
end
10 changes: 10 additions & 0 deletions stdlib/json.rb
Expand Up @@ -161,3 +161,13 @@ def to_json
strftime("%FT%T%z").to_json
end
end

class Date
def to_json
to_s.to_json
end

def as_json
to_s
end
end

0 comments on commit 9ba58b6

Please sign in to comment.