dm-validations 0.9.11 → 0.10.0

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 (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,60 @@
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
+ describe "with doc no with value of 'BAD CODE :)'" do
10
+ before :all do
11
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.merge(:doc_no => 'BAD CODE :)'))
12
+ end
13
+
14
+ it_should_behave_like 'invalid model'
15
+
16
+ it "has meaningful error message on invalid field" do
17
+ @model.errors.on(:doc_no).should == [ 'Doc no has an invalid format' ]
18
+ end
19
+ end
20
+
21
+ describe "with doc no with value of 'A1234'" do
22
+ before :all do
23
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.merge(:doc_no => 'A1234'))
24
+ end
25
+
26
+ it_should_behave_like 'valid model'
27
+ end
28
+
29
+ describe "with doc no with value of 'B123456X12'" do
30
+ before :all do
31
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.merge(:doc_no => 'B123456X12'))
32
+ end
33
+
34
+ it_should_behave_like 'valid model'
35
+ end
36
+
37
+ describe "with missing url" do
38
+ before :all do
39
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.except(:url))
40
+ end
41
+
42
+ it_should_behave_like 'invalid model'
43
+ end
44
+
45
+ describe "with blank name" do
46
+ before :all do
47
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.merge(:username => ''))
48
+ end
49
+
50
+ it_should_behave_like 'valid model'
51
+ end
52
+
53
+ describe "with blank email" do
54
+ before :all do
55
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.merge(:email => ''))
56
+ end
57
+
58
+ it_should_behave_like 'valid model'
59
+ end
60
+ end
@@ -0,0 +1,29 @@
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
+ describe "with code of 123456" do
10
+ before :all do
11
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.merge(:code => '123456'))
12
+ end
13
+
14
+ it_should_behave_like 'valid model'
15
+ end
16
+
17
+
18
+ describe "with code of 12" do
19
+ before :all do
20
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.merge(:code => '12'))
21
+ end
22
+
23
+ it_should_behave_like 'invalid model'
24
+
25
+ it "has a meaningful error message" do
26
+ @model.errors.on(:code).should == [ 'Code format is invalid' ]
27
+ end
28
+ end
29
+ 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,63 @@
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
+ [ 'http:// example.com', 'ftp://example.com', 'http://.com', 'http://', 'test', '...',
10
+ # these are valid URIs from RFC perspective,
11
+ # but too often not the case for web apps
12
+ #
13
+ # TODO: add another format that is strictly
14
+ # RFC compliant so it can be used, for instance,
15
+ # for internal apps that may refer to local domains
16
+ # like http://backend:8080
17
+ "http://localhost:4000", "http://localhost" ].each do |uri|
18
+ describe "with URL of #{uri}" do
19
+ before :all do
20
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.merge(:url => uri))
21
+ end
22
+
23
+ it_should_behave_like "invalid model"
24
+
25
+ it "has a meaningful error message" do
26
+ @model.errors.on(:url).should == [ 'Url has an invalid format' ]
27
+ end
28
+ end
29
+ end # each
30
+
31
+
32
+ [ 'http://apple.com',
33
+ 'http://www.apple.com',
34
+ "http://apple.com/",
35
+ "http://apple.com/iphone",
36
+ "http://www.google.com/search?client=safari&rls=en-us&q=LED&ie=UTF-8&oe=UTF-8",
37
+ "http://books.google.com",
38
+ "http://books.google.com/",
39
+ "http://db2.clouds.megacorp.net:8080",
40
+ "https://github.com",
41
+ "https://github.com/",
42
+ "http://www.example.com:8088/never/ending/path/segments/",
43
+ "http://db2.clouds.megacorp.net:8080/resources/10",
44
+ "http://www.example.com:8088/never/ending/path/segments",
45
+ "http://books.google.com/books?id=uSUJ3VhH4BsC&printsec=frontcover&dq=subject:%22+Linguistics+%22&as_brr=3&ei=DAHPSbGQE5rEzATk1sShAQ&rview=1",
46
+ "http://books.google.com:80/books?uid=14472359158468915761&rview=1",
47
+ "http://books.google.com/books?id=Ar3-TXCYXUkC&printsec=frontcover&rview=1",
48
+ "http://books.google.com/books/vp6ae081e454d30f89b6bca94e0f96fc14.js",
49
+ "http://www.google.com/images/cleardot.gif",
50
+ "http://books.google.com:80/books?id=Ar3-TXCYXUkC&printsec=frontcover&rview=1#PPA5,M1",
51
+ "http://www.hulu.com/watch/64923/terminator-the-sarah-connor-chronicles-to-the-lighthouse",
52
+ "http://hulu.com:80/browse/popular/tv",
53
+ "http://www.hulu.com/watch/62475/the-simpsons-gone-maggie-gone#s-p1-so-i0"
54
+ ].each do |uri|
55
+ describe "with URL of #{uri}" do
56
+ before :all do
57
+ @model = DataMapper::Validate::Fixtures::BillOfLading.new(valid_attributes.merge(:url => uri))
58
+ end
59
+
60
+ it_should_behave_like "valid model"
61
+ end
62
+ end # each
63
+ end # describe DataMapper::Validate::Fixtures::BillOfLading
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+ require 'integration/length_validator/spec_helper'
3
+
4
+ describe ::DataMapper::Validate::Fixtures::BoatDock do
5
+ before :all do
6
+ @model = ::DataMapper::Validate::Fixtures::BoatDock.new
7
+ end
8
+
9
+ describe "with default values that are valid" do
10
+ it_should_behave_like "valid model"
11
+ end
12
+ end
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'integration/length_validator/spec_helper'
5
+
6
+ describe "entity with wrong destination MAC address length", :shared => true do
7
+ it "has error message with range bounds" do
8
+ @model.errors.on(:destination_mac).should == [ 'Destination mac must be 6 characters long' ]
9
+ end
10
+ end
11
+
12
+
13
+ describe DataMapper::Validate::Fixtures::EthernetFrame do
14
+ before :all do
15
+ @model = DataMapper::Validate::Fixtures::EthernetFrame.valid_instance
16
+ @model.link_support_fragmentation = false
17
+ end
18
+
19
+ it_should_behave_like "valid model"
20
+
21
+ describe "with destination MAC 3 'bits' long" do
22
+ before :all do
23
+ @model.destination_mac = "123"
24
+ @model.valid?
25
+ end
26
+
27
+ it_should_behave_like "invalid model"
28
+
29
+ it_should_behave_like "entity with wrong destination MAC address length"
30
+ end
31
+
32
+ describe "with destination MAC 8 'bits' long" do
33
+ before :all do
34
+ @model.destination_mac = "123abce8"
35
+ @model.valid?
36
+ end
37
+
38
+ it_should_behave_like "invalid model"
39
+
40
+ it_should_behave_like "entity with wrong destination MAC address length"
41
+ end
42
+
43
+ # arguable but reasonable for 80% of cases
44
+ # to treat nil as a 0 lengh value
45
+ # reported in
46
+ # http://datamapper.lighthouseapp.com/projects/20609/tickets/646
47
+ describe "that has no destination MAC address" do
48
+ before :all do
49
+ @model.destination_mac = nil
50
+ @model.valid?
51
+ end
52
+
53
+ it_should_behave_like "invalid model"
54
+
55
+ it_should_behave_like "entity with wrong destination MAC address length"
56
+ end
57
+
58
+ describe "with a 6 'bits' destination MAC address" do
59
+ before :all do
60
+ @model.destination_mac = "a1b2c3"
61
+ @model.valid?
62
+ end
63
+
64
+ it_should_behave_like "valid model"
65
+ end
66
+
67
+ describe "with multibyte characters" do
68
+ before :all do
69
+ @model = DataMapper::Validate::Fixtures::Currency.valid_instance(
70
+ :name => 'Euro',
71
+ :code => 'EUR',
72
+ :symbol => '€'
73
+ )
74
+ @model.valid?
75
+ end
76
+
77
+ it_should_behave_like "valid model"
78
+ end
79
+ end
@@ -1,23 +1,20 @@
1
- require 'pathname'
2
- __dir__ = Pathname(__FILE__).dirname.expand_path
1
+ require 'spec_helper'
2
+ require 'integration/length_validator/spec_helper'
3
3
 
