2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
2019-10-06 23:30:37 +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",
|
2021-04-15 19:35:35 +00:00
|
|
|
pattern: "^[0-9][0-9]$",
|
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)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(seen_value).must_equal value
|
2019-02-07 17:31:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-07 16:45:49 +00:00
|
|
|
|
2019-10-01 15:13:27 +00: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 23:17:22 +00:00
|
|
|
value: 12,
|
|
|
|
title: "Best input ever",
|
|
|
|
description: "important",
|
|
|
|
type: "Numeric",
|
2021-04-15 19:35:35 +00:00
|
|
|
pattern: "^[0-9][0-9]$",
|
2019-11-07 23:17:22 +00:00
|
|
|
required: true)
|
|
|
|
|
2019-10-01 15:13:27 +00:00
|
|
|
_(input.to_hash).must_equal({
|
|
|
|
name: "test_input",
|
|
|
|
options: {
|
2019-10-04 18:06:49 +00:00
|
|
|
value: 12,
|
|
|
|
title: "Best input ever",
|
|
|
|
description: "important",
|
|
|
|
type: "Numeric",
|
2021-04-15 19:35:35 +00:00
|
|
|
pattern: "^[0-9][0-9]$",
|
2019-10-04 18:06:49 +00:00
|
|
|
required: true,
|
2019-10-02 16:22:20 +00:00
|
|
|
},
|
2019-10-01 15:13:27 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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"
|
2019-09-30 22:31:55 +00:00
|
|
|
_(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-09-30 22:31:55 +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
|
|
|
|
_(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-09-30 22:31:55 +00: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 15:05:31 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "has a dummy value that can be called like a nested map" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(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-09-30 22:31:55 +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")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(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-09-30 22:31:55 +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-09-30 22:31:55 +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"
|
2019-09-30 22:31:55 +00:00
|
|
|
_(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")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(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")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(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"
|
2019-09-30 22:31:55 +00:00
|
|
|
_(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
|
2019-09-30 22:31:55 +00:00
|
|
|
_(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
|
2019-09-30 22:31:55 +00:00
|
|
|
_(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
|