Skip to content
Browse files

fix the validation code and add more specs

  • Loading branch information...
1 parent b3e0fd0 commit 8b7979403fb8b5b936b703bdce19fb267b5c5677 @rafaelss committed May 15, 2012
Showing with 25 additions and 4 deletions.
  1. +1 −1 lib/cielo/card/validity.rb
  2. +24 −3 spec/cielo/validity_spec.rb
View
2 lib/cielo/card/validity.rb
@@ -8,7 +8,7 @@ def initialize(year, month)
@month = month.to_i
now = Time.now
- if @year < now.year || @month < now.month
+ if @year < now.year || (@year == now.year && @month < now.month)
raise InvalidDate, "#{@month}/#{@year} is not valid"
end
end
View
27 spec/cielo/validity_spec.rb
@@ -17,8 +17,29 @@
expect { described_class.new(Time.now.year, Time.now.month - 1) }.should raise_error(described_class::InvalidDate)
end
- it "#to_s" do
- validity = described_class.new(Time.now.year, Time.now.month)
- validity.to_s.should == "201205"
+ it "raises an exception if both year and month are not valid" do
+ expect { described_class.new(Time.now.year - 1, Time.now.month - 1) }.should raise_error(described_class::InvalidDate)
+ end
+
+ describe "#to_s" do
+ it "works with current year and month" do
+ validity = described_class.new(Time.now.year, Time.now.month)
+ validity.to_s.should == "201205"
+ end
+
+ it "works with next year but current month" do
+ validity = described_class.new(Time.now.year + 1, Time.now.month)
+ validity.to_s.should == "201305"
+ end
+
+ it "works with next year and next month" do
+ validity = described_class.new(Time.now.year + 1, Time.now.month + 1)
+ validity.to_s.should == "201306"
+ end
+
+ it "works with two-digits month" do
+ validity = described_class.new(Time.now.year + 1, 12)
+ validity.to_s.should == "201312"
+ end
end
end

0 comments on commit 8b79794

Please sign in to comment.
Something went wrong with that request. Please try again.