4
- # global first, then local to length validators
5
- require __dir__.parent.parent + "spec_helper"
6
- require __dir__ + 'spec_helper'
4
+ describe DataMapper::Validate::Fixtures::Jabberwock do
5
+ before :all do
6
+ @model = DataMapper::Validate::Fixtures::Jabberwock.new
7
+ end
8
+
9
+ describe "without snickersnack" do
10
+ before :all do
11
+ @model.snickersnack = nil
12
+ end
13
+
14
+ it_should_behave_like "invalid model"
7
15
 
8
- describe DataMapper::Validate::LengthValidator do
9
- it "lets user specify custom error message" do
10
- class Jabberwock
11
- include DataMapper::Resource
12
- property :id, Integer, :key => true
13
- property :snickersnack, String
14
- validates_length :snickersnack, :within => 3..40, :message => "worble warble"
16
+ it "has custom error message" do
17
+ @model.errors.on(:snickersnack).should == [ 'worble warble' ]
15
18
  end
16
- wock = Jabberwock.new
17
- wock.should_not be_valid
18
- wock.errors.on(:snickersnack).should include('worble warble')
19
- wock.snickersnack = "hello"
20
- wock.id = 1
21
- wock.should be_valid
22
19
  end
23
20
  end
@@ -1,31 +1,47 @@
1
- require 'pathname'
2
- __dir__ = Pathname(__FILE__).dirname.expand_path
3
-
4
- # global first, then local to length validators
5
- require __dir__.parent.parent + "spec_helper"
6
- require __dir__ + 'spec_helper'
7
-
8
- describe DataMapper::Validate::LengthValidator do
9
- it "lets user specify a maximum length of a string field" do
10
- class ::MotorLaunch
11
- validators.clear!
12
- validates_length :name, :max => 5
1
+ require 'spec_helper'
2
+ require 'integration/length_validator/spec_helper'
3
+
4
+ describe "barcode with invalid code length", :shared => true do
5
+ it "has a meaninful error message with length restrictions mentioned" do
6
+ @model.errors.on(:code).should == [ 'Code must be at most 10 characters long' ]
7
+ end
8
+ end
9
+
10
+ describe ::DataMapper::Validate::Fixtures::Barcode do
11
+ before :all do
12
+ @model = DataMapper::Validate::Fixtures::Barcode.valid_instance
13
+ end
14
+
15
+ it_should_behave_like "valid model"
16
+
17
+ describe "with a 17 characters long code" do
18
+ before :all do
19
+ @model.code = "18283849284728124"
20
+ @model.valid?
13
21
  end
