sbf-dm-validations 1.3.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +38 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +468 -0
  5. data/.travis.yml +51 -0
  6. data/Gemfile +60 -0
  7. data/LICENSE +21 -0
  8. data/README.rdoc +122 -0
  9. data/Rakefile +4 -0
  10. data/dm-validations.gemspec +20 -0
  11. data/lib/data_mapper/core.rb +1 -0
  12. data/lib/data_mapper/support/assertions.rb +1 -0
  13. data/lib/data_mapper/support/equalizer.rb +1 -0
  14. data/lib/data_mapper/support/ordered_set.rb +2 -0
  15. data/lib/data_mapper/validation/backward.rb +205 -0
  16. data/lib/data_mapper/validation/context.rb +57 -0
  17. data/lib/data_mapper/validation/contextual_rule_set.rb +210 -0
  18. data/lib/data_mapper/validation/exceptions.rb +7 -0
  19. data/lib/data_mapper/validation/inferred.rb +264 -0
  20. data/lib/data_mapper/validation/macros.rb +449 -0
  21. data/lib/data_mapper/validation/message_transformer.rb +111 -0
  22. data/lib/data_mapper/validation/model_extensions.rb +17 -0
  23. data/lib/data_mapper/validation/resource_extensions.rb +131 -0
  24. data/lib/data_mapper/validation/rule/absence.rb +31 -0
  25. data/lib/data_mapper/validation/rule/acceptance.rb +49 -0
  26. data/lib/data_mapper/validation/rule/block.rb +37 -0
  27. data/lib/data_mapper/validation/rule/confirmation.rb +47 -0
  28. data/lib/data_mapper/validation/rule/format/proc.rb +34 -0
  29. data/lib/data_mapper/validation/rule/format/regexp.rb +51 -0
  30. data/lib/data_mapper/validation/rule/format.rb +86 -0
  31. data/lib/data_mapper/validation/rule/formats/email.rb +54 -0
  32. data/lib/data_mapper/validation/rule/formats/url.rb +13 -0
  33. data/lib/data_mapper/validation/rule/length/equal.rb +48 -0
  34. data/lib/data_mapper/validation/rule/length/maximum.rb +50 -0
  35. data/lib/data_mapper/validation/rule/length/minimum.rb +50 -0
  36. data/lib/data_mapper/validation/rule/length/range.rb +50 -0
  37. data/lib/data_mapper/validation/rule/length.rb +96 -0
  38. data/lib/data_mapper/validation/rule/method.rb +42 -0
  39. data/lib/data_mapper/validation/rule/numericalness/equal.rb +34 -0
  40. data/lib/data_mapper/validation/rule/numericalness/greater_than.rb +34 -0
  41. data/lib/data_mapper/validation/rule/numericalness/greater_than_or_equal.rb +34 -0
  42. data/lib/data_mapper/validation/rule/numericalness/integer.rb +41 -0
  43. data/lib/data_mapper/validation/rule/numericalness/less_than.rb +34 -0
  44. data/lib/data_mapper/validation/rule/numericalness/less_than_or_equal.rb +34 -0
  45. data/lib/data_mapper/validation/rule/numericalness/not_equal.rb +34 -0
  46. data/lib/data_mapper/validation/rule/numericalness/numeric.rb +68 -0
  47. data/lib/data_mapper/validation/rule/numericalness.rb +91 -0
  48. data/lib/data_mapper/validation/rule/presence.rb +52 -0
  49. data/lib/data_mapper/validation/rule/primitive_type.rb +32 -0
  50. data/lib/data_mapper/validation/rule/uniqueness.rb +64 -0
  51. data/lib/data_mapper/validation/rule/within/range/bounded.rb +29 -0
  52. data/lib/data_mapper/validation/rule/within/range/unbounded_begin.rb +29 -0
  53. data/lib/data_mapper/validation/rule/within/range/unbounded_end.rb +29 -0
  54. data/lib/data_mapper/validation/rule/within/range.rb +55 -0
  55. data/lib/data_mapper/validation/rule/within/set.rb +45 -0
  56. data/lib/data_mapper/validation/rule/within.rb +32 -0
  57. data/lib/data_mapper/validation/rule.rb +232 -0
  58. data/lib/data_mapper/validation/rule_set.rb +157 -0
  59. data/lib/data_mapper/validation/support/object.rb +19 -0
  60. data/lib/data_mapper/validation/support/ordered_hash.rb +434 -0
  61. data/lib/data_mapper/validation/version.rb +5 -0
  62. data/lib/data_mapper/validation/violation.rb +136 -0
  63. data/lib/data_mapper/validation/violation_set.rb +115 -0
  64. data/lib/data_mapper/validation.rb +105 -0
  65. data/lib/dm-validations.rb +24 -0
  66. data/spec/data_mapper/validation/resource_extensions/save_spec.rb +56 -0
  67. data/spec/data_mapper/validation/resource_extensions/validate_spec.rb +103 -0
  68. data/spec/fixtures/barcode.rb +40 -0
  69. data/spec/fixtures/basketball_court.rb +58 -0
  70. data/spec/fixtures/basketball_player.rb +34 -0
  71. data/spec/fixtures/beta_tester_account.rb +33 -0
  72. data/spec/fixtures/bill_of_landing.rb +47 -0
  73. data/spec/fixtures/boat_dock.rb +26 -0
  74. data/spec/fixtures/city.rb +24 -0
  75. data/spec/fixtures/company.rb +93 -0
  76. data/spec/fixtures/corporate_world.rb +39 -0
  77. data/spec/fixtures/country.rb +24 -0
  78. data/spec/fixtures/ethernet_frame.rb +56 -0
  79. data/spec/fixtures/event.rb +44 -0
  80. data/spec/fixtures/g3_concert.rb +57 -0
  81. data/spec/fixtures/integer_dumped_as_string_property.rb +24 -0
  82. data/spec/fixtures/jabberwock.rb +27 -0
  83. data/spec/fixtures/kayak.rb +28 -0
  84. data/spec/fixtures/lernean_hydra.rb +39 -0
  85. data/spec/fixtures/llama_spaceship.rb +15 -0
  86. data/spec/fixtures/mathematical_function.rb +34 -0
  87. data/spec/fixtures/memory_object.rb +35 -0
  88. data/spec/fixtures/mittelschnauzer.rb +39 -0
  89. data/spec/fixtures/motor_launch.rb +21 -0
  90. data/spec/fixtures/multibyte.rb +16 -0
  91. data/spec/fixtures/page.rb +32 -0
  92. data/spec/fixtures/phone_number.rb +28 -0
  93. data/spec/fixtures/pirogue.rb +28 -0
  94. data/spec/fixtures/programming_language.rb +83 -0
  95. data/spec/fixtures/reservation.rb +38 -0
  96. data/spec/fixtures/scm_operation.rb +56 -0
  97. data/spec/fixtures/sms_message.rb +22 -0
  98. data/spec/fixtures/udp_packet.rb +49 -0
  99. data/spec/integration/absent_field_validator/absent_field_validator_spec.rb +90 -0
  100. data/spec/integration/absent_field_validator/spec_helper.rb +7 -0
  101. data/spec/integration/acceptance_validator/acceptance_validator_spec.rb +196 -0
  102. data/spec/integration/acceptance_validator/spec_helper.rb +7 -0
  103. data/spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb +57 -0
  104. data/spec/integration/automatic_validation/disabling_inferred_validation_spec.rb +49 -0
  105. data/spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +100 -0
  106. data/spec/integration/automatic_validation/inferred_float_property_validation_spec.rb +45 -0
  107. data/spec/integration/automatic_validation/inferred_format_validation_spec.rb +35 -0
  108. data/spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +70 -0
  109. data/spec/integration/automatic_validation/inferred_length_validation_spec.rb +142 -0
  110. data/spec/integration/automatic_validation/inferred_presence_validation_spec.rb +45 -0
  111. data/spec/integration/automatic_validation/inferred_primitive_validation_spec.rb +22 -0
  112. data/spec/integration/automatic_validation/inferred_uniqueness_validation_spec.rb +48 -0
  113. data/spec/integration/automatic_validation/inferred_within_validation_spec.rb +35 -0
  114. data/spec/integration/automatic_validation/spec_helper.rb +57 -0
  115. data/spec/integration/block_validator/spec_helper.rb +5 -0
  116. data/spec/integration/conditional_validation/if_condition_spec.rb +63 -0
  117. data/spec/integration/conditional_validation/spec_helper.rb +5 -0
  118. data/spec/integration/confirmation_validator/confirmation_validator_spec.rb +76 -0
  119. data/spec/integration/confirmation_validator/spec_helper.rb +5 -0
  120. data/spec/integration/datamapper_models/association_validation_spec.rb +29 -0
  121. data/spec/integration/datamapper_models/inheritance_spec.rb +82 -0
  122. data/spec/integration/dirty_attributes/dirty_attributes_spec.rb +13 -0
  123. data/spec/integration/duplicated_validations/duplicated_validations_spec.rb +24 -0
  124. data/spec/integration/duplicated_validations/spec_helper.rb +5 -0
  125. data/spec/integration/format_validator/email_format_validator_spec.rb +139 -0
  126. data/spec/integration/format_validator/format_validator_spec.rb +64 -0
  127. data/spec/integration/format_validator/regexp_validator_spec.rb +33 -0
  128. data/spec/integration/format_validator/spec_helper.rb +5 -0
  129. data/spec/integration/format_validator/url_format_validator_spec.rb +91 -0
  130. data/spec/integration/length_validator/default_value_spec.rb +14 -0
  131. data/spec/integration/length_validator/equality_spec.rb +83 -0
  132. data/spec/integration/length_validator/error_message_spec.rb +22 -0
  133. data/spec/integration/length_validator/maximum_spec.rb +47 -0
  134. data/spec/integration/length_validator/minimum_spec.rb +54 -0
  135. data/spec/integration/length_validator/range_spec.rb +87 -0
  136. data/spec/integration/length_validator/spec_helper.rb +7 -0
  137. data/spec/integration/method_validator/method_validator_spec.rb +243 -0
  138. data/spec/integration/method_validator/spec_helper.rb +5 -0
  139. data/spec/integration/numeric_validator/equality_with_float_type_spec.rb +65 -0
  140. data/spec/integration/numeric_validator/equality_with_integer_type_spec.rb +41 -0
  141. data/spec/integration/numeric_validator/float_type_spec.rb +90 -0
  142. data/spec/integration/numeric_validator/gt_with_float_type_spec.rb +37 -0
  143. data/spec/integration/numeric_validator/gte_with_float_type_spec.rb +36 -0
  144. data/spec/integration/numeric_validator/integer_only_true_spec.rb +91 -0
  145. data/spec/integration/numeric_validator/integer_type_spec.rb +86 -0
  146. data/spec/integration/numeric_validator/lt_with_float_type_spec.rb +37 -0
  147. data/spec/integration/numeric_validator/lte_with_float_type_spec.rb +37 -0
  148. data/spec/integration/numeric_validator/spec_helper.rb +5 -0
  149. data/spec/integration/primitive_validator/primitive_validator_spec.rb +112 -0
  150. data/spec/integration/primitive_validator/spec_helper.rb +5 -0
  151. data/spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +118 -0
  152. data/spec/integration/required_field_validator/association_spec.rb +69 -0
  153. data/spec/integration/required_field_validator/boolean_type_value_spec.rb +164 -0
  154. data/spec/integration/required_field_validator/date_type_value_spec.rb +127 -0
  155. data/spec/integration/required_field_validator/datetime_type_value_spec.rb +127 -0
  156. data/spec/integration/required_field_validator/float_type_value_spec.rb +131 -0
  157. data/spec/integration/required_field_validator/integer_type_value_spec.rb +99 -0
  158. data/spec/integration/required_field_validator/plain_old_ruby_object_spec.rb +35 -0
  159. data/spec/integration/required_field_validator/shared_examples.rb +27 -0
  160. data/spec/integration/required_field_validator/spec_helper.rb +7 -0
  161. data/spec/integration/required_field_validator/string_type_value_spec.rb +167 -0
  162. data/spec/integration/required_field_validator/text_type_value_spec.rb +49 -0
  163. data/spec/integration/shared/default_validation_context.rb +13 -0
  164. data/spec/integration/shared/valid_and_invalid_model.rb +35 -0
  165. data/spec/integration/uniqueness_validator/spec_helper.rb +5 -0
  166. data/spec/integration/uniqueness_validator/uniqueness_validator_spec.rb +116 -0
  167. data/spec/integration/within_validator/spec_helper.rb +5 -0
  168. data/spec/integration/within_validator/within_validator_spec.rb +168 -0
  169. data/spec/public/resource_spec.rb +113 -0
  170. data/spec/spec_helper.rb +28 -0
  171. data/spec/unit/contextual_validators/emptiness_spec.rb +50 -0
  172. data/spec/unit/contextual_validators/execution_spec.rb +48 -0
  173. data/spec/unit/contextual_validators/spec_helper.rb +37 -0
  174. data/spec/unit/generic_validator/equality_operator_spec.rb +26 -0
  175. data/spec/unit/generic_validator/optional_spec.rb +54 -0
  176. data/spec/unit/validators/within_validator_spec.rb +23 -0
  177. data/spec/unit/violation_set/adding_spec.rb +54 -0
  178. data/spec/unit/violation_set/emptiness_spec.rb +38 -0
  179. data/spec/unit/violation_set/enumerable_spec.rb +32 -0
  180. data/spec/unit/violation_set/reading_spec.rb +35 -0
  181. data/spec/unit/violation_set/respond_to_spec.rb +15 -0
  182. data/tasks/spec.rake +21 -0
  183. data/tasks/yard.rake +9 -0
  184. data/tasks/yardstick.rake +19 -0
  185. metadata +245 -0
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../../spec_helper'
4
+ require_relative 'spec_helper'
5
+
6
+ shared_examples 'entity with wrong destination MAC address length' do
7
+ it 'has error message with range bounds' do
8
+ expect(@model.errors.on(:destination_mac)).to eq ['Destination mac must be 6 characters long']
9
+ end
10
+ end
11
+
12
+ describe 'DataMapper::Validations::Fixtures::EthernetFrame' do
13
+ before :all do
14
+ DataMapper::Validations::Fixtures::EthernetFrame.auto_migrate!
15
+
16
+ @model = DataMapper::Validations::Fixtures::EthernetFrame.valid_instance
17
+ @model.link_support_fragmentation = false
18
+ end
19
+
20
+ it_behaves_like 'valid model'
21
+
22
+ describe "with destination MAC 3 'bits' long" do
23
+ before :all do
24
+ @model.destination_mac = "123"
25
+ @model.valid?
26
+ end
27
+
28
+ it_behaves_like 'invalid model'
29
+ it_behaves_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_behaves_like 'invalid model'
39
+ it_behaves_like 'entity with wrong destination MAC address length'
40
+ end
41
+
42
+ # arguable but reasonable for 80% of cases
43
+ # to treat nil as a 0 lengh value
44
+ # reported in
45
+ # http://datamapper.lighthouseapp.com/projects/20609/tickets/646
46
+ describe "that has no destination MAC address" do
47
+ before :all do
48
+ @model.destination_mac = nil
49
+ @model.valid?
50
+ end
51
+
52
+ it_behaves_like 'invalid model'
53
+ it_behaves_like 'entity with wrong destination MAC address length'
54
+ end
55
+
56
+ describe "with a 6 'bits' destination MAC address" do
57
+ before :all do
58
+ @model.destination_mac = "a1b2c3"
59
+ @model.valid?
60
+ end
61
+
62
+ it_behaves_like 'valid model'
63
+ end
64
+
65
+ describe "with multibyte characters" do
66
+ before :all do
67
+ begin
68
+ # force normal encoding in this block
69
+ original, $KCODE = $KCODE, 'N' if RUBY_VERSION <= '1.8.6'
70
+
71
+ # example from: http://intertwingly.net/stories/2004/04/14/i18n.html
72
+ @model = DataMapper::Validations::Fixtures::Multibyte.new(
73
+ :name => 'Iñtërnâtiônàlizætiøn'
74
+ )
75
+ expect(@model).to be_valid
76
+ ensure
77
+ $KCODE = original if RUBY_VERSION <= '1.8.6'
78
+ end
79
+ end
80
+
81
+ it_behaves_like 'valid model'
82
+ end
83
+ end
@@ -0,0 +1,22 @@
1
+ require_relative '../../spec_helper'
2
+ require_relative 'spec_helper'
3
+
4
+ describe 'DataMapper::Validations::Fixtures::Jabberwock' do
5
+ before :all do
6
+ DataMapper::Validations::Fixtures::Jabberwock.auto_migrate!
7
+
8
+ @model = DataMapper::Validations::Fixtures::Jabberwock.new
9
+ end
10
+
11
+ describe "without snickersnack" do
12
+ before :all do
13
+ @model.snickersnack = nil
14
+ end
15
+
16
+ it_behaves_like 'invalid model'
17
+
18
+ it "has custom error message" do
19
+ expect(@model.errors.on(:snickersnack)).to eq [ 'worble warble' ]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,47 @@
1
+ require_relative '../../spec_helper'
2
+ require_relative 'spec_helper'
3
+
4
+ shared_examples 'barcode with invalid code length' do
5
+ it 'has a meaningful error message with length restrictions mentioned' do
6
+ expect(@model.errors.on(:code)).to eq ['Code must be at most 10 characters long']
7
+ end
8
+ end
9
+
10
+ describe 'DataMapper::Validations::Fixtures::Barcode' do
11
+ before :all do
12
+ DataMapper::Validations::Fixtures::Barcode.auto_migrate!
13
+
14
+ @model = DataMapper::Validations::Fixtures::Barcode.valid_instance
15
+ end
16
+
17
+ it_behaves_like 'valid model'
18
+
19
+ describe "with a 17 characters long code" do
20
+ before :all do
21
+ @model.code = "18283849284728124"
22
+ @model.valid?
23
+ end
24
+
25
+ it_behaves_like 'invalid model'
26
+ it_behaves_like 'barcode with invalid code length'
27
+ end
28
+
29
+ describe "with a 7 characters long code" do
30
+ before :all do
31
+ @model.code = "8372786"
32
+ @model.valid?
33
+ end
34
+
35
+ it_behaves_like 'valid model'
36
+ end
37
+
38
+ describe "with an 11 characters long code" do
39
+ before :all do
40
+ @model.code = "83727868754"
41
+ @model.valid?
42
+ end
43
+
44
+ it_behaves_like 'invalid model'
45
+ it_behaves_like 'barcode with invalid code length'
46
+ end
47
+ end
@@ -0,0 +1,54 @@
1
+ require_relative '../../spec_helper'
2
+ require_relative 'spec_helper'
3
+
4
+ shared_examples 'entity with a name shorter than 2 characters' do
5
+ it 'has a meaningful error message with length restrictions mentioned' do
6
+ expect(@model.errors.on(:name)).to eq ['Name must be at least 2 characters long']
7
+ end
8
+ end
9
+
10
+ describe 'DataMapper::Validations::Fixtures::Mittelschnauzer' do
11
+ before :all do
12
+ DataMapper::Validations::Fixtures::Mittelschnauzer.auto_migrate!
13
+
14
+ @model = DataMapper::Validations::Fixtures::Mittelschnauzer.valid_instance
15
+ end
16
+
17
+ it_behaves_like 'valid model'
18
+
19
+ describe 'with a 13 characters long name' do
20
+ it_behaves_like 'valid model'
21
+ end
22
+
23
+ describe "with a single character name" do
24
+ before :all do
25
+ @model.name = "R"
26
+ @model.valid?
27
+ end
28
+
29
+ it_behaves_like 'invalid model'
30
+
31
+ it_behaves_like 'entity with a name shorter than 2 characters'
32
+ end
33
+
34
+ describe "with blank name" do
35
+ before :all do
36
+ @model.name = ""
37
+ @model.valid?
38
+ end
39
+
40
+ it_behaves_like 'invalid model'
41
+
42
+ it_behaves_like 'entity with a name shorter than 2 characters'
43
+ end
44
+
45
+ describe "persisted, with a single character owner" do
46
+ before :all do
47
+ @model.save
48
+ @model.owner = 'a'
49
+ @model.valid?
50
+ end
51
+
52
+ it_behaves_like 'invalid model'
53
+ end
54
+ end
@@ -0,0 +1,87 @@
1
+ require_relative '../../spec_helper'
2
+ require_relative 'spec_helper'
3
+
4
+ describe 'DataMapper::Validations::Fixtures::EthernetFrame' do
5
+ before :all do
6
+ DataMapper::Validations::Fixtures::EthernetFrame.auto_migrate!
7
+
8
+ @model = DataMapper::Validations::Fixtures::EthernetFrame.valid_instance
9
+ @model.link_support_fragmentation = false
10
+ end
11
+
12
+ it_behaves_like 'valid model'
13
+
14
+ describe "with payload that is 7 'bits' long (too short)" do
15
+ before :all do
16
+ @model.payload = "1234567"
17
+ @model.valid?
18
+ end
19
+
20
+ it_behaves_like 'invalid model'
21
+
22
+ it "has error message with range bounds" do
23
+ expect(@model.errors.on(:payload)).to eq [ 'Payload must be between 46 and 1500 characters long' ]
24
+ end
25
+ end
26
+
27
+
28
+ describe "with a one character long payload (too short)" do
29
+ before :all do
30
+ @model.payload = 'a'
31
+ @model.valid?
32
+ end
33
+
34
+ it_behaves_like 'invalid model'
35
+
36
+ it "has error message with range bounds" do
37
+ expect(@model.errors.on(:payload)).to eq [ 'Payload must be between 46 and 1500 characters long' ]
38
+ end
39
+ end
40
+
41
+
42
+ describe "with a 1600 'bits' long payload (needs fragmentation)" do
43
+ before :all do
44
+ @model.payload = 'a'
45
+ @model.valid?
46
+ end
47
+
48
+ it_behaves_like 'invalid model'
49
+
50
+ it "has error message with range bounds" do
51
+ expect(@model.errors.on(:payload)).to eq [ 'Payload must be between 46 and 1500 characters long' ]
52
+ end
53
+ end
54
+
55
+
56
+ # arguable but reasonable for 80% of cases
57
+ # to treat nil as a 0 lengh value
58
+ # reported in
59
+ # http://datamapper.lighthouseapp.com/projects/20609/tickets/646
60
+ describe "that has no payload" do
61
+ before :all do
62
+ @model.payload = nil
63
+ @model.valid?
64
+ end
65
+
66
+ it_behaves_like 'invalid model'
67
+
68
+ it "has error message with range bounds" do
69
+ expect(@model.errors.on(:payload)).to eq [ 'Payload must be between 46 and 1500 characters long' ]
70
+ end
71
+ end
72
+
73
+
74
+
75
+ describe "with a 50 characters long payload" do
76
+ before :all do
77
+ @model.payload = 'Imagine yourself a beautiful bag full of bits here'
78
+ @model.valid?
79
+ end
80
+
81
+ it_behaves_like 'valid model'
82
+
83
+ it "has blank error message" do
84
+ expect(@model.errors.on(:payload)).to be_nil
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,7 @@
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
@@ -0,0 +1,243 @@
1
+ require_relative '../../spec_helper'
2
+ require_relative 'spec_helper'
3
+
4
+ shared_examples 'a good fit for DSLs' do
5
+ it 'is a good fit for DSLs' do
6
+ expect(@model).to be_valid_for_implementing_a_dsl
7
+ end
8
+
9
+ it 'is totally fine for DSLs' do
10
+ expect(@model.ensure_appropriate_for_dsls).to be(true)
11
+ end
12
+ end
13
+
14
+ shared_examples 'a poor candidate for DSLs' do
15
+ it 'is a poor candidate for DSLs' do
16
+ expect(@model).not_to be_valid_for_implementing_a_dsl
17
+ end
18
+
19
+ it 'is unappropriate for DSLs' do
20
+ expect(@model.ensure_appropriate_for_dsls.first).to be(false)
21
+ end
22
+
23
+ it 'has a (more or less) meaningful error message' do
24
+ expect(@model.errors.on(:ensure_appropriate_for_dsls)).to eq ['may not be so good for domain specific languages']
25
+ end
26
+
27
+ it 'has a violation type' do
28
+ expect(@model.errors[:ensure_appropriate_for_dsls].map(&:type)).to eq [:unsatisfied_condition]
29
+ end
30
+ end
31
+
32
+ shared_examples 'a good fit for game engine core' do
33
+ it 'is a good fit for game engine core' do
34
+ expect(@model).to be_valid_for_implementing_a_game_engine_core
35
+ end
36
+
37
+ it 'is appropriate for system programming' do
38
+ expect(@model.ensure_appropriate_for_system_programming).to be(true)
39
+ end
40
+ end
41
+
42
+ shared_examples 'a poor candidate for game engine core' do
43
+ it 'is a poor candidate for game engine core' do
44
+ expect(@model).not_to be_valid_for_implementing_a_game_engine_core
45
+ end
46
+ end
47
+
48
+ shared_examples 'a good fit for kernel hacking' do
49
+ it 'is a good fit for kernel hacking' do
50
+ expect(@model).to be_valid_for_hacking_on_the_kernel
51
+ end
52
+
53
+ it 'is appropriate for system programming' do
54
+ expect(@model.ensure_appropriate_for_system_programming).to be(true)
55
+ end
56
+ end
57
+
58
+ shared_examples 'a poor candidate for kernel hacking' do
59
+ it 'is not a good fit for kernel hacking' do
60
+ expect(@model).not_to be_valid_for_hacking_on_the_kernel
61
+ end
62
+ end
63
+
64
+ #
65
+ # Note that ProgrammingLanguage is not a DM resource,
66
+ # it is a plain old Ruby object (PORO), or simply put,
67
+ # just a 'regular' Ruby class with a bunch of accessors.
68
+ #
69
+ # And yes, assumptions below are full of bullshit and
70
+ # author is a moron, and your favourite programming
71
+ # language is good for EVARYTHIN'!
72
+ #
73
+ #Please see spec_helper.rb next to this file
74
+ #
75
+
76
+ describe 'C' do
77
+ before :all do
78
+ @model = ::DataMapper::Validations::Fixtures::ProgrammingLanguage.new(:name => 'C',
79
+ :allows_system_calls => true,
80
+ :allows_manual_memory_management => true,
81
+ :approved_by_linus => true,
82
+ :compiler_excels_at_utilizing_cpu_cache => true,
83
+ :is_very_high_level => false,
84
+ :does_not_require_explicit_return_keyword => false,
85
+ :allows_operator_overload => false,
86
+ :allows_optional_parentheses => false
87
+ )
88
+ end
89
+
90
+ it_behaves_like 'a good fit for kernel hacking'
91
+
92
+ it_behaves_like 'a good fit for game engine core'
93
+
94
+ it_behaves_like 'a poor candidate for DSLs'
95
+ end
96
+
97
+
98
+
99
+ describe 'C++' do
100
+ before :all do
101
+ @model = ::DataMapper::Validations::Fixtures::ProgrammingLanguage.new(:name => 'C++',
102
+ :allows_system_calls => true,
103
+ :allows_manual_memory_management => true,
104
+ :approved_by_linus => false,
105
+ :compiler_excels_at_utilizing_cpu_cache => true,
106
+ :is_very_high_level => false,
107
+ :does_not_require_explicit_return_keyword => false,
108
+ :allows_operator_overload => true,
109
+ :allows_optional_parentheses => false
110
+ )
111
+ end
112
+
113
+ it_behaves_like 'a poor candidate for kernel hacking'
114
+
115
+ it_behaves_like 'a good fit for game engine core'
116
+
117
+ it_behaves_like 'a poor candidate for DSLs'
118
+
119
+ it 'is not approved by Linus' do
120
+ @model.valid?(:hacking_on_the_kernel)
121
+ expect(@model.errors.on(:ensure_approved_by_linus_himself)).not_to be_empty
122
+ expect(@model.errors[:ensure_approved_by_linus_himself].map(&:type)).to eq [:unapproved_by_linus]
123
+ end
124
+ end
125
+
126
+
127
+ describe 'Ruby' do
128
+ before :all do
129
+ @model = ::DataMapper::Validations::Fixtures::ProgrammingLanguage.new(:name => 'Ruby',
130
+ # well, C extensions do not really count
131
+ :allows_system_calls => false,
132
+ :allows_manual_memory_management => false,
133
+ :approved_by_linus => false,
134
+ :compiler_excels_at_utilizing_cpu_cache => false,
135
+ :is_very_high_level => true,
136
+ :does_not_require_explicit_return_keyword => true,
137
+ :allows_operator_overload => true,
138
+ :allows_optional_parentheses => true
139
+ )
140
+ end
141
+
142
+ it_behaves_like 'a good fit for DSLs'
143
+
144
+ it_behaves_like 'a poor candidate for kernel hacking'
145
+
146
+ it_behaves_like 'a poor candidate for game engine core'
147
+
148
+ it 'has a (more or less) meaningful error message' do
149
+ @model.valid?(:doing_system_programming)
150
+ expect(@model.errors.on(:ensure_appropriate_for_system_programming)).to eq [ 'try something that is closer to the metal' ]
151
+ end
152
+ end
153
+
154
+
155
+ describe 'Scala' do
156
+ before :all do
157
+ @model = ::DataMapper::Validations::Fixtures::ProgrammingLanguage.new(:name => 'Scala',
158
+ :allows_system_calls => false,
159
+ :allows_manual_memory_management => false,
160
+ :approved_by_linus => false,
161
+ :compiler_excels_at_utilizing_cpu_cache => false,
162
+ :is_very_high_level => true,
163
+ :does_not_require_explicit_return_keyword => true,
164
+ :allows_operator_overload => true,
165
+ :allows_optional_parentheses => true
166
+ )
167
+ end
168
+
169
+ it_behaves_like 'a good fit for DSLs'
170
+
171
+ it_behaves_like 'a poor candidate for kernel hacking'
172
+
173
+ it_behaves_like 'a poor candidate for game engine core'
174
+
175
+ it 'has a (more or less) meaningful error message' do
176
+ @model.valid?(:doing_system_programming)
177
+ expect(@model.errors.on(:ensure_appropriate_for_system_programming)).to eq [ 'try something that is closer to the metal' ]
178
+ end
179
+ end
180
+
181
+
182
+
183
+ describe 'Haskell' do
184
+ before :all do
185
+ @model = ::DataMapper::Validations::Fixtures::ProgrammingLanguage.new(
186
+ :name => 'Haskell',
187
+ :allows_system_calls => false,
188
+ :allows_manual_memory_management => false,
189
+ :approved_by_linus => false,
190
+ :compiler_excels_at_utilizing_cpu_cache => true,
191
+ :is_very_high_level => true,
192
+ :does_not_require_explicit_return_keyword => true,
193
+ # operators are not different from any other function
194
+ :allows_operator_overload => true,
195
+ # or, allows explicit parentheses? who cares, we
196
+ # are just trying to come up with a relatively
197
+ # non-stupid spec example
198
+ :allows_optional_parentheses => true
199
+ )
200
+ end
201
+
202
+ it_behaves_like 'a good fit for DSLs'
203
+
204
+ it_behaves_like 'a poor candidate for kernel hacking'
205
+
206
+ it_behaves_like 'a poor candidate for game engine core'
207
+
208
+ it 'has a (more or less) meaningful error message' do
209
+ @model.valid?(:doing_system_programming)
210
+ expect(@model.errors.on(:ensure_appropriate_for_system_programming)).to eq [ 'try something that is closer to the metal' ]
211
+ end
212
+ end
213
+
214
+
215
+ describe 'DataMapper::Validations::Fixtures::Event' do
216
+ before :all do
217
+ DataMapper::Validations::Fixtures::Event.auto_migrate!
218
+
219
+ @model = DataMapper::Validations::Fixtures::Event.new(:name => 'Fools day 2009')
220
+ end
221
+
222
+ describe 'with start time before end time' do
223
+ before :all do
224
+ @model.starts_at = DateTime.new(2009, 4, 1, 00, 00, 01)
225
+ @model.ends_at = DateTime.new(2009, 4, 1, 23, 59, 59)
226
+ end
227
+
228
+ it_behaves_like 'valid model'
229
+ end
230
+
231
+ describe 'with start time after end time' do
232
+ before :all do
233
+ @model.starts_at = DateTime.new(2009, 4, 1, 23, 59, 59)
234
+ @model.ends_at = DateTime.new(2009, 4, 1, 00, 00, 01)
235
+ end
236
+
237
+ it_behaves_like 'invalid model'
238
+
239
+ it 'uses custom error message for property starts_at' do
240
+ expect(@model.errors.on(:starts_at)).to eq [ 'Start time cannot be after end time' ]
241
+ end
242
+ end
243
+ 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,65 @@
1
+ require_relative '../../spec_helper'
2
+ require_relative 'spec_helper'
3
+
4
+ describe 'DataMapper::Validations::Fixtures::BasketballCourt' do
5
+ before :all do
6
+ DataMapper::Validations::Fixtures::BasketballCourt.auto_migrate!
7
+ end
8
+
9
+ describe "with valid set of attributes" do
10
+ before :all do
11
+ @model = DataMapper::Validations::Fixtures::BasketballCourt.valid_instance
12
+ @model.valid?
13
+ end
14
+
15
+ it_behaves_like 'valid model'
16
+ end
17
+
18
+
19
+ describe "with rim height of 3.05" do
20
+ before :all do
21
+ @model = DataMapper::Validations::Fixtures::BasketballCourt.valid_instance(:rim_height => 3.05)
22
+ @model.valid?
23
+ end
24
+
25
+ it_behaves_like 'valid model'
26
+ end
27
+
28
+
29
+ describe "with rim height of 3.30" do
30
+ before :all do
31
+ @model = DataMapper::Validations::Fixtures::BasketballCourt.valid_instance(:rim_height => 3.30)
32
+ @model.valid?
33
+ end
34
+
35
+ it_behaves_like 'invalid model'
36
+
37
+ it "has a meaningful error message" do
38
+ expect(@model.errors.on(:rim_height)).to eq [ 'Rim height must be equal to 3.05' ]
39
+ end
40
+ end
41
+
42
+
43
+ describe "with free throw line distance of 4.57" do
44
+ before :all do
45
+ @model = DataMapper::Validations::Fixtures::BasketballCourt.valid_instance(:free_throw_line_distance => 4.57)
46
+ @model.valid?
47
+ end
48
+
49
+ it_behaves_like 'valid model'
50
+ end
51
+
52
+
53
+ describe "with free throw line distance of 3.10" do
54
+ before :all do
55
+ @model = DataMapper::Validations::Fixtures::BasketballCourt.valid_instance(:free_throw_line_distance => 3.10)
56
+ @model.valid?
57
+ end
58
+
59
+ it_behaves_like 'invalid model'
60
+
61
+ it "has a meaningful error message" do
62
+ expect(@model.errors.on(:free_throw_line_distance)).to eq [ 'Free throw line distance must be equal to 4.57' ]
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,41 @@
1
+ require_relative '../../spec_helper'
2
+ require_relative 'spec_helper'
3
+
4
+ describe 'DataMapper::Validations::Fixtures::LerneanHydra' do
5
+ before :all do
6
+ DataMapper::Validations::Fixtures::LerneanHydra.auto_migrate!
7
+ end
8
+
9
+ describe "with valid set of attributes" do
10
+ before :all do
11
+ @model = DataMapper::Validations::Fixtures::LerneanHydra.valid_instance
12
+ @model.valid?
13
+ end
14
+
15
+ it_behaves_like 'valid model'
16
+ end
17
+
18
+
19
+ describe "with 9 heads" do
20
+ before :all do
21
+ @model = DataMapper::Validations::Fixtures::LerneanHydra.valid_instance(:head_count => 9)
22
+ @model.valid?
23
+ end
24
+
25
+ it_behaves_like 'valid model'
26
+ end
27
+
28
+
29
+ describe "with only 3 heads" do
30
+ before :all do
31
+ @model = DataMapper::Validations::Fixtures::LerneanHydra.valid_instance(:head_count => 3)
32
+ @model.valid?
33
+ end
34
+
35
+ it_behaves_like 'invalid model'
36
+
37
+ it "has a meaningful error message" do
38
+ expect(@model.errors.on(:head_count)).to eq [ 'Lernean hydra is said to have exactly 9 heads' ]
39
+ end
40
+ end
41
+ end