diff --git a/appveyor.yml b/appveyor.yml index 5121096..1a639e8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,7 @@ # http://www.appveyor.com/docs/appveyor-yml environment: - base_version: 1.2.2 + base_version: 1.2.3 version: $(base_version).{build} diff --git a/build/Build.cs b/build/Build.cs index 141a972..95cf87d 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -39,7 +39,7 @@ class Build : NukeBuild AbsolutePath TestsDirectory => RootDirectory / "Tests"; [Parameter("Version to be injected in the Build")] - public string Version { get; set; } = $"1.2.2"; + public string Version { get; set; } = $"1.2.3"; [Parameter("The Buildnumber provided by the CI")] public string BuildNo = "2"; diff --git a/src/Tests/YamlMap.Tests/Reader/ParseQuotatedTypeStringTests.cs b/src/Tests/YamlMap.Tests/Reader/ParseQuotatedTypeStringTests.cs new file mode 100644 index 0000000..c90d906 --- /dev/null +++ b/src/Tests/YamlMap.Tests/Reader/ParseQuotatedTypeStringTests.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using System.Text; +using NUnit.Framework; + +namespace YamlMap.Tests.Reader +{ + [TestFixture] + public class ParseQuotatedTypeStringTests + { + [Test] + public void ParseQuotatedTypeString_DoubleQuotations() + { + var lines = new[] + { + "Value: \"[value] test\"" + }; + + var reader = new YamlReader(); + var parsed = reader.Read(typeof(PrimitiveValues), lines) as PrimitiveValues; + + Assert.AreEqual("[value] test", parsed.Value); + } + + [Test] + public void ParseQuotatedTypeString_SingleQuotations() + { + var lines = new[] + { + "Value: '[value] test'" + }; + + var reader = new YamlReader(); + var parsed = reader.Read(typeof(PrimitiveValues), lines) as PrimitiveValues; + + Assert.AreEqual("[value] test", parsed.Value); + } + + [Test] + public void ParseQuotatedTypeString_DoubleQuotations_InnerSingle() + { + var lines = new[] + { + "Value: \"[value] 'test\"" + }; + + var reader = new YamlReader(); + var parsed = reader.Read(typeof(PrimitiveValues), lines) as PrimitiveValues; + + Assert.AreEqual("[value] 'test", parsed.Value); + } + + [Test] + public void ParseQuotatedTypeString_SingleQuotations_InnerDouble() + { + var lines = new[] + { + "Value: '[value] \"test'" + }; + + var reader = new YamlReader(); + var parsed = reader.Read(typeof(PrimitiveValues), lines) as PrimitiveValues; + + Assert.AreEqual("[value] \"test", parsed.Value); + } + + [Test] + public void ParseQuotatedTypeString_DoubleQuotations_NoEnd() + { + var lines = new[] + { + "Value: \"[value] test" + }; + + var reader = new YamlReader(); + Assert.Throws(() => reader.Read(typeof(PrimitiveValues), lines)); + } + + [Test] + public void ParseQuotatedTypeString_SingleQuotations_NoEnd() + { + var lines = new[] + { + "Value: '[value] test" + }; + + var reader = new YamlReader(); + Assert.Throws(() => reader.Read(typeof(PrimitiveValues), lines)); + } + + [Test] + public void ParseQuotatedTypeString_DoubleQuotations_EndInString() + { + var lines = new[] + { + "Value: \"[value] t\"est" + }; + + var reader = new YamlReader(); + Assert.Throws(() => reader.Read(typeof(PrimitiveValues), lines)); + } + + [Test] + public void ParseQuotatedTypeString_SingleQuotations_EndInString() + { + var lines = new[] + { + "Value: '[value] t'est" + }; + + var reader = new YamlReader(); + Assert.Throws(() => reader.Read(typeof(PrimitiveValues), lines)); + } + + + [Test] + public void ParseQuotatedTypeString_DoubleQuotations_Special() + { + var lines = new[] + { + "Value: \"c: is a drive\"" + }; + + var reader = new YamlReader(); + var parsed = reader.Read(typeof(PrimitiveValues), lines) as PrimitiveValues; + + Assert.AreEqual("c: is a drive", parsed.Value); + } + + public class PrimitiveValues + { + public string Value { get; set; } + + public List ValueList { get; set; } + } + } +} diff --git a/src/Tests/YamlMap.Tests/Reader/YamlReaderDictionaryTypeTests.cs b/src/Tests/YamlMap.Tests/Reader/YamlReaderDictionaryTypeTests.cs new file mode 100644 index 0000000..400baf8 --- /dev/null +++ b/src/Tests/YamlMap.Tests/Reader/YamlReaderDictionaryTypeTests.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; + +namespace YamlMap.Tests.Reader +{ + [TestFixture] + public class YamlReaderDictionaryTypeTests + { + [Test] + public void YamlReaderDictionaryType_Dictionary() + { + var lines = new[] + { + "Dictionary:", + " Name: one", + " Id: 1" + }; + + var reader = new YamlReader(); + var data = reader.Read(typeof(StringNode), lines) as StringNode; + + Assert.That(data.Dictionary.Count() == 2); + Assert.That(data.Dictionary["Name"] == "one"); + Assert.That(data.Dictionary["Id"] == "1"); + } + + [Test] + public void YamlReaderDictionaryType_Dictionary_Interface() + { + var lines = new[] + { + "IDictionary:", + " Name: one", + " Id: 1" + }; + + var reader = new YamlReader(); + var data = reader.Read(typeof(StringNode), lines) as StringNode; + + Assert.That(data.IDictionary.Count() == 2); + Assert.That(data.IDictionary["Name"] == "one"); + Assert.That(data.IDictionary["Id"] == "1"); + } + + [Test] + public void YamlReaderDictionaryType_Dictionary_List() + { + // this is a list of object instead of a dictionary + // to use dictionary remove the - + var lines = new[] + { + "Dictionary:", + " - Name: one", + " - Id: 1" + }; + + var reader = new YamlReader(); + Assert.Throws(() => reader.Read(typeof(StringNode), lines)); + } + + [Test] + public void YamlReaderDictionaryType_Dictionary_Objects() + { + var lines = new[] + { + "ObjectDictionary:", + " one:", + " Id: 1", + " Name: test one", + " two:", + " Id: 2", + " Name: test two" + }; + + var reader = new YamlReader(); + var data = reader.Read(typeof(StringNode), lines) as StringNode; + + Assert.That(data.ObjectDictionary.Count() == 2); + Assert.That(data.ObjectDictionary["one"].Id == 1); + Assert.That(data.ObjectDictionary["one"].Name == "test one"); + Assert.That(data.ObjectDictionary["two"].Id == 2); + Assert.That(data.ObjectDictionary["two"].Name == "test two"); + } + + public class StringNode + { + public Dictionary Dictionary { get; set; } + + public IDictionary IDictionary { get; set; } + + public Dictionary ObjectDictionary { get; set; } + } + + public class DictObject + { + public int Id { get; set; } + + public string Name { get; set; } + } + } +} diff --git a/src/Tests/YamlMap.Tests/Serialization/DeserializerTests.cs b/src/Tests/YamlMap.Tests/Serialization/DeserializerTests.cs index 6e7a9a4..62ea8ad 100644 --- a/src/Tests/YamlMap.Tests/Serialization/DeserializerTests.cs +++ b/src/Tests/YamlMap.Tests/Serialization/DeserializerTests.cs @@ -25,6 +25,23 @@ public void Serializer_Deserialize() item.MatchSnapshot(); } + [Test] + public void Serializer_Deserialize_ByType() + { + + var value = new StringBuilder().AppendLine("Simple: root") + .AppendLine("StringList:") + .AppendLine(" - one") + .AppendLine(" - 2") + .AppendLine("ObjList:") + .AppendLine(" - Simple: simple") + .AppendLine(" - Child:") + .AppendLine(" Simple: child").ToString(); + + var item = Serializer.Deserialize(typeof(TestlItem), value); + item.MatchSnapshot(); + } + [Test] public void Deserialize_Type() { diff --git a/src/Tests/YamlMap.Tests/Serialization/_Snapshots/DeserializerTests_Serializer_Deserialize_ByType.snapshot b/src/Tests/YamlMap.Tests/Serialization/_Snapshots/DeserializerTests_Serializer_Deserialize_ByType.snapshot new file mode 100644 index 0000000..85a1117 --- /dev/null +++ b/src/Tests/YamlMap.Tests/Serialization/_Snapshots/DeserializerTests_Serializer_Deserialize_ByType.snapshot @@ -0,0 +1,19 @@ +---data +Child: +ObjList: + Child: + ObjList: + Simple: simple + StringList: + Child: + Child: + ObjList: + Simple: child + StringList: + ObjList: + Simple: null + StringList: +Simple: root +StringList: + one + 2 diff --git a/src/YamlMap/Serializer.cs b/src/YamlMap/Serializer.cs index 0c13c1d..626371d 100644 --- a/src/YamlMap/Serializer.cs +++ b/src/YamlMap/Serializer.cs @@ -32,5 +32,16 @@ public static string Serialize(T item) return reader.Read(yaml); } - } + /// + /// Deserialize a string to a object + /// + /// + /// + /// + public static object Deserialize(Type type, string yaml) + { + var reader = new YamlReader(); + return reader.Read(type, yaml); + } + } } diff --git a/src/YamlMap/YamlReader.cs b/src/YamlMap/YamlReader.cs index a71d3ba..5968cff 100644 --- a/src/YamlMap/YamlReader.cs +++ b/src/YamlMap/YamlReader.cs @@ -24,6 +24,19 @@ public class YamlReader return Read(lines); } + /// + /// Read a yaml string amd map to a object of the given type + /// + /// + /// + /// + public object Read(Type type, string yaml) + { + var lines = yaml.Split(new[] { Environment.NewLine }, StringSplitOptions.None); + + return Read(type, lines); + } + /// /// Read a array of yaml string and map it to a object /// @@ -32,7 +45,18 @@ public class YamlReader /// public T Read(string[] lines) where T : class, new() { - var deserializer = new TokenDeserializer(typeof(T), null); + return (T)Read(typeof(T), lines); + } + + /// + /// Read a array of yaml string amd map to a object of the given type + /// + /// + /// + /// + public object Read(Type type, string[] lines) + { + var deserializer = new TokenDeserializer(type, null); var scanner = new Scanner(lines); var parser = new Parser(scanner); @@ -40,10 +64,10 @@ public class YamlReader for (var i = 0; i < tokens.Count; i++) { - deserializer.Deserialize(tokens[i]); + deserializer.Deserialize(tokens[i]); } - return (T)deserializer.Node; + return deserializer.Node; } private string FindFile(string file)