mirror of
https://github.com/inspec/inspec
synced 2024-11-11 07:34:15 +00:00
Support false
for attribute value (#2168)
The logic in `Inspec::Attribute` prohibited the use of `false` (FalseClass) as a valid attribute. If the attribute value supplied was `false`, then it would fall back to the default value. This change properly allows the use of `false` as a value, adds the initial tests for Inspec::Attribute, and also uses better attr_writer semantics for writing/storing the value. Signed-off-by: Adam Leff <adam@leff.co>
This commit is contained in:
parent
4a71140052
commit
e400b8dd4c
3 changed files with 27 additions and 6 deletions
|
@ -3,6 +3,8 @@
|
||||||
module Inspec
|
module Inspec
|
||||||
class Attribute
|
class Attribute
|
||||||
attr_accessor :name
|
attr_accessor :name
|
||||||
|
attr_writer :value
|
||||||
|
|
||||||
def initialize(name, options)
|
def initialize(name, options)
|
||||||
@name = name
|
@name = name
|
||||||
@opts = options
|
@opts = options
|
||||||
|
@ -10,11 +12,8 @@ module Inspec
|
||||||
end
|
end
|
||||||
|
|
||||||
# implicit call is done by inspec to determine the value of an attribute
|
# implicit call is done by inspec to determine the value of an attribute
|
||||||
def value(newvalue = nil)
|
def value
|
||||||
unless newvalue.nil?
|
@value.nil? ? default : @value
|
||||||
@value = newvalue
|
|
||||||
end
|
|
||||||
@value || default
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def default
|
def default
|
||||||
|
|
|
@ -177,7 +177,7 @@ module Inspec
|
||||||
# we need to return an attribute object, to allow dermination of default values
|
# we need to return an attribute object, to allow dermination of default values
|
||||||
attr = Attribute.new(name, options)
|
attr = Attribute.new(name, options)
|
||||||
# read value from given gived values
|
# read value from given gived values
|
||||||
attr.value(@conf['attributes'][attr.name]) unless @conf['attributes'].nil?
|
attr.value = @conf['attributes'][attr.name] unless @conf['attributes'].nil?
|
||||||
@attributes.push(attr)
|
@attributes.push(attr)
|
||||||
attr.value
|
attr.value
|
||||||
end
|
end
|
||||||
|
|
22
test/unit/objects/attribute_test.rb
Normal file
22
test/unit/objects/attribute_test.rb
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
it 'returns the actual value, not the default, if one is assigned' do
|
||||||
|
attribute.value = 'new_value'
|
||||||
|
attribute.value.must_equal 'new_value'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'support storing and returning false' do
|
||||||
|
attribute.value = false
|
||||||
|
attribute.value.must_equal false
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue