Permalink
Browse files

Better backporting of the ActiveModel tests

  • Loading branch information...
1 parent 1646648 commit a3854dc8660c68c62bf078fa979b7ce932106aaf @bcardarella committed Jun 23, 2011
@@ -41,7 +41,12 @@ module ClassMethods
MODULE
end
-require 'client_side_validations/rails_2/active_record/active_model/validations/numericality'
+%w{
+ length
+ numericality
+}.each do |validator|
+ require "client_side_validations/rails_2/active_record/active_model/validations/#{validator}"
+end
module ActiveModel::Validations::ClassMethods
def validates_size_of(*attr_names)
@@ -0,0 +1,4 @@
+class ActiveModel::Validations::LengthValidator
+ MESSAGES = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }.freeze
+end
+
@@ -11,7 +11,113 @@ def user.name
user.new
end
- def test_validations_to_client_side_hash
+ def test_acceptance_validation_to_client_side_hash
+ user = new_user do |p|
+ p.validates_acceptance_of :name
+ end
+ expected_hash = {
+ :name => {
+ :acceptance => {
+ :message => "must be accepted"
+ }
+ }
+ }
+ assert_equal expected_hash, user.client_side_validation_hash
+ end
+
+ def test_confirmation_validation_to_client_side_hash
+ user = new_user do |p|
+ p.validates_confirmation_of :name
+ end
+ expected_hash = {
+ :name => {
+ :confirmation => {
+ :message => "doesn't match confirmation"
+ }
+ }
+ }
+ assert_equal expected_hash, user.client_side_validation_hash
+ end
+
+ def test_exclusion_validation_to_client_side_hash
+ user = new_user do |p|
+ p.validates_exclusion_of :name, :in => %w{1}
+ end
+ expected_hash = {
+ :name => {
+ :exclusion => {
+ :message => "is reserved",
+ :in => ["1"]
+ }
+ }
+ }
+ assert_equal expected_hash, user.client_side_validation_hash
+ end
+
+ def test_format_validation_to_client_side_hash
+ user = new_user do |p|
+ p.validates_format_of :name, :with => /\.+/
+ end
+ expected_hash = {
+ :name => {
+ :format => {
+ :message => "is invalid",
+ :with => /\.+/
+ }
+ }
+ }
+ assert_equal expected_hash, user.client_side_validation_hash
+ end
+
+ def test_inclusion_validation_to_client_side_hash
+ user = new_user do |p|
+ p.validates_inclusion_of :name, :in => %w{1}
+ end
+ expected_hash = {
+ :name => {
+ :inclusion => {
+ :message => "is not included in the list",
+ :in => ["1"]
+ }
+ }
+ }
+ assert_equal expected_hash, user.client_side_validation_hash
+ end
+
+ def test_length_validation_to_client_side_hash
+ user = new_user do |p|
+ p.validates_length_of :name, :is => 5
+ end
+ expected_hash = {
+ :name => {
+ :length => {
+ :messages => {
+ :is => "is the wrong length (should be 5 characters)"
+ },
+ :is => 5
+ }
+ }
+ }
+ assert_equal expected_hash, user.client_side_validation_hash
+ end
+
+ def test_numericality_validation_to_client_side_hash
+ user = new_user do |p|
+ p.validates_numericality_of :name
+ end
+ expected_hash = {
+ :name => {
+ :numericality => {
+ :messages => {
+ :numericality => "is not a number"
+ }
+ }
+ }
+ }
+ assert_equal expected_hash, user.client_side_validation_hash
+ end
+
+ def test_presence_validation_to_client_side_hash
user = new_user do |p|
p.validates_presence_of :name
end
@@ -24,4 +130,19 @@ def test_validations_to_client_side_hash
}
assert_equal expected_hash, user.client_side_validation_hash
end
+
+ def test_uniqueness_validation_to_client_side_hash
+ user = new_user do |p|
+ p.validates_uniqueness_of :name
+ end
+ expected_hash = {
+ :name => {
+ :uniqueness => {
+ :message => "has already been taken",
+ :case_sensitive => nil
+ }
+ }
+ }
+ assert_equal expected_hash, user.client_side_validation_hash
+ end
end

0 comments on commit a3854dc

Please sign in to comment.