mirror of
https://github.com/inspec/inspec
synced 2025-02-17 06:28:40 +00:00
forgiving default attributes (#2177)
* forgiving default attributes When default attributes arent specified provide one that is much more forgiving. See this https://github.com/chef/inspec/issues/2176 Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
parent
d2a47fa9fb
commit
e2004a436f
2 changed files with 33 additions and 7 deletions
|
@ -5,7 +5,17 @@ module Inspec
|
|||
attr_accessor :name
|
||||
attr_writer :value
|
||||
|
||||
def initialize(name, options)
|
||||
DEFAULT_ATTRIBUTE = Class.new do
|
||||
def method_missing(*_)
|
||||
self
|
||||
end
|
||||
|
||||
def respond_to_missing?(_, _)
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(name, options = {})
|
||||
@name = name
|
||||
@opts = options
|
||||
@value = nil
|
||||
|
@ -17,7 +27,7 @@ module Inspec
|
|||
end
|
||||
|
||||
def default
|
||||
@opts[:default]
|
||||
@opts[:default] || DEFAULT_ATTRIBUTE.new
|
||||
end
|
||||
|
||||
def title
|
||||
|
|
|
@ -4,11 +4,7 @@ require 'helper'
|
|||
require 'inspec/objects/attribute'
|
||||
|
||||
describe Inspec::Attribute do
|
||||
let(:attribute) { Inspec::Attribute.new('test_attribute', default: 'default_value') }
|
||||
|
||||
it 'returns the default value if no value is assigned' do
|
||||
attribute.value.must_equal 'default_value'
|
||||
end
|
||||
let(:attribute) { Inspec::Attribute.new('test_attribute') }
|
||||
|
||||
it 'returns the actual value, not the default, if one is assigned' do
|
||||
attribute.value = 'new_value'
|
||||
|
@ -19,4 +15,24 @@ describe Inspec::Attribute do
|
|||
attribute.value = false
|
||||
attribute.value.must_equal false
|
||||
end
|
||||
|
||||
it 'returns the default value if no value is assigned' do
|
||||
attribute.value.must_be_kind_of Inspec::Attribute::DEFAULT_ATTRIBUTE
|
||||
end
|
||||
|
||||
it 'has a default value that can be called like a nested map' do
|
||||
attribute.value['hello']['world'][1][2]['three'].wont_be_nil
|
||||
end
|
||||
|
||||
it 'has a default value that can take any nested method calls' do
|
||||
attribute.value.call.some.fancy.functions.wont_be_nil
|
||||
end
|
||||
|
||||
describe 'attribute with a default value set' do
|
||||
let(:attribute) { Inspec::Attribute.new('test_attribute', default: 'default_value') }
|
||||
|
||||
it 'returns the user-configured default value if no value is assigned' do
|
||||
attribute.value.must_equal 'default_value'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue