|
23 | 23 | end |
24 | 24 | let(:feature_support) { [] } |
25 | 25 |
|
26 | | - it { expect { described_class.new(nil) }.to raise_error Puppet::DevError, %r{TypeDefinition requires definition to be a Hash} } |
| 26 | + it { expect { described_class.new(nil) }.to raise_error Puppet::DevError, %r{Type definition must be a Hash} } |
27 | 27 |
|
28 | 28 | describe '.name' do |
29 | 29 | it { expect(type.name).to eq 'some_resource' } |
30 | 30 | end |
31 | 31 |
|
32 | 32 | describe '#ensurable?' do |
33 | 33 | context 'when type is ensurable' do |
34 | | - let(:definition) { { attributes: { ensure: true } } } |
| 34 | + let(:definition) { { name: 'some_resource', attributes: { ensure: { type: 'Enum[absent, present]' } } } } |
35 | 35 |
|
36 | 36 | it { expect(type).to be_ensurable } |
37 | 37 | it { expect(type.attributes).to be_key(:ensure) } |
38 | 38 | end |
39 | 39 |
|
40 | 40 | context 'when type is not ensurable' do |
41 | | - let(:definition) { { attributes: { string: 'something' } } } |
| 41 | + let(:definition) { { name: 'some_resource', attributes: { name: { type: 'String' } } } } |
42 | 42 |
|
43 | 43 | it { expect(type).not_to be_ensurable } |
44 | | - it { expect(type.attributes).to be_key(:string) } |
| 44 | + it { expect(type.attributes).to be_key(:name) } |
45 | 45 | end |
46 | 46 | end |
47 | 47 |
|
|
61 | 61 |
|
62 | 62 | describe '#attributes' do |
63 | 63 | context 'when type has attributes' do |
64 | | - let(:definition) { { attributes: { string: 'test_string' } } } |
| 64 | + let(:definition) { { name: 'some_resource', attributes: { wibble: { type: 'String' } } } } |
65 | 65 |
|
66 | | - it { expect(type.attributes).to be_key(:string) } |
67 | | - it { expect(type.attributes[:string]).to eq('test_string') } |
| 66 | + it { expect(type.attributes).to be_key(:wibble) } |
68 | 67 | end |
69 | 68 | end |
70 | 69 |
|
|
102 | 101 | end |
103 | 102 | end |
104 | 103 |
|
105 | | - describe '#check_schemas' do |
| 104 | + describe '#check_schema' do |
106 | 105 | context 'when resource does not contain its namevar' do |
107 | 106 | let(:resource) { { nom: 'some_resource', prop: 1, ensure: 'present' } } |
108 | 107 |
|
|
183 | 182 | end |
184 | 183 | end |
185 | 184 | end |
| 185 | + |
| 186 | + describe '#validate_schema' do |
| 187 | + context 'when the type definition does not have a name' do |
| 188 | + let(:definition) { { attributes: 'some_string' } } |
| 189 | + |
| 190 | + it { expect { type }.to raise_error Puppet::DevError, %r{Type definition must have a name} } |
| 191 | + end |
| 192 | + |
| 193 | + context 'when attributes is not a hash' do |
| 194 | + let(:definition) { { name: 'some_resource', attributes: 'some_string' } } |
| 195 | + |
| 196 | + it { expect { type }.to raise_error Puppet::DevError, %r{`some_resource.attributes` must be a hash} } |
| 197 | + end |
| 198 | + |
| 199 | + context 'when the schema contains title_patterns and it is not an array' do |
| 200 | + let(:definition) { { name: 'some_resource', title_patterns: {}, attributes: {} } } |
| 201 | + |
| 202 | + it { expect { type }.to raise_error Puppet::DevError, %r{`:title_patterns` must be an array} } |
| 203 | + end |
| 204 | + |
| 205 | + context 'when an attribute is not a hash' do |
| 206 | + let(:definition) { { name: 'some_resource', attributes: { name: 'some_string' } } } |
| 207 | + |
| 208 | + it { expect { type }.to raise_error Puppet::DevError, %r{`some_resource.name` must be a Hash} } |
| 209 | + end |
| 210 | + |
| 211 | + context 'when an attribute has no type' do |
| 212 | + let(:definition) { { name: 'some_resource', attributes: { name: { desc: 'message' } } } } |
| 213 | + |
| 214 | + it { expect { type }.to raise_error Puppet::DevError, %r{has no type} } |
| 215 | + end |
| 216 | + |
| 217 | + context 'when an attribute has no descrption' do |
| 218 | + let(:definition) { { name: 'some_resource', attributes: { name: { type: 'String' } } } } |
| 219 | + |
| 220 | + it 'Raises a warning message' do |
| 221 | + expect(Puppet).to receive(:warning).with('`some_resource.name` has no docs') |
| 222 | + type |
| 223 | + end |
| 224 | + end |
| 225 | + |
| 226 | + context 'when an attribute has an unsupported type' do |
| 227 | + let(:definition) { { name: 'some_resource', attributes: { name: { type: 'basic' } } } } |
| 228 | + |
| 229 | + it { expect { type }.to raise_error %r{<basic> is not a valid type specification} } |
| 230 | + end |
| 231 | + |
| 232 | + context 'with both behavior and behaviour' do |
| 233 | + let(:definition) do |
| 234 | + { |
| 235 | + name: 'bad_behaviour', |
| 236 | + attributes: { |
| 237 | + name: { |
| 238 | + type: 'String', |
| 239 | + behaviour: :namevar, |
| 240 | + behavior: :namevar, |
| 241 | + }, |
| 242 | + }, |
| 243 | + } |
| 244 | + end |
| 245 | + |
| 246 | + it { expect { type }.to raise_error Puppet::DevError, %r{name.*attribute has both} } |
| 247 | + end |
| 248 | + |
| 249 | + context 'when registering a type with badly formed attribute type' do |
| 250 | + let(:definition) do |
| 251 | + { |
| 252 | + name: 'bad_syntax', |
| 253 | + attributes: { |
| 254 | + name: { |
| 255 | + type: 'Optional[String', |
| 256 | + }, |
| 257 | + }, |
| 258 | + } |
| 259 | + end |
| 260 | + |
| 261 | + it { expect { type }.to raise_error Puppet::DevError, %r{The type of the `name` attribute `Optional\[String` could not be parsed:} } |
| 262 | + end |
| 263 | + end |
186 | 264 | end |
0 commit comments