29d78965867baf96285fbfecfff5d4eef74b9c70
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / pick_default_spec.rb
1 require 'spec_helper'
2
3 describe 'pick_default' do
4   it { is_expected.not_to eq(nil) }
5   it { is_expected.to run.with_params.and_raise_error(RuntimeError, %r{Must receive at least one argument}) }
6
7   it { is_expected.to run.with_params('one', 'two').and_return('one') }
8   it { is_expected.to run.with_params('ớņệ', 'ťωơ').and_return('ớņệ') }
9   it { is_expected.to run.with_params('', 'two').and_return('two') }
10   it { is_expected.to run.with_params(:undef, 'two').and_return('two') }
11   it { is_expected.to run.with_params(:undefined, 'two').and_return('two') }
12   it { is_expected.to run.with_params(nil, 'two').and_return('two') }
13
14   ['', :undef, :undefined, nil, {}, [], 1, 'default'].each do |value|
15     describe "when providing #{value.inspect} as default" do
16       it { is_expected.to run.with_params('one', value).and_return('one') }
17       it { is_expected.to run.with_params('ớņệ', value).and_return('ớņệ') }
18       it { is_expected.to run.with_params([], value).and_return([]) }
19       it { is_expected.to run.with_params({}, value).and_return({}) }
20       it { is_expected.to run.with_params(value, value).and_return(mapped_value(value)) }
21       it { is_expected.to run.with_params(:undef, value).and_return(mapped_value(value)) }
22       it { is_expected.to run.with_params(:undefined, value).and_return(mapped_value(value)) }
23       it { is_expected.to run.with_params(nil, value).and_return(mapped_value(value)) }
24     end
25   end
26
27   if Puppet::Util::Package.versioncmp(Puppet.version, '6.0.0') < 0 ||
28      Puppet::Util::Package.versioncmp(Puppet.version, '5.5.7') == 0
29     def mapped_value(v)
30       v
31     end
32   else
33     def mapped_value(v)
34       # Puppet 6.0.0 will always map arguments the same way as the Puppet Language
35       # even if function is called from Ruby via call_function
36       # The 3x function API expects nil and :undef to be represented as empty string
37       (v.nil? || v == :undef) ? '' : v
38     end
39   end
40 end