MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections;6using System.Collections.Generic;7using System.Linq;8using System.Management.Automation;9using Xunit;10 11namespace PSTests.Parallel12{13 public static class SerializationTests14 {15 [Fact]16 public static void TestSerializerEnumerate()17 {18 var source = new List<object> { 1, 2, 3 };19 var expected = $"<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\">{Environment.NewLine} <I32>1</I32>{Environment.NewLine} <I32>2</I32>{Environment.NewLine} <I32>3</I32>{Environment.NewLine}</Objs>";20 var serialized = PSSerializer.Serialize(source, depth: 2, enumerate: true);21 Assert.Equal(expected, serialized);22 var deserialized = PSSerializer.Deserialize(serialized);23 Assert.IsType<object[]>(deserialized);24 var array = ((IEnumerable)deserialized).Cast<object>().ToArray();25 Assert.Equal(3, array.Length);26 Assert.Equal(1, array[0]);27 Assert.Equal(2, array[1]);28 Assert.Equal(3, array[2]);29 }30 31 [Fact]32 public static void TestSerializerWithoutEnumerate()33 {34 var listAssemblyDisplayName = System.Reflection.Assembly.GetAssembly(typeof(List<object>)).FullName;35 var source = new List<object> { 1, 2, 3 };36 var expected = $"<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\">{Environment.NewLine} <Obj RefId=\"0\">{Environment.NewLine} <TN RefId=\"0\">{Environment.NewLine} <T>System.Collections.Generic.List`1[[System.Object, {listAssemblyDisplayName}]]</T>{Environment.NewLine} <T>System.Object</T>{Environment.NewLine} </TN>{Environment.NewLine} <LST>{Environment.NewLine} <I32>1</I32>{Environment.NewLine} <I32>2</I32>{Environment.NewLine} <I32>3</I32>{Environment.NewLine} </LST>{Environment.NewLine} </Obj>{Environment.NewLine}</Objs>";37 var serialized = PSSerializer.Serialize(source, depth: 2, enumerate: false);38 Assert.Equal(expected, serialized);39 var deserialized = PSSerializer.Deserialize(serialized);40 Assert.IsType<PSObject>(deserialized);41 var baseObject = PSObject.AsPSObject(deserialized).BaseObject;42 Assert.IsType<ArrayList>(baseObject);43 var arrayList = (ArrayList)baseObject;44 Assert.Equal(3, arrayList.Count);45 Assert.Equal(1, arrayList[0]);46 Assert.Equal(2, arrayList[1]);47 Assert.Equal(3, arrayList[2]);48 }49 }50}51 