2019-06-11 15:24:35 -07:00
|
|
|
require "helper"
|
2019-10-06 19:30:37 -04:00
|
|
|
require "inspec/input"
|
2017-09-21 12:17:44 -04:00
|
|
|
|
2019-02-11 22:39:25 -05:00
|
|
|
describe Inspec::Input do
|
2019-06-11 15:24:35 -07:00
|
|
|
let(:opts) { {} }
|
|
|
|
let(:input) { Inspec::Input.new("test_input", opts) }
|
2017-09-21 12:17:44 -04:00
|
|
|
|
2019-02-07 11:45:49 -05:00
|
|
|
#==============================================================#
|
|
|
|
# Metadata
|
|
|
|
#==============================================================#
|
2019-06-11 15:24:35 -07:00
|
|
|
describe "setting and reading metadata" do
|
2019-02-07 12:31:17 -05:00
|
|
|
{
|
2019-06-11 15:24:35 -07:00
|
|
|
description: "My favorite attribute",
|
|
|
|
identifier: "a_ruby_permitted_name",
|
2019-02-07 12:31:17 -05:00
|
|
|
required: true,
|
2019-06-11 15:24:35 -07:00
|
|
|
title: "how is this different than description",
|
|
|
|
type: "Numeric",
|
2019-02-07 12:31:17 -05:00
|
|
|
}.each do |field, value|
|
|
|
|
it "should be able to recall the #{field} field" do
|
|
|
|
opts[field] = value
|
2019-06-11 15:24:35 -07:00
|
|
|
ipt = Inspec::Input.new("test_attribute", opts)
|
2019-02-07 12:31:17 -05:00
|
|
|
seen_value = ipt.send(field)
|
2019-09-30 15:31:55 -07:00
|
|
|
_(seen_value).must_equal value
|
2019-02-07 12:31:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-07 11:45:49 -05:00
|
|
|
|
2019-10-01 11:13:27 -04:00
|
|
|
describe "marshalling" do
|
|
|
|
it "should be able to represent an Input as a Hash" do
|
|
|
|
input = Inspec::Input.new("test_input",
|
2019-11-07 15:17:22 -08:00
|
|
|
value: 12,
|
|
|
|
title: "Best input ever",
|
|
|
|
description: "important",
|
|
|
|
type: "Numeric",
|
|
|
|
required: true)
|
|
|
|
|
2019-10-01 11:13:27 -04:00
|
|
|
_(input.to_hash).must_equal({
|
|
|
|
name: "test_input",
|
|
|
|
options: {
|
2019-10-04 14:06:49 -04:00
|
|
|
value: 12,
|
|
|
|
title: "Best input ever",
|
|
|
|
description: "important",
|
|
|
|
type: "Numeric",
|
|
|
|
required: true,
|
2019-10-02 12:22:20 -04:00
|
|
|
},
|
2019-10-01 11:13:27 -04:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-07 13:05:46 -05:00
|
|
|
#==============================================================#
|
2019-02-06 22:00:03 -05:00
|
|
|
# Setting Value - One Shot
|
|
|
|
# (see events_test.rb for overwrite support)
|
|
|
|
#==============================================================#
|
2019-06-11 15:24:35 -07:00
|
|
|
describe "the dummy value used when value is not set" do
|
|
|
|
it "returns the actual value, not the dummy object, if one is assigned" do
|
|
|
|
input.value = "new_value"
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value).must_equal "new_value"
|
2019-01-28 00:08:40 -05:00
|
|
|
end
|
2017-09-22 14:57:51 +02:00
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "returns the dummy value if no value is assigned" do
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value).must_be_kind_of Inspec::Input::NO_VALUE_SET
|
|
|
|
_(input.value.is_a?(Inspec::Input::NO_VALUE_SET)).must_equal true
|
|
|
|
_(input.value.to_s).must_equal "Input 'test_input' does not have a value. Skipping test."
|
2019-01-28 00:08:40 -05:00
|
|
|
end
|
2017-09-22 14:57:51 +02:00
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "the dummy value responds true to the legacy class checks" do
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value.is_a?(Inspec::Attribute::DEFAULT_ATTRIBUTE)).must_equal true
|
|
|
|
_(input.value).must_be_kind_of Inspec::Attribute::DEFAULT_ATTRIBUTE
|
2019-02-22 10:05:31 -05:00
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "has a dummy value that can be called like a nested map" do
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value["hello"]["world"][1][2]["three"]).wont_be_nil
|
2019-01-28 00:08:40 -05:00
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "has a dummy value that can take any nested method calls" do
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value.call.some.fancy.functions).wont_be_nil
|
2019-01-28 00:08:40 -05:00
|
|
|
end
|
2017-09-22 14:57:51 +02:00
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
describe "setting a value in the constructor using value:" do
|
|
|
|
it "returns the user-configured value" do
|
|
|
|
input = Inspec::Input.new("test_input", value: "some_value")
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value).must_equal "some_value"
|
2017-09-22 14:57:51 +02:00
|
|
|
end
|
2017-12-21 14:20:59 +01:00
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "returns the user-configured value if nil is explicitly assigned" do
|
|
|
|
input = Inspec::Input.new("test_input", value: nil)
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value).must_be_nil
|
2017-12-21 14:20:59 +01:00
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "returns the user-configured value if false is explicitly assigned" do
|
|
|
|
input = Inspec::Input.new("test_input", value: false)
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value).must_equal false
|
2017-12-21 14:20:59 +01:00
|
|
|
end
|
2018-09-12 16:42:58 -04:00
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "returns a new value if the value has been assigned by value=" do
|
|
|
|
input = Inspec::Input.new("test_input", value: "original_value")
|
|
|
|
input.value = "new_value"
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value).must_equal "new_value"
|
2018-09-12 16:42:58 -04:00
|
|
|
end
|
2019-01-28 00:40:46 -05:00
|
|
|
|
|
|
|
it 'accepts the legacy ":default" option' do
|
2019-06-11 15:24:35 -07:00
|
|
|
input = Inspec::Input.new("test_input", default: "a_default")
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value).must_equal "a_default"
|
2019-01-28 00:40:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts the legacy ":default" and ":value" options' do
|
2019-06-11 15:24:35 -07:00
|
|
|
input = Inspec::Input.new("test_input", default: "a_default", value: "a_value")
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value).must_equal "a_value"
|
2019-01-28 00:40:46 -05:00
|
|
|
end
|
2018-09-12 16:42:58 -04:00
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
describe "setting a value using value=" do
|
|
|
|
it "supports storing and returning a value" do
|
|
|
|
input.value = "a_value"
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value).must_equal "a_value"
|
2018-09-12 16:42:58 -04:00
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "supports storing and returning false" do
|
2019-02-06 22:00:03 -05:00
|
|
|
input.value = false
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value).must_equal false
|
2018-09-12 16:42:58 -04:00
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "supports storing and returning nil" do
|
2019-02-06 22:00:03 -05:00
|
|
|
input.value = nil
|
2019-09-30 15:31:55 -07:00
|
|
|
_(input.value).must_be_nil
|
2018-09-12 16:42:58 -04:00
|
|
|
end
|
2017-09-22 14:57:51 +02:00
|
|
|
end
|
2017-09-21 12:17:44 -04:00
|
|
|
end
|