ramsingla-validatable 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. data/README.rdoc +123 -0
  2. data/Rakefile +54 -0
  3. data/VERSION.yml +4 -0
  4. data/lib/child_validation.rb +15 -0
  5. data/lib/errors.rb +105 -0
  6. data/lib/included_validation.rb +9 -0
  7. data/lib/macros.rb +284 -0
  8. data/lib/object_extension.rb +21 -0
  9. data/lib/requireable.rb +26 -0
  10. data/lib/understandable.rb +31 -0
  11. data/lib/validatable.rb +24 -0
  12. data/lib/validatable_class_methods.rb +85 -0
  13. data/lib/validatable_instance_methods.rb +116 -0
  14. data/lib/validations/validates_acceptance_of.rb +14 -0
  15. data/lib/validations/validates_confirmation_of.rb +23 -0
  16. data/lib/validations/validates_each.rb +14 -0
  17. data/lib/validations/validates_format_of.rb +16 -0
  18. data/lib/validations/validates_length_of.rb +30 -0
  19. data/lib/validations/validates_numericality_of.rb +27 -0
  20. data/lib/validations/validates_presence_of.rb +17 -0
  21. data/lib/validations/validates_true_for.rb +13 -0
  22. data/lib/validations/validation_base.rb +86 -0
  23. data/test/all_tests.rb +1 -0
  24. data/test/functional/test_validatable.rb +553 -0
  25. data/test/functional/test_validates_acceptance_of.rb +16 -0
  26. data/test/functional/test_validates_confirmation_of.rb +56 -0
  27. data/test/functional/test_validates_each.rb +14 -0
  28. data/test/functional/test_validates_format_of.rb +34 -0
  29. data/test/functional/test_validates_length_of.rb +64 -0
  30. data/test/functional/test_validates_numericality_of.rb +57 -0
  31. data/test/functional/test_validates_presence_of.rb +16 -0
  32. data/test/functional/test_validates_true_for.rb +27 -0
  33. data/test/test_helper.rb +35 -0
  34. data/test/unit/test_errors.rb +64 -0
  35. data/test/unit/test_understandable.rb +19 -0
  36. data/test/unit/test_validatable.rb +38 -0
  37. data/test/unit/test_validates_acceptance_of.rb +45 -0
  38. data/test/unit/test_validates_confirmation_of.rb +76 -0
  39. data/test/unit/test_validates_format_of.rb +44 -0
  40. data/test/unit/test_validates_length_of.rb +80 -0
  41. data/test/unit/test_validates_numericality_of.rb +76 -0
  42. data/test/unit/test_validates_presence_of.rb +35 -0
  43. data/test/unit/test_validates_true_for.rb +29 -0
  44. data/test/unit/test_validation_base.rb +52 -0
  45. metadata +119 -0
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ module Functional
4
+ class ValidatesLengthOfTest < Test::Unit::TestCase
5
+ test "given short value, when validated, then error is in the objects error collection" do
6
+ klass = Class.new do
7
+ include Validatable
8
+ attr_accessor :name
9
+ validates_length_of :name, :minimum => 2
10
+ end
11
+ instance = klass.new
12
+ instance.valid?
13
+ assert_equal Validatable::Errors::MessageCodeFor[:length], instance.errors.on(:name)
14
+ end
15
+
16
+ test "given is constraint, when validated, then error is in the objects error collection" do
17
+ klass = Class.new do
18
+ include Validatable
19
+ attr_accessor :name
20
+ validates_length_of :name, :is => 2
21
+ end
22
+
23
+ instance = klass.new
24
+ instance.valid?
25
+ assert_equal Validatable::Errors::MessageCodeFor[:length], instance.errors.on(:name)
26
+ end
27
+
28
+ test "given is constraint is met, when validated, then valid is true" do
29
+ klass = Class.new do
30
+ include Validatable
31
+ attr_accessor :name
32
+ validates_length_of :name, :is => 2
33
+ end
34
+
35
+ instance = klass.new
36
+ instance.name = "bk"
37
+ assert_equal true, instance.valid?
38
+ end
39
+
40
+ test "given within constraint, when validated, then error is in the objects error collection" do
41
+ klass = Class.new do
42
+ include Validatable
43
+ attr_accessor :name
44
+ validates_length_of :name, :within => 2..4
45
+ end
46
+
47
+ instance = klass.new
48
+ instance.valid?
49
+ assert_equal Validatable::Errors::MessageCodeFor[:length], instance.errors.on(:name)
50
+ end
51
+
52
+ test "given within constraint, when validated, then valid is true" do
53
+ klass = Class.new do
54
+ include Validatable
55
+ attr_accessor :name
56
+ validates_length_of :name, :within => 2..4
57
+ end
58
+
59
+ instance = klass.new
60
+ instance.name = "bk"
61
+ assert_equal true, instance.valid?
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ module Functional
4
+ class ValidatesNumericalityOfTest < Test::Unit::TestCase
5
+ test "when validating numericality and the value is nil an error should exist on the instance" do
6
+ klass = Class.new do
7
+ include Validatable
8
+ attr_accessor :nothing
9
+ validates_numericality_of :nothing
10
+ end
11
+ instance = klass.new
12
+ instance.valid?
13
+ assert_equal Validatable::Errors::MessageCodeFor[:numeric], instance.errors.on(:nothing)
14
+ end
15
+
16
+ test "when validating numericality and the value has a non numeric character an error should exist on the instance" do
17
+ klass = Class.new do
18
+ include Validatable
19
+ validates_numericality_of :some_string
20
+
21
+ def some_string
22
+ "some_string"
23
+ end
24
+ end
25
+ instance = klass.new
26
+ instance.valid?
27
+ assert_equal Validatable::Errors::MessageCodeFor[:numeric], instance.errors.on(:some_string)
28
+ end
29
+
30
+ test "when validating a number no error will be in the instance" do
31
+ klass = Class.new do
32
+ include Validatable
33
+ validates_numericality_of :valid_number
34
+
35
+ def valid_number
36
+ 1.23
37
+ end
38
+ end
39
+ instance = klass.new
40
+ instance.valid?
41
+ assert_equal nil, instance.errors.on(:valid_number)
42
+ end
43
+
44
+ test "when validating an integer and the value is a decimal an error should exist on the instance" do
45
+ klass = Class.new do
46
+ include Validatable
47
+ validates_numericality_of :valid_number, :only_integer => true
48
+ attr_accessor :valid_number
49
+ end
50
+
51
+ instance = klass.new
52
+ instance.valid_number = 1.23
53
+ instance.valid?
54
+ assert_equal Validatable::Errors::MessageCodeFor[:numeric], instance.errors.on(:valid_number)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,16 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ module Functional
4
+ class ValidatesPresenceOfTest < Test::Unit::TestCase
5
+ test "given no name, when validated, then error is in the objects error collection" do
6
+ klass = Class.new do
7
+ include Validatable
8
+ attr_accessor :name
9
+ validates_presence_of :name
10
+ end
11
+ instance = klass.new
12
+ instance.valid?
13
+ assert_equal Validatable::Errors::MessageCodeFor[:required], instance.errors.on(:name)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ module Functional
4
+ class ValidatesFormatOfTest < Test::Unit::TestCase
5
+ test "given invalid name, when validated, then error is in the objects error collection" do
6
+ klass = Class.new do
7
+ include Validatable
8
+ attr_accessor :name
9
+ validates_true_for :name, :logic => lambda { name == "nombre" }
10
+ end
11
+ instance = klass.new
12
+ instance.valid?
13
+ assert_equal Validatable::Errors::MessageCodeFor[:logic], instance.errors.on(:name)
14
+ end
15
+
16
+ test "given valid name, when validated, then no error is in the objects error collection" do
17
+ klass = Class.new do
18
+ include Validatable
19
+ attr_accessor :name
20
+ validates_true_for :name, :logic => lambda { name == "nombre" }
21
+ end
22
+ instance = klass.new
23
+ instance.name = "nombre"
24
+ assert_equal true, instance.valid?
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ gem 'test-unit'
3
+
4
+ require 'test/unit'
5
+ require 'mocha'
6
+ require 'dust'
7
+ require 'set'
8
+ require 'expectations'
9
+
10
+ require File.dirname(__FILE__) + '/../lib/validatable'
11
+
12
+ class << Test::Unit::TestCase
13
+ def expect(expected_value, &block)
14
+ define_method :"test_#{caller.first.split("/").last}" do
15
+ begin
16
+ assert_equal expected_value, instance_eval(&block)
17
+ rescue Exception => ex
18
+ raise ex unless expected_value.is_a?(Class) && ex.is_a?(expected_value)
19
+ assert_equal expected_value, ex.class
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ class Test::Unit::TestCase
26
+ def assert_array_equal a, b
27
+ assert_equal Set.new(a), Set.new(b)
28
+ end
29
+ end
30
+
31
+ class Array
32
+ def to_blank_options_hash
33
+ self.inject({}) {|hash, value| hash[value] = nil; hash }
34
+ end
35
+ end
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect "message" do
5
+ errors = Validatable::Errors.new
6
+ errors.add(:attribute, "message")
7
+ errors.on(:attribute)
8
+ end
9
+
10
+ expect ["message"] do
11
+ errors = Validatable::Errors.new
12
+ errors.add(:attribute, "message")
13
+ errors.raw(:attribute)
14
+ end
15
+
16
+ expect "something new" do
17
+ errors = Validatable::Errors.new
18
+ errors.add(:attribute, "something old")
19
+ errors.replace(:attribute, ["something new"])
20
+ errors.on(:attribute)
21
+ end
22
+
23
+ expect "Capitalized word" do
24
+ errors = Validatable::Errors.new
25
+ errors.humanize("capitalized_word")
26
+ end
27
+
28
+ expect "Capitalized word without" do
29
+ errors = Validatable::Errors.new
30
+ errors.humanize("capitalized_word_without_id")
31
+ end
32
+
33
+ expect ["A humanized message", "a base message"] do
34
+ errors = Validatable::Errors.new
35
+ errors.add(:base, "a base message")
36
+ errors.add(:a_humanized, "message")
37
+ errors.full_messages.sort
38
+ end
39
+
40
+ expect true do
41
+ Validatable::Errors.included_modules.include?(Enumerable)
42
+ end
43
+
44
+ expect ["message1", "message2"] do
45
+ errors = Validatable::Errors.new
46
+ errors.add(:attribute, "message1")
47
+ errors.add(:attribute, "message2")
48
+ errors.on(:attribute)
49
+ end
50
+
51
+ expect 2 do
52
+ errors = Validatable::Errors.new
53
+ errors.add(:attribute, "message1")
54
+ errors.add(:attribute, "message2")
55
+ errors.count
56
+ end
57
+
58
+ expect 2 do
59
+ errors = Validatable::Errors.new
60
+ errors.add(:attribute1, "message1")
61
+ errors.add(:attribute2, "message2")
62
+ errors.count
63
+ end
64
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect [:c, :b, :a] do
5
+ a = Class.new do
6
+ include Validatable::Understandable
7
+ understands :a
8
+ end
9
+ b = Class.new(a) do
10
+ include Validatable::Understandable
11
+ understands :b
12
+ end
13
+ c = Class.new(b) do
14
+ include Validatable::Understandable
15
+ understands :c
16
+ end
17
+ c.all_understandings
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect false do
5
+ validation = stub_everything(:should_validate? => true, :attribute => "attribute", :level => 1, :groups => [])
6
+ klass = Class.new do
7
+ include Validatable
8
+ validations << validation
9
+ end
10
+ klass.new.valid?
11
+ end
12
+
13
+ expect true do
14
+ klass = Class.new do
15
+ include Validatable
16
+ end
17
+ instance = klass.new
18
+ instance.errors.add(:attribute, "message")
19
+ instance.valid?
20
+ instance.errors.empty?
21
+ end
22
+
23
+ expect false do
24
+ klass = Class.new do
25
+ include Validatable
26
+ end
27
+ klass.validation_keys_include?("anything")
28
+ end
29
+
30
+ expect true do
31
+ validation = stub_everything(:key => "key")
32
+ klass = Class.new do
33
+ include Validatable
34
+ validations << validation
35
+ end
36
+ klass.validation_keys_include?("key")
37
+ end
38
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect true do
5
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance
6
+ instance = stub(:acceptance=>'1')
7
+ validation.valid?(instance)
8
+ end
9
+
10
+ expect true do
11
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance
12
+ instance = stub(:acceptance=>'t')
13
+ validation.valid?(instance)
14
+ end
15
+
16
+ expect true do
17
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance
18
+ instance = stub(:acceptance=>'true')
19
+ validation.valid?(instance)
20
+ end
21
+
22
+ expect true do
23
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance, :allow_nil => true
24
+ instance = stub(:acceptance=>nil)
25
+ validation.valid?(instance)
26
+ end
27
+
28
+ expect true do
29
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance, :allow_blank => true
30
+ instance = stub(:acceptance=>'')
31
+ validation.valid?(instance)
32
+ end
33
+
34
+ expect false do
35
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance
36
+ instance = stub(:acceptance=>'0')
37
+ validation.valid?(instance)
38
+ end
39
+
40
+ expect true do
41
+ options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil}
42
+ Validatable::ValidatesAcceptanceOf.new(stub_everything, :test).must_understand(options)
43
+ end
44
+
45
+ end
@@ -0,0 +1,76 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+
5
+ expect true do
6
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username
7
+ instance = stub(:username=>"username", :username_confirmation=>"username")
8
+ validation.valid?(instance)
9
+ end
10
+
11
+ expect false do
12
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username
13
+ instance = stub(:username=>"username", :username_confirmation=>"usessrname")
14
+ validation.valid?(instance)
15
+ end
16
+
17
+ expect true do
18
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :allow_nil => true
19
+ instance = stub(:username=>"username", :username_confirmation=>nil)
20
+ validation.valid?(instance)
21
+ end
22
+
23
+ expect true do
24
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :allow_blank => true
25
+ instance = stub(:username=>"username", :username_confirmation=>'')
26
+ validation.valid?(instance)
27
+ end
28
+
29
+ expect true do
30
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => false
31
+ instance = stub(:username=>"username", :username_confirmation=>"USERNAME")
32
+ validation.valid?(instance)
33
+ end
34
+
35
+ expect false do
36
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => true
37
+ instance = stub(:username=>"username", :username_confirmation=>"USERNAME")
38
+ validation.valid?(instance)
39
+ end
40
+
41
+ expect false do
42
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => true
43
+ validation.valid?(stub(:username => nil, :username_confirmation => 'something'))
44
+ end
45
+
46
+ expect false do
47
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => true
48
+ validation.valid?(stub(:username => 'something', :username_confirmation => nil))
49
+ end
50
+
51
+ expect true do
52
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => true
53
+ validation.valid?(stub(:username => nil, :username_confirmation => nil))
54
+ end
55
+
56
+ expect false do
57
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => false
58
+ validation.valid?(stub(:username => nil, :username_confirmation => 'something'))
59
+ end
60
+
61
+ expect false do
62
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => false
63
+ validation.valid?(stub(:username => 'something', :username_confirmation => nil))
64
+ end
65
+
66
+ expect true do
67
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => false
68
+ validation.valid?(stub(:username => nil, :username_confirmation => nil))
69
+ end
70
+
71
+ expect true do
72
+ options = { :message => nil, :if => nil, :times => nil, :level => nil, :groups => nil, :case_sensitive => nil }
73
+ Validatable::ValidatesConfirmationOf.new(stub_everything, :test).must_understand(options)
74
+ end
75
+
76
+ end
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+
5
+ expect false do
6
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :name, :with => /book/
7
+ validation.valid?(stub_everything)
8
+ end
9
+
10
+ expect true do
11
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :name, :with => /book/
12
+ validation.valid?(stub(:name=>"book"))
13
+ end
14
+
15
+ expect true do
16
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :name, :with => /book/, :allow_nil => true
17
+ validation.valid?(stub(:name=>nil))
18
+ end
19
+
20
+ expect true do
21
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :name, :with => /book/, :allow_blank => true
22
+ validation.valid?(stub(:name=>''))
23
+ end
24
+
25
+ expect true do
26
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :age, :with => /14/
27
+ validation.valid?(stub(:age=>14))
28
+ end
29
+
30
+ expect ArgumentError do
31
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :age
32
+ end
33
+
34
+ expect true do
35
+ options = [:message, :if, :times, :level, :groups, :with, :key]
36
+ Validatable::ValidatesFormatOf.new(stub_everything, :test, options.to_blank_options_hash).must_understand(options.to_blank_options_hash)
37
+ end
38
+
39
+ expect true do
40
+ options = [:with]
41
+ Validatable::ValidatesFormatOf.new(stub_everything, :name, options.to_blank_options_hash).requires(options.to_blank_options_hash)
42
+ end
43
+
44
+ end
@@ -0,0 +1,80 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect false do
5
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :maximum => 8
6
+ validation.valid?(stub(:username=>"usernamefdfd"))
7
+ end
8
+
9
+ expect false do
10
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :minimum => 2
11
+ instance = stub(:username=>"u")
12
+ validation.valid?(instance)
13
+ end
14
+
15
+ expect true do
16
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :minimum => 2, :maximum => 8
17
+ instance = stub(:username=>"udfgdf")
18
+ validation.valid?(instance)
19
+ end
20
+
21
+ expect false do
22
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :is => 2
23
+ instance = stub(:username=>"u")
24
+ validation.valid?(instance)
25
+ end
26
+
27
+ expect true do
28
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :is => 2
29
+ instance = stub(:username=>"uu")
30
+ validation.valid?(instance)
31
+ end
32
+
33
+ expect true do
34
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4
35
+ instance = stub(:username => "aa")
36
+ validation.valid?(instance)
37
+ end
38
+
39
+ expect false do
40
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4
41
+ instance = stub(:username => "a")
42
+ validation.valid?(instance)
43
+ end
44
+
45
+ expect true do
46
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4
47
+ instance = stub(:username => "aaaa")
48
+ validation.valid?(instance)
49
+ end
50
+
51
+ expect false do
52
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4
53
+ instance = stub(:username => "aaaaa")
54
+ validation.valid?(instance)
55
+ end
56
+
57
+ expect false do
58
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4
59
+ instance = stub(:username => nil)
60
+ validation.valid?(instance)
61
+ end
62
+
63
+ expect true do
64
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4, :allow_nil => true
65
+ instance = stub(:username => nil)
66
+ validation.valid?(instance)
67
+ end
68
+
69
+ expect true do
70
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4, :allow_blank => true
71
+ instance = stub(:username => '')
72
+ validation.valid?(instance)
73
+ end
74
+
75
+ expect true do
76
+ options = [:message, :if, :times, :level, :groups, :maximum, :minimum, :is, :within, :allow_nil]
77
+ Validatable::ValidatesLengthOf.new(stub_everything, :test).must_understand(options.to_blank_options_hash)
78
+ end
79
+
80
+ end
@@ -0,0 +1,76 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+
5
+ expect false do
6
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :nothing
7
+ instance = stub(:nothing => nil)
8
+ validation.valid?(instance)
9
+ end
10
+
11
+ expect true do
12
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_int
13
+ instance = stub(:some_int => 50)
14
+ validation.valid?(instance)
15
+ end
16
+
17
+ expect false do
18
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_int
19
+ instance = stub(:some_int => 0, :some_int_before_typecast => 'string')
20
+ validation.valid?(instance)
21
+ end
22
+
23
+ expect true do
24
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_int
25
+ instance = stub(:some_int => 21, :some_int_before_typecast => '21')
26
+ validation.valid?(instance)
27
+ end
28
+
29
+ expect true do
30
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_int, :allow_nil => true
31
+ instance = stub(:some_int => nil)
32
+ validation.valid?(instance)
33
+ end
34
+
35
+ expect true do
36
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_int, :allow_blank => true
37
+ instance = stub(:some_int => '')
38
+ validation.valid?(instance)
39
+ end
40
+
41
+ expect true do
42
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_decimal
43
+ instance = stub(:some_decimal => 1.23)
44
+ validation.valid?(instance)
45
+ end
46
+
47
+ expect false do
48
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_decimal, :only_integer => true
49
+ instance = stub(:some_decimal => 1.23)
50
+ validation.valid?(instance)
51
+ end
52
+
53
+ expect true do
54
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_negative_number, :only_integer => true
55
+ instance = stub(:some_negative_number => "-1")
56
+ validation.valid?(instance)
57
+ end
58
+
59
+ expect false do
60
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_non_numeric
61
+ instance = stub(:some_non_numeric => "50F")
62
+ validation.valid?(instance)
63
+ end
64
+
65
+ expect false do
66
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :multiple_dots
67
+ instance = stub(:multiple_dots => "50.0.0")
68
+ validation.valid?(instance)
69
+ end
70
+
71
+ expect true do
72
+ options = [:message, :if, :times, :level, :groups, :only_integer]
73
+ Validatable::ValidatesNumericalityOf.new(stub_everything, :test).must_understand(options.to_blank_options_hash)
74
+ end
75
+
76
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+
5
+ expect false do
6
+ validation = Validatable::ValidatesPresenceOf.new stub_everything, :name
7
+ validation.valid?(stub_everything)
8
+ end
9
+
10
+ expect true do
11
+ validation = Validatable::ValidatesPresenceOf.new stub_everything, :name
12
+ validation.valid?(stub(:name=>"book"))
13
+ end
14
+
15
+ expect true do
16
+ validation = Validatable::ValidatesPresenceOf.new stub_everything, :name, :allow_nil => true
17
+ validation.valid?(stub(:name=>nil))
18
+ end
19
+
20
+ expect true do
21
+ validation = Validatable::ValidatesPresenceOf.new stub_everything, :name, :allow_blank => true
22
+ validation.valid?(stub(:name=>''))
23
+ end
24
+
25
+ expect true do
26
+ validation = Validatable::ValidatesPresenceOf.new stub_everything, :employee
27
+ validation.valid?(stub(:employee => stub(:nil? => false)))
28
+ end
29
+
30
+ expect true do
31
+ options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil}
32
+ Validatable::ValidatesPresenceOf.new(stub_everything, :test).must_understand(options)
33
+ end
34
+
35
+ end