/
allow_list_spec.rb
54 lines (43 loc) · 1.68 KB
/
allow_list_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
require 'spec_helper'
describe Hashie::Extensions::Dash::AllowList do
class DashWithAllowedValues < Hashie::Dash
include Hashie::Extensions::Dash::AllowList
property :gender, allow: %i[male female prefer_not_to_say]
property :age, allow: (0..150)
end
it 'allows value within the allow list' do
valid_dash = DashWithAllowedValues.new(gender: :male)
expect(valid_dash.gender).to eq(:male)
end
it 'rejects value outside the allow list' do
expect { DashWithAllowedValues.new(gender: :unicorn) }
.to raise_error(ArgumentError, %(The property 'gender' is not in the allow list))
end
it 'accepts a range for allow list' do
expect { DashWithAllowedValues.new(age: -1) }
.to raise_error(ArgumentError, %(The property 'age' is not in the allow list))
end
it 'allows property to be nil' do
expect { DashWithAllowedValues.new }
.not_to raise_error
end
it 'rejects non array or range for allow list' do
expect do
class DashWithUnsupportedAllowedValueType < Hashie::Dash
include Hashie::Extensions::Dash::AllowList
property :name, allow: -> { :foo }
end
end.to raise_error(ArgumentError, %(`allow` accepts an Array or a Range.))
end
class DashWithAllowedValuesSubClass < DashWithAllowedValues
property :language, allow: %i[ruby javascript]
end
it 'passes property allow list to subclasses' do
expect { DashWithAllowedValuesSubClass.new(gender: :unicorn) }
.to raise_error(ArgumentError, %(The property 'gender' is not in the allow list))
end
it 'allows subclass to define allow list' do
expect { DashWithAllowedValuesSubClass.new(language: :ruby) }
.not_to raise_error
end
end