|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package yaml_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + |
| 22 | + . "gopkg.in/check.v1" |
| 23 | + yaml "sigs.k8s.io/yaml/goyaml.v3" |
| 24 | +) |
| 25 | + |
| 26 | +func (s *S) TestCompactSeqIndentDefault(c *C) { |
| 27 | + var buf bytes.Buffer |
| 28 | + enc := yaml.NewEncoder(&buf) |
| 29 | + enc.CompactSeqIndent() |
| 30 | + err := enc.Encode(map[string]interface{}{"a": []string{"b", "c"}}) |
| 31 | + c.Assert(err, Equals, nil) |
| 32 | + err = enc.Close() |
| 33 | + c.Assert(err, Equals, nil) |
| 34 | + // The default indent is 4, so these sequence elements get 2 indents as before |
| 35 | + c.Assert(buf.String(), Equals, `a: |
| 36 | + - b |
| 37 | + - c |
| 38 | +`) |
| 39 | +} |
| 40 | + |
| 41 | +func (s *S) TestCompactSequenceWithSetIndent(c *C) { |
| 42 | + var buf bytes.Buffer |
| 43 | + enc := yaml.NewEncoder(&buf) |
| 44 | + enc.CompactSeqIndent() |
| 45 | + enc.SetIndent(2) |
| 46 | + err := enc.Encode(map[string]interface{}{"a": []string{"b", "c"}}) |
| 47 | + c.Assert(err, Equals, nil) |
| 48 | + err = enc.Close() |
| 49 | + c.Assert(err, Equals, nil) |
| 50 | + // The sequence indent is 2, so these sequence elements don't get indented at all |
| 51 | + c.Assert(buf.String(), Equals, `a: |
| 52 | +- b |
| 53 | +- c |
| 54 | +`) |
| 55 | +} |
| 56 | + |
| 57 | +type normal string |
| 58 | +type compact string |
| 59 | + |
| 60 | +// newlinePlusNormalToNewlinePlusCompact maps the normal encoding (prefixed with a newline) |
| 61 | +// to the compact encoding (prefixed with a newline), for test cases in marshalTests |
| 62 | +var newlinePlusNormalToNewlinePlusCompact = map[normal]compact{ |
| 63 | + normal(` |
| 64 | +v: |
| 65 | + - A |
| 66 | + - B |
| 67 | +`): compact(` |
| 68 | +v: |
| 69 | + - A |
| 70 | + - B |
| 71 | +`), |
| 72 | + |
| 73 | + normal(` |
| 74 | +v: |
| 75 | + - A |
| 76 | + - |- |
| 77 | + B |
| 78 | + C |
| 79 | +`): compact(` |
| 80 | +v: |
| 81 | + - A |
| 82 | + - |- |
| 83 | + B |
| 84 | + C |
| 85 | +`), |
| 86 | + |
| 87 | + normal(` |
| 88 | +v: |
| 89 | + - A |
| 90 | + - 1 |
| 91 | + - B: |
| 92 | + - 2 |
| 93 | + - 3 |
| 94 | +`): compact(` |
| 95 | +v: |
| 96 | + - A |
| 97 | + - 1 |
| 98 | + - B: |
| 99 | + - 2 |
| 100 | + - 3 |
| 101 | +`), |
| 102 | + |
| 103 | + normal(` |
| 104 | +a: |
| 105 | + - 1 |
| 106 | + - 2 |
| 107 | +`): compact(` |
| 108 | +a: |
| 109 | + - 1 |
| 110 | + - 2 |
| 111 | +`), |
| 112 | + |
| 113 | + normal(` |
| 114 | +a: |
| 115 | + b: |
| 116 | + - c: 1 |
| 117 | + d: 2 |
| 118 | +`): compact(` |
| 119 | +a: |
| 120 | + b: |
| 121 | + - c: 1 |
| 122 | + d: 2 |
| 123 | +`), |
| 124 | +} |
| 125 | + |
| 126 | +func (s *S) TestEncoderCompactIndents(c *C) { |
| 127 | + for i, item := range marshalTests { |
| 128 | + c.Logf("test %d. %q", i, item.data) |
| 129 | + var buf bytes.Buffer |
| 130 | + enc := yaml.NewEncoder(&buf) |
| 131 | + enc.CompactSeqIndent() |
| 132 | + err := enc.Encode(item.value) |
| 133 | + c.Assert(err, Equals, nil) |
| 134 | + err = enc.Close() |
| 135 | + c.Assert(err, Equals, nil) |
| 136 | + |
| 137 | + // Default to expecting the item data |
| 138 | + expected := item.data |
| 139 | + // If there's a different compact representation, use that |
| 140 | + if c, ok := newlinePlusNormalToNewlinePlusCompact[normal("\n"+item.data)]; ok { |
| 141 | + expected = string(c[1:]) |
| 142 | + } |
| 143 | + |
| 144 | + c.Assert(buf.String(), Equals, expected) |
| 145 | + } |
| 146 | +} |
0 commit comments