14
22
 
15
- launch = MotorLaunch.new
16
- launch.name = 'a' * 6
17
- launch.should_not be_valid
18
- launch.errors.on(:name).should include('Name must be less than 5 characters long')
23
+ it_should_behave_like "invalid model"
24
+
25
+ it_should_behave_like "barcode with invalid code length"
19
26
  end
20
27
 
21
- it "aliases :maximum for :max" do
22
- class ::MotorLaunch
23
- validators.clear!
24
- validates_length :name, :maximum => 5
28
+ describe "with a 7 characters long code" do
29
+ before :all do
30
+ @model.code = "8372786"
31
+ @model.valid?
25
32
  end
26
- launch = MotorLaunch.new
27
- launch.name = 'a' * 6
28
- launch.should_not be_valid
29
- launch.errors.on(:name).should include('Name must be less than 5 characters long')
33
+
34
+ it_should_behave_like "valid model"
35
+ end
36
+
37
+ describe "with an 11 characters long code" do
38
+ before :all do
39
+ @model.code = "83727868754"
40
+ @model.valid?
41
+ end
42
+
43
+ it_should_behave_like "invalid model"
44
+
45
+ it_should_behave_like "barcode with invalid code length"
30
46
  end
31
47
  end
@@ -1,31 +1,42 @@
1
- require 'pathname'
2
- __dir__ = Pathname(__FILE__).dirname.expand_path
1
+ require 'spec_helper'
2
+ require 'integration/length_validator/spec_helper'
3
3
 
