Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed some bugs and refactored both code and specs

  • Loading branch information...
commit 68829226eed53587970ee8066d4675a6a3a9354a 1 parent a2fc5b6
@txus txus authored Codegram committed
View
2  .rspec
@@ -0,0 +1,2 @@
+--colour
+--format documentation
View
27 Rakefile
@@ -11,30 +11,27 @@ begin
gem.homepage = "http://github.com/codegram/date_validator"
gem.authors = ["Oriol Gual", "Josep Mª Bach", "Josep Jaume Rey"]
- gem.add_dependency 'activemodel', '>= 3.0.0.beta3'
+ gem.add_dependency 'activemodel', '>= 3.0.0.beta4'
- gem.add_development_dependency "rspec"
-
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ gem.add_development_dependency "rspec", '>= 2.0.0.beta.12'
+ gem.add_development_dependency "activesupport", '>= 3.0.0.beta4'
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end
-require 'spec/rake/spectask'
-Spec::Rake::SpecTask.new(:spec) do |spec|
- spec.libs << 'lib' << 'spec'
- spec.spec_files = FileList['spec/**/*_spec.rb']
-end
-Spec::Rake::SpecTask.new(:rcov) do |spec|
- spec.libs << 'lib' << 'spec'
- spec.pattern = 'spec/**/*_spec.rb'
- spec.rcov = true
-end
+# Rake RSpec2 task stuff
+gem 'rspec', '>= 2.0.0.beta.12'
+gem 'rspec-expectations'
-task :spec => :check_dependencies
+require 'rspec/core/rake_task'
+
+desc "Run the specs under spec"
+RSpec::Core::RakeTask.new do |t|
+
+end
task :default => :spec
View
4 lib/date_validator.rb
@@ -11,8 +11,8 @@ def initialize(options)
def check_validity!
keys = CHECKS.keys
options.slice(*keys).each do |option, value|
- next if is_time?(value) || value.is_a?(Proc) || value.is_a?(Symbol)
- raise ArgumentError, ":#{option} must be a time, a date, a symbol or a proc"
+ next if is_time?(value) || value.is_a?(Proc) || value.is_a?(Symbol) || (defined?(ActiveSupport::TimeWithZone) and value.is_a? ActiveSupport::TimeWithZone)
+ raise ArgumentError, ":#{option} must be a time, a date, a time_with_zone, a symbol or a proc"
end
end
View
47 spec/date_validator_spec.rb
@@ -1,5 +1,7 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'active_support/time' # For testing Date and TimeWithZone objects
+
describe "DateValidator" do
before(:each) do
@@ -8,23 +10,22 @@
it "should check validity of the arguments" do
[3, "foo", 1..6].each do |wrong_argument|
- begin
+ expect {
TestRecord.validates :expiration_date, :date => {:before => wrong_argument}
- fail "should not accept a #{wrong_argument.class} as an option (only Time, Date, Symbol or Proc)"
- rescue=>e
- fail e unless e.is_a?(ArgumentError)
- end
+ }.to raise_error(ArgumentError, ":before must be a time, a date, a time_with_zone, a symbol or a proc")
end
end
[:after, :before, :after_or_equal_to, :before_or_equal_to].each do |check|
[:valid,:invalid].each do |should_be|
+ now = Time.now.to_datetime
+
model_date = case check
- when :after then should_be == :valid ? Time.now + 21000 : Time.now - 1
- when :before then should_be == :valid ? Time.now - 21000 : Time.now + 1
- when :after_or_equal_to then should_be == :valid ? Time.now + 21000 : Time.now - 21000
- when :before_or_equal_to then should_be == :valid ? Time.now - 21000 : Time.now + 21000
+ when :after then should_be == :valid ? now + 21000 : now - 1
+ when :before then should_be == :valid ? now - 21000 : now + 1
+ when :after_or_equal_to then should_be == :valid ? now : now - 21000
+ when :before_or_equal_to then should_be == :valid ? now : now + 21000
end
it "should ensure that an attribute is #{should_be} when #{should_be == :valid ? 'respecting' : 'offending' } the #{check} check" do
@@ -37,34 +38,42 @@
end
extra_types = [:proc, :symbol]
- # extra_types.push(:date) if defined?(Date) and defined?(DateTime)
+ extra_types.push(:date) if defined?(Date) and defined?(DateTime)
+ extra_types.push(:time_with_zone) if defined?(ActiveSupport::TimeWithZone)
extra_types.each do |type|
it "should accept a #{type} as an argument to a check" do
case type
when :proc then
- TestRecord.validates :expiration_date, :date => {:after => Proc.new{Time.now + 21000}}
+ expect {
+ TestRecord.validates :expiration_date, :date => {:after => Proc.new{Time.now + 21000}}
+ }.to_not raise_error
when :symbol then
- begin
+ expect {
TestRecord.send(:define_method, :min_date, lambda { Time.now + 21000 })
TestRecord.validates :expiration_date, :date => {:after => :min_date}
- rescue=>e
- fail "is not accepting a #{type} as an argument to a check"
- end
- # when :date then
- # TestRecord.validates :expiration_date, :date => {:after => Time.now.to_date}
+ }.to_not raise_error
+ when :date then
+ expect {
+ TestRecord.validates :expiration_date, :date => {:after => Time.now.to_date}
+ }.to_not raise_error
+ when :time_with_zone then
+ expect {
+ Time.zone = "Hawaii"
+ TestRecord.validates :expiration_date, :date => {:before => Time.zone.parse((Time.now + 21000).to_s)}
+ }.to_not raise_error
end
end
end
it "should gracefully handle an unexpected result from a proc argument evaluation" do
TestRecord.validates :expiration_date, :date => {:after => Proc.new{ nil }}
- TestRecord.new(Time.now).valid?.should be_false
+ TestRecord.new(Time.now).should_not be_valid
end
it "should gracefully handle an unexpected result from a symbol argument evaluation" do
TestRecord.send(:define_method, :min_date, lambda { nil })
TestRecord.validates :expiration_date, :date => {:after => :min_date}
- TestRecord.new(Time.now).valid?.should be_false
+ TestRecord.new(Time.now).should_not be_valid
end
end
View
2  spec/spec.opts
@@ -1,2 +0,0 @@
---colour
---format progress
View
9 spec/spec_helper.rb
@@ -5,9 +5,8 @@
require 'active_model'
require 'lib/date_validator'
-require 'spec'
-require 'spec/autorun'
-
+require 'rspec'
+require 'rspec/autorun'
class TestRecord
include ActiveModel::Validations
@@ -17,7 +16,3 @@ def initialize(expiration_date)
@expiration_date = expiration_date
end
end
-
-Spec::Runner.configure do |config|
-
-end
Please sign in to comment.
Something went wrong with that request. Please try again.