rspec_sequel_matchers 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.document +5 -0
  2. data/.gitignore +5 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +47 -0
  5. data/Rakefile +46 -0
  6. data/lib/rspec_sequel/association.rb +30 -0
  7. data/lib/rspec_sequel/base.rb +33 -0
  8. data/lib/rspec_sequel/matchers/have_column.rb +35 -0
  9. data/lib/rspec_sequel/matchers/have_many_to_many.rb +22 -0
  10. data/lib/rspec_sequel/matchers/have_many_to_one.rb +22 -0
  11. data/lib/rspec_sequel/matchers/have_one_to_many.rb +22 -0
  12. data/lib/rspec_sequel/matchers/validate_exact_length.rb +25 -0
  13. data/lib/rspec_sequel/matchers/validate_format.rb +25 -0
  14. data/lib/rspec_sequel/matchers/validate_includes.rb +25 -0
  15. data/lib/rspec_sequel/matchers/validate_integer.rb +21 -0
  16. data/lib/rspec_sequel/matchers/validate_length_range.rb +25 -0
  17. data/lib/rspec_sequel/matchers/validate_max_length.rb +25 -0
  18. data/lib/rspec_sequel/matchers/validate_min_length.rb +25 -0
  19. data/lib/rspec_sequel/matchers/validate_not_string.rb +21 -0
  20. data/lib/rspec_sequel/matchers/validate_numeric.rb +21 -0
  21. data/lib/rspec_sequel/matchers/validate_presence.rb +21 -0
  22. data/lib/rspec_sequel/matchers/validate_unique.rb +33 -0
  23. data/lib/rspec_sequel/validation.rb +77 -0
  24. data/lib/rspec_sequel_matchers.rb +9 -0
  25. data/rspec_sequel_matchers.gemspec +107 -0
  26. data/spec/have_column_matcher_spec.rb +89 -0
  27. data/spec/have_many_to_many_matcher_spec.rb +56 -0
  28. data/spec/have_many_to_one_matcher_spec.rb +55 -0
  29. data/spec/have_one_to_many_matcher_spec.rb +55 -0
  30. data/spec/migrations/001_create_items.rb +15 -0
  31. data/spec/migrations/002_create_comments.rb +17 -0
  32. data/spec/migrations/003_create_comments_items.rb +15 -0
  33. data/spec/spec_helper.rb +51 -0
  34. data/spec/validate_exact_length_matcher_spec.rb +88 -0
  35. data/spec/validate_format_matcher_spec.rb +88 -0
  36. data/spec/validate_includes_matcher_spec.rb +88 -0
  37. data/spec/validate_integer_matcher_spec.rb +77 -0
  38. data/spec/validate_length_range_matcher_spec.rb +88 -0
  39. data/spec/validate_max_length_matcher_spec.rb +88 -0
  40. data/spec/validate_min_length_matcher_spec.rb +88 -0
  41. data/spec/validate_not_string_matcher_spec.rb +77 -0
  42. data/spec/validate_numeric_matcher_spec.rb +77 -0
  43. data/spec/validate_presence_matcher_spec.rb +77 -0
  44. data/spec/validate_unique_matcher_spec.rb +79 -0
  45. metadata +153 -0
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe "validate_includes_matcher" do
4
+
5
+ before do
6
+ define_model :item do
7
+ plugin :validation_helpers
8
+ def validate
9
+ validates_includes ["Joseph", "Jonathan"], :name, :allow_nil => true
10
+ end
11
+ end
12
+ end
13
+
14
+ subject{ Item }
15
+
16
+ describe "arguments" do
17
+ it "should require attribute" do
18
+ lambda{
19
+ @matcher = validate_includes
20
+ }.should raise_error(ArgumentError)
21
+ end
22
+ it "should require additionnal parameters" do
23
+ lambda{
24
+ @matcher = validate_includes :name
25
+ }.should raise_error(ArgumentError)
26
+ end
27
+ it "should refuse invalid additionnal parameters" do
28
+ lambda{
29
+ @matcher = validate_includes :id, :name
30
+ }.should raise_error(ArgumentError)
31
+ end
32
+ it "should accept valid additionnal parameters" do
33
+ lambda{
34
+ @matcher = validate_includes ["Joseph", "Jonathan"], :name
35
+ }.should_not raise_error(ArgumentError)
36
+ end
37
+ end
38
+
39
+ describe "messages" do
40
+ describe "without option" do
41
+ it "should contain a description" do
42
+ @matcher = validate_includes ["Joseph", "Jonathan"], :name
43
+ @matcher.description.should == 'validate that :name is included in ["Joseph", "Jonathan"]'
44
+ end
45
+ it "should set failure messages" do
46
+ @matcher = validate_includes ["Joseph", "Jonathan"], :name
47
+ @matcher.matches? subject
48
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
49
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
50
+ end
51
+ end
52
+ describe "with options" do
53
+ it "should contain a description" do
54
+ @matcher = validate_includes ["Joseph", "Jonathan"], :name, :allow_nil => true
55
+ @matcher.description.should == 'validate that :name is included in ["Joseph", "Jonathan"] with option(s) :allow_nil => true'
56
+ end
57
+ it "should set failure messages" do
58
+ @matcher = validate_includes ["Joseph", "Jonathan"], :price, :allow_nil => true
59
+ @matcher.matches? subject
60
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
61
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
62
+ end
63
+ it "should explicit used options if different than expected" do
64
+ @matcher = validate_includes ["Joseph", "Jonathan"], :name, :allow_blank => true
65
+ @matcher.matches? subject
66
+ explanation = " but called with option(s) :allow_nil => true instead"
67
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
68
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
69
+ end
70
+ it "should warn if invalid options are used" do
71
+ @matcher = validate_includes ["Joseph", "Jonathan"], :name, :allow_anything => true
72
+ @matcher.matches? subject
73
+ explanation = " but option :allow_anything is not valid"
74
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
75
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "matchers" do
81
+ it{ should validate_includes(["Joseph", "Jonathan"], :name) }
82
+ it{ should validate_includes(["Joseph", "Jonathan"], :name, :allow_nil => true) }
83
+ it{ should_not validate_includes(["Joseph", "Jonathan"], :price) }
84
+ it{ should_not validate_includes(["Joseph", "Jonathan", "Alice"], :name) }
85
+ it{ should_not validate_includes(["Joseph", "Jonathan"], :name, :allow_blank => true) }
86
+ end
87
+
88
+ end
@@ -0,0 +1,77 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe "validate_integer_matcher" do
4
+
5
+ before do
6
+ define_model :item do
7
+ plugin :validation_helpers
8
+ def validate
9
+ validates_integer [:id, :name], :allow_nil => true
10
+ end
11
+ end
12
+ end
13
+
14
+ subject{ Item }
15
+
16
+ describe "arguments" do
17
+ it "should require attribute" do
18
+ lambda{
19
+ @matcher = validate_integer
20
+ }.should raise_error(ArgumentError)
21
+ end
22
+ it "should refuse additionnal parameters" do
23
+ lambda{
24
+ @matcher = validate_integer :name, :id
25
+ }.should raise_error(ArgumentError)
26
+ end
27
+ end
28
+
29
+ describe "messages" do
30
+ describe "without option" do
31
+ it "should contain a description" do
32
+ @matcher = validate_integer :name
33
+ @matcher.description.should == "validate that :name is a valid integer"
34
+ end
35
+ it "should set failure messages" do
36
+ @matcher = validate_integer :name
37
+ @matcher.matches? subject
38
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
39
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
40
+ end
41
+ end
42
+ describe "with options" do
43
+ it "should contain a description" do
44
+ @matcher = validate_integer :name, :allow_nil => true
45
+ @matcher.description.should == "validate that :name is a valid integer with option(s) :allow_nil => true"
46
+ end
47
+ it "should set failure messages" do
48
+ @matcher = validate_integer :price, :allow_nil => true
49
+ @matcher.matches? subject
50
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
51
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
52
+ end
53
+ it "should explicit used options if different than expected" do
54
+ @matcher = validate_integer :name, :allow_blank => true
55
+ @matcher.matches? subject
56
+ explanation = " but called with option(s) :allow_nil => true instead"
57
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
58
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
59
+ end
60
+ it "should warn if invalid options are used" do
61
+ @matcher = validate_integer :name, :allow_anything => true
62
+ @matcher.matches? subject
63
+ explanation = " but option :allow_anything is not valid"
64
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
65
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "matchers" do
71
+ it{ should validate_integer(:name) }
72
+ it{ should validate_integer(:name, :allow_nil => true) }
73
+ it{ should_not validate_integer(:price) }
74
+ it{ should_not validate_integer(:name, :allow_blank => true) }
75
+ end
76
+
77
+ end
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe "validate_length_range_matcher" do
4
+
5
+ before do
6
+ define_model :item do
7
+ plugin :validation_helpers
8
+ def validate
9
+ validates_length_range 1..10, :name, :allow_nil => true
10
+ end
11
+ end
12
+ end
13
+
14
+ subject{ Item }
15
+
16
+ describe "arguments" do
17
+ it "should require attribute" do
18
+ lambda{
19
+ @matcher = validate_length_range
20
+ }.should raise_error(ArgumentError)
21
+ end
22
+ it "should require additionnal parameters" do
23
+ lambda{
24
+ @matcher = validate_length_range :name
25
+ }.should raise_error(ArgumentError)
26
+ end
27
+ it "should refuse invalid additionnal parameters" do
28
+ lambda{
29
+ @matcher = validate_length_range :id, :name
30
+ }.should raise_error(ArgumentError)
31
+ end
32
+ it "should accept valid additionnal parameters" do
33
+ lambda{
34
+ @matcher = validate_length_range 1..10, :name
35
+ }.should_not raise_error(ArgumentError)
36
+ end
37
+ end
38
+
39
+ describe "messages" do
40
+ describe "without option" do
41
+ it "should contain a description" do
42
+ @matcher = validate_length_range 1..10, :name
43
+ @matcher.description.should == "validate length of :name is included in 1..10"
44
+ end
45
+ it "should set failure messages" do
46
+ @matcher = validate_length_range 1..10, :name
47
+ @matcher.matches? subject
48
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
49
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
50
+ end
51
+ end
52
+ describe "with options" do
53
+ it "should contain a description" do
54
+ @matcher = validate_length_range 1..10, :name, :allow_nil => true
55
+ @matcher.description.should == "validate length of :name is included in 1..10 with option(s) :allow_nil => true"
56
+ end
57
+ it "should set failure messages" do
58
+ @matcher = validate_length_range 1..10, :price, :allow_nil => true
59
+ @matcher.matches? subject
60
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
61
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
62
+ end
63
+ it "should explicit used options if different than expected" do
64
+ @matcher = validate_length_range 1..10, :name, :allow_blank => true
65
+ @matcher.matches? subject
66
+ explanation = " but called with option(s) :allow_nil => true instead"
67
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
68
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
69
+ end
70
+ it "should warn if invalid options are used" do
71
+ @matcher = validate_length_range 1..10, :name, :allow_anything => true
72
+ @matcher.matches? subject
73
+ explanation = " but option :allow_anything is not valid"
74
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
75
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "matchers" do
81
+ it{ should validate_length_range(1..10, :name) }
82
+ it{ should validate_length_range(1..10, :name, :allow_nil => true) }
83
+ it{ should_not validate_length_range(1..10, :price) }
84
+ it{ should_not validate_length_range(0..10, :name) }
85
+ it{ should_not validate_length_range(1..10, :name, :allow_blank => true) }
86
+ end
87
+
88
+ end
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe "validate_max_length_matcher" do
4
+
5
+ before do
6
+ define_model :item do
7
+ plugin :validation_helpers
8
+ def validate
9
+ validates_max_length 4, :name, :allow_nil => true
10
+ end
11
+ end
12
+ end
13
+
14
+ subject{ Item }
15
+
16
+ describe "arguments" do
17
+ it "should require attribute" do
18
+ lambda{
19
+ @matcher = validate_max_length
20
+ }.should raise_error(ArgumentError)
21
+ end
22
+ it "should require additionnal parameters" do
23
+ lambda{
24
+ @matcher = validate_max_length :name
25
+ }.should raise_error(ArgumentError)
26
+ end
27
+ it "should refuse invalid additionnal parameters" do
28
+ lambda{
29
+ @matcher = validate_max_length :id, :name
30
+ }.should raise_error(ArgumentError)
31
+ end
32
+ it "should accept valid additionnal parameters" do
33
+ lambda{
34
+ @matcher = validate_max_length 4, :name
35
+ }.should_not raise_error(ArgumentError)
36
+ end
37
+ end
38
+
39
+ describe "messages" do
40
+ describe "without option" do
41
+ it "should contain a description" do
42
+ @matcher = validate_max_length 4, :name
43
+ @matcher.description.should == "validate length of :name is less than or equal to 4"
44
+ end
45
+ it "should set failure messages" do
46
+ @matcher = validate_max_length 4, :name
47
+ @matcher.matches? subject
48
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
49
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
50
+ end
51
+ end
52
+ describe "with options" do
53
+ it "should contain a description" do
54
+ @matcher = validate_max_length 4, :name, :allow_nil => true
55
+ @matcher.description.should == "validate length of :name is less than or equal to 4 with option(s) :allow_nil => true"
56
+ end
57
+ it "should set failure messages" do
58
+ @matcher = validate_max_length 4, :price, :allow_nil => true
59
+ @matcher.matches? subject
60
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
61
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
62
+ end
63
+ it "should explicit used options if different than expected" do
64
+ @matcher = validate_max_length 4, :name, :allow_blank => true
65
+ @matcher.matches? subject
66
+ explanation = " but called with option(s) :allow_nil => true instead"
67
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
68
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
69
+ end
70
+ it "should warn if invalid options are used" do
71
+ @matcher = validate_max_length 4, :name, :allow_anything => true
72
+ @matcher.matches? subject
73
+ explanation = " but option :allow_anything is not valid"
74
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
75
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "matchers" do
81
+ it{ should validate_max_length(4, :name) }
82
+ it{ should validate_max_length(4, :name, :allow_nil => true) }
83
+ it{ should_not validate_max_length(4, :price) }
84
+ it{ should_not validate_max_length(3, :name) }
85
+ it{ should_not validate_max_length(4, :name, :allow_blank => true) }
86
+ end
87
+
88
+ end
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe "validate_min_length_matcher" do
4
+
5
+ before do
6
+ define_model :item do
7
+ plugin :validation_helpers
8
+ def validate
9
+ validates_min_length 4, :name, :allow_nil => true
10
+ end
11
+ end
12
+ end
13
+
14
+ subject{ Item }
15
+
16
+ describe "arguments" do
17
+ it "should require attribute" do
18
+ lambda{
19
+ @matcher = validate_min_length
20
+ }.should raise_error(ArgumentError)
21
+ end
22
+ it "should require additionnal parameters" do
23
+ lambda{
24
+ @matcher = validate_min_length :name
25
+ }.should raise_error(ArgumentError)
26
+ end
27
+ it "should refuse invalid additionnal parameters" do
28
+ lambda{
29
+ @matcher = validate_min_length :id, :name
30
+ }.should raise_error(ArgumentError)
31
+ end
32
+ it "should accept valid additionnal parameters" do
33
+ lambda{
34
+ @matcher = validate_min_length 4, :name
35
+ }.should_not raise_error(ArgumentError)
36
+ end
37
+ end
38
+
39
+ describe "messages" do
40
+ describe "without option" do
41
+ it "should contain a description" do
42
+ @matcher = validate_min_length 4, :name
43
+ @matcher.description.should == "validate length of :name is greater than or equal to 4"
44
+ end
45
+ it "should set failure messages" do
46
+ @matcher = validate_min_length 4, :name
47
+ @matcher.matches? subject
48
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
49
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
50
+ end
51
+ end
52
+ describe "with options" do
53
+ it "should contain a description" do
54
+ @matcher = validate_min_length 4, :name, :allow_nil => true
55
+ @matcher.description.should == "validate length of :name is greater than or equal to 4 with option(s) :allow_nil => true"
56
+ end
57
+ it "should set failure messages" do
58
+ @matcher = validate_min_length 4, :price, :allow_nil => true
59
+ @matcher.matches? subject
60
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
61
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
62
+ end
63
+ it "should explicit used options if different than expected" do
64
+ @matcher = validate_min_length 4, :name, :allow_blank => true
65
+ @matcher.matches? subject
66
+ explanation = " but called with option(s) :allow_nil => true instead"
67
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
68
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
69
+ end
70
+ it "should warn if invalid options are used" do
71
+ @matcher = validate_min_length 4, :name, :allow_anything => true
72
+ @matcher.matches? subject
73
+ explanation = " but option :allow_anything is not valid"
74
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
75
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "matchers" do
81
+ it{ should validate_min_length(4, :name) }
82
+ it{ should validate_min_length(4, :name, :allow_nil => true) }
83
+ it{ should_not validate_min_length(4, :price) }
84
+ it{ should_not validate_min_length(3, :name) }
85
+ it{ should_not validate_min_length(4, :name, :allow_blank => true) }
86
+ end
87
+
88
+ end
@@ -0,0 +1,77 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe "validate_not_string_matcher" do
4
+
5
+ before do
6
+ define_model :item do
7
+ plugin :validation_helpers
8
+ def validate
9
+ validates_not_string [:id, :name], :allow_nil => true
10
+ end
11
+ end
12
+ end
13
+
14
+ subject{ Item }
15
+
16
+ describe "arguments" do
17
+ it "should require attribute" do
18
+ lambda{
19
+ @matcher = validate_not_string
20
+ }.should raise_error(ArgumentError)
21
+ end
22
+ it "should refuse additionnal parameters" do
23
+ lambda{
24
+ @matcher = validate_not_string :name, :id
25
+ }.should raise_error(ArgumentError)
26
+ end
27
+ end
28
+
29
+ describe "messages" do
30
+ describe "without option" do
31
+ it "should contain a description" do
32
+ @matcher = validate_not_string :name
33
+ @matcher.description.should == "validate that :name is not a string"
34
+ end
35
+ it "should set failure messages" do
36
+ @matcher = validate_not_string :name
37
+ @matcher.matches? subject
38
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
39
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
40
+ end
41
+ end
42
+ describe "with options" do
43
+ it "should contain a description" do
44
+ @matcher = validate_not_string :name, :allow_nil => true
45
+ @matcher.description.should == "validate that :name is not a string with option(s) :allow_nil => true"
46
+ end
47
+ it "should set failure messages" do
48
+ @matcher = validate_not_string :price, :allow_nil => true
49
+ @matcher.matches? subject
50
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
51
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
52
+ end
53
+ it "should explicit used options if different than expected" do
54
+ @matcher = validate_not_string :name, :allow_blank => true
55
+ @matcher.matches? subject
56
+ explanation = " but called with option(s) :allow_nil => true instead"
57
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
58
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
59
+ end
60
+ it "should warn if invalid options are used" do
61
+ @matcher = validate_not_string :name, :allow_anything => true
62
+ @matcher.matches? subject
63
+ explanation = " but option :allow_anything is not valid"
64
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
65
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "matchers" do
71
+ it{ should validate_not_string(:name) }
72
+ it{ should validate_not_string(:name, :allow_nil => true) }
73
+ it{ should_not validate_not_string(:price) }
74
+ it{ should_not validate_not_string(:name, :allow_blank => true) }
75
+ end
76
+
77
+ end
@@ -0,0 +1,77 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe "validate_numeric_matcher" do
4
+
5
+ before do
6
+ define_model :item do
7
+ plugin :validation_helpers
8
+ def validate
9
+ validates_numeric [:id, :name], :allow_nil => true
10
+ end
11
+ end
12
+ end
13
+
14
+ subject{ Item }
15
+
16
+ describe "arguments" do
17
+ it "should require attribute" do
18
+ lambda{
19
+ @matcher = validate_numeric
20
+ }.should raise_error(ArgumentError)
21
+ end
22
+ it "should refuse additionnal parameters" do
23
+ lambda{
24
+ @matcher = validate_numeric :name, :id
25
+ }.should raise_error(ArgumentError)
26
+ end
27
+ end
28
+
29
+ describe "messages" do
30
+ describe "without option" do
31
+ it "should contain a description" do
32
+ @matcher = validate_numeric :name
33
+ @matcher.description.should == "validate that :name is a valid float"
34
+ end
35
+ it "should set failure messages" do
36
+ @matcher = validate_numeric :name
37
+ @matcher.matches? subject
38
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
39
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
40
+ end
41
+ end
42
+ describe "with options" do
43
+ it "should contain a description" do
44
+ @matcher = validate_numeric :name, :allow_nil => true
45
+ @matcher.description.should == "validate that :name is a valid float with option(s) :allow_nil => true"
46
+ end
47
+ it "should set failure messages" do
48
+ @matcher = validate_numeric :price, :allow_nil => true
49
+ @matcher.matches? subject
50
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
51
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
52
+ end
53
+ it "should explicit used options if different than expected" do
54
+ @matcher = validate_numeric :name, :allow_blank => true
55
+ @matcher.matches? subject
56
+ explanation = " but called with option(s) :allow_nil => true instead"
57
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
58
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
59
+ end
60
+ it "should warn if invalid options are used" do
61
+ @matcher = validate_numeric :name, :allow_anything => true
62
+ @matcher.matches? subject
63
+ explanation = " but option :allow_anything is not valid"
64
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
65
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "matchers" do
71
+ it{ should validate_numeric(:name) }
72
+ it{ should validate_numeric(:name, :allow_nil => true) }
73
+ it{ should_not validate_numeric(:price) }
74
+ it{ should_not validate_numeric(:name, :allow_blank => true) }
75
+ end
76
+
77
+ end
@@ -0,0 +1,77 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe "validate_presence_matcher" do
4
+
5
+ before do
6
+ define_model :item do
7
+ plugin :validation_helpers
8
+ def validate
9
+ validates_presence [:id, :name], :allow_nil => true
10
+ end
11
+ end
12
+ end
13
+
14
+ subject{ Item }
15
+
16
+ describe "arguments" do
17
+ it "should require attribute" do
18
+ lambda{
19
+ @matcher = validate_presence
20
+ }.should raise_error(ArgumentError)
21
+ end
22
+ it "should refuse additionnal parameters" do
23
+ lambda{
24
+ @matcher = validate_presence :name, :id
25
+ }.should raise_error(ArgumentError)
26
+ end
27
+ end
28
+
29
+ describe "messages" do
30
+ describe "without option" do
31
+ it "should contain a description" do
32
+ @matcher = validate_presence :name
33
+ @matcher.description.should == "validate presence of :name"
34
+ end
35
+ it "should set failure messages" do
36
+ @matcher = validate_presence :name
37
+ @matcher.matches? subject
38
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
39
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
40
+ end
41
+ end
42
+ describe "with options" do
43
+ it "should contain a description" do
44
+ @matcher = validate_presence :name, :allow_nil => true
45
+ @matcher.description.should == "validate presence of :name with option(s) :allow_nil => true"
46
+ end
47
+ it "should set failure messages" do
48
+ @matcher = validate_presence :price, :allow_nil => true
49
+ @matcher.matches? subject
50
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
51
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
52
+ end
53
+ it "should explicit used options if different than expected" do
54
+ @matcher = validate_presence :name, :allow_blank => true
55
+ @matcher.matches? subject
56
+ explanation = " but called with option(s) :allow_nil => true instead"
57
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
58
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
59
+ end
60
+ it "should warn if invalid options are used" do
61
+ @matcher = validate_presence :name, :allow_anything => true
62
+ @matcher.matches? subject
63
+ explanation = " but option :allow_anything is not valid"
64
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
65
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "matchers" do
71
+ it{ should validate_presence(:name) }
72
+ it{ should validate_presence(:name, :allow_nil => true) }
73
+ it{ should_not validate_presence(:price) }
74
+ it{ should_not validate_presence(:name, :allow_blank => true) }
75
+ end
76
+
77
+ end