aequitas 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (216) hide show
  1. data/.gitignore +4 -0
  2. data/.rvmrc +1 -0
  3. data/Gemfile +6 -0
  4. data/LICENSE +21 -0
  5. data/README.rdoc +125 -0
  6. data/Rakefile +25 -0
  7. data/VERSION +1 -0
  8. data/aequitas.gemspec +20 -0
  9. data/lib/aequitas.rb +80 -0
  10. data/lib/aequitas/class_methods.rb +26 -0
  11. data/lib/aequitas/context.rb +53 -0
  12. data/lib/aequitas/contextual_rule_set.rb +221 -0
  13. data/lib/aequitas/exceptions.rb +10 -0
  14. data/lib/aequitas/macros.rb +466 -0
  15. data/lib/aequitas/message_transformer.rb +127 -0
  16. data/lib/aequitas/rule.rb +153 -0
  17. data/lib/aequitas/rule/absence.rb +14 -0
  18. data/lib/aequitas/rule/absence/blank.rb +21 -0
  19. data/lib/aequitas/rule/absence/nil.rb +21 -0
  20. data/lib/aequitas/rule/acceptance.rb +36 -0
  21. data/lib/aequitas/rule/block.rb +33 -0
  22. data/lib/aequitas/rule/confirmation.rb +41 -0
  23. data/lib/aequitas/rule/format.rb +81 -0
  24. data/lib/aequitas/rule/format/proc.rb +28 -0
  25. data/lib/aequitas/rule/format/regexp.rb +44 -0
  26. data/lib/aequitas/rule/formats/email.rb +52 -0
  27. data/lib/aequitas/rule/formats/url.rb +14 -0
  28. data/lib/aequitas/rule/guard.rb +51 -0
  29. data/lib/aequitas/rule/length.rb +87 -0
  30. data/lib/aequitas/rule/length/equal.rb +46 -0
  31. data/lib/aequitas/rule/length/maximum.rb +46 -0
  32. data/lib/aequitas/rule/length/minimum.rb +46 -0
  33. data/lib/aequitas/rule/length/range.rb +46 -0
  34. data/lib/aequitas/rule/method.rb +31 -0
  35. data/lib/aequitas/rule/numericalness.rb +85 -0
  36. data/lib/aequitas/rule/numericalness/equal.rb +30 -0
  37. data/lib/aequitas/rule/numericalness/greater_than.rb +30 -0
  38. data/lib/aequitas/rule/numericalness/greater_than_or_equal.rb +30 -0
  39. data/lib/aequitas/rule/numericalness/integer.rb +37 -0
  40. data/lib/aequitas/rule/numericalness/less_than.rb +30 -0
  41. data/lib/aequitas/rule/numericalness/less_than_or_equal.rb +30 -0
  42. data/lib/aequitas/rule/numericalness/non_integer.rb +64 -0
  43. data/lib/aequitas/rule/numericalness/not_equal.rb +30 -0
  44. data/lib/aequitas/rule/presence.rb +14 -0
  45. data/lib/aequitas/rule/presence/not_blank.rb +21 -0
  46. data/lib/aequitas/rule/presence/not_nil.rb +21 -0
  47. data/lib/aequitas/rule/primitive_type.rb +28 -0
  48. data/lib/aequitas/rule/skip_condition.rb +104 -0
  49. data/lib/aequitas/rule/within.rb +28 -0
  50. data/lib/aequitas/rule/within/range.rb +53 -0
  51. data/lib/aequitas/rule/within/range/bounded.rb +25 -0
  52. data/lib/aequitas/rule/within/range/unbounded_begin.rb +25 -0
  53. data/lib/aequitas/rule/within/range/unbounded_end.rb +25 -0
  54. data/lib/aequitas/rule/within/set.rb +39 -0
  55. data/lib/aequitas/rule_set.rb +80 -0
  56. data/lib/aequitas/support/blank.rb +22 -0
  57. data/lib/aequitas/support/equalizable.rb +113 -0
  58. data/lib/aequitas/support/ordered_hash.rb +438 -0
  59. data/lib/aequitas/version.rb +5 -0
  60. data/lib/aequitas/violation.rb +116 -0
  61. data/lib/aequitas/violation_set.rb +123 -0
  62. data/lib/aequitas/virtus.rb +29 -0
  63. data/lib/aequitas/virtus/inline_attribute_rule_extractor.rb +41 -0
  64. data/lib/aequitas/virtus/inline_attribute_rule_extractor/boolean.rb +17 -0
  65. data/lib/aequitas/virtus/inline_attribute_rule_extractor/object.rb +25 -0
  66. data/lib/aequitas/virtus/inline_attribute_rule_extractor/string.rb +27 -0
  67. data/spec/integration/aequitas/macros/validates_absence_of_spec.rb +19 -0
  68. data/spec/integration/aequitas/macros/validates_acceptance_of_spec.rb +19 -0
  69. data/spec/integration/aequitas/macros/validates_confirmation_of_spec.rb +25 -0
  70. data/spec/integration/aequitas/macros/validates_format_of_spec.rb +87 -0
  71. data/spec/integration/aequitas/macros/validates_length_of.rb +77 -0
  72. data/spec/integration/aequitas/macros/validates_numericalness_of_spec.rb +221 -0
  73. data/spec/integration/aequitas/macros/validates_presence_of_spec.rb +19 -0
  74. data/spec/integration/aequitas/macros/validates_with_block.rb +23 -0
  75. data/spec/integration/aequitas/macros/validates_with_method.rb +19 -0
  76. data/spec/integration/aequitas/macros/validates_within.rb +87 -0
  77. data/spec/integration/shared/macros/integration_spec.rb +67 -0
  78. data/spec/integration/virtus/boolean/presence_spec.rb +49 -0
  79. data/spec/integration/virtus/string/format/email_address_spec.rb +55 -0
  80. data/spec/integration/virtus/string/format/regexp_spec.rb +55 -0
  81. data/spec/integration/virtus/string/format/url_spec.rb +55 -0
  82. data/spec/integration/virtus/string/length/equal_spec.rb +49 -0
  83. data/spec/integration/virtus/string/length/range_spec.rb +49 -0
  84. data/spec/integration/virtus/string/presence_spec.rb +49 -0
  85. data/spec/rcov.opts +6 -0
  86. data/spec/spec_helper.rb +5 -0
  87. data/spec/suite.rb +5 -0
  88. data/spec/unit/aequitas/rule/absence/blank_spec.rb +45 -0
  89. data/spec/unit/aequitas/rule/acceptance_spec.rb +71 -0
  90. data/spec/unit/aequitas/rule/confirmation_spec.rb +80 -0
  91. data/spec/unit/aequitas/rule/guard_spec.rb +120 -0
  92. data/spec/unit/aequitas/rule/skip_condition_spec.rb +170 -0
  93. data/spec/unit/aequitas/rule_spec.rb +40 -0
  94. data/spec/unit/aequitas/support/blank_spec.rb +83 -0
  95. data/spec/unit/aequitas/support/equalizable/equalizer_spec.rb +21 -0
  96. data/spec/unit/aequitas/support/equalizable_spec.rb +67 -0
  97. data/spec/unit/aequitas/violation_set_spec.rb +154 -0
  98. data/spec_legacy/fixtures/barcode.rb +40 -0
  99. data/spec_legacy/fixtures/basketball_court.rb +58 -0
  100. data/spec_legacy/fixtures/basketball_player.rb +34 -0
  101. data/spec_legacy/fixtures/beta_tester_account.rb +33 -0
  102. data/spec_legacy/fixtures/bill_of_landing.rb +47 -0
  103. data/spec_legacy/fixtures/boat_dock.rb +26 -0
  104. data/spec_legacy/fixtures/city.rb +24 -0
  105. data/spec_legacy/fixtures/company.rb +93 -0
  106. data/spec_legacy/fixtures/corporate_world.rb +39 -0
  107. data/spec_legacy/fixtures/country.rb +24 -0
  108. data/spec_legacy/fixtures/ethernet_frame.rb +56 -0
  109. data/spec_legacy/fixtures/event.rb +44 -0
  110. data/spec_legacy/fixtures/g3_concert.rb +57 -0
  111. data/spec_legacy/fixtures/jabberwock.rb +27 -0
  112. data/spec_legacy/fixtures/kayak.rb +28 -0
  113. data/spec_legacy/fixtures/lernean_hydra.rb +39 -0
  114. data/spec_legacy/fixtures/llama_spaceship.rb +15 -0
  115. data/spec_legacy/fixtures/mathematical_function.rb +34 -0
  116. data/spec_legacy/fixtures/memory_object.rb +30 -0
  117. data/spec_legacy/fixtures/mittelschnauzer.rb +39 -0
  118. data/spec_legacy/fixtures/motor_launch.rb +21 -0
  119. data/spec_legacy/fixtures/multibyte.rb +16 -0
  120. data/spec_legacy/fixtures/page.rb +32 -0
  121. data/spec_legacy/fixtures/phone_number.rb +28 -0
  122. data/spec_legacy/fixtures/pirogue.rb +28 -0
  123. data/spec_legacy/fixtures/programming_language.rb +83 -0
  124. data/spec_legacy/fixtures/reservation.rb +38 -0
  125. data/spec_legacy/fixtures/scm_operation.rb +56 -0
  126. data/spec_legacy/fixtures/sms_message.rb +22 -0
  127. data/spec_legacy/fixtures/udp_packet.rb +49 -0
  128. data/spec_legacy/integration/absent_field_validator/absent_field_validator_spec.rb +90 -0
  129. data/spec_legacy/integration/absent_field_validator/spec_helper.rb +7 -0
  130. data/spec_legacy/integration/acceptance_validator/acceptance_validator_spec.rb +196 -0
  131. data/spec_legacy/integration/acceptance_validator/spec_helper.rb +7 -0
  132. data/spec_legacy/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb +57 -0
  133. data/spec_legacy/integration/automatic_validation/disabling_inferred_validation_spec.rb +49 -0
  134. data/spec_legacy/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +100 -0
  135. data/spec_legacy/integration/automatic_validation/inferred_float_property_validation_spec.rb +45 -0
  136. data/spec_legacy/integration/automatic_validation/inferred_format_validation_spec.rb +35 -0
  137. data/spec_legacy/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +70 -0
  138. data/spec_legacy/integration/automatic_validation/inferred_length_validation_spec.rb +142 -0
  139. data/spec_legacy/integration/automatic_validation/inferred_presence_validation_spec.rb +45 -0
  140. data/spec_legacy/integration/automatic_validation/inferred_primitive_validation_spec.rb +22 -0
  141. data/spec_legacy/integration/automatic_validation/inferred_uniqueness_validation_spec.rb +48 -0
  142. data/spec_legacy/integration/automatic_validation/inferred_within_validation_spec.rb +35 -0
  143. data/spec_legacy/integration/automatic_validation/spec_helper.rb +57 -0
  144. data/spec_legacy/integration/block_validator/block_validator_spec.rb +32 -0
  145. data/spec_legacy/integration/block_validator/spec_helper.rb +5 -0
  146. data/spec_legacy/integration/conditional_validation/if_condition_spec.rb +63 -0
  147. data/spec_legacy/integration/conditional_validation/spec_helper.rb +5 -0
  148. data/spec_legacy/integration/confirmation_validator/confirmation_validator_spec.rb +76 -0
  149. data/spec_legacy/integration/confirmation_validator/spec_helper.rb +5 -0
  150. data/spec_legacy/integration/datamapper_models/association_validation_spec.rb +29 -0
  151. data/spec_legacy/integration/datamapper_models/inheritance_spec.rb +82 -0
  152. data/spec_legacy/integration/dirty_attributes/dirty_attributes_spec.rb +13 -0
  153. data/spec_legacy/integration/duplicated_validations/duplicated_validations_spec.rb +24 -0
  154. data/spec_legacy/integration/duplicated_validations/spec_helper.rb +5 -0
  155. data/spec_legacy/integration/format_validator/email_format_validator_spec.rb +139 -0
  156. data/spec_legacy/integration/format_validator/format_validator_spec.rb +64 -0
  157. data/spec_legacy/integration/format_validator/regexp_validator_spec.rb +33 -0
  158. data/spec_legacy/integration/format_validator/spec_helper.rb +5 -0
  159. data/spec_legacy/integration/format_validator/url_format_validator_spec.rb +93 -0
  160. data/spec_legacy/integration/length_validator/default_value_spec.rb +14 -0
  161. data/spec_legacy/integration/length_validator/equality_spec.rb +87 -0
  162. data/spec_legacy/integration/length_validator/error_message_spec.rb +22 -0
  163. data/spec_legacy/integration/length_validator/maximum_spec.rb +49 -0
  164. data/spec_legacy/integration/length_validator/minimum_spec.rb +54 -0
  165. data/spec_legacy/integration/length_validator/range_spec.rb +87 -0
  166. data/spec_legacy/integration/length_validator/spec_helper.rb +7 -0
  167. data/spec_legacy/integration/method_validator/method_validator_spec.rb +242 -0
  168. data/spec_legacy/integration/method_validator/spec_helper.rb +5 -0
  169. data/spec_legacy/integration/numeric_validator/equality_with_float_type_spec.rb +65 -0
  170. data/spec_legacy/integration/numeric_validator/equality_with_integer_type_spec.rb +41 -0
  171. data/spec_legacy/integration/numeric_validator/float_type_spec.rb +90 -0
  172. data/spec_legacy/integration/numeric_validator/gt_with_float_type_spec.rb +37 -0
  173. data/spec_legacy/integration/numeric_validator/gte_with_float_type_spec.rb +37 -0
  174. data/spec_legacy/integration/numeric_validator/integer_only_true_spec.rb +91 -0
  175. data/spec_legacy/integration/numeric_validator/integer_type_spec.rb +86 -0
  176. data/spec_legacy/integration/numeric_validator/lt_with_float_type_spec.rb +37 -0
  177. data/spec_legacy/integration/numeric_validator/lte_with_float_type_spec.rb +37 -0
  178. data/spec_legacy/integration/numeric_validator/spec_helper.rb +5 -0
  179. data/spec_legacy/integration/primitive_validator/primitive_validator_spec.rb +92 -0
  180. data/spec_legacy/integration/primitive_validator/spec_helper.rb +5 -0
  181. data/spec_legacy/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +118 -0
  182. data/spec_legacy/integration/required_field_validator/association_spec.rb +69 -0
  183. data/spec_legacy/integration/required_field_validator/boolean_type_value_spec.rb +164 -0
  184. data/spec_legacy/integration/required_field_validator/date_type_value_spec.rb +127 -0
  185. data/spec_legacy/integration/required_field_validator/datetime_type_value_spec.rb +127 -0
  186. data/spec_legacy/integration/required_field_validator/float_type_value_spec.rb +131 -0
  187. data/spec_legacy/integration/required_field_validator/integer_type_value_spec.rb +99 -0
  188. data/spec_legacy/integration/required_field_validator/plain_old_ruby_object_spec.rb +35 -0
  189. data/spec_legacy/integration/required_field_validator/shared_examples.rb +26 -0
  190. data/spec_legacy/integration/required_field_validator/spec_helper.rb +7 -0
  191. data/spec_legacy/integration/required_field_validator/string_type_value_spec.rb +167 -0
  192. data/spec_legacy/integration/required_field_validator/text_type_value_spec.rb +49 -0
  193. data/spec_legacy/integration/shared/default_validation_context.rb +13 -0
  194. data/spec_legacy/integration/shared/valid_and_invalid_model.rb +35 -0
  195. data/spec_legacy/integration/uniqueness_validator/spec_helper.rb +5 -0
  196. data/spec_legacy/integration/uniqueness_validator/uniqueness_validator_spec.rb +116 -0
  197. data/spec_legacy/integration/within_validator/spec_helper.rb +5 -0
  198. data/spec_legacy/integration/within_validator/within_validator_spec.rb +168 -0
  199. data/spec_legacy/public/resource_spec.rb +105 -0
  200. data/spec_legacy/spec.opts +4 -0
  201. data/spec_legacy/spec_helper.rb +29 -0
  202. data/spec_legacy/unit/contextual_validators/emptiness_spec.rb +50 -0
  203. data/spec_legacy/unit/contextual_validators/execution_spec.rb +48 -0
  204. data/spec_legacy/unit/contextual_validators/spec_helper.rb +37 -0
  205. data/spec_legacy/unit/generic_validator/equality_operator_spec.rb +26 -0
  206. data/spec_legacy/unit/generic_validator/optional_spec.rb +54 -0
  207. data/spec_legacy/unit/validators/within_validator_spec.rb +23 -0
  208. data/spec_legacy/unit/violation_set/adding_spec.rb +54 -0
  209. data/spec_legacy/unit/violation_set/emptiness_spec.rb +38 -0
  210. data/spec_legacy/unit/violation_set/enumerable_spec.rb +32 -0
  211. data/spec_legacy/unit/violation_set/reading_spec.rb +35 -0
  212. data/spec_legacy/unit/violation_set/respond_to_spec.rb +15 -0
  213. data/tasks/spec.rake +38 -0
  214. data/tasks/yard.rake +9 -0
  215. data/tasks/yardstick.rake +19 -0
  216. metadata +302 -0
