summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rwxr-xr-xspec/acceptance/validate_array_spec.rb16
-rwxr-xr-xspec/acceptance/validate_bool_spec.rb16
-rwxr-xr-xspec/acceptance/validate_hash_spec.rb16
-rw-r--r--spec/functions/dig44_spec.rb119
4 files changed, 114 insertions, 53 deletions
diff --git a/spec/acceptance/validate_array_spec.rb b/spec/acceptance/validate_array_spec.rb
index 2f549d5..b53e98c 100755
--- a/spec/acceptance/validate_array_spec.rb
+++ b/spec/acceptance/validate_array_spec.rb
@@ -20,14 +20,14 @@ describe 'validate_array function', :unless => UNSUPPORTED_PLATFORMS.include?(fa
apply_manifest(pp, :catch_failures => true)
end
- [
- %{validate_array({'a' => 'hash' })},
- %{validate_array('string')},
- %{validate_array(false)},
- %{validate_array(undef)}
- ].each do |pp|
- it "rejects #{pp.inspect}" do
- expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/is not an Array\. It looks to be a/)
+ it 'validates a non-array' do
+ {
+ %{validate_array({'a' => 'hash' })} => "Hash",
+ %{validate_array('string')} => "String",
+ %{validate_array(false)} => "FalseClass",
+ %{validate_array(undef)} => "String"
+ }.each do |pp,type|
+ expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/a #{type}/)
end
end
end
diff --git a/spec/acceptance/validate_bool_spec.rb b/spec/acceptance/validate_bool_spec.rb
index 5c52d0f..c837f08 100755
--- a/spec/acceptance/validate_bool_spec.rb
+++ b/spec/acceptance/validate_bool_spec.rb
@@ -20,14 +20,14 @@ describe 'validate_bool function', :unless => UNSUPPORTED_PLATFORMS.include?(fac
apply_manifest(pp, :catch_failures => true)
end
- [
- %{validate_bool('true')},
- %{validate_bool('false')},
- %{validate_bool([true])},
- %{validate_bool(undef)}
- ].each do |pp|
- it "rejects #{pp.inspect}" do
- expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/is not a boolean\. It looks to be a/)
+ it 'validates a non-bool' do
+ {
+ %{validate_bool('true')} => "String",
+ %{validate_bool('false')} => "String",
+ %{validate_bool([true])} => "Array",
+ %{validate_bool(undef)} => "String",
+ }.each do |pp,type|
+ expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/a #{type}/)
end
end
end
diff --git a/spec/acceptance/validate_hash_spec.rb b/spec/acceptance/validate_hash_spec.rb
index 637df0a..52fb615 100755
--- a/spec/acceptance/validate_hash_spec.rb
+++ b/spec/acceptance/validate_hash_spec.rb
@@ -20,14 +20,14 @@ describe 'validate_hash function', :unless => UNSUPPORTED_PLATFORMS.include?(fac
apply_manifest(pp, :catch_failures => true)
end
- [
- %{validate_hash('{ "not" => "hash" }')},
- %{validate_hash('string')},
- %{validate_hash(["array"])},
- %{validate_hash(undef)}
- ].each do |pp|
- it "rejects #{pp.inspect}" do
- expect(apply_manifest(pp, :expect_failures => true).stderr).to match(//)
+ it 'validates a non-hash' do
+ {
+ %{validate_hash('{ "not" => "hash" }')} => "String",
+ %{validate_hash('string')} => "String",
+ %{validate_hash(["array"])} => "Array",
+ %{validate_hash(undef)} => "String",
+ }.each do |pp,type|
+ expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/a #{type}/)
end
end
end
diff --git a/spec/functions/dig44_spec.rb b/spec/functions/dig44_spec.rb
index fd451ff..4f7408d 100644
--- a/spec/functions/dig44_spec.rb
+++ b/spec/functions/dig44_spec.rb
@@ -1,44 +1,105 @@
require 'spec_helper'
describe 'dig44' do
- it "should exist" do
- expect(Puppet::Parser::Functions.function("dig44")).to eq("function_dig44")
+
+ let(:data) do
+ {
+ 'a' => {
+ 'g' => '2',
+ 'e' => [
+ 'f0',
+ 'f1',
+ {
+ 'x' => {
+ 'y' => 'z'
+ }
+ },
+ 'f3',
+ ]
+ },
+ 'b' => true,
+ 'c' => false,
+ 'd' => '1',
+ 'e' => :undef,
+ 'f' => nil,
+ }
end
- it "should raise a ParseError if there are less than 2 arguments" do
- expect { scope.function_dig44([]) }.to raise_error(Puppet::ParseError)
- end
+ context 'single values' do
+ it 'should exist' do
+ is_expected.not_to be_nil
+ end
- it "should raise a ParseError if the first argument isn't a hash or array" do
- expect { scope.function_dig44(['bad', []]) }.to raise_error(Puppet::ParseError)
- end
+ it 'should require two arguments' do
+ is_expected.to run.with_params().and_raise_error(ArgumentError)
+ end
- it "should raise a ParseError if the second argument isn't an array" do
- expect { scope.function_dig44([{}, 'bad']) }.to raise_error(Puppet::ParseError)
- end
+ it 'should fail if the data is not a structure' do
+ is_expected.to run.with_params('test', []).and_raise_error(Puppet::Error)
+ end
- it "should return an empty hash when given empty parameters" do
- result = scope.function_dig44([{}, []])
- expect(result).to(eq({}))
- end
+ it 'should fail if the path is not an array' do
+ is_expected.to run.with_params({}, '').and_raise_error(Puppet::Error)
+ end
- it "should return value when given simple hash" do
- result = scope.function_dig44([{"a" => "b"}, ["a"]])
- expect(result).to(eq("b"))
- end
+ it 'should return the value if the value is string' do
+ is_expected.to run.with_params(data, ['d'], 'default').and_return('1')
+ end
- it "should find hash values two levels deep" do
- result = scope.function_dig44([{"a" => {"b" => "c"}}, ["a", "b"]])
- expect(result).to(eq("c"))
- end
+ it 'should return true if the value is true' do
+ is_expected.to run.with_params(data, ['b'], 'default').and_return(true)
+ end
- it "should return default value if nothing was found" do
- result = scope.function_dig44([{}, ["a", "b"], "d"])
- expect(result).to(eq("d"))
+ it 'should return false if the value is false' do
+ is_expected.to run.with_params(data, ['c'], 'default').and_return(false)
+ end
+
+ it 'should return the default if the value is nil' do
+ is_expected.to run.with_params(data, ['f'], 'default').and_return('default')
+ end
+
+ it 'should return the default if the value is :undef (same as nil)' do
+ is_expected.to run.with_params(data, ['e'], 'default').and_return('default')
+ end
+
+ it 'should return the default if the path is not found' do
+ is_expected.to run.with_params(data, ['missing'], 'default').and_return('default')
+ end
end
- it "should work on booleans as well as strings" do
- result = scope.function_dig44([{"a" => false}, ["a"]])
- expect(result).to(eq(false))
+ context 'structure values' do
+
+ it 'should be able to extract a deeply nested hash value' do
+ is_expected.to run.with_params(data, %w(a g), 'default').and_return('2')
+ end
+
+ it 'should return the default value if the path is too long' do
+ is_expected.to run.with_params(data, %w(a g c d), 'default').and_return('default')
+ end
+
+ it 'should support an array index (number) in the path' do
+ is_expected.to run.with_params(data, ['a', 'e', 1], 'default').and_return('f1')
+ end
+
+ it 'should support an array index (string) in the path' do
+ is_expected.to run.with_params(data, %w(a e 1), 'default').and_return('f1')
+ end
+
+ it 'should return the default value if an array index is not a number' do
+ is_expected.to run.with_params(data, %w(a b c), 'default').and_return('default')
+ end
+
+ it 'should return the default value if and index is out of array length' do
+ is_expected.to run.with_params(data, %w(a e 5), 'default').and_return('default')
+ end
+
+ it 'should be able to path though both arrays and hashes' do
+ is_expected.to run.with_params(data, %w(a e 2 x y), 'default').and_return('z')
+ end
+
+ it 'should return "nil" if value is not found and no default value is provided' do
+ is_expected.to run.with_params(data, %w(a 1)).and_return(nil)
+ end
+
end
end