Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fleshed out validation features

  • Loading branch information...
commit e4363354fdf11d631bcb586152fd4d1efd79cae8 1 parent 73dfc59
@spohlenz authored
View
2  features/document/destroying.feature
@@ -1,5 +1,5 @@
@completed
-Scenario: Destroying documents
+Feature: Destroying documents
Destroying a Recliner::Document removes it from the database
It also runs any before/after_destroy callbacks that are defined
View
18 features/step_definitions/document_steps.rb
@@ -23,7 +23,8 @@
end
Given /^the following document definition:$/ do |code|
- eval(code)
+ @defined_constants ||= []
+ @defined_constants += ActiveSupport::Dependencies.new_constants_in(Object) { eval(code) }
end
Given /^I have an unsaved instance of "([^\"]*)"$/ do |klass|
@@ -172,3 +173,18 @@
Then /^the instance should not save$/ do
@instance.save.should be_false
end
+
+Then /^its errors should be empty$/ do
+ @instance.valid?
+ @instance.errors.should be_empty
+end
+
+Then /^its errors should include "([^\"]*)"$/ do |error|
+ @instance.valid?
+ @instance.errors.full_messages.should include(error)
+end
+
+Then /^it should have (\d+) error$/ do |num_errors|
+ @instance.valid?
+ @instance.errors.size.should == num_errors.to_i
+end
View
12 features/support/env.rb
@@ -19,6 +19,18 @@ def eval_hash_keys(hash)
h = hash.dup
h.each { |k, v| h[k] = eval(v) rescue v }
end
+
+ def remove_defined_constants
+ if @defined_constants
+ @defined_constants.each do |const|
+ Object.send(:remove_const, const)
+ end
+
+ @defined_constants = nil
+ end
+ end
end
World(ReclinerWorld)
+
+After { remove_defined_constants }
View
30 features/validation/acceptance.feature
@@ -3,25 +3,39 @@ Feature: Validation: validates_acceptance_of
The validates_acceptance_of validation specifies that a virtual
attribute must be set to "1" (i.e. from a HTML checkbox)
- Background:
+ Scenario: validation requirements met
Given the following document definition:
"""
class User < Recliner::Document
validates_acceptance_of :terms_and_conditions
end
"""
-
- Scenario: validation requirements met
+
When I create an instance of "User"
And I set its terms_and_conditions to "1"
Then the instance should be valid
- And the instance should save
+
+ Scenario: validation requirements met (nil allowed)
+ Given the following document definition:
+ """
+ class User < Recliner::Document
+ validates_acceptance_of :terms_and_conditions, :allow_nil => true
+ end
+ """
+
+ When I create an instance of "User"
+ Then the instance should be valid
Scenario: validation requirements failing
+ Given the following document definition:
+ """
+
+ class User < Recliner::Document
+ validates_acceptance_of :terms_and_conditions
+ end
+ """
When I create an instance of "User"
And I set its terms_and_conditions to "0"
Then the instance should not be valid
- And the instance should not save
-
- When I save! the instance
- Then a "Recliner::DocumentInvalid" exception should be raised
+ And its errors should include "Terms and conditions must be accepted"
+
View
34 features/validation/confirmation.feature
@@ -12,19 +12,33 @@ Feature: Validation: validates_confirmation_of
end
"""
- Scenario: validation requirements met
+ Scenario Outline: validation requirements met
When I create an instance of "User"
- And I set its email to "me@example.com"
- And I set its email_confirmation to "me@example.com"
+ And I set its email to "<email>"
+ And I set its email_confirmation to "<confirmation>"
Then the instance should be valid
- And the instance should save
+
+ Examples:
+ | email | confirmation |
+ | me@example.com | me@example.com |
+ | test@test.com | test@test.com |
+ | abc | abc |
- Scenario: validation requirements failing
+ Scenario Outline: validation requirements failing
When I create an instance of "User"
- And I set its email to "me@example.com"
- And I set its email_confirmation to "me@asmallbutsignificanttypo.com"
+ And I set its email to "<email>"
+ And I set its email_confirmation to "<confirmation>"
Then the instance should not be valid
- And the instance should not save
+ And its errors should include "Email doesn't match confirmation"
- When I save! the instance
- Then a "Recliner::DocumentInvalid" exception should be raised
+ Examples:
+ | email | confirmation |
+ | me1@example.com | me2@example.com |
+ | test@test.com | test@testt.com |
+ | abc | def |
+
+ Scenario: confirmation not provided
+ When I create an instance of "User"
+ And I set its email to "myemail"
+ Then the instance should be valid
+
View
34 features/validation/exclusion.feature
@@ -6,23 +6,33 @@ Feature: Validation: validates_exclusion_of
Background:
Given the following document definition:
"""
- class User < Recliner::Document
- property :age, Integer
- validates_exclusion_of :age, :in => 13..18
+ class Account < Recliner::Document
+ property :subdomain, String
+ validates_exclusion_of :subdomain, :in => [ 'www', 'mail', 'ftp' ]
end
"""
- Scenario: validation requirements met
- When I create an instance of "User"
- And I set its age to "42"
+ Scenario Outline: validation requirements met
+ When I create an instance of "Account"
+ And I set its subdomain to "<subdomain>"
Then the instance should be valid
And the instance should save
+
+ Examples:
+ | subdomain |
+ | myaccount |
+ | www1 |
+ | ftpabc |
- Scenario: validation requirements failing
- When I create an instance of "User"
- And I set its age to "16"
+ Scenario Outline: validation requirements failing
+ When I create an instance of "Account"
+ And I set its subdomain to "<subdomain>"
Then the instance should not be valid
- And the instance should not save
+ And its errors should include "Subdomain is reserved"
- When I save! the instance
- Then a "Recliner::DocumentInvalid" exception should be raised
+ Examples:
+ | subdomain |
+ | www |
+ | mail |
+ | ftp |
+
View
106 features/validation/format.feature
@@ -3,26 +3,106 @@ Feature: Validation: validates_format_of
The validates_format_of validation specifies that a given
attribute must match a certain regular expression
- Background:
+ Scenario Outline: validation requirements met (using :with)
Given the following document definition:
"""
- class Person < Recliner::Document
+ class Sample < Recliner::Document
property :dna, String
validates_format_of :dna, :with => /^[ACTG]+$/
end
"""
-
- Scenario: validation requirements met
- When I create an instance of "Person"
- And I set its dna to "ACGACACTGATGTCAT"
+
+ When I create an instance of "Sample"
+ And I set its dna to "<dna>"
Then the instance should be valid
- And the instance should save
+
+ Examples:
+ | dna |
+ | A |
+ | ACGT |
+ | ACGACACTGATGTCAT |
- Scenario: validation requirements failing
- When I create an instance of "Person"
- And I set its dna to "ABCDEFGHIJKLMNOP"
+ Scenario Outline: validation requirements failing (using :with)
+ Given the following document definition:
+ """
+ class Sample < Recliner::Document
+ property :dna, String
+ validates_format_of :dna, :with => /^[ACTG]+$/
+ end
+ """
+
+ When I create an instance of "Sample"
+ And I set its dna to "<dna>"
Then the instance should not be valid
- And the instance should not save
+ And its errors should include "Dna is invalid"
- When I save! the instance
- Then a "Recliner::DocumentInvalid" exception should be raised
+ Examples:
+ | dna |
+ | |
+ | ABCDEFGHIJKLMNOP |
+ | ACTG1 |
+
+ Scenario: validation requirements met (blank allowed)
+ Given the following document definition:
+ """
+ class Sample < Recliner::Document
+ property :dna, String
+ validates_format_of :dna, :with => /^[ACTG]+$/, :allow_blank => true
+ end
+ """
+
+ When I create an instance of "Sample"
+ And I set its dna to ""
+ Then the instance should be valid
+
+ Scenario: validation requirements met (nil allowed)
+ Given the following document definition:
+ """
+ class Sample < Recliner::Document
+ property :dna, String
+ validates_format_of :dna, :with => /^[ACTG]+$/, :allow_nil => true
+ end
+ """
+
+ When I create an instance of "Sample"
+ Then the instance should be valid
+
+ Scenario Outline: validation requirements met (using :without)
+ Given the following document definition:
+ """
+ class User < Recliner::Document
+ property :login, String
+ validates_format_of :login, :without => /\d /
+ end
+ """
+
+ When I create an instance of "User"
+ And I set its login to "<login>"
+ Then the instance should be valid
+
+ Examples:
+ | login |
+ | abc |
+ | test |
+ | Helloworld |
+
+ Scenario Outline: validation requirements failing (using :without)
+ Given the following document definition:
+ """
+ class User < Recliner::Document
+ property :login, String
+ validates_format_of :login, :without => /[\d ]/
+ end
+ """
+
+ When I create an instance of "User"
+ And I set its login to "<login>"
+ Then the instance should not be valid
+ And its errors should include "Login is invalid"
+
+ Examples:
+ | login |
+ | abc1 |
+ | 99test |
+ | Hello world |
+
View
60 features/validation/inclusion.feature
@@ -3,7 +3,7 @@ Feature: Validation: validates_inclusion_of
The validates_inclusion_of validation specifies that an
attribute must be one of a predefined set
- Background:
+ Scenario Outline: validation requirements met
Given the following document definition:
"""
class User < Recliner::Document
@@ -11,18 +11,58 @@ Feature: Validation: validates_inclusion_of
validates_inclusion_of :gender, :in => [ 'Male', 'Female' ]
end
"""
-
- Scenario: validation requirements met
+
When I create an instance of "User"
- And I set its gender to "Male"
+ And I set its gender to "<gender>"
Then the instance should be valid
- And the instance should save
+
+ Examples:
+ | gender |
+ | Male |
+ | Female |
- Scenario: validation requirements failing
+ Scenario Outline: validation requirements failing
+ Given the following document definition:
+ """
+ class User < Recliner::Document
+ property :gender, String
+ validates_inclusion_of :gender, :in => [ 'Male', 'Female' ]
+ end
+ """
+
When I create an instance of "User"
- And I set its gender to "Other"
+ And I set its gender to "<gender>"
Then the instance should not be valid
- And the instance should not save
+ And its errors should include "Gender is not included in the list"
- When I save! the instance
- Then a "Recliner::DocumentInvalid" exception should be raised
+ Examples:
+ | gender |
+ | |
+ | other |
+ | male |
+ | female |
+
+ Scenario: validation requirements met (blank allowed)
+ Given the following document definition:
+ """
+ class User < Recliner::Document
+ property :gender, String
+ validates_inclusion_of :gender, :in => [ 'Male', 'Female' ], :allow_blank => true
+ end
+ """
+
+ When I create an instance of "User"
+ And I set its gender to ""
+ Then the instance should be valid
+
+ Scenario: validation requirements met (nil allowed)
+ Given the following document definition:
+ """
+ class User < Recliner::Document
+ property :gender, String
+ validates_inclusion_of :gender, :in => [ 'Male', 'Female' ], :allow_nil => true
+ end
+ """
+
+ When I create an instance of "User"
+ Then the instance should be valid
View
76 features/validation/length.feature
@@ -3,26 +3,80 @@ Feature: Validation: validates_length_of
The validates_length_of validation specifies that the length of
an attribute must meet certain requirements
- Background:
+ Scenario Outline: validation requirements met
Given the following document definition:
"""
class User < Recliner::Document
+ @@word_tokenizer = lambda { |str| str.scan(/\w+/) }
+
property :login, String
- validates_length_of :login, :minimum => 4
+ validates_length_of :login, <options>
end
"""
-
- Scenario: validation requirements met
+
When I create an instance of "User"
- And I set its login to "validlogin"
+ And I set its login to "<login>"
Then the instance should be valid
- And the instance should save
+
+ Examples:
+ | options | login |
+ | :minimum => 4 | valid |
+ | :maximum => 6 | okay |
+ | :maximum => 8 | tothemax |
+ | :is => 8 | exactly8 |
+ | :within => 4..8 | four |
+ | :within => 4..8 | inside |
+ | :within => 4..8 | eightchs |
+ | :in => 4..8 | four |
+ | :in => 4..8 | inside |
+ | :in => 4..8 | eightchs |
+ | :is => 2, :tokenizer => @@word_tokenizer | two words |
- Scenario: validation requirements failing
+ Scenario Outline: validation requirements failing
+ Given the following document definition:
+ """
+ class User < Recliner::Document
+ @@word_tokenizer = lambda { |str| str.scan(/\w+/) }
+
+ property :login, String
+ validates_length_of :login, <options>
+ end
+ """
+
When I create an instance of "User"
- And I set its login to "abc"
+ And I set its login to "<login>"
Then the instance should not be valid
- And the instance should not save
+ And its errors should include "<error>"
- When I save! the instance
- Then a "Recliner::DocumentInvalid" exception should be raised
+ Examples:
+ | options | login | error |
+ | :minimum => 4 | abc | Login is too short (minimum is 4 characters) |
+ | :maximum => 6 | toolong | Login is too long (maximum is 6 characters) |
+ | :is => 8 | abcdefg | Login is the wrong length (should be 8 characters) |
+ | :is => 8 | abcdefghi | Login is the wrong length (should be 8 characters) |
+ | :within => 6..9 | short | Login is too short (minimum is 6 characters) |
+ | :within => 6..9 | waytoolong | Login is too long (maximum is 9 characters) |
+ | :in => 6..9 | short | Login is too short (minimum is 6 characters) |
+ | :in => 6..9 | waytoolong | Login is too long (maximum is 9 characters) |
+ | :is => 2, :tokenizer => @@word_tokenizer | more than two words | Login is the wrong length (should be 2 characters) |
+
+ Scenario Outline: validation requirements failing with custom messages
+ Given the following document definition:
+ """
+ class User < Recliner::Document
+ property :login, String
+ validates_length_of :login, <options>, <messages>
+ end
+ """
+
+ When I create an instance of "User"
+ And I set its login to "<login>"
+ Then the instance should not be valid
+ And its errors should include "<error>"
+
+ Examples:
+ | options | messages | login | error |
+ | :in => 4..6 | :too_short => "has gotta be longer" | abc | Login has gotta be longer |
+ | :in => 4..6 | :too_long => "is just too long" | toolong | Login is just too long |
+ | :is => 5 | :wrong_length => "must be 5 characters" | abc | Login must be 5 characters |
+ | :is => 5 | :message => "isn't right yet" | abc | Login isn't right yet |
View
56 features/validation/numericality.feature
@@ -3,26 +3,58 @@ Feature: Validation: validates_numericality_of
The validates_numericality_of validation specifies that
a numerical field must meet certain constraints
- Background:
+ Scenario Outline: validation requirements met
Given the following document definition:
"""
class Booking < Recliner::Document
property :number_of_people, Integer
- validates_numericality_of :number_of_people, :even => true
+ validates_numericality_of :number_of_people, <options>
end
"""
-
- Scenario: validation requirements met
+
When I create an instance of "Booking"
- And I set its number_of_people to "8"
+ And I set its number_of_people to "<number>"
Then the instance should be valid
- And the instance should save
+
+ Examples:
+ | options | number |
+ | :even => true | 6 |
+ | :odd => true | 5 |
+ | :only_integer => true | 2 |
+ | :greater_than => 100 | 101 |
+ | :greater_than_or_equal_to => 100 | 101 |
+ | :greater_than_or_equal_to => 100 | 100 |
+ | :less_than => 100 | 99 |
+ | :less_than_or_equal_to => 100 | 99 |
+ | :less_than_or_equal_to => 100 | 100 |
+ | :equal_to => 10 | 10 |
+ | :equal_to => 10 | 10.0 |
- Scenario: validation requirements failing
+ Scenario Outline: validation requirements failing
+ Given the following document definition:
+ """
+ class Booking < Recliner::Document
+ property :number_of_people, Integer
+ validates_numericality_of :number_of_people, <options>
+ end
+ """
+
When I create an instance of "Booking"
- And I set its number_of_people to "5"
+ And I set its number_of_people to "<number>"
Then the instance should not be valid
- And the instance should not save
-
- When I save! the instance
- Then a "Recliner::DocumentInvalid" exception should be raised
+ And its errors should include "<error>"
+
+ Examples:
+ | options | number | error |
+ | :odd => true | 6 | Number of people must be odd |
+ | :even => true | 5 | Number of people must be even |
+ | :only_integer => true | 2.5 | Number of people is not a number |
+ | :greater_than => 100 | 50 | Number of people must be greater than 100 |
+ | :greater_than => 100 | 100 | Number of people must be greater than 100 |
+ | :greater_than_or_equal_to => 100 | 99 | Number of people must be greater than or equal to 100 |
+ | :less_than => 100 | 101 | Number of people must be less than 100 |
+ | :less_than => 100 | 100 | Number of people must be less than 100 |
+ | :less_than_or_equal_to => 100 | 101 | Number of people must be less than or equal to 100 |
+ | :equal_to => 10 | 9 | Number of people must be equal to 10 |
+ | :equal_to => 10 | 10.001 | Number of people must be equal to 10 |
+
View
7 features/validation/presence.feature
@@ -16,13 +16,10 @@ Feature: Validation: validates_presence_of
When I create an instance of "Article"
And I set its title to "Article title"
Then the instance should be valid
- And the instance should save
Scenario: validation requirements failing
When I create an instance of "Article"
+ And I set its title to ""
Then the instance should not be valid
- And the instance should not save
-
- When I save! the instance
- Then a "Recliner::DocumentInvalid" exception should be raised
+ And its errors should include "Title can't be blank"
View
1  vendor/activesupport/lib/active_support/autoload.rb
@@ -7,6 +7,7 @@ module ActiveSupport
autoload :Callbacks, 'active_support/callbacks'
autoload :Concern, 'active_support/concern'
autoload :ConcurrentHash, 'active_support/concurrent_hash'
+ autoload :Dependencies, 'active_support/dependencies'
autoload :DependencyModule, 'active_support/dependency_module'
autoload :Deprecation, 'active_support/deprecation'
autoload :Gzip, 'active_support/gzip'
Please sign in to comment.
Something went wrong with that request. Please try again.