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