Replace to_hsh method, and add unit test for it

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2019-10-01 11:13:27 -04:00
parent f4c979cabd
commit 51812a9e4e
2 changed files with 37 additions and 0 deletions

View file

@ -318,6 +318,21 @@ module Inspec
!current_value.is_a? NO_VALUE_SET
end
#--------------------------------------------------------------------------#
# Marshalling
#--------------------------------------------------------------------------#
def to_hash
as_hash = { name: name, options: {} }
%i{description title identifier type required value}.each do |field|
val = send(field)
next if val.nil?
as_hash[:options][field] = val
end
as_hash
end
#--------------------------------------------------------------------------#
# Value Type Coercion
#--------------------------------------------------------------------------#

View file

@ -25,6 +25,28 @@ describe Inspec::Input do
end
end
describe "marshalling" do
it "should be able to represent an Input as a Hash" do
input = Inspec::Input.new("test_input",
value: 12,
title: "Best input ever",
description: "important",
type: "Numeric",
required: true
)
_(input.to_hash).must_equal({
name: "test_input",
options: {
value: 12,
title: "Best input ever",
description: "important",
type: "Numeric",
required: true
}
})
end
end
#==============================================================#
# Setting Value - One Shot
# (see events_test.rb for overwrite support)