-
-
Notifications
You must be signed in to change notification settings - Fork 462
/
YamlStreamTests.cs
317 lines (264 loc) · 10.4 KB
/
YamlStreamTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using FluentAssertions;
using Xunit;
using YamlDotNet.Core;
using YamlDotNet.RepresentationModel;
namespace YamlDotNet.Test.RepresentationModel
{
public class YamlStreamTests
{
[Fact]
public void LoadSimpleDocument()
{
var stream = new YamlStream();
stream.Load(Yaml.ReaderFrom("02-scalar-in-imp-doc.yaml"));
Assert.Equal(1, stream.Documents.Count);
Assert.IsType<YamlScalarNode>(stream.Documents[0].RootNode);
Assert.Equal("a scalar", ((YamlScalarNode)stream.Documents[0].RootNode).Value);
Assert.Equal(YamlNodeType.Scalar, stream.Documents[0].RootNode.NodeType);
}
[Fact]
public void AccessingAllNodesOnInfinitelyRecursiveDocumentThrows()
{
var stream = new YamlStream();
stream.Load(Yaml.ParserForText("&a [*a]"));
var accessAllNodes = new Action(() => stream.Documents.Single().AllNodes.ToList());
accessAllNodes.ShouldThrow<MaximumRecursionLevelReachedException>("because the document is infinitely recursive.");
}
[Fact]
public void InfinitelyRecursiveNodeToStringSucceeds()
{
var stream = new YamlStream();
stream.Load(Yaml.ParserForText("&a [*a]"));
var toString = stream.Documents.Single().RootNode.ToString();
toString.Should().Contain("WARNING! INFINITE RECURSION!");
}
[Fact]
public void BackwardAliasReferenceWorks()
{
var stream = new YamlStream();
stream.Load(Yaml.ReaderFrom("backwards-alias.yaml"));
Assert.Equal(1, stream.Documents.Count);
Assert.IsType<YamlSequenceNode>(stream.Documents[0].RootNode);
var sequence = (YamlSequenceNode)stream.Documents[0].RootNode;
Assert.Equal(3, sequence.Children.Count);
Assert.Equal("a scalar", ((YamlScalarNode)sequence.Children[0]).Value);
Assert.Equal("another scalar", ((YamlScalarNode)sequence.Children[1]).Value);
Assert.Equal("a scalar", ((YamlScalarNode)sequence.Children[2]).Value);
Assert.Same(sequence.Children[0], sequence.Children[2]);
}
[Fact]
public void ForwardAliasReferenceWorks()
{
var stream = new YamlStream();
stream.Load(Yaml.ReaderFrom("forward-alias.yaml"));
Assert.Equal(1, stream.Documents.Count);
Assert.IsType<YamlSequenceNode>(stream.Documents[0].RootNode);
var sequence = (YamlSequenceNode)stream.Documents[0].RootNode;
Assert.Equal(3, sequence.Children.Count);
Assert.Equal("a scalar", ((YamlScalarNode)sequence.Children[0]).Value);
Assert.Equal("another scalar", ((YamlScalarNode)sequence.Children[1]).Value);
Assert.Equal("a scalar", ((YamlScalarNode)sequence.Children[2]).Value);
Assert.Same(sequence.Children[0], sequence.Children[2]);
}
[Fact]
public void RoundtripExample1()
{
RoundtripTest("01-directives.yaml");
}
[Fact]
public void RoundtripExample2()
{
RoundtripTest("02-scalar-in-imp-doc.yaml");
}
[Fact]
public void RoundtripExample3()
{
RoundtripTest("03-scalar-in-exp-doc.yaml");
}
[Fact]
public void RoundtripExample4()
{
RoundtripTest("04-scalars-in-multi-docs.yaml");
}
[Fact]
public void RoundtripExample5()
{
RoundtripTest("06-float-tag.yaml");
}
[Fact]
public void RoundtripExample6()
{
RoundtripTest("06-float-tag.yaml");
}
[Fact]
public void RoundtripExample7()
{
RoundtripTest("07-scalar-styles.yaml");
}
[Fact]
public void RoundtripExample8()
{
RoundtripTest("08-flow-sequence.yaml");
}
[Fact]
public void RoundtripExample9()
{
RoundtripTest("09-flow-mapping.yaml");
}
[Fact]
public void RoundtripExample10()
{
RoundtripTest("10-mixed-nodes-in-sequence.yaml");
}
[Fact]
public void RoundtripExample11()
{
RoundtripTest("11-mixed-nodes-in-mapping.yaml");
}
[Fact]
public void RoundtripExample12()
{
RoundtripTest("12-compact-sequence.yaml");
}
[Fact]
public void RoundtripExample13()
{
RoundtripTest("13-compact-mapping.yaml");
}
[Fact]
public void RoundtripExample14()
{
RoundtripTest("14-mapping-wo-indent.yaml");
}
[Fact]
public void RoundtripBackreference()
{
RoundtripTest("backreference.yaml");
}
[Fact]
public void FailBackreference()
{
RoundtripTest("fail-backreference.yaml");
}
[Fact]
public void Roundtrip32BitsUnicodeEscape()
{
RoundtripTest("unicode-32bits-escape.yaml");
}
[Fact]
public void AnchorsOverwriting()
{
RoundtripTest("anchors-overwriting.yaml");
}
[Fact]
public void AllAliasesMustBeResolved()
{
var original = new YamlStream();
Assert.Throws<AnchorNotFoundException>(() => original.Load(Yaml.ReaderFrom("invalid-reference.yaml")));
}
[Fact]
public void CanReadValueWithQuotes()
{
var yamlStream = new YamlStream();
yamlStream.Load(Yaml.ParserForText("description: -\"\" should be zero"));
var nodes = yamlStream.Documents.Single().AllNodes.ToArray();
Assert.Equal("description", nodes[1].ToString());
Assert.Equal("-\"\" should be zero", nodes[2].ToString());
}
private void RoundtripTest(string yamlFileName)
{
var original = new YamlStream();
original.Load(Yaml.ReaderFrom(yamlFileName));
var buffer = new StringBuilder();
original.Save(new StringWriter(buffer));
var final = new YamlStream();
final.Load(new StringReader(buffer.ToString()));
var originalBuilder = new YamlDocumentStructureBuilder();
original.Accept(originalBuilder);
var finalBuilder = new YamlDocumentStructureBuilder();
final.Accept(finalBuilder);
Assert.Equal(originalBuilder.Events.Count, finalBuilder.Events.Count);
for (var i = 0; i < originalBuilder.Events.Count; ++i)
{
var originalEvent = originalBuilder.Events[i];
var finalEvent = finalBuilder.Events[i];
Assert.Equal(originalEvent.Type, finalEvent.Type);
Assert.Equal(originalEvent.Value, finalEvent.Value);
}
}
private class YamlDocumentStructureBuilder : YamlVisitorBase
{
private readonly List<YamlNodeEvent> events = new List<YamlNodeEvent>();
public IList<YamlNodeEvent> Events
{
get
{
return events;
}
}
public override void Visit(YamlScalarNode scalar)
{
events.Add(new YamlNodeEvent(YamlNodeEventType.Scalar, scalar.Anchor, scalar.Tag, scalar.Value));
}
public override void Visit(YamlSequenceNode sequence)
{
events.Add(new YamlNodeEvent(YamlNodeEventType.SequenceStart, sequence.Anchor, sequence.Tag, null));
base.Visit(sequence);
events.Add(new YamlNodeEvent(YamlNodeEventType.SequenceEnd, sequence.Anchor, sequence.Tag, null));
}
public override void Visit(YamlMappingNode mapping)
{
events.Add(new YamlNodeEvent(YamlNodeEventType.MappingStart, mapping.Anchor, mapping.Tag, null));
base.Visit(mapping);
events.Add(new YamlNodeEvent(YamlNodeEventType.MappingEnd, mapping.Anchor, mapping.Tag, null));
}
}
private class YamlNodeEvent
{
public YamlNodeEventType Type { get; }
public AnchorName Anchor { get; }
public TagName Tag { get; }
public string Value { get; }
public YamlNodeEvent(YamlNodeEventType type, AnchorName anchor, TagName tag, string value)
{
Type = type;
Anchor = anchor;
Tag = tag;
Value = value;
}
}
private enum YamlNodeEventType
{
SequenceStart,
SequenceEnd,
MappingStart,
MappingEnd,
Scalar,
}
}
}