summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorDmitry Ilyin <dilyin@mirantis.com>2015-09-01 21:39:16 +0300
committerDmitry Ilyin <dilyin@mirantis.com>2015-09-01 21:45:44 +0300
commit823a352f0f47d4481844bb6b6a6c00224ed556b8 (patch)
treea8224d823ec61c69d6364dc2b65e4afa138a1590 /spec
parentf820bb156038f638d8e488286d0c2b92c5636925 (diff)
downloadpuppet-stdlib-823a352f0f47d4481844bb6b6a6c00224ed556b8.tar.gz
puppet-stdlib-823a352f0f47d4481844bb6b6a6c00224ed556b8.tar.bz2
Add a new function "try_get_value"
* Extracts a value from a deeply-nested data structure * Returns default if a value could not be extracted
Diffstat (limited to 'spec')
-rwxr-xr-xspec/acceptance/try_get_value_spec.rb47
-rw-r--r--spec/functions/try_get_value_spec.rb100
2 files changed, 147 insertions, 0 deletions
diff --git a/spec/acceptance/try_get_value_spec.rb b/spec/acceptance/try_get_value_spec.rb
new file mode 100755
index 0000000..46b1c4d
--- /dev/null
+++ b/spec/acceptance/try_get_value_spec.rb
@@ -0,0 +1,47 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper_acceptance'
+
+describe 'try_get_value function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
+ describe 'success' do
+ it 'try_get_valuees a value' do
+ pp = <<-EOS
+ $data = {
+ 'a' => { 'b' => 'passing'}
+ }
+
+ $tests = try_get_value($a, 'a/b')
+ notice(inline_template('tests are <%= @tests.inspect %>'))
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/tests are "passing"/)
+ end
+ end
+ end
+ describe 'failure' do
+ it 'uses a default value' do
+ pp = <<-EOS
+ $data = {
+ 'a' => { 'b' => 'passing'}
+ }
+
+ $tests = try_get_value($a, 'c/d', 'using the default value')
+ notice(inline_template('tests are <%= @tests.inspect %>'))
+ EOS
+
+ apply_manifest(pp, :expect_failures => true) do |r|
+ expect(r.stdout).to match(/using the default value/)
+ end
+ end
+
+ it 'raises error on incorrect number of arguments' do
+ pp = <<-EOS
+ $o = try_get_value()
+ EOS
+
+ apply_manifest(pp, :expect_failures => true) do |r|
+ expect(r.stderr).to match(/wrong number of arguments/i)
+ end
+ end
+ end
+end
diff --git a/spec/functions/try_get_value_spec.rb b/spec/functions/try_get_value_spec.rb
new file mode 100644
index 0000000..38c0efd
--- /dev/null
+++ b/spec/functions/try_get_value_spec.rb
@@ -0,0 +1,100 @@
+require 'spec_helper'
+
+describe 'try_get_value' do
+
+ let(:data) do
+ {
+ 'a' => {
+ 'g' => '2',
+ 'e' => [
+ 'f0',
+ 'f1',
+ {
+ 'x' => {
+ 'y' => 'z'
+ }
+ },
+ 'f3',
+ ]
+ },
+ 'b' => true,
+ 'c' => false,
+ 'd' => '1',
+ }
+ end
+
+ context 'single values' do
+ it 'should exist' do
+ is_expected.not_to eq(nil)
+ end
+
+ it 'should be able to return a single value' do
+ is_expected.to run.with_params('test').and_return('test')
+ end
+
+ it 'should use the default value if data is a single value and path is present' do
+ is_expected.to run.with_params('test', 'path', 'default').and_return('default')
+ end
+
+ it 'should return default if there is no data' do
+ is_expected.to run.with_params(nil, nil, 'default').and_return('default')
+ end
+
+ it 'should be able to use data structures as default values' do
+ is_expected.to run.with_params('test', 'path', data).and_return(data)
+ end
+ end
+
+ context 'structure values' do
+ it 'should be able to extracts a single hash value' do
+ is_expected.to run.with_params(data, 'd', 'default').and_return('1')
+ end
+
+ it 'should be able to extract a deeply nested hash value' do
+ is_expected.to run.with_params(data, 'a/g', 'default').and_return('2')
+ end
+
+ it 'should return the default value if the path is not found' do
+ is_expected.to run.with_params(data, 'missing', 'default').and_return('default')
+ end
+
+ it 'should return the default value if the path is too long' do
+ is_expected.to run.with_params(data, 'a/g/c/d', 'default').and_return('default')
+ end
+
+ it 'should support an array index in the path' do
+ is_expected.to run.with_params(data, '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, '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, '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, 'a/e/2/x/y', 'default').and_return('z')
+ end
+
+ it 'should be able to return "true" value' do
+ is_expected.to run.with_params(data, 'b', 'default').and_return(true)
+ is_expected.to run.with_params(data, 'm', true).and_return(true)
+ end
+
+ it 'should be able to return "false" value' do
+ is_expected.to run.with_params(data, 'c', 'default').and_return(false)
+ is_expected.to run.with_params(data, 'm', false).and_return(false)
+ end
+
+ it 'should return "nil" if value is not found and no default value is provided' do
+ is_expected.to run.with_params(data, 'a/1').and_return(nil)
+ end
+
+ it 'should be able to use a custom path separator' do
+ is_expected.to run.with_params(data, 'a::g', 'default', '::').and_return('2')
+ is_expected.to run.with_params(data, 'a::c', 'default', '::').and_return('default')
+ end
+ end
+end