#region License // Copyright (c) 2007 James Newton-King // // 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. #endregion #if !(NET20 || NET35 || NET40 || PORTABLE40) using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Text; using System.Threading.Tasks; #if DNXCORE50 using Xunit; using Test = Xunit.FactAttribute; using Assert = Newtonsoft.Json.Tests.XUnitAssert; #else using NUnit.Framework; #endif using Newtonsoft.Json.Linq; namespace Newtonsoft.Json.Tests.Serialization { [TestFixture] public class ImmutableCollectionsTests : TestFixtureBase { #region List [Test] public void SerializeList() { ImmutableList l = ImmutableList.CreateRange(new List { "One", "II", "3" }); string json = JsonConvert.SerializeObject(l, Formatting.Indented); StringAssert.AreEqual(@"[ ""One"", ""II"", ""3"" ]", json); } [Test] public void DeserializeList() { string json = @"[ ""One"", ""II"", ""3"" ]"; ImmutableList l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Count); Assert.AreEqual("One", l[0]); Assert.AreEqual("II", l[1]); Assert.AreEqual("3", l[2]); } [Test] public void DeserializeListInterface() { string json = @"[ 'Volibear', 'Teemo', 'Katarina' ]"; // what sorcery is this?! IImmutableList champions = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, champions.Count); Assert.AreEqual("Volibear", champions[0]); Assert.AreEqual("Teemo", champions[1]); Assert.AreEqual("Katarina", champions[2]); } #endregion #region Array [Test] public void SerializeArray() { ImmutableArray l = ImmutableArray.CreateRange(new List { "One", "II", "3" }); string json = JsonConvert.SerializeObject(l, Formatting.Indented); StringAssert.AreEqual(@"[ ""One"", ""II"", ""3"" ]", json); } [Test] public void DeserializeArray() { string json = @"[ ""One"", ""II"", ""3"" ]"; ImmutableArray l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Length); Assert.AreEqual("One", l[0]); Assert.AreEqual("II", l[1]); Assert.AreEqual("3", l[2]); } [Test] public void SerializeDefaultArray() { ExceptionAssert.Throws( () => JsonConvert.SerializeObject(default(ImmutableArray), Formatting.Indented), "This operation cannot be performed on a default instance of ImmutableArray. Consider initializing the array, or checking the ImmutableArray.IsDefault property."); } #endregion #region Queue [Test] public void SerializeQueue() { ImmutableQueue l = ImmutableQueue.CreateRange(new List { "One", "II", "3" }); string json = JsonConvert.SerializeObject(l, Formatting.Indented); StringAssert.AreEqual(@"[ ""One"", ""II"", ""3"" ]", json); } [Test] public void DeserializeQueue() { string json = @"[ ""One"", ""II"", ""3"" ]"; ImmutableQueue l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Count()); Assert.AreEqual("One", l.ElementAt(0)); Assert.AreEqual("II", l.ElementAt(1)); Assert.AreEqual("3", l.ElementAt(2)); } [Test] public void DeserializeQueueInterface() { string json = @"[ ""One"", ""II"", ""3"" ]"; IImmutableQueue l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Count()); Assert.AreEqual("One", l.ElementAt(0)); Assert.AreEqual("II", l.ElementAt(1)); Assert.AreEqual("3", l.ElementAt(2)); } #endregion #region Stack [Test] public void SerializeStack() { ImmutableStack l = ImmutableStack.CreateRange(new List { "One", "II", "3" }); string json = JsonConvert.SerializeObject(l, Formatting.Indented); StringAssert.AreEqual(@"[ ""3"", ""II"", ""One"" ]", json); } [Test] public void DeserializeStack() { string json = @"[ ""One"", ""II"", ""3"" ]"; ImmutableStack l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Count()); Assert.AreEqual("3", l.ElementAt(0)); Assert.AreEqual("II", l.ElementAt(1)); Assert.AreEqual("One", l.ElementAt(2)); } [Test] public void DeserializeStackInterface() { string json = @"[ ""One"", ""II"", ""3"" ]"; IImmutableStack l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Count()); Assert.AreEqual("3", l.ElementAt(0)); Assert.AreEqual("II", l.ElementAt(1)); Assert.AreEqual("One", l.ElementAt(2)); } #endregion #region HashSet [Test] public void SerializeHashSet() { ImmutableHashSet l = ImmutableHashSet.CreateRange(new List { "One", "II", "3" }); string json = JsonConvert.SerializeObject(l, Formatting.Indented); JArray a = JArray.Parse(json); Assert.AreEqual(3, a.Count); Assert.IsTrue(a.Any(t => t.DeepEquals("One"))); Assert.IsTrue(a.Any(t => t.DeepEquals("II"))); Assert.IsTrue(a.Any(t => t.DeepEquals("3"))); } [Test] public void DeserializeHashSet() { string json = @"[ ""One"", ""II"", ""3"" ]"; ImmutableHashSet l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Count()); Assert.IsTrue(l.Contains("3")); Assert.IsTrue(l.Contains("II")); Assert.IsTrue(l.Contains("One")); } [Test] public void DeserializeHashSetInterface() { string json = @"[ ""One"", ""II"", ""3"" ]"; IImmutableSet l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Count()); Assert.IsTrue(l.Contains("3")); Assert.IsTrue(l.Contains("II")); Assert.IsTrue(l.Contains("One")); Assert.IsTrue(l is ImmutableHashSet); } #endregion #region SortedSet [Test] public void SerializeSortedSet() { ImmutableSortedSet l = ImmutableSortedSet.CreateRange(new List { "One", "II", "3" }); string json = JsonConvert.SerializeObject(l, Formatting.Indented); StringAssert.AreEqual(@"[ ""3"", ""II"", ""One"" ]", json); } [Test] public void DeserializeSortedSet() { string json = @"[ ""One"", ""II"", ""3"" ]"; ImmutableSortedSet l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Count()); Assert.IsTrue(l.Contains("3")); Assert.IsTrue(l.Contains("II")); Assert.IsTrue(l.Contains("One")); } #endregion #region Dictionary [Test] public void SerializeDictionary() { ImmutableDictionary l = ImmutableDictionary.CreateRange(new Dictionary { { 1, "One" }, { 2, "II" }, { 3, "3" } }); string json = JsonConvert.SerializeObject(l, Formatting.Indented); JObject a = JObject.Parse(json); Assert.AreEqual(3, a.Count); Assert.AreEqual("One", (string)a["1"]); Assert.AreEqual("II", (string)a["2"]); Assert.AreEqual("3", (string)a["3"]); } [Test] public void DeserializeDictionary() { string json = @"{ ""1"": ""One"", ""2"": ""II"", ""3"": ""3"" }"; ImmutableDictionary l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Count); Assert.AreEqual("One", l[1]); Assert.AreEqual("II", l[2]); Assert.AreEqual("3", l[3]); } [Test] public void DeserializeDictionaryInterface() { string json = @"{ ""1"": ""One"", ""2"": ""II"", ""3"": ""3"" }"; IImmutableDictionary l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Count); Assert.AreEqual("One", l[1]); Assert.AreEqual("II", l[2]); Assert.AreEqual("3", l[3]); Assert.IsTrue(l is ImmutableDictionary); } #endregion #region SortedDictionary [Test] public void SerializeSortedDictionary() { ImmutableSortedDictionary l = ImmutableSortedDictionary.CreateRange(new SortedDictionary { { 1, "One" }, { 2, "II" }, { 3, "3" } }); string json = JsonConvert.SerializeObject(l, Formatting.Indented); StringAssert.AreEqual(@"{ ""1"": ""One"", ""2"": ""II"", ""3"": ""3"" }", json); } [Test] public void DeserializeSortedDictionary() { string json = @"{ ""1"": ""One"", ""2"": ""II"", ""3"": ""3"" }"; ImmutableSortedDictionary l = JsonConvert.DeserializeObject>(json); Assert.AreEqual(3, l.Count); Assert.AreEqual("One", l[1]); Assert.AreEqual("II", l[2]); Assert.AreEqual("3", l[3]); } #endregion } } #endif