|
| 1 | +// This file is part of YamlDotNet - A .NET library for YAML. |
| 2 | +// Copyright (c) Antoine Aubry and contributors |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | +// this software and associated documentation files (the "Software"), to deal in |
| 6 | +// the Software without restriction, including without limitation the rights to |
| 7 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 | +// of the Software, and to permit persons to whom the Software is furnished to do |
| 9 | +// so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | + |
| 22 | +using System.Collections.Generic; |
| 23 | +using System.IO; |
| 24 | +using FluentAssertions; |
| 25 | +using Xunit; |
| 26 | +using YamlDotNet.Core; |
| 27 | +using YamlDotNet.Serialization; |
| 28 | +using YamlDotNet.Test.Core; |
| 29 | + |
| 30 | +namespace YamlDotNet.Test.Serialization |
| 31 | +{ |
| 32 | + public class MergingParserTests : EmitterTestsHelper |
| 33 | + { |
| 34 | + [Fact] |
| 35 | + public void MergingParserWithMergeObjectWithSequence_EachLevelsShouldEquals() |
| 36 | + { |
| 37 | + var yaml = @"base_level: &base |
| 38 | + tenant: |
| 39 | + - a1 |
| 40 | + - a2 |
| 41 | +Level1: &Level1 |
| 42 | + <<: [*base] |
| 43 | +Level2: &Level2 |
| 44 | + <<: *Level1 |
| 45 | +"; |
| 46 | + |
| 47 | + var etalonLevel = @"tenant: |
| 48 | +- a1 |
| 49 | +- a2 |
| 50 | +".NormalizeNewLines(); |
| 51 | + |
| 52 | + var mergingParser = new MergingParser(new Parser(new StringReader(yaml))); |
| 53 | + var yamlObject = new DeserializerBuilder().Build().Deserialize<Dictionary<string, object>>(mergingParser); |
| 54 | + yamlObject.Should().NotBeNull(); |
| 55 | + |
| 56 | + var serializer = new SerializerBuilder().Build(); |
| 57 | + serializer.Serialize(yamlObject["base_level"]).NormalizeNewLines().Should().Be(etalonLevel); |
| 58 | + serializer.Serialize(yamlObject["Level1"]).NormalizeNewLines().Should().Be(etalonLevel); |
| 59 | + serializer.Serialize(yamlObject["Level2"]).NormalizeNewLines().Should().Be(etalonLevel); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void MergingParserWithMergeObjectWithSequence_EmittedTextShouldNotContainsDeletedEvents() |
| 64 | + { |
| 65 | + var yaml = @"base_level: &base |
| 66 | + tenant: |
| 67 | + - a1 |
| 68 | + - a2 |
| 69 | +Level1: &Level1 |
| 70 | + <<: [*base] |
| 71 | +Level2: |
| 72 | + <<: *Level1 |
| 73 | +"; |
| 74 | + |
| 75 | + var etalonEmittedText = @"base_level: &base |
| 76 | + tenant: |
| 77 | + - a1 |
| 78 | + - a2 |
| 79 | +Level1: &Level1 |
| 80 | + tenant: |
| 81 | + - a1 |
| 82 | + - a2 |
| 83 | +Level2: |
| 84 | + tenant: |
| 85 | + - a1 |
| 86 | + - a2 |
| 87 | +".NormalizeNewLines(); |
| 88 | + |
| 89 | + var mergingParser = new MergingParser(new Parser(new StringReader(yaml))); |
| 90 | + var events = EnumerationOf(mergingParser); |
| 91 | + EmittedTextFrom(events).NormalizeNewLines().Should().Be(etalonEmittedText); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void MergingParserWithMergeObjectWithSequenceAndScalarItems_EmittedTextShouldNotContainsDeletedEvents() |
| 96 | + { |
| 97 | + var yaml = @"base_level: &base |
| 98 | + tenant: |
| 99 | + - a1 |
| 100 | + - a2 |
| 101 | +Level1: &Level1 |
| 102 | + <<: [*base] |
| 103 | + item1: |
| 104 | +Level2: |
| 105 | + <<: *Level1 |
| 106 | + item2: |
| 107 | +"; |
| 108 | + |
| 109 | + var etalonEmittedText = @"base_level: &base |
| 110 | + tenant: |
| 111 | + - a1 |
| 112 | + - a2 |
| 113 | +Level1: &Level1 |
| 114 | + tenant: |
| 115 | + - a1 |
| 116 | + - a2 |
| 117 | + item1: '' |
| 118 | +Level2: |
| 119 | + tenant: |
| 120 | + - a1 |
| 121 | + - a2 |
| 122 | + item1: '' |
| 123 | + item2: '' |
| 124 | +".NormalizeNewLines(); |
| 125 | + |
| 126 | + var mergingParser = new MergingParser(new Parser(new StringReader(yaml))); |
| 127 | + var events = EnumerationOf(mergingParser); |
| 128 | + EmittedTextFrom(events).NormalizeNewLines().Should().Be(etalonEmittedText); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public void MergingParserWithNestedSequence_ShouldNotThrowException() |
| 133 | + { |
| 134 | + var yaml = @" |
| 135 | +base_level: &base {} |
| 136 | +Level1: &Level1 |
| 137 | + <<: [*base] |
| 138 | +Level2: &Level2 |
| 139 | + <<: [*Level1] |
| 140 | +Level3: |
| 141 | + <<: *Level2 |
| 142 | +"; |
| 143 | + var etalonMergedYaml = @"base_level: {} |
| 144 | +Level1: {} |
| 145 | +Level2: {} |
| 146 | +Level3: {} |
| 147 | +".NormalizeNewLines(); |
| 148 | + |
| 149 | + var mergingParserFailed = new MergingParser(new Parser(new StringReader(yaml))); |
| 150 | + var yamlObject = new DeserializerBuilder().Build().Deserialize(mergingParserFailed); |
| 151 | + |
| 152 | + new SerializerBuilder().Build().Serialize(yamlObject!).NormalizeNewLines().Should().Be(etalonMergedYaml); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments