dm-validations 0.9.11 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. data/{History.txt → History.rdoc} +10 -3
  2. data/LICENSE +1 -0
  3. data/Manifest.txt +103 -33
  4. data/README.rdoc +172 -0
  5. data/Rakefile +4 -5
  6. data/lib/dm-validations.rb +73 -75
  7. data/lib/dm-validations/auto_validate.rb +98 -56
  8. data/lib/dm-validations/contextual_validators.rb +37 -14
  9. data/lib/dm-validations/exceptions.rb +3 -0
  10. data/lib/dm-validations/formats/url.rb +3 -1
  11. data/lib/dm-validations/support/context.rb +54 -0
  12. data/lib/dm-validations/validation_errors.rb +51 -27
  13. data/lib/dm-validations/{absent_field_validator.rb → validators/absent_field_validator.rb} +2 -8
  14. data/lib/dm-validations/{acceptance_validator.rb → validators/acceptance_validator.rb} +7 -7
  15. data/lib/dm-validations/{block_validator.rb → validators/block_validator.rb} +0 -0
  16. data/lib/dm-validations/{confirmation_validator.rb → validators/confirmation_validator.rb} +7 -7
  17. data/lib/dm-validations/{format_validator.rb → validators/format_validator.rb} +10 -16
  18. data/lib/dm-validations/{generic_validator.rb → validators/generic_validator.rb} +28 -6
  19. data/lib/dm-validations/validators/length_validator.rb +214 -0
  20. data/lib/dm-validations/validators/method_validator.rb +68 -0
  21. data/lib/dm-validations/validators/numeric_validator.rb +171 -0
  22. data/lib/dm-validations/{primitive_validator.rb → validators/primitive_validator.rb} +2 -7
  23. data/lib/dm-validations/{required_field_validator.rb → validators/required_field_validator.rb} +0 -6
  24. data/lib/dm-validations/{uniqueness_validator.rb → validators/uniqueness_validator.rb} +9 -20
  25. data/lib/dm-validations/{within_validator.rb → validators/within_validator.rb} +4 -4
  26. data/lib/dm-validations/version.rb +1 -1
  27. data/spec/fixtures/barcode.rb +32 -0
  28. data/spec/fixtures/basketball_court.rb +58 -0
  29. data/spec/fixtures/basketball_player.rb +37 -0
  30. data/spec/fixtures/beta_tester_account.rb +33 -0
  31. data/spec/fixtures/bill_of_landing.rb +42 -0
  32. data/spec/fixtures/boat_dock.rb +26 -0
  33. data/spec/fixtures/city.rb +25 -0
  34. data/spec/fixtures/company.rb +95 -0
  35. data/spec/fixtures/corporate_world.rb +47 -0
  36. data/spec/fixtures/country.rb +25 -0
  37. data/spec/fixtures/currency.rb +42 -0
  38. data/spec/fixtures/ethernet_frame.rb +56 -0
  39. data/spec/fixtures/event.rb +44 -0
  40. data/spec/fixtures/g3_concert.rb +57 -0
  41. data/spec/fixtures/jabberwock.rb +27 -0
  42. data/spec/fixtures/kayak.rb +28 -0
  43. data/spec/fixtures/lernean_hydra.rb +39 -0
  44. data/spec/fixtures/mathematical_function.rb +34 -0
  45. data/spec/fixtures/memory_object.rb +30 -0
  46. data/spec/fixtures/mittelschnauzer.rb +35 -0
  47. data/spec/fixtures/motor_launch.rb +21 -0
  48. data/spec/fixtures/page.rb +32 -0
  49. data/spec/fixtures/phone_number.rb +28 -0
  50. data/spec/fixtures/pirogue.rb +28 -0
  51. data/spec/fixtures/programming_language.rb +83 -0
  52. data/spec/fixtures/reservation.rb +38 -0
  53. data/spec/fixtures/scm_operation.rb +67 -0
  54. data/spec/fixtures/sms_message.rb +22 -0
  55. data/spec/fixtures/udp_packet.rb +49 -0
  56. data/spec/integration/absent_field_validator/absent_field_validator_spec.rb +86 -0
  57. data/spec/integration/absent_field_validator/spec_helper.rb +7 -0
  58. data/spec/integration/acceptance_validator/acceptance_validator_spec.rb +196 -0
  59. data/spec/integration/acceptance_validator/spec_helper.rb +7 -0
  60. data/spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb +47 -0
  61. data/spec/integration/automatic_validation/disabling_inferred_validation_spec.rb +41 -0
  62. data/spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +104 -0
  63. data/spec/integration/automatic_validation/inferred_float_property_validation_spec.rb +35 -0
  64. data/spec/integration/automatic_validation/inferred_format_validation_spec.rb +33 -0
  65. data/spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +73 -0
  66. data/spec/integration/automatic_validation/inferred_length_validation_spec.rb +121 -0
  67. data/spec/integration/automatic_validation/inferred_presence_validation_spec.rb +41 -0
  68. data/spec/integration/automatic_validation/inferred_primitive_validation_spec.rb +20 -0
  69. data/spec/integration/automatic_validation/inferred_within_validation_spec.rb +33 -0
  70. data/spec/integration/automatic_validation/spec_helper.rb +74 -0
  71. data/spec/integration/block_validator/block_validator_spec.rb +32 -0
  72. data/spec/integration/block_validator/spec_helper.rb +5 -0
  73. data/spec/integration/conditional_validation/if_condition_spec.rb +61 -0
  74. data/spec/integration/conditional_validation/spec_helper.rb +5 -0
  75. data/spec/integration/confirmation_validator/confirmation_validator_spec.rb +74 -0
  76. data/spec/integration/confirmation_validator/spec_helper.rb +5 -0
  77. data/spec/integration/datamapper_models/association_validation_spec.rb +23 -0
  78. data/spec/integration/datamapper_models/inheritance_spec.rb +78 -0
  79. data/spec/integration/duplicated_validations/duplicated_validations_spec.rb +22 -0
  80. data/spec/integration/duplicated_validations/spec_helper.rb +5 -0
  81. data/spec/integration/format_validator/email_format_validator_spec.rb +113 -0
  82. data/spec/integration/format_validator/format_validator_spec.rb +60 -0
  83. data/spec/integration/format_validator/regexp_validator_spec.rb +29 -0
  84. data/spec/integration/format_validator/spec_helper.rb +5 -0
  85. data/spec/integration/format_validator/url_format_validator_spec.rb +63 -0
  86. data/spec/integration/length_validator/default_value_spec.rb +12 -0
  87. data/spec/integration/length_validator/equality_spec.rb +79 -0
  88. data/spec/integration/length_validator/error_message_spec.rb +15 -18
  89. data/spec/integration/length_validator/maximum_spec.rb +40 -24
  90. data/spec/integration/length_validator/minimum_spec.rb +32 -21
  91. data/spec/integration/length_validator/range_spec.rb +50 -60
  92. data/spec/integration/length_validator/spec_helper.rb +7 -12
  93. data/spec/integration/method_validator/method_validator_spec.rb +239 -0
  94. data/spec/integration/method_validator/spec_helper.rb +5 -0
  95. data/spec/integration/numeric_validator/equality_with_float_type_spec.rb +61 -0
  96. data/spec/integration/numeric_validator/equality_with_integer_type_spec.rb +37 -0
  97. data/spec/integration/numeric_validator/float_type_spec.rb +42 -56
  98. data/spec/integration/numeric_validator/gt_with_float_type_spec.rb +35 -0
  99. data/spec/integration/numeric_validator/gte_with_float_type_spec.rb +35 -0
  100. data/spec/integration/numeric_validator/integer_only_true_spec.rb +3 -6
  101. data/spec/integration/numeric_validator/integer_type_spec.rb +8 -24
  102. data/spec/integration/numeric_validator/lt_with_float_type_spec.rb +35 -0
  103. data/spec/integration/numeric_validator/lte_with_float_type_spec.rb +35 -0
  104. data/spec/integration/numeric_validator/spec_helper.rb +5 -77
  105. data/spec/integration/primitive_validator/primitive_validator_spec.rb +90 -0
  106. data/spec/integration/primitive_validator/spec_helper.rb +5 -0
  107. data/spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +118 -0
  108. data/spec/integration/required_field_validator/association_spec.rb +5 -8
  109. data/spec/integration/required_field_validator/boolean_type_value_spec.rb +4 -7
  110. data/spec/integration/required_field_validator/date_type_value_spec.rb +3 -6
  111. data/spec/integration/required_field_validator/datetime_type_value_spec.rb +3 -6
  112. data/spec/integration/required_field_validator/float_type_value_spec.rb +3 -6
  113. data/spec/integration/required_field_validator/integer_type_value_spec.rb +2 -5
  114. data/spec/integration/required_field_validator/plain_old_ruby_object_spec.rb +5 -8
  115. data/spec/integration/required_field_validator/shared_examples.rb +1 -1
  116. data/spec/integration/required_field_validator/spec_helper.rb +6 -67
  117. data/spec/integration/required_field_validator/string_type_value_spec.rb +2 -5
  118. data/spec/integration/required_field_validator/text_type_value_spec.rb +2 -5
  119. data/spec/integration/shared/default_validation_context.rb +13 -0
  120. data/spec/integration/shared/valid_and_invalid_model.rb +27 -0
  121. data/spec/integration/uniqueness_validator/spec_helper.rb +5 -0
  122. data/spec/integration/uniqueness_validator/uniqueness_validator_spec.rb +114 -0
  123. data/spec/integration/within_validator/spec_helper.rb +5 -0
  124. data/spec/integration/within_validator/within_validator_spec.rb +164 -0
  125. data/spec/public/resource_spec.rb +68 -0
  126. data/spec/spec.opts +1 -0
  127. data/spec/spec_helper.rb +12 -4
  128. data/spec/unit/contextual_validators/emptiness_spec.rb +50 -0
  129. data/spec/unit/contextual_validators/execution_spec.rb +55 -0
  130. data/spec/unit/contextual_validators/spec_helper.rb +37 -0
  131. data/spec/unit/generic_validator/equality_operator_spec.rb +26 -0
  132. data/spec/unit/validation_errors/adding_spec.rb +54 -0
  133. data/spec/unit/validation_errors/emptiness_spec.rb +38 -0
  134. data/spec/unit/validation_errors/enumerable_spec.rb +32 -0
  135. data/tasks/install.rb +1 -1
  136. data/tasks/spec.rb +4 -4
  137. metadata +118 -55
  138. data/README.txt +0 -72
  139. data/lib/dm-validations/custom_validator.rb +0 -72
  140. data/lib/dm-validations/length_validator.rb +0 -113
  141. data/lib/dm-validations/method_validator.rb +0 -68
  142. data/lib/dm-validations/numeric_validator.rb +0 -83
  143. data/spec/integration/absent_field_validator_spec.rb +0 -36
  144. data/spec/integration/acceptance_validator_spec.rb +0 -87
  145. data/spec/integration/auto_validate_spec.rb +0 -342
  146. data/spec/integration/block_validator_spec.rb +0 -30
  147. data/spec/integration/confirmation_validator_spec.rb +0 -105
  148. data/spec/integration/contextual_validators_spec.rb +0 -27
  149. data/spec/integration/custom_validator_spec.rb +0 -9
  150. data/spec/integration/format_validator_spec.rb +0 -193
  151. data/spec/integration/generic_validator_spec.rb +0 -17
  152. data/spec/integration/length_validator/valid_objects_spec.rb +0 -13
  153. data/spec/integration/method_validator_spec.rb +0 -58
  154. data/spec/integration/numeric_validator_spec.rb +0 -253
  155. data/spec/integration/primitive_validator_spec.rb +0 -30
  156. data/spec/integration/uniqueness_validator_spec.rb +0 -97
  157. data/spec/integration/validation_errors_spec.rb +0 -18
  158. data/spec/integration/validation_spec.rb +0 -404
  159. data/spec/integration/within_validator_spec.rb +0 -79