@@ -0,0 +1,49 @@
1
+ require_relative '../../../spec_helper'
2
+ require 'virtus'
3
+ require 'aequitas'
4
+ require 'aequitas/virtus'
5
+
6
+ describe Aequitas::Virtus::ClassMethodOverrides do
7
+ let(:class_under_test) do
8
+ Class.new do
9
+ include Virtus
10
+ include Aequitas
11
+
12
+ attribute :validated_attribute, String, :required => true
13
+
14
+ self
15
+ end
16
+ end
17
+
18
+ describe '.validation_rules' do
19
+ it 'includes a Rule::Presence::NotBlank for :validated_attribute' do
20
+ attribute_rules = class_under_test.validation_rules[:validated_attribute]
21
+ refute_predicate attribute_rules, :empty?
22
+ assert_instance_of Aequitas::Rule::Presence::NotBlank, attribute_rules.first
23
+ end
24
+ end
25
+
26
+ describe '#valid?' do
27
+ subject { class_under_test.new(:validated_attribute => attribute_value) }
28
+
29
+ describe 'when empty string' do
30
+ let(:attribute_value) { '' }
31
+ it('is not valid') { refute_predicate subject, :valid? }
32
+ end
33
+
34
+ describe 'when nil' do
35
+ let(:attribute_value) { nil }
36
+ it('is not valid') { refute_predicate subject, :valid? }
37
+ end
38
+
39
+ describe 'when a non-empty string' do
40
+ let(:attribute_value) { 'foo' }
41
+ it('is valid') { assert_predicate subject, :valid? }
42
+ end
43
+
44
+ describe 'when true' do
45
+ let(:attribute_value) { true }
46
+ it('is valid') { assert_predicate subject, :valid? }
47
+ end
48
+ end
49
+ end
data/spec/rcov.opts ADDED
@@ -0,0 +1,6 @@
1
+ --exclude "spec,^/"
2
+ --sort coverage
3
+ --callsites
4
+ --xrefs
5
+ --profile
6
+ --text-summary
@@ -0,0 +1,5 @@
1
+ gem 'minitest', '~>2.8'
2
+
3
+ require 'minitest/autorun'
4
+
5
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
data/spec/suite.rb ADDED
@@ -0,0 +1,5 @@
1
+ spec_dir = File.expand_path('..', __FILE__)
2
+
3
+ %w[unit integration].each do |spec_type|
4
+ Dir[File.join(spec_dir, spec_type, '**', '*.rb')].each { |spec| require spec }
5
+ end
@@ -0,0 +1,45 @@
1
+ require_relative '../../../../spec_helper'
2
+ require 'aequitas/rule/absence/blank'
3
+
4
+ describe Aequitas::Rule::Absence::Blank do
5
+ subject { Aequitas::Rule::Absence::Blank.new(attribute_name, options) }
6
+ let(:attribute_name) { :foo }
7
+ let(:options) { Hash.new }
8
+
9
+ describe '#valid?' do
10
+ let(:resource) { MiniTest::Mock.new }
11
+
12
+ it "is false if the target's attribute is a non-empty string" do
13
+ resource.expect(:validation_attribute_value, 'a', [attribute_name])
14
+ refute_operator subject, :valid?, resource
15
+ end
16
+
17
+ it "is false if the target's attribute is a symbol" do
18
+ resource.expect(:validation_attribute_value, :a, [attribute_name])
19
+ refute_operator subject, :valid?, resource
20
+ end
21
+
22
+ it "is true if the target's attribute is an empty string" do
23
+ resource.expect(:validation_attribute_value, '', [attribute_name])
24
+ assert_operator subject, :valid?, resource
25
+ end
26
+
27
+ it "is true if the target's attribute is false" do
28
+ resource.expect(:validation_attribute_value, false, [attribute_name])
29
+ assert_operator subject, :valid?, resource
30
+ end
31
+
32
+ it "is true if the target's attribute is nil" do
33
+ resource.expect(:validation_attribute_value, nil, [attribute_name])
34
+ assert_operator subject, :valid?, resource
35
+ end
36
+ end
37
+
38
+ describe '#violation_type' do
39
+ it 'returns :absent' do
40
+ resource = MiniTest::Mock.new
41
+ assert_equal :not_blank, subject.violation_type(resource)
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,71 @@
1
+ require_relative '../../../spec_helper'
2
+ require 'aequitas/rule/acceptance'
3
+
4
+ module Aequitas
5
+ class Rule
6
+ describe Acceptance do
7
+ subject { Acceptance.new(attribute_name, options) }
8
+ let(:attribute_name) { :foo }
9
+ let(:options) { Hash.new }
10
+
11
+ describe '#initialize' do
12
+ describe 'when the :accept option is present' do
13
+ let(:options) { { accept: [:foo] } }
14
+
15
+ it 'sets #accept to the provided value' do
16
+ assert_equal [:foo], subject.accept
17
+ end
18
+ end
19
+
20
+ describe 'when the :accept option is not present' do
21
+ let(:options) { Hash.new }
22
+
23
+ it 'sets #accept to the default accepted values' do
24
+ assert_equal Acceptance::DEFAULT_ACCEPTED_VALUES, subject.accept
25
+ end
26
+ end
27
+
28
+ describe 'when the :allow_nil option is absent' do
29
+ it 'sets allow_nil? to true' do
30
+ assert_predicate subject.skip_condition, :allow_nil?
31
+ end
32
+ end
33
+
34
+ describe 'when the :allow_nil option is false' do
35
+ let(:options) { { allow_nil: false } }
36
+
37
+ it 'sets allow_nil? to false' do
38
+ refute_predicate subject.skip_condition, :allow_nil?
39
+ end
40
+ end
41
+ end
42
+
43
+ describe '#valid?' do
44
+ let(:resource) { MiniTest::Mock.new }
45
+ let(:options) { { accept: ['a'] } }
46
+
47
+ describe "when the target attribute's value is among the #accept values" do
48
+ it 'returns true' do
49
+ resource.expect(:validation_attribute_value, 'a', [attribute_name])
50
+ assert_operator subject, :valid?, resource
51
+ end
52
+ end
53
+
54
+ describe "when the target attribute's value is not among the #accept values" do
55
+ it 'returns false' do
56
+ resource.expect(:validation_attribute_value, 'b', [attribute_name])
57
+ refute_operator subject, :valid?, resource
58
+ end
59
+ end
60
+ end
61
+
62
+ describe '#violation_type' do
63
+ it 'returns :accepted' do
64
+ resource = MiniTest::Mock.new
65
+ assert_equal :accepted, subject.violation_type(resource)
66
+ end
67
+ end
68
+
69
+ end
70
+ end # class Rule
71
+ end # module Aequitas
@@ -0,0 +1,80 @@
1
+ require_relative '../../../spec_helper'
2
+ require 'aequitas/rule/confirmation'
3
+
4
+ module Aequitas
5
+ class Rule
6
+ describe Confirmation do
7
+ subject { Confirmation.new(attribute_name, options) }
8
+ let(:attribute_name) { :foo }
9
+ let(:options) { {} }
10
+
11
+ describe '#initialize' do
12
+ it 'calls #default_to_allowing_nil! on its skip_condition' do
13
+ skip_condition = MiniTest::Mock.new
14
+ skip_condition.expect(:default_to_allowing_nil!, nil)
15
+ skip_condition.expect(:default_to_allowing_blank!, nil)
16
+ Confirmation.new(:foo, skip_condition: skip_condition)
17
+ end
18
+
19
+ it 'calls #default_to_allowing_blank! on its skip_condition' do
20
+ skip_condition = MiniTest::Mock.new
21
+ skip_condition.expect(:default_to_allowing_nil!, nil)
22
+ skip_condition.expect(:default_to_allowing_blank!, nil)
23
+ Confirmation.new(:foo, skip_condition: skip_condition)
24
+ end
25
+ end
26
+
27
+ describe '#valid?' do
28
+ let(:resource) { MiniTest::Mock.new }
29
+ let(:options) { { skip_condition: MiniTest::Mock.new } }
30
+ let(:attribute_value) { :bar }
31
+
32
+ before do
33
+ resource.expect(:validation_attribute_value, attribute_value, [attribute_name])
34
+ options[:skip_condition].expect(:default_to_allowing_nil!, nil)
35
+ options[:skip_condition].expect(:default_to_allowing_blank!, nil)
36
+ end
37
+
38
+ describe 'when #skip? returns true' do
39
+ before { options[:skip_condition].expect(:skip?, true, [attribute_value]) }
40
+
41
+ it 'returns true' do
42
+
43
+ assert_operator subject, :valid?, resource
44
+ end
45
+ end
46
+
47
+ describe 'when #skip? returns false' do
48
+ describe 'and the value equals the confirmation value' do
49
+ before do
50
+ options[:skip_condition].expect(:skip?, false, [attribute_value])
51
+ resource.expect(:instance_variable_get, attribute_value, ["@#{attribute_name}_confirmation"])
52
+ end
53
+
54
+ it 'returns true' do
55
+ assert_operator subject, :valid?, resource
56
+ end
57
+ end
58
+
59
+ describe 'and the value does not equal the confirmation value' do
60
+ before do
61
+ options[:skip_condition].expect(:skip?, false, [attribute_value])
62
+ resource.expect(:instance_variable_get, "#{attribute_value}asdf", ["@#{attribute_name}_confirmation"])
63
+ end
64
+
65
+ it 'returns false' do
66
+ refute_operator subject, :valid?, resource
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ describe 'violation_type' do
73
+ it 'returns :confirmation' do
74
+ assert_equal :confirmation, subject.violation_type(MiniTest::Mock.new)
75
+ end
76
+ end
77
+
78
+ end
79
+ end # class Rule
80
+ end # module Aequitas
@@ -0,0 +1,120 @@
1
+ require_relative '../../../spec_helper'
2
+ require 'aequitas/rule/guard'
3
+
4
+ module Aequitas
5
+ class Rule
6
+ describe Guard do
7
+ describe '#initialize' do
8
+ it 'stores the :if option as #if_test' do
9
+ expected = 'foo'
10
+ assert_same expected, Guard.new(if: expected).if_test
11
+ end
12
+
13
+ it 'stores the :unless option as #unless_test' do
14
+ expected = 'foo'
15
+ assert_same expected, Guard.new(unless: expected).unless_test
16
+ end
17
+ end
18
+
19
+ describe '#allow?' do
20
+ let(:rule_guard) { Guard.new(if: if_test, unless: unless_test) }
21
+ let(:if_test) { nil }
22
+ let(:unless_test) { nil }
23
+
24
+ describe 'when #if_test is present' do
25
+ let(:if_test) { MiniTest::Mock.new }
26
+
27
+ describe 'and #if_test responds to #call' do
28
+ it 'invokes #call on the #if_test' do
29
+ if_test.expect(:call, nil, [Object])
30
+ rule_guard.allow?(Object.new)
31
+ end
32
+ end
33
+
34
+ describe 'and #if_test is a Symbol' do
35
+ let(:if_test) { :foo }
36
+
37
+ it 'invokes the message identified by #if_test on the argument' do
38
+ arg = MiniTest::Mock.new
39
+ arg.expect(:foo, nil)
40
+ rule_guard.allow?(arg)
41
+ end
42
+ end
43
+
44
+ describe 'and evaluating #if_test results in a truthy value' do
45
+ it 'returns true' do
46
+ if_test.expect(:call, Object.new, [Object])
47
+ assert_operator rule_guard, :allow?, Object.new
48
+ end
49
+ end
50
+
51
+ describe 'and evaluating #if_test results in a falsy value' do
52
+ it 'returns false' do
53
+ if_test.expect(:call, nil, [Object])
54
+ refute_operator rule_guard, :allow?, Object.new
55
+ end
56
+ end
57
+ end
58
+
59
+ describe 'when #unless_test is present' do
60
+ let(:unless_test) { MiniTest::Mock.new }
61
+
62
+ describe 'and #unless_test responds to #call' do
63
+ it 'invokes #call on the clause' do
64
+ unless_test.expect(:call, nil, [Object])
65
+ rule_guard.allow?(Object.new)
66
+ end
67
+ end
68
+
69
+ describe 'and #unless_test is a Symbol' do
70
+ let(:unless_test) { :foo }
71
+
72
+ it 'invokes the message identified by #unless_test on the argument' do
73
+ arg = MiniTest::Mock.new
74
+ arg.expect(:foo, nil)
75
+ rule_guard.allow?(arg)
76
+ end
77
+ end
78
+
79
+ describe 'and evaluating #unless_test results in a truthy value' do
80
+ it 'returns false' do
81
+ unless_test.expect(:call, Object.new, [Object])
82
+ refute_operator rule_guard, :allow?, Object.new
83
+ end
84
+ end
85
+
86
+ describe 'and evaluating #unless_test results in a falsy value' do
87
+ it 'returns true if #call returns something falsy' do
88
+ unless_test.expect(:call, nil, [Object])
89
+ assert_operator rule_guard, :allow?, Object.new
90
+ end
91
+ end
92
+ end
93
+
94
+ describe 'when both #if_test and #unless_test are present' do
95
+ let(:if_test) { MiniTest::Mock.new }
96
+ let(:unless_test) { MiniTest::Mock.new }
97
+
98
+ describe 'and #if_test responds to #call' do
99
+ it 'invokes #call on #if_test and ignores #unless_test' do
100
+ arg = MiniTest::Mock.new
101
+ if_test.expect(:call, nil, [Object])
102
+ rule_guard.allow?(arg)
103
+ end
104
+ end
105
+
106
+ describe 'and #if_test is a Symbol' do
107
+ let(:if_test) { :foo }
108
+
109
+ it 'invokes the message identified by #if_test on the argument and ignores #unless_test' do
110
+ arg = MiniTest::Mock.new
111
+ arg.expect(:foo, nil)
112
+ rule_guard.allow?(arg)
113
+ end
114
+ end
115
+ end
116
+
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,170 @@
1
+ require_relative '../../../spec_helper'
2
+ require 'aequitas/rule/skip_condition'
3
+
4
+ module Aequitas
5
+ class Rule
6
+ describe SkipCondition do
7
+ subject { SkipCondition.new(options) }
8
+
9
+ describe '#initialize' do
10
+ describe 'when :allow_nil is included and false' do
11
+ let(:options) { { allow_nil: false } }
12
+
13
+ end
14
+ end
15
+
16
+ describe '#skip?' do
17
+ it 'is false when :allow_nil is absent and the value is nil' do
18
+ refute_operator SkipCondition.new, :skip?, nil
19
+ end
20
+
21
+ it 'is false when :allow_nil is absent and the value is not nil' do
22
+ refute_operator SkipCondition.new, :skip?, :foo
23
+ end
24
+
25
+ it 'is true when :allow_nil is true and the value is nil' do
26
+ assert_operator SkipCondition.new(allow_nil: true), :skip?, nil
27
+ end
28
+
29
+ it 'is false when :allow_nil is true and the value is non-nil' do
30
+ refute_operator SkipCondition.new(allow_nil: true), :skip?, :foo
31
+ end
32
+
33
+ it 'is false when :allow_nil is false and the value is nil' do
34
+ refute_operator SkipCondition.new(allow_nil: false), :skip?, nil
35
+ end
36
+
37
+ it 'is false when :allow_nil is false and the value is non-nil' do
38
+ refute_operator SkipCondition.new(allow_nil: false), :skip?, :foo
39
+ end
40
+
41
+ it 'is true when :allow_blank is true and the value is nil' do
42
+ assert_operator SkipCondition.new(allow_blank: true), :skip?, nil
43
+ end
44
+
45
+ it 'is false when :allow_blank is true and the value is non-nil' do
46
+ refute_operator SkipCondition.new(allow_blank: true), :skip?, :foo
47
+ end
48
+ end
49
+
50
+ describe '#allow_nil?' do
51
+ it 'is false when :allow_nil option is absent' do
52
+ refute_predicate SkipCondition.new, :allow_nil?
53
+ end
54
+
55
+ it 'is false when :allow_nil option is false' do
56
+ refute_predicate SkipCondition.new(allow_nil: false), :allow_nil?
57
+ end
58
+
59
+ it 'is true when :allow_nil option is true' do
60
+ assert_predicate SkipCondition.new(allow_nil: true), :allow_nil?
61
+ end
62
+ end
63
+
64
+ describe '#allow_blank?' do
65
+ it 'is false when :allow_blank option is absent' do
66
+ refute_predicate SkipCondition.new, :allow_blank?
67
+ end
68
+
69
+ it 'is false when :allow_blank option is false' do
70
+ refute_predicate SkipCondition.new(allow_blank: false), :allow_blank?
71
+ end
72
+
73
+ it 'is true when :allow_blank option is true' do
74
+ assert_predicate SkipCondition.new(allow_blank: true), :allow_blank?
75
+ end
76
+ end
77
+
78
+ describe '#allow_nil!' do
79
+ subject { SkipCondition.new }
80
+
81
+ it 'returns the receiver' do
82
+ assert_same subject, subject.allow_nil!
83
+ end
84
+
85
+ it 'configures the receiver to allow nil' do
86
+ subject.allow_nil!
87
+ assert_predicate subject, :allow_nil?
88
+ end
89
+ end
90
+
91
+ describe '#allow_blank!' do
92
+ subject { SkipCondition.new }
93
+
94
+ it 'returns the receiver' do
95
+ assert_same subject, subject.allow_blank!
96
+ end
97
+
98
+ it 'configures the receiver to allow blank' do
99
+ subject.allow_blank!
100
+ assert_predicate subject, :allow_blank?
101
+ end
102
+ end
103
+
104
+ describe '#reject_nil!' do
105
+ subject { SkipCondition.new }
106
+
107
+ it 'returns the receiver' do
108
+ assert_same subject, subject.reject_nil!
109
+ end
110
+
111
+ it 'configures the receiver to reject nil' do
112
+ subject.reject_nil!
113
+ refute_predicate subject, :allow_nil?
114
+ end
115
+ end
116
+
117
+ describe '#reject_blank!' do
118
+ subject { SkipCondition.new }
119
+
120
+ it 'returns the receiver' do
121
+ assert_same subject, subject.reject_blank!
122
+ end
123
+
124
+ it 'configures the receiver to reject blank' do
125
+ subject.reject_blank!
126
+ refute_predicate subject, :allow_blank?
127
+ end
128
+ end
129
+
130
+ describe '#default_to_allowing_nil!' do
131
+ subject { SkipCondition.new }
132
+
133
+ it 'returns the receiver' do
134
+ assert_same subject, subject.default_to_allowing_nil!
135
+ end
136
+
137
+ it 'configures the receiver to allow nil if not configured' do
138
+ subject.default_to_allowing_nil!
139
+ assert_predicate subject, :allow_nil?
140
+ end
141
+
142
+ it 'does not configure the receiver to allow nil if already configured' do
143
+ subject.reject_nil!
144
+ subject.default_to_allowing_nil!
145
+ refute_predicate subject, :allow_nil?
146
+ end
147
+ end
148
+
149
+ describe '#default_to_allowing_blank!' do
150
+ subject { SkipCondition.new }
151
+
152
+ it 'returns the receiver' do
153
+ assert_same subject, subject.default_to_allowing_blank!
154
+ end
155
+
156
+ it 'configures the receiver to allow blank' do
157
+ subject.default_to_allowing_blank!
158
+ assert_predicate subject, :allow_blank?
159
+ end
160
+
161
+ it 'does not configure the receiver to allow blank if already configured' do
162
+ subject.reject_blank!
163
+ subject.default_to_allowing_blank!
164
+ refute_predicate subject, :allow_blank?
165
+ end
166
+ end
167
+
168
+ end
169
+ end
170
+ end