|
1 | 1 | require 'spec_helper'
|
2 | 2 |
|
3 | 3 | describe 'parseyaml' do
|
4 |
| - it { is_expected.not_to eq(nil) } |
5 |
| - it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } |
6 |
| - it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } |
7 |
| - it { is_expected.to run.with_params('["one", "two", "three"]').and_return(['one', 'two', 'three']) } |
8 |
| - context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do |
9 |
| - it { is_expected.to run.with_params('["one"').and_raise_error(Psych::SyntaxError) } |
| 4 | + it 'should exist' do |
| 5 | + is_expected.not_to eq(nil) |
10 | 6 | end
|
11 |
| - context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do |
12 |
| - it { is_expected.to run.with_params('["one"').and_raise_error(ArgumentError) } |
| 7 | + |
| 8 | + it 'should raise an error if called without any arguments' do |
| 9 | + is_expected.to run.with_params(). |
| 10 | + and_raise_error(/wrong number of arguments/i) |
| 11 | + end |
| 12 | + |
| 13 | + context 'with correct YAML data' do |
| 14 | + it 'should be able to parse a YAML data with a String' do |
| 15 | + is_expected.to run.with_params('--- just a string'). |
| 16 | + and_return('just a string') |
| 17 | + is_expected.to run.with_params('just a string'). |
| 18 | + and_return('just a string') |
| 19 | + end |
| 20 | + |
| 21 | + it 'should be able to parse a 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 'should be able to parse a 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 'should be able to parse a 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 'should not return the default value if the data was parsed correctly' do |
| 37 | + is_expected.to run.with_params("---\na: '1'\n", 'default_value'). |
| 38 | + and_return({'a' => '1'}) |
| 39 | + end |
| 40 | + |
13 | 41 | end
|
| 42 | + |
| 43 | + context 'with incorrect YAML data' do |
| 44 | + it 'should return "nil" if a default value should be returned but is not provided' do |
| 45 | + is_expected.to run.with_params(''). |
| 46 | + and_return(nil) |
| 47 | + end |
| 48 | + |
| 49 | + it 'should support a structure for a default value' do |
| 50 | + is_expected.to run.with_params('', {'a' => '1'}). |
| 51 | + and_return({'a' => '1'}) |
| 52 | + end |
| 53 | + |
| 54 | + [1, 1.2, nil, true, false, [], {}, :yaml].each do |value| |
| 55 | + it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do |
| 56 | + is_expected.to run.with_params(value, 'default_value'). |
| 57 | + and_return('default_value') |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do |
| 62 | + ['---', '...', '*8', ''].each do |value| |
| 63 | + it "should return the default value for an incorrect #{value.inspect} string parameter" do |
| 64 | + is_expected.to run.with_params(value, 'default_value'). |
| 65 | + and_return('default_value') |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + end |
| 71 | + |
14 | 72 | end
|
0 commit comments