4
- # global first, then local to length validators
5
- require __dir__.parent.parent + "spec_helper"
6
- require __dir__ + 'spec_helper'
4
+ describe "entity with a name shorter than 2 characters", :shared => true do
5
+ it "has a meaninful error message with length restrictions mentioned" do
6
+ @model.errors.on(:name).should == [ 'Name must be at least 2 characters long' ]
7
+ end
8
+ end
9
+
10
+ describe ::DataMapper::Validate::Fixtures::Mittelschnauzer do
11
+ before :all do
12
+ @model = DataMapper::Validate::Fixtures::Mittelschnauzer.valid_instance
13
+ end
14
+
15
+ it_should_behave_like "valid model"
7
16
 
8
- describe DataMapper::Validate::LengthValidator do
9
- it "lets user specify a minimum length of a string field" do
10
- class ::MotorLaunch
11
- validates_length :name, :min => 3
17
+ describe "with a 13 characters long name" do
18
+ it_should_behave_like "valid model"
19
+ end
20
+
21
+ describe "with a single character name" do
22
+ before :all do
23
+ @model.name = "R"
24
+ @model.valid?
12
25
  end
13
26
 
14
- launch = MotorLaunch.new
15
- launch.name = 'Ab'
16
- launch.should_not be_valid
17
- launch.errors.on(:name).should include('Name must be more than 3 characters long')
27
+ it_should_behave_like "invalid model"
28
+
29
+ it_should_behave_like "entity with a name shorter than 2 characters"
18
30
  end
19
31
 
20
- it "aliases :minimum for :min" do
21
- class ::MotorLaunch
22
- validators.clear!
23
- validates_length :name, :minimum => 3
32
+ describe "with blank name" do
33
+ before :all do
34
+ @model.name = ""
35
+ @model.valid?
24
36
  end
25
37
 
26
- launch = MotorLaunch.new
27
- launch.name = 'Ab'
28
- launch.should_not be_valid
29
- launch.errors.on(:name).should include('Name must be more than 3 characters long')
38
+ it_should_behave_like "invalid model"
39
+
40
+ it_should_behave_like "entity with a name shorter than 2 characters"
30
41
  end
31
42
  end
@@ -1,47 +1,52 @@
1
- require 'pathname'
2
- __dir__ = Pathname(__FILE__).dirname.expand_path
1
+ require 'spec_helper'
2
+ require 'integration/length_validator/spec_helper'
3
3
 
4
- # global first, then local to length validators
5
- require __dir__.parent.parent + "spec_helper"
6
- require __dir__ + 'spec_helper'
4
+ describe DataMapper::Validate::Fixtures::EthernetFrame do
5
+ before :all do
6
+ @model = DataMapper::Validate::Fixtures::EthernetFrame.valid_instance
7
+ @model.link_support_fragmentation = false
8
+ end
7
9
 
8
- class MotorLaunch
9
- validators.clear!
10
- validates_length :name, :in => (3..5)
11
- end
10
+ it_should_behave_like "valid model"
12
11
 
12
+ describe "with payload that is 7 'bits' long (too short)" do
13
+ before :all do
14
+ @model.payload = "1234567"
15
+ @model.valid?
16
+ end
13
17
 
14
- describe MotorLaunch do
15
- before :each do
16
- @launch = MotorLaunch.new
17
- end
18
+ it_should_behave_like "invalid model"
18
19
 
