/
attribute.spec.rb
196 lines (172 loc) · 5.25 KB
/
attribute.spec.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# frozen_string_literal: true
require_relative '../../../../../lib/mapper/type/attribute'
require_relative '../../../../../lib/mapper/error/compliance_error'
require_relative '../../../../../lib/mapper/error/validation_error'
klass = ::AMA::Entity::Mapper::Type::Attribute
compliance_error_class = ::AMA::Entity::Mapper::Error::ComplianceError
validation_error_class = ::AMA::Entity::Mapper::Error::ValidationError
describe klass do
let(:type) do
double(
type: Class.new,
is_a?: true,
to_s: 'Type',
to_def: 'Type',
resolved?: true,
resolved!: nil
)
end
let(:other_type) do
double(
type: Class.new,
is_a?: true,
to_s: 'OtherType',
to_def: 'OtherType',
resolved?: true,
resolved!: nil
)
end
let(:context) do
double(
path: nil,
advance: nil
)
end
let(:instance) do
klass.new(type, :id, type)
end
let(:attribute) do
instance
end
describe '#eql?' do
it 'should return true if owners and names match' do
instances = Array.new(2) do
klass.new(type, :id, type)
end
expect(instances.first).to eq(instances.last)
end
it 'should return false if owners differ' do
instances = [type, other_type].map do |type|
klass.new(type, :id, type)
end
expect(instances.first).not_to eq(instances.last)
end
it 'should return false if names differ' do
instances = Array.new(2) do |index|
klass.new(type, index.to_s.to_sym, type)
end
expect(instances.first).not_to eq(instances.last)
end
end
describe '#hash' do
it 'should return equal values for identical instances' do
instances = Array.new(2) do
klass.new(type, :id, type)
end
expect(instances.first.hash).to eq(instances.last.hash)
end
end
describe '#initialize' do
it 'should raise error if provided owner is not a type' do
proc = lambda do
klass.new(double(is_a?: false), :id, type)
end
expect(&proc).to raise_error(compliance_error_class)
end
it 'should raise error if not a symbol is passed as name' do
proc = lambda do
klass.new(type, 'id', type)
end
expect(&proc).to raise_error(compliance_error_class)
end
it 'should raise error if invalid type is specified' do
proc = lambda do
klass.new(type, :id, double(is_a?: false))
end
expect(&proc).to raise_error(compliance_error_class)
end
it 'should raise error if no types are specified' do
proc = lambda do
klass.new(type, :id)
end
expect(&proc).to raise_error(compliance_error_class)
end
end
describe '#resolved?' do
it 'should pass call through onto types' do
expect(type).to receive(:resolved?).at_least(:once)
attribute = klass.new(type, :id, type)
expect(attribute.resolved?).to be true
end
end
describe '#resolved!' do
it 'should pass call through onto types' do
expect(type).to receive(:resolved!).at_least(:once)
attribute = klass.new(type, :id, type)
proc = lambda do
attribute.resolved!
end
expect(&proc).not_to raise_error
end
end
describe '#resolve_parameter' do
it 'should clone itself and pass the call to types' do
expect(type).to receive(:resolve_parameter).and_return(type).exactly(:once)
attribute = klass.new(type, :id, type)
clone = attribute.resolve_parameter(:T, nil)
expect(clone).to eq(attribute)
expect(clone).not_to equal(attribute)
end
end
describe '#validator_block' do
it 'accepts validator via block' do
value = double
proc = lambda do |listener|
attribute.validator_block(&listener)
attribute.validator.validate(value, attribute, context)
end
expect(&proc).to yield_with_args(value, attribute, context)
end
end
describe '#violations' do
it 'calls validator with provided value, self and context' do
attribute = klass.new(type, :id, type)
value = double
validator = double
result = double
match = receive(:validate)
.with(value, attribute, context)
.and_return(result)
expect(validator).to(match)
attribute.validator = validator
expect(attribute.violations(value, context)).to eq(result)
end
end
describe '#valid?' do
it 'returns true if validator returns no violations' do
attribute.validator = double(validate: [])
expect(attribute.valid?(double, context)).to be true
end
it 'returns false if validator returns at least one violation' do
attribute.validator = double(validate: ['violation'])
expect(attribute.valid?(double, context)).to be false
end
end
describe '#valid!' do
it 'doesn\'t raise if validator returns no violations' do
attribute.validator = double(validate: [])
proc = lambda do
attribute.valid!(double, context)
end
expect(&proc).not_to raise_error
end
it 'raises validation error if validator returns at least one violation' do
validator = double(validate: ['violation'])
attribute.validator = validator
proc = lambda do
attribute.valid!(double, context)
end
expect(&proc).to raise_error(validation_error_class)
end
end
end