forked from thoughtbot/shoulda-matchers
/
define_enum_for_matcher.rb
147 lines (124 loc) · 3.69 KB
/
define_enum_for_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
module Shoulda
module Matchers
module ActiveRecord
# The `define_enum_for` matcher is used to test that the `enum` macro has
# been used to decorate an attribute with enum methods.
#
# class Process < ActiveRecord::Base
# enum status: [:running, :stopped, :suspended]
# end
#
# # RSpec
# RSpec.describe Process, type: :model do
# it { should define_enum_for(:status) }
# end
#
# # Minitest (Shoulda)
# class ProcessTest < ActiveSupport::TestCase
# should define_enum_for(:status)
# end
#
# #### Qualifiers
#
# ##### with
#
# Use `with` to test that the enum has been defined with a certain set of
# known values.
#
# class Process < ActiveRecord::Base
# enum status: [:running, :stopped, :suspended]
# end
#
# # RSpec
# RSpec.describe Process, type: :model do
# it do
# should define_enum_for(:status).
# with([:running, :stopped, :suspended])
# end
# end
#
# # Minitest (Shoulda)
# class ProcessTest < ActiveSupport::TestCase
# should define_enum_for(:status).
# with([:running, :stopped, :suspended])
# end
#
# @return [DefineEnumForMatcher]
#
def define_enum_for(attribute_name)
DefineEnumForMatcher.new(attribute_name)
end
# @private
class DefineEnumForMatcher
def initialize(attribute_name)
@attribute_name = attribute_name
@options = {}
end
def with(expected_enum_values)
options[:expected_enum_values] = expected_enum_values
self
end
def matches?(subject)
@record = subject
enum_defined? && enum_values_match? && column_type_is_integer?
end
def failure_message
"Expected #{expectation}"
end
alias :failure_message_for_should :failure_message
def failure_message_when_negated
"Did not expect #{expectation}"
end
alias :failure_message_for_should_not :failure_message_when_negated
def description
desc = "define :#{attribute_name} as an enum"
if options[:expected_enum_values]
desc << " with #{options[:expected_enum_values]}"
end
desc << " and store the value in a column with an integer type"
desc
end
protected
attr_reader :record, :attribute_name, :options
def expectation
"#{model.name} to #{description}"
end
def expected_enum_values
hashify(options[:expected_enum_values]).with_indifferent_access
end
def actual_enum_values
model.send(attribute_name.to_s.pluralize)
end
def enum_defined?
model.defined_enums.include?(attribute_name.to_s)
end
def enum_values_match?
expected_enum_values.empty? || actual_enum_values == expected_enum_values
end
def column_type_is_integer?
column.type == :integer
end
def column
model.columns_hash[attribute_name.to_s]
end
def model
record.class
end
def hashify(value)
if value.nil?
return {}
end
if value.is_a?(Array)
new_value = {}
value.each_with_index do |v, i|
new_value[v] = i
end
new_value
else
value
end
end
end
end
end
end