Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / parseyaml_spec.rb
1 require 'spec_helper'
2
3 describe 'parseyaml' do
4   it 'exists' do
5     is_expected.not_to eq(nil)
6   end
7
8   it 'raises an error if called without any arguments' do
9     is_expected.to run.with_params
10                       .and_raise_error(%r{wrong number of arguments}i)
11   end
12
13   context 'with correct YAML data' do
14     it 'is able to parse a YAML data with a String' do
15       actual_array = ['--- just a string', 'just a string']
16       actual_array.each do |actual|
17         is_expected.to run.with_params(actual).and_return('just a string')
18       end
19     end
20
21     it 'is able to parse YAML data with a Hash' do
22       is_expected.to run.with_params("---\na: '1'\nb: '2'\n")
23                         .and_return('a' => '1', 'b' => '2')
24     end
25
26     it 'is able to parse YAML data with an Array' do
27       is_expected.to run.with_params("---\n- a\n- b\n- c\n")
28                         .and_return(['a', 'b', 'c'])
29     end
30
31     it 'is able to parse YAML data with a mixed structure' do
32       is_expected.to run.with_params("---\na: '1'\nb: 2\nc:\n  d:\n  - :a\n  - true\n  - false\n")
33                         .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [:a, true, false] })
34     end
35
36     it 'is able to parse YAML data with a UTF8 and double byte characters' do
37       is_expected.to run.with_params("---\na: ×\nこれ: 記号\nです:\n  ©:\n  - Á\n  - ß\n")
38                         .and_return('a' => '×', 'これ' => '記号', 'です' => { '©' => ['Á', 'ß'] })
39     end
40
41     it 'does not return the default value if the data was parsed correctly' do
42       is_expected.to run.with_params("---\na: '1'\n", 'default_value')
43                         .and_return('a' => '1')
44     end
45   end
46
47   context 'on a modern ruby', :unless => RUBY_VERSION == '1.8.7' do
48     it 'raises an error with invalid YAML and no default' do
49       is_expected.to run.with_params('["one"')
50                         .and_raise_error(Psych::SyntaxError)
51     end
52   end
53
54   context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do
55     it 'raises an error with invalid YAML and no default' do
56       is_expected.to run.with_params('["one"')
57                         .and_raise_error(ArgumentError)
58     end
59   end
60
61   context 'with incorrect YAML data' do
62     it 'supports a structure for a default value' do
63       is_expected.to run.with_params('', 'a' => '1')
64                         .and_return('a' => '1')
65     end
66
67     [1, 1.2, nil, true, false, [], {}, :yaml].each do |value|
68       it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do
69         is_expected.to run.with_params(value, 'default_value')
70                           .and_return('default_value')
71       end
72     end
73
74     context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
75       ['---', '...', '*8', ''].each do |value|
76         it "should return the default value for an incorrect #{value.inspect} string parameter" do
77           is_expected.to run.with_params(value, 'default_value')
78                             .and_return('default_value')
79         end
80       end
81     end
82   end
83 end