forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_confirmation_of_matcher.rb
162 lines (146 loc) · 4.49 KB
/
validate_confirmation_of_matcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
module Shoulda
module Matchers
module ActiveModel
# The `validate_confirmation_of` matcher tests usage of the
# `validates_confirmation_of` validation.
#
# class User
# include ActiveModel::Model
# attr_accessor :email
#
# validates_confirmation_of :email
# end
#
# # RSpec
# RSpec.describe User, type: :model do
# it { should validate_confirmation_of(:email) }
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_confirmation_of(:email)
# end
#
# #### Qualifiers
#
# ##### on
#
# Use `on` if your validation applies only under a certain context.
#
# class User
# include ActiveModel::Model
# attr_accessor :password
#
# validates_confirmation_of :password, on: :create
# end
#
# # RSpec
# RSpec.describe User, type: :model do
# it { should validate_confirmation_of(:password).on(:create) }
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_confirmation_of(:password).on(:create)
# end
#
# ##### with_message
#
# Use `with_message` if you are using a custom validation message.
#
# class User
# include ActiveModel::Model
# attr_accessor :password
#
# validates_confirmation_of :password,
# message: 'Please re-enter your password'
# end
#
# # RSpec
# RSpec.describe User, type: :model do
# it do
# should validate_confirmation_of(:password).
# with_message('Please re-enter your password')
# end
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_confirmation_of(:password).
# with_message('Please re-enter your password')
# end
#
# @return [ValidateConfirmationOfMatcher]
#
def validate_confirmation_of(attr)
ValidateConfirmationOfMatcher.new(attr)
end
# @private
class ValidateConfirmationOfMatcher < ValidationMatcher
include Helpers
attr_reader :attribute, :confirmation_attribute
def initialize(attribute)
super
@expected_message = :confirmation
@confirmation_attribute = "#{attribute}_confirmation"
end
def simple_description
"validate that :#{@confirmation_attribute} matches :#{@attribute}"
end
def matches?(subject)
super(subject)
disallows_different_value &&
allows_same_value &&
allows_missing_confirmation
end
def does_not_match?(subject)
super(subject)
allows_different_value ||
disallows_same_value ||
disallows_missing_confirmation
end
private
def allows_different_value
allows_value_of('different value') do |matcher|
qualify_matcher(matcher, 'some value')
end
end
def disallows_different_value
disallows_value_of('different value') do |matcher|
qualify_matcher(matcher, 'some value')
end
end
def allows_same_value
allows_value_of('same value') do |matcher|
qualify_matcher(matcher, 'same value')
end
end
def disallows_same_value
disallows_value_of('same value') do |matcher|
qualify_matcher(matcher, 'same value')
end
end
def allows_missing_confirmation
allows_value_of('any value') do |matcher|
qualify_matcher(matcher, nil)
end
end
def disallows_missing_confirmation
disallows_value_of('any value') do |matcher|
qualify_matcher(matcher, nil)
end
end
def qualify_matcher(matcher, confirmation_attribute_value)
matcher.values_to_preset = {
confirmation_attribute => confirmation_attribute_value
}
matcher.with_message(
@expected_message,
against: confirmation_attribute,
values: { attribute: attribute }
)
end
end
end
end
end