@@ -0,0 +1,74 @@
1
+ # put validator specific fixture models and helpers here
2
+ #
3
+ # make sure you check out spec/fixtures to see fixture models
4
+ # already written
5
+ #
6
+ # DataMapper developers feels strongly against foobars in the spec
7
+ # suite
8
+
9
+ # TODO: one day we need to get rid of this remaining foobarness
10
+ # and use a few more realistic models with ParanoidBoolean and all
11
+ # that
12
+
13
+ module TypecastBypassSetter
14
+ # Bypass typecasting so we can set values for specs
15
+ def set(attributes)
16
+ attributes.each do |k, v|
17
+ instance_variable_set("@#{k}", v)
18
+ end
19
+ end
20
+ end
21
+
22
+ class SailBoat
23
+ include DataMapper::Resource
24
+
25
+ # this one is not Serial intentionally
26
+ # use Serial in real world apps
27
+ property :id, Integer, :key => true, :min => 1, :max => 10
28
+
29
+ property :name, String, :nullable => false, :validates => :presence_test
30
+ property :description, String, :length => 10, :validates => :length_test_1
31
+ property :notes, String, :length => 2..10, :validates => :length_test_2
32
+ property :no_validation, String, :auto_validation => false
33
+ property :salesman, String, :nullable => false, :validates => [:multi_context_1, :multi_context_2]
34
+ property :code, String, :format => Proc.new { |code| code =~ /A\d{4}\z/ }, :validates => :format_test
35
+ property :allow_nil, String, :length => 5..10, :nullable => true, :validates => :nil_test
36
+ property :build_date, Date, :validates => :primitive_test
37
+ property :float, Float, :precision => 2, :scale => 1
38
+ property :big_decimal, BigDecimal, :precision => 2, :scale => 1
39
+
40
+ include TypecastBypassSetter
41
+ end
42
+
43
+ class HasNullableBoolean
44
+ include DataMapper::Resource
45
+
46
+ # this one is not Serial intentionally
47
+ # use Serial in real world apps
48
+ property :id, Integer, :key => true
49
+ property :bool, Boolean # :nullable => true by default
50
+
51
+ include TypecastBypassSetter
52
+ end
53
+
54
+ class HasNotNullableBoolean
55
+ include DataMapper::Resource
56
+
57
+ # this one is not Serial intentionally
58
+ # use Serial in real world apps
59
+ property :id, Integer, :key => true
60
+ property :bool, Boolean, :nullable => false
61
+
62
+ include TypecastBypassSetter
63
+ end
64
+
65
+ class HasNotNullableParanoidBoolean
66
+ include DataMapper::Resource
67
+
68
+ # this one is not Serial intentionally
69
+ # use Serial in real world apps
70
+ property :id, Integer, :key => true
71
+ property :bool, ParanoidBoolean, :nullable => false
72
+
73
+ include TypecastBypassSetter
74
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'integration/block_validator/spec_helper'
3
+
4
+ describe DataMapper::Validate::Fixtures::G3Concert do
5
+ before :all do
6
+ @model = DataMapper::Validate::Fixtures::G3Concert.new(:year => 2004, :participants => "Joe Satriani, Steve Vai, Yngwie Malmsteen", :city => "Denver")
7
+ @model.should be_valid
8
+ end
9
+
10
+ describe "some non existing year/participants/city combination" do
11
+ before :all do
12
+ @model.year = 2015
13
+ end
14
+
15
+ it_should_behave_like "invalid model"
16
+
17
+ it "uses error messages returned by the validation block" do
18
+ @model.errors.on(:participants).should == [ 'this G3 is probably yet to take place' ]
19
+ end
20
+ end
21
+
22
+
23
+ describe "existing year/participants/city combination" do
24
+ before :all do
25
+ @model.year = 2001
26
+ @model.city = "Los Angeles"
27
+ @model.participants = "Joe Satriani, Steve Vai, John Petrucci"
28
+ end
29
+
30
+ it_should_behave_like "valid model"
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # make sure you check out spec/fixtures to see fixture models
2
+ # already written
3
+ #
4
+ # DataMapper developers feels strongly against foobars in the spec
5
+ # suite
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+ require 'integration/conditional_validation/spec_helper'
3
+
4
+ describe DataMapper::Validate::Fixtures::UDPPacket do
5
+ before :all do
6
+ @model = DataMapper::Validate::Fixtures::UDPPacket.new
7
+ end
8
+
9
+ describe "that is transported encapsulated into IPv4 packet" do
10
+ before :all do
11
+ @model.underlying_ip_version = 4
12
+ end
13
+
14
+ describe "and has no checksum" do
15
+ before :all do
16
+ @model.checksum = nil
17
+ end
18
+
19
+ it_should_behave_like "valid model"
20
+ end
21
+
22
+ describe "and has no checksum algorithm" do
23
+ before :all do
24
+ @model.checksum_algorithm = nil
25
+ end
26
+
27
+ it_should_behave_like "valid model"
28
+ end
29
+ end
30
+
31
+
32
+ describe "that is transported encapsulated into IPv6 packet" do
33
+ before :all do
34
+ @model.underlying_ip_version = 6
35
+ end
36
+
37
+ describe "and has no checksum" do
38
+ before :all do
39
+ @model.checksum = nil
40
+ end
41
+
42
+ it_should_behave_like "invalid model"
43
+
44
+ it "has a meaningful error message" do
45
+ @model.errors.on(:checksum).should == [ 'Checksum is mandatory when used with IPv6' ]
46
+ end
47
+ end
48
+
49
+ describe "and has no checksum algorithm" do
50
+ before :all do
51
+ @model.checksum_algorithm = nil
52
+ end
53
+
54
+ it_should_behave_like "invalid model"
55
+
56
+ it "has a meaningful error message" do
57
+ @model.errors.on(:checksum_algorithm).should == [ 'Checksum is mandatory when used with IPv6' ]
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ # make sure you check out spec/fixtures to see fixture models
2
+ # already written
3
+ #
4
+ # DataMapper developers feels strongly against foobars in the spec
5
+ # suite
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+ require 'integration/confirmation_validator/spec_helper'
3
+
4
+ describe "reservation with mismatched person name", :shared => true do
5
+ it "has meaningful error message" do
6
+ @model.errors.on(:person_name).should == [ 'Person name does not match the confirmation' ]
7
+ end
8
+ end
9
+
10
+ describe "reservation with mismatched seats number", :shared => true do
11
+ it "has meaningful error message" do
12
+ # Proc gets expanded here
13
+ @model.errors.on(:number_of_seats).should == [ 'Reservation requires confirmation for number_of_seats' ]
14
+ end
15
+ end
16
+
17
+
18
+ describe DataMapper::Validate::Fixtures::Reservation do
19
+ before :all do
20
+ @model = DataMapper::Validate::Fixtures::Reservation.new(:person_name => "Tyler Durden",
21
+ :person_name_confirmation => "Tyler Durden",
22
+ :number_of_seats => 2,
23
+ :seats_confirmation => 2)
24
+ @model.should be_valid
25
+ end
26
+
27
+ describe "with matching person name and confirmation" do
28
+ before :all do
29
+ @model.person_name = "mismatch"
30
+ end
31
+
32
+ it_should_behave_like "invalid model"
33
+ it_should_behave_like "reservation with mismatched person name"
34
+ end
35
+
36
+
37
+ describe "with a blank person name and confirmation" do
38
+ before :all do
39
+ @model.person_name = ""
40
+ end
41
+
42
+ it_should_behave_like "invalid model"
43
+ it_should_behave_like "reservation with mismatched person name"
44
+ end
45
+
46
+
47
+ describe "with a missing person name and confirmation" do
48
+ before :all do
49
+ @model.person_name = nil
50
+ end
51
+
52
+ it_should_behave_like "invalid model"
53
+ it_should_behave_like "reservation with mismatched person name"
54
+ end
55
+
56
+
57
+ describe "with mismatching number of seats and confirmation" do
58
+ before :all do
59
+ @model.number_of_seats = -1
60
+ end
61
+
62
+ it_should_behave_like "invalid model"
63
+ it_should_behave_like "reservation with mismatched seats number"
64
+ end
65
+
66
+
67
+ describe "with a blank number of seats and confirmation" do
68
+ before :all do
69
+ @model.number_of_seats = nil
70
+ end
71
+
72
+ it_should_behave_like "valid model"
73
+ end
74
+ end
@@ -0,0 +1,5 @@
1
+ # make sure you check out spec/fixtures to see fixture models
2
+ # already written
3
+ #
4
+ # DataMapper developers feels strongly against foobars in the spec
5
+ # suite
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMapper::Validate::Fixtures::Product do
4
+ before :all do
5
+ @parent = DataMapper::Validate::Fixtures::ProductCompany.create(:title => "Apple", :flagship_product => "Macintosh")
6
+ @parent.should be_valid
7
+
8
+ @model = DataMapper::Validate::Fixtures::Product.new(:name => "MacBook Pro", :company => @parent)
9
+ @model.should be_valid
10
+ end
11
+
12
+ describe "without company" do
13
+ before :all do
14
+ @model.company = nil
15
+ end
16
+
17
+ it_should_behave_like "invalid model"
18
+
19
+ it "has a meaningful error message" do
20
+ @model.errors.on(:company).should == [ 'Company must not be blank' ]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMapper::Validate::Fixtures::ServiceCompany do
4
+ before :all do
5
+ @model = DataMapper::Validate::Fixtures::ServiceCompany.new(:title => "Monsters, Inc.", :area_of_expertise => "Little children's nightmares")
6
+ @model.valid?
7
+ end
8
+
9
+ describe "without title" do
10
+ before :all do
11
+ @model.title = nil
12
+ end
13
+
14
+ it_should_behave_like "invalid model"
15
+
16
+ it "has a meaningful error message for inherited property" do
17
+ @model.errors.on(:title).should == [ 'Company name is a required field' ]
18
+ end
19
+ end
20
+
21
+ describe "without area of expertise" do
22
+ before :all do
23
+ @model.area_of_expertise = nil
24
+ end
25
+
26
+ it_should_behave_like "invalid model"
27
+
28
+ it "has a meaningful error message for own property" do
29
+ @model.errors.on(:area_of_expertise).should == [ 'Area of expertise must not be blank' ]
30
+ end
31
+ end
32
+ end
33
+
34
+
35
+
36
+ describe DataMapper::Validate::Fixtures::ProductCompany do
37
+ before :all do
38
+ @model = DataMapper::Validate::Fixtures::ProductCompany.new(:title => "Apple", :flagship_product => "Macintosh")
39
+ @model.valid?
40
+ end
41
+
42
+ it_should_behave_like "valid model"
43
+
44
+ describe "without title" do
45
+ before :all do
46
+ @model.title = nil
47
+ end
48
+
49
+ it_should_behave_like "invalid model"
50
+
51
+ it "has error message from the subclass itself" do
52
+ @model.errors.on(:title).should include('Product company must have a name')
53
+ end
54
+
55
+ # this may or may not be a desired behavior,
56
+ # but append vs. replace is a matter of opinion
57
+ # anyway
58
+ #
59
+ # TODO: there should be a way to clear validations for a field
60
+ # that subclasses can use
61
+ it "has error message from superclass" do
62
+ @model.errors.on(:title).should include('Company name is a required field')
63
+ end
64
+ end
65
+
66
+
67
+ describe "without flagship product" do
68
+ before :all do
69
+ @model.flagship_product = nil
70
+ end
71
+
72
+ it_should_behave_like "invalid model"
73
+
74
+ it "has a meaningful error message for own property" do
75
+ @model.errors.on(:flagship_product).should == [ 'Flagship product must not be blank' ]
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'integration/duplicated_validations/spec_helper'
5
+
6
+ describe DataMapper::Validate::Fixtures::Page do
7
+ before :all do
8
+ @model = DataMapper::Validate::Fixtures::Page.new(:id => 1024)
9
+ end
10
+
11
+ describe "without body" do
12
+ before :all do
13
+ @model.body = nil
14
+ end
15
+
16
+ it_should_behave_like "invalid model"
17
+
18
+ it "does not have duplicated error messages" do
19
+ @model.errors.on(:body).should == ["Body must not be blank"]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # make sure you check out spec/fixtures to see fixture models
2
+ # already written
3
+ #
4
+ # DataMapper developers feels strongly against foobars in the spec
5
+ # suite
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+ require 'integration/format_validator/spec_helper'
3
+
4
+ describe DataMapper::Validate::Fixtures::BillOfLading do
5
+ def valid_attributes
6
+ { :id => 1, :doc_no => 'A1234', :email => 'user@example.com', :url => 'http://example.com' }
7
+ end
8
+
9
+ @valid_email_addresses = [
10
+ '+1~1+@example.com',
11
+ '{_dave_}@example.com',
12
+ '"[[ dave ]]"@example.com',
13
+ 'dave."dave"@example.com',
14
+ 'test@localhost',
15
+ 'test@example.com',
16
+ 'test@example.co.uk',
17
+ 'test@example.com.br',
18
+ '"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
19
+ 'me@[187.223.45.119]',
20
+ 'someone@123.com',
21
+ 'simon&garfunkel@songs.com'].each do |email|
22
+
23
+ describe "with email value of #{email} (RFC2822 compliant)" do
24
+ before :all do
25
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.merge(:email => email))
26
+ end
27
+
28
+ it_should_behave_like "valid model"
29
+ end
30
+ end
31
+
32
+
33
+ @invalid_email_addresses = [
34
+ '-- dave --@example.com',
35
+ '[dave]@example.com',
36
+ '.dave@example.com',
37
+ 'Max@Job 3:14',
38
+ 'Job@Book of Job',
39
+ 'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com'].each do |email|
40
+ describe "with email value of #{email} (non RFC2822 compliant)" do
41
+ before :all do
42
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.merge(:email => email))
43
+ end
44
+
45
+ it_should_behave_like "invalid model"
46
+ end
47
+ end
48
+
49
+
50
+ it 'should have a pre-defined URL format' do
51
+ bad = [ 'http:// example.com',
52
+ 'ftp://example.com',
53
+ 'http://.com',
54
+ 'http://',
55
+ 'test',
56
+ '...'
57
+ ]
58
+
59
+ good = [
60
+ 'http://example.com',
61
+ 'http://www.example.com',
62
+ ]
63
+
64
+ bol = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.except(:url))
65
+ bol.should_not be_valid
66
+ bol.errors.on(:url).should == [ 'Url has an invalid format' ]
67
+
68
+ bad.map do |e|
69
+ bol.url = e
70
+ bol.valid?
71
+ bol.errors.on(:url).should == [ 'Url has an invalid format' ]
72
+ end
73
+
74
+ good.map do |e|
75
+ bol.url = e
76
+ bol.valid?
77
+ bol.errors.on(:url).should be_nil
78
+ end
79
+
80
+ end
81
+
82
+ describe 'with a regexp' do
83
+ before do
84
+ @bol = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes)
85
+ @bol.should be_valid
86
+ end
87
+
88
+ describe 'if matched' do
89
+ before do
90
+ @bol.username = 'a12345'
91
+ end
92
+
93
+ it 'should validate' do
94
+ @bol.should be_valid
95
+ end
96
+ end
97
+
98
+ describe 'if not matched' do
99
+ before do
100
+ @bol.username = '12345'
101
+ end
102
+
103
+ it 'should not validate' do
104
+ @bol.should_not be_valid
105
+ end
106
+
107
+ it 'should set an error message' do
108
+ @bol.valid?
109
+ @bol.errors.on(:username).should == [ 'Username must have at least one letter' ]
110
+ end
111
+ end
112
+ end
113
+ end