|
1 | 1 | # frozen_string_literal: true |
2 | | - |
3 | 2 | require 'spec_helper' |
4 | 3 |
|
5 | 4 | describe 'stdlib::to_yaml' do |
6 | 5 | it { is_expected.not_to be_nil } |
| 6 | + |
| 7 | + # Basic scalars |
7 | 8 | it { is_expected.to run.with_params('').and_return("--- ''\n") } |
8 | | - it { is_expected.to run.with_params(true).and_return(%r{--- true\n}) } |
9 | | - it { is_expected.to run.with_params('one').and_return(%r{--- one\n}) } |
| 9 | + it { is_expected.to run.with_params(true).and_return("--- true\n") } |
| 10 | + it { is_expected.to run.with_params(false).and_return("--- false\n") } |
| 11 | + it { is_expected.to run.with_params(42).and_return("--- 42\n") } |
| 12 | + it { is_expected.to run.with_params('one').and_return("--- one\n") } |
| 13 | + |
| 14 | + # Unicode |
| 15 | + it { is_expected.to run.with_params('‰').and_return("--- \"‰\"\n") } |
| 16 | + it { is_expected.to run.with_params('∇').and_return("--- \"∇\"\n") } |
| 17 | + |
| 18 | + # Arrays |
10 | 19 | it { is_expected.to run.with_params([]).and_return("--- []\n") } |
11 | 20 | it { is_expected.to run.with_params(['one']).and_return("---\n- one\n") } |
12 | 21 | it { is_expected.to run.with_params(['one', 'two']).and_return("---\n- one\n- two\n") } |
| 22 | + |
| 23 | + # Hashes |
13 | 24 | it { is_expected.to run.with_params({}).and_return("--- {}\n") } |
14 | 25 | it { is_expected.to run.with_params('key' => 'value').and_return("---\nkey: value\n") } |
15 | 26 |
|
| 27 | + # Nested structures |
16 | 28 | it { |
17 | | - expect(subject).to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) |
18 | | - .and_return("---\none:\n oneA: A\n oneB:\n oneB1: '1'\n oneB2: '2'\ntwo:\n- twoA\n- twoB\n") |
| 29 | + is_expected.to run.with_params( |
| 30 | + 'one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, |
| 31 | + 'two' => ['twoA', 'twoB'] |
| 32 | + ).and_return("---\none:\n oneA: A\n oneB:\n oneB1: '1'\n oneB2: '2'\ntwo:\n- twoA\n- twoB\n") |
19 | 33 | } |
20 | 34 |
|
21 | | - it { is_expected.to run.with_params('‰').and_return("--- \"‰\"\n") } |
22 | | - it { is_expected.to run.with_params('∇').and_return("--- \"∇\"\n") } |
| 35 | + # Options: indentation |
| 36 | + it { |
| 37 | + is_expected.to run.with_params( |
| 38 | + { 'foo' => { 'bar' => true, 'baz' => false } }, |
| 39 | + 'indentation' => 4 |
| 40 | + ).and_return("---\nfoo:\n bar: true\n baz: false\n") |
| 41 | + } |
23 | 42 |
|
24 | | - it { is_expected.to run.with_params({ 'foo' => { 'bar' => true, 'baz' => false } }, 'indentation' => 4).and_return("---\nfoo:\n bar: true\n baz: false\n") } |
| 43 | + # Frozen options hash must not raise |
| 44 | + it { |
| 45 | + is_expected.to run.with_params( |
| 46 | + { 'foo' => { 'bar' => true } }, |
| 47 | + { 'indentation' => 4, 'sort_keys' => true }.freeze |
| 48 | + ).and_return("---\nfoo:\n bar: true\n") |
| 49 | + } |
25 | 50 |
|
| 51 | + # sort_keys: default behaviour preserves insertion order |
| 52 | + it { |
| 53 | + is_expected.to run.with_params( |
| 54 | + 'z' => 1, 'a' => 2, 'm' => 3 |
| 55 | + ).and_return("---\nz: 1\na: 2\nm: 3\n") |
| 56 | + } |
| 57 | + |
| 58 | + # sort_keys => true: top-level keys sorted |
| 59 | + it { |
| 60 | + is_expected.to run.with_params( |
| 61 | + { 'z' => 1, 'a' => 2, 'm' => 3 }, |
| 62 | + 'sort_keys' => true |
| 63 | + ).and_return("---\na: 2\nm: 3\nz: 1\n") |
| 64 | + } |
| 65 | + |
| 66 | + # sort_keys => true: nested keys sorted |
| 67 | + it { |
| 68 | + is_expected.to run.with_params( |
| 69 | + { 'parent' => { 'z' => 1, 'a' => 2 } }, |
| 70 | + 'sort_keys' => true |
| 71 | + ).and_return("---\nparent:\n a: 2\n z: 1\n") |
| 72 | + } |
| 73 | + |
| 74 | + # sort_keys => true: deeply nested keys sorted |
| 75 | + it { |
| 76 | + is_expected.to run.with_params( |
| 77 | + { 'z_parent' => { 'z_child' => 1, 'a_child' => 2 }, 'a_parent' => { 'x' => 5 } }, |
| 78 | + 'sort_keys' => true |
| 79 | + ).and_return("---\na_parent:\n x: 5\nz_parent:\n a_child: 2\n z_child: 1\n") |
| 80 | + } |
| 81 | + |
| 82 | + # sort_keys => true: array order preserved, hashes inside arrays sorted |
| 83 | + it { |
| 84 | + is_expected.to run.with_params( |
| 85 | + { 'items' => [{ 'z' => 1, 'a' => 2 }, { 'x' => 3, 'b' => 4 }] }, |
| 86 | + 'sort_keys' => true |
| 87 | + ).and_return("---\nitems:\n- a: 2\n z: 1\n- b: 4\n x: 3\n") |
| 88 | + } |
| 89 | + |
| 90 | + # sort_keys => true: plain array element order preserved |
| 91 | + it { |
| 92 | + is_expected.to run.with_params( |
| 93 | + { 'items' => [3, 1, 2] }, |
| 94 | + 'sort_keys' => true |
| 95 | + ).and_return("---\nitems:\n- 3\n- 1\n- 2\n") |
| 96 | + } |
| 97 | + |
| 98 | + # sort_keys combined with indentation |
| 99 | + it { |
| 100 | + is_expected.to run.with_params( |
| 101 | + { 'z' => { 'b' => 1, 'a' => 2 }, 'a' => 3 }, |
| 102 | + 'sort_keys' => true, 'indentation' => 4 |
| 103 | + ).and_return("---\na: 3\nz:\n a: 2\n b: 1\n") |
| 104 | + } |
| 105 | + |
| 106 | + # Sensitive data |
26 | 107 | context 'with data containing sensitive' do |
27 | | - it { is_expected.to run.with_params('key' => sensitive('value')).and_return(sensitive("---\nkey: value\n")) } |
| 108 | + it { |
| 109 | + is_expected.to run.with_params( |
| 110 | + 'key' => sensitive('value') |
| 111 | + ).and_return(sensitive("---\nkey: value\n")) |
| 112 | + } |
| 113 | + |
| 114 | + it { |
| 115 | + is_expected.to run.with_params( |
| 116 | + { 'z' => sensitive('secret'), 'a' => 'public' }, |
| 117 | + 'sort_keys' => true |
| 118 | + ).and_return(sensitive("---\na: public\nz: secret\n")) |
| 119 | + } |
28 | 120 | end |
29 | 121 | end |
0 commit comments