e9428e344ea5e990531273b787ce16d19c5710f7
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / loadyaml_spec.rb
1 require 'spec_helper'
2
3 describe 'loadyaml' do
4   it { is_expected.not_to eq(nil) }
5   it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
6
7   context 'when a non-existing file is specified' do
8     let(:filename) { '/tmp/doesnotexist' }
9     before {
10       File.expects(:exists?).with(filename).returns(false).once
11       YAML.expects(:load_file).never
12     }
13     it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
14     it { is_expected.to run.with_params(filename, {'đẽƒằưļŧ' => '٧ẵłựέ'}).and_return({'đẽƒằưļŧ' => '٧ẵłựέ'}) }
15     it { is_expected.to run.with_params(filename, {'デフォルト' => '値'}).and_return({'デフォルト' => '値'}) }
16   end
17
18   context 'when an existing file is specified' do
19     let(:filename) { '/tmp/doesexist' }
20     let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値'} }
21     before {
22       File.expects(:exists?).with(filename).returns(true).once
23       YAML.expects(:load_file).with(filename).returns(data).once
24     }
25     it { is_expected.to run.with_params(filename).and_return(data) }
26   end
27
28   context 'when the file could not be parsed' do
29     let(:filename) { '/tmp/doesexist' }
30     before {
31       File.expects(:exists?).with(filename).returns(true).once
32       YAML.stubs(:load_file).with(filename).once.raises StandardError, 'Something terrible have happened!'
33     }
34     it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
35   end
36 end