// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Management.Automation; using Xunit; namespace PSTests.Parallel { public static class SerializationTests { [Fact] public static void TestSerializerEnumerate() { var source = new List { 1, 2, 3 }; var expected = $"{Environment.NewLine} 1{Environment.NewLine} 2{Environment.NewLine} 3{Environment.NewLine}"; var serialized = PSSerializer.Serialize(source, depth: 2, enumerate: true); Assert.Equal(expected, serialized); var deserialized = PSSerializer.Deserialize(serialized); Assert.IsType(deserialized); var array = ((IEnumerable)deserialized).Cast().ToArray(); Assert.Equal(3, array.Length); Assert.Equal(1, array[0]); Assert.Equal(2, array[1]); Assert.Equal(3, array[2]); } [Fact] public static void TestSerializerWithoutEnumerate() { var listAssemblyDisplayName = System.Reflection.Assembly.GetAssembly(typeof(List)).FullName; var source = new List { 1, 2, 3 }; var expected = $"{Environment.NewLine} {Environment.NewLine} {Environment.NewLine} System.Collections.Generic.List`1[[System.Object, {listAssemblyDisplayName}]]{Environment.NewLine} System.Object{Environment.NewLine} {Environment.NewLine} {Environment.NewLine} 1{Environment.NewLine} 2{Environment.NewLine} 3{Environment.NewLine} {Environment.NewLine} {Environment.NewLine}"; var serialized = PSSerializer.Serialize(source, depth: 2, enumerate: false); Assert.Equal(expected, serialized); var deserialized = PSSerializer.Deserialize(serialized); Assert.IsType(deserialized); var baseObject = PSObject.AsPSObject(deserialized).BaseObject; Assert.IsType(baseObject); var arrayList = (ArrayList)baseObject; Assert.Equal(3, arrayList.Count); Assert.Equal(1, arrayList[0]); Assert.Equal(2, arrayList[1]); Assert.Equal(3, arrayList[2]); } } }