-
-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from aaubry/feat-iyamlconvertible
Improve the usability of YamlDocument et al
- Loading branch information
Showing
17 changed files
with
481 additions
and
23 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
YamlDotNet.Test/Serialization/RepresentationModelSerializationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using YamlDotNet.Core; | ||
using YamlDotNet.RepresentationModel; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace YamlDotNet.Test.Serialization | ||
{ | ||
public class RepresentationModelSerializationTests | ||
{ | ||
[Theory] | ||
[InlineData("hello", "hello")] | ||
[InlineData("'hello'", "hello")] | ||
[InlineData("\"hello\"", "hello")] | ||
[InlineData("!!int 123", "123")] | ||
public void ScalarIsSerializable(string yaml, string expectedValue) | ||
{ | ||
var deserializer = new Deserializer(); | ||
var node = deserializer.Deserialize<YamlScalarNode>(Yaml.ReaderForText(yaml)); | ||
|
||
Assert.NotNull(node); | ||
Assert.Equal(expectedValue, node.Value); | ||
|
||
var serializer = new Serializer(); | ||
var buffer = new StringWriter(); | ||
serializer.Serialize(buffer, node); | ||
Assert.Equal(yaml, buffer.ToString().TrimEnd('\r', '\n', '.')); | ||
} | ||
|
||
[Theory] | ||
[InlineData("[a]", new[] { "a" })] | ||
[InlineData("['a']", new[] { "a" })] | ||
[InlineData("- a\r\n- b", new[] { "a", "b" })] | ||
public void SequenceIsSerializable(string yaml, string[] expectedValues) | ||
{ | ||
var deserializer = new Deserializer(); | ||
var node = deserializer.Deserialize<YamlSequenceNode>(Yaml.ReaderForText(yaml)); | ||
|
||
Assert.NotNull(node); | ||
Assert.Equal(expectedValues.Length, node.Children.Count); | ||
for (int i = 0; i < expectedValues.Length; i++) | ||
{ | ||
Assert.Equal(expectedValues[i], ((YamlScalarNode)node.Children[i]).Value); | ||
} | ||
|
||
var serializer = new Serializer(); | ||
var buffer = new StringWriter(); | ||
serializer.Serialize(buffer, node); | ||
Assert.Equal(yaml, buffer.ToString().TrimEnd('\r', '\n', '.')); | ||
} | ||
|
||
[Theory] | ||
[InlineData("{a: b}", new[] { "a", "b" })] | ||
[InlineData("{'a': \"b\"}", new[] { "a", "b" })] | ||
[InlineData("a: b\r\nc: d", new[] { "a", "b", "c", "d" })] | ||
public void MappingIsSerializable(string yaml, string[] expectedKeysAndValues) | ||
{ | ||
var deserializer = new Deserializer(); | ||
var node = deserializer.Deserialize<YamlMappingNode>(Yaml.ReaderForText(yaml)); | ||
|
||
Assert.NotNull(node); | ||
Assert.Equal(expectedKeysAndValues.Length / 2, node.Children.Count); | ||
for (int i = 0; i < expectedKeysAndValues.Length; i += 2) | ||
{ | ||
Assert.Equal(expectedKeysAndValues[i + 1], ((YamlScalarNode)node.Children[expectedKeysAndValues[i]]).Value); | ||
} | ||
|
||
var serializer = new Serializer(); | ||
var buffer = new StringWriter(); | ||
serializer.Serialize(buffer, node); | ||
Assert.Equal(yaml, buffer.ToString().TrimEnd('\r', '\n', '.')); | ||
} | ||
} | ||
|
||
public class ByteArrayConverter : IYamlTypeConverter | ||
{ | ||
public bool Accepts(Type type) | ||
{ | ||
return type == typeof(byte[]); | ||
} | ||
|
||
public object ReadYaml(IParser parser, Type type) | ||
{ | ||
var scalar = (YamlDotNet.Core.Events.Scalar)parser.Current; | ||
var bytes = Convert.FromBase64String(scalar.Value); | ||
parser.MoveNext(); | ||
return bytes; | ||
} | ||
|
||
public void WriteYaml(IEmitter emitter, object value, Type type) | ||
{ | ||
var bytes = (byte[])value; | ||
emitter.Emit(new YamlDotNet.Core.Events.Scalar( | ||
null, | ||
"tag:yaml.org,2002:binary", | ||
Convert.ToBase64String(bytes), | ||
ScalarStyle.Plain, | ||
false, | ||
false | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.