19
- describe "with a value that is out of range bounds (too long)" do
20
- before :each do
21
- @launch.name = 'a'
22
- @launch.valid?
20
+ it "has error message with range bounds" do
21
+ @model.errors.on(:payload).should == [ 'Payload must be between 46 and 1500 characters long' ]
23
22
  end
23
+ end
24
24
 
25
- it "is invalid" do
26
- @launch.should_not be_valid
25
+
26
+ describe "with a one character long payload (too short)" do
27
+ before :all do
28
+ @model.payload = 'a'
29
+ @model.valid?
27
30
  end
28
- it "includes range bounds and field name in error message" do
29
- @launch.errors.on(:name).should include('Name must be between 3 and 5 characters long')
31
+
32
+ it_should_behave_like "invalid model"
33
+
34
+ it "has error message with range bounds" do
35
+ @model.errors.on(:payload).should == [ 'Payload must be between 46 and 1500 characters long' ]
30
36
  end
31
37
  end
32
38
 
33
39
 
34
- describe "with a value that is out of range bounds (too short)" do
35
- before :each do
36
- @launch.name = 'L'
37
- @launch.valid?
40
+ describe "with a 1600 'bits' long payload (needs fragmentation)" do
41
+ before :all do
42
+ @model.payload = 'a'
43
+ @model.valid?
38
44
  end
39
45
 
40
- it "is invalid" do
41
- @launch.should_not be_valid
42
- end
43
- it "includes range bounds and field name in error message" do
44
- @launch.errors.on(:name).should include('Name must be between 3 and 5 characters long')
46
+ it_should_behave_like "invalid model"
47
+
48
+ it "has error message with range bounds" do
49
+ @model.errors.on(:payload).should == [ 'Payload must be between 46 and 1500 characters long' ]
45
50
  end
46
51
  end
47
52
 
@@ -50,46 +55,31 @@ describe MotorLaunch do
50
55
  # to treat nil as a 0 lengh value
51
56
  # reported in
52
57
  # http://datamapper.lighthouseapp.com/projects/20609/tickets/646
53
- describe "with a nil value" do
54
- before :each do
55
- @launch.name = nil
56
- @launch.valid?
58
+ describe "that has no payload" do
59
+ before :all do
60
+ @model.payload = nil
61
+ @model.valid?
57
62
  end
58
63
 
59
- it "is invalid" do
60
- @launch.should_not be_valid
61
- end
62
- it "includes range bounds and field name in error message" do
63
- @launch.errors.on(:name).should include('Name must be between 3 and 5 characters long')
64
+ it_should_behave_like "invalid model"
65
+
66
+ it "has error message with range bounds" do
67
+ @model.errors.on(:payload).should == [ 'Payload must be between 46 and 1500 characters long' ]
64
68
  end
65
69
  end
66
70
 
67
71
 
68
72
 
69
- describe "with a value that is within range bounds" do
70
- before :each do
71
- @launch.name = 'Lisp'
72
- @launch.valid?
73
- end
74
-
75
- it "is valid" do
76
- @launch.should be_valid
77
- end
78
- it "has blank error message" do
79
- @launch.errors.on(:name).should be_blank
73
+ describe "with a 50 characters long payload" do
74
+ before :all do
75
+ @model.payload = 'Imagine yourself a beautiful bag full of bits here'
76
+ @model.valid?
80
77
  end
81
- end
82
-
83
78
 
79
+ it_should_behave_like "valid model"
84
80
 
85
- it "aliases :within for :in" do
86
- class ::MotorLaunch
87
- validators.clear!
88
- validates_length :name, :within => (3..5)
81
+ it "has blank error message" do
82
+ @model.errors.on(:payload).should be_blank
89
83
  end
90
-
91
- launch = MotorLaunch.new
92
- launch.name = 'Ride'
93
- launch.valid?.should == true
94
84
  end
95
85
  end