From 9e5019c96a9b2cf62af76938108648e02e63974b Mon Sep 17 00:00:00 2001 From: Antoine Aubry Date: Wed, 3 Feb 2016 16:41:20 +0000 Subject: [PATCH] Add AOT test for FullObjectGraphTraversalStrategy. --- YamlDotNet.AotTest/Program.cs | 20 +- YamlDotNet.AotTest/YamlDotNet.AotTest.csproj | 5 +- .../Serialization/GenericTestDictionary.cs | 119 ++++++++++++ .../Serialization/GenericTestList.cs | 111 +++++++++++ .../Serialization/SerializationTests.cs | 176 ------------------ YamlDotNet.Test/YamlDotNet.Test.csproj | 2 + 6 files changed, 253 insertions(+), 180 deletions(-) create mode 100644 YamlDotNet.Test/Serialization/GenericTestDictionary.cs create mode 100644 YamlDotNet.Test/Serialization/GenericTestList.cs diff --git a/YamlDotNet.AotTest/Program.cs b/YamlDotNet.AotTest/Program.cs index 6ca95e4f1..b6bf0bf2a 100644 --- a/YamlDotNet.AotTest/Program.cs +++ b/YamlDotNet.AotTest/Program.cs @@ -3,6 +3,7 @@ using System.IO; using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; +using YamlDotNet.Test.Serialization; namespace YamlDotNet.AotTest { @@ -21,6 +22,7 @@ static int Main(string[] args) TryDeserialize("DictionaryNodeDeserializer", "myDictionary: { winners: 3 }"); TryDeserialize("CollectionNodeDeserializer", "myList: [ 1, 2, 3 ]"); TryDeserialize("ArayNodeDeserializer", "myArray: [ 1, 2, 3 ]"); + TrySerialize("TraverseGenericDictionary", new GenericTestDictionary { { 1, 2 } }); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; @@ -37,17 +39,29 @@ static int Main(string[] args) private static int succeededTestCount; private static int failedTestCount; + private static void TrySerialize(string testName, T graph) + { + var output = new StringWriter(); + var serializer = new Serializer(namingConvention: new CamelCaseNamingConvention()); + PerformTest(testName, () => serializer.Serialize(output, graph)); + } + private static void TryDeserialize(string testName, string yaml) + { + var input = new StringReader(yaml); + var deserializer = new Deserializer(namingConvention: new CamelCaseNamingConvention()); + PerformTest(testName, () => deserializer.Deserialize(input)); + } + + private static void PerformTest(string testName, Action act) { Console.ForegroundColor = ConsoleColor.Gray; Console.Write(" "); Console.Write(testName.PadRight(70)); - var input = new StringReader(yaml); - var deserializer = new Deserializer(namingConvention: new CamelCaseNamingConvention()); try { - deserializer.Deserialize(input); + act(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("[success]"); Console.ForegroundColor = ConsoleColor.Gray; diff --git a/YamlDotNet.AotTest/YamlDotNet.AotTest.csproj b/YamlDotNet.AotTest/YamlDotNet.AotTest.csproj index 24940456f..0201edfc1 100644 --- a/YamlDotNet.AotTest/YamlDotNet.AotTest.csproj +++ b/YamlDotNet.AotTest/YamlDotNet.AotTest.csproj @@ -27,6 +27,9 @@ + + GenericTestDictionary.cs + @@ -47,4 +50,4 @@ --> - + \ No newline at end of file diff --git a/YamlDotNet.Test/Serialization/GenericTestDictionary.cs b/YamlDotNet.Test/Serialization/GenericTestDictionary.cs new file mode 100644 index 000000000..00be6ab0d --- /dev/null +++ b/YamlDotNet.Test/Serialization/GenericTestDictionary.cs @@ -0,0 +1,119 @@ +// 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.Collections; +using System.Collections.Generic; + +namespace YamlDotNet.Test.Serialization +{ + /// + /// Test Dictionary that implements IDictionary<,>, but not IDictionary + /// + public class GenericTestDictionary : IDictionary + { + private readonly Dictionary dictionary; + + public GenericTestDictionary() + { + dictionary = new Dictionary(); + } + public void Add(TKey key, TValue value) + { + dictionary.Add(key, value); + } + + public bool ContainsKey(TKey key) + { + return dictionary.ContainsKey(key); + } + + public ICollection Keys + { + get { return dictionary.Keys; } + } + + public bool Remove(TKey key) + { + return dictionary.Remove(key); + } + + public bool TryGetValue(TKey key, out TValue value) + { + return dictionary.TryGetValue(key, out value); + } + + public ICollection Values + { + get { return dictionary.Values; } + } + + public TValue this[TKey key] + { + get { return dictionary[key]; } + set { dictionary[key] = value; } + } + + public void Add(KeyValuePair item) + { + ((IDictionary)dictionary).Add(item); + } + + public void Clear() + { + dictionary.Clear(); + } + + public bool Contains(KeyValuePair item) + { + return ((IDictionary)dictionary).Contains(item); + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + ((IDictionary)dictionary).CopyTo(array, arrayIndex); + } + + public int Count + { + get { return dictionary.Count; } + } + + public bool IsReadOnly + { + get { return false; } + } + + public bool Remove(KeyValuePair item) + { + return ((IDictionary)dictionary).Remove(item); + } + + public IEnumerator> GetEnumerator() + { + return dictionary.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return dictionary.GetEnumerator(); + } + } +} \ No newline at end of file diff --git a/YamlDotNet.Test/Serialization/GenericTestList.cs b/YamlDotNet.Test/Serialization/GenericTestList.cs new file mode 100644 index 000000000..44f907bed --- /dev/null +++ b/YamlDotNet.Test/Serialization/GenericTestList.cs @@ -0,0 +1,111 @@ +// 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.Collections; +using System.Collections.Generic; + +namespace YamlDotNet.Test.Serialization +{ + /// + /// Test List that implements IList<>, but not IList + /// + public class GenericTestList : IList + { + private readonly List list; + + public GenericTestList() + { + list = new List(); + } + + public int IndexOf(T item) + { + return list.IndexOf(item); + } + + public void Insert(int index, T item) + { + list.Insert(index, item); + } + + public void RemoveAt(int index) + { + list.RemoveAt(index); + } + + public T this[int index] + { + get + { + return list[index]; + } + set + { + list[index] = value; + } + } + + public void Add(T item) + { + list.Add(item); + } + + public void Clear() + { + list.Clear(); + } + + public bool Contains(T item) + { + return list.Contains(item); + } + + public void CopyTo(T[] array, int arrayIndex) + { + list.CopyTo(array, arrayIndex); + } + + public int Count + { + get { return list.Count; } + } + + public bool IsReadOnly + { + get { return false; } + } + + public bool Remove(T item) + { + return list.Remove(item); + } + + public IEnumerator GetEnumerator() + { + return list.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return list.GetEnumerator(); + } + } +} \ No newline at end of file diff --git a/YamlDotNet.Test/Serialization/SerializationTests.cs b/YamlDotNet.Test/Serialization/SerializationTests.cs index 248b02b97..0171d68b7 100644 --- a/YamlDotNet.Test/Serialization/SerializationTests.cs +++ b/YamlDotNet.Test/Serialization/SerializationTests.cs @@ -1071,181 +1071,5 @@ public void GenericListThatDoesNotImplementIListCanBeDeserialized() Assert.Contains("a", deserialized); Assert.Contains("b", deserialized); } - - #region Test Dictionary that implements IDictionary<,>, but not IDictionary - public class GenericTestDictionary : IDictionary - { - private readonly Dictionary dictionary; - - public GenericTestDictionary() - { - dictionary = new Dictionary(); - } - public void Add(TKey key, TValue value) - { - dictionary.Add(key, value); - } - - public bool ContainsKey(TKey key) - { - return dictionary.ContainsKey(key); - } - - public ICollection Keys - { - get { return dictionary.Keys; } - } - - public bool Remove(TKey key) - { - return dictionary.Remove(key); - } - - public bool TryGetValue(TKey key, out TValue value) - { - return dictionary.TryGetValue(key, out value); - } - - public ICollection Values - { - get { return dictionary.Values; } - } - - public TValue this[TKey key] - { - get { return dictionary[key]; } - set { dictionary[key] = value; } - } - - public void Add(KeyValuePair item) - { - ((IDictionary)dictionary).Add(item); - } - - public void Clear() - { - dictionary.Clear(); - } - - public bool Contains(KeyValuePair item) - { - return ((IDictionary)dictionary).Contains(item); - } - - public void CopyTo(KeyValuePair[] array, int arrayIndex) - { - ((IDictionary)dictionary).CopyTo(array, arrayIndex); - } - - public int Count - { - get { return dictionary.Count; } - } - - public bool IsReadOnly - { - get { return false; } - } - - public bool Remove(KeyValuePair item) - { - return ((IDictionary)dictionary).Remove(item); - } - - public IEnumerator> GetEnumerator() - { - return dictionary.GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return dictionary.GetEnumerator(); - } - } - #endregion - - #region Test List that implements IList<>, but not IList - public class GenericTestList : IList - { - private readonly List list; - - public GenericTestList() - { - list = new List(); - } - - public int IndexOf(T item) - { - return list.IndexOf(item); - } - - public void Insert(int index, T item) - { - list.Insert(index, item); - } - - public void RemoveAt(int index) - { - list.RemoveAt(index); - } - - public T this[int index] - { - get - { - return list[index]; - } - set - { - list[index] = value; - } - } - - public void Add(T item) - { - list.Add(item); - } - - public void Clear() - { - list.Clear(); - } - - public bool Contains(T item) - { - return list.Contains(item); - } - - public void CopyTo(T[] array, int arrayIndex) - { - list.CopyTo(array, arrayIndex); - } - - public int Count - { - get { return list.Count; } - } - - public bool IsReadOnly - { - get { return false; } - } - - public bool Remove(T item) - { - return list.Remove(item); - } - - public IEnumerator GetEnumerator() - { - return list.GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return list.GetEnumerator(); - } - } - #endregion } } diff --git a/YamlDotNet.Test/YamlDotNet.Test.csproj b/YamlDotNet.Test/YamlDotNet.Test.csproj index d2fe18590..b0ea8f9e9 100644 --- a/YamlDotNet.Test/YamlDotNet.Test.csproj +++ b/YamlDotNet.Test/YamlDotNet.Test.csproj @@ -98,6 +98,8 @@ + +