2020-03-24 19:08:37 +01:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
2019-03-21 02:43:52 +01:00
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Management.Automation.Language;
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace PSTests.Parallel
|
|
|
|
{
|
|
|
|
using System.Linq;
|
|
|
|
using System.Management.Automation;
|
|
|
|
using System.Management.Automation.Runspaces;
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
using Microsoft.Management.Infrastructure;
|
|
|
|
|
|
|
|
public static class PSObjectTests
|
|
|
|
{
|
|
|
|
[Fact]
|
|
|
|
public static void TestEmptyObjectHasNoProperty()
|
|
|
|
{
|
2019-03-23 05:52:30 +01:00
|
|
|
var pso = new PSObject();
|
|
|
|
var actual = pso.GetFirstPropertyOrDefault(name => true);
|
2019-03-21 02:43:52 +01:00
|
|
|
Assert.Null(actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void TestWrappedDateTimeHasReflectedMember()
|
|
|
|
{
|
2019-03-23 05:52:30 +01:00
|
|
|
var pso = new PSObject(DateTime.Now);
|
|
|
|
var member = pso.GetFirstPropertyOrDefault(name => name == "DayOfWeek");
|
2019-03-21 02:43:52 +01:00
|
|
|
Assert.NotNull(member);
|
|
|
|
Assert.Equal("DayOfWeek", member.Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void TestAdaptedMember()
|
|
|
|
{
|
2019-03-23 05:52:30 +01:00
|
|
|
var pso = new PSObject(DateTime.Now);
|
|
|
|
pso.Members.Add(new PSNoteProperty("NewMember", "AValue"));
|
|
|
|
var member = pso.GetFirstPropertyOrDefault(name => name == "NewMember");
|
2019-03-21 02:43:52 +01:00
|
|
|
Assert.NotNull(member);
|
|
|
|
Assert.Equal("NewMember", member.Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void TestShadowedMember()
|
|
|
|
{
|
2019-03-23 05:52:30 +01:00
|
|
|
var pso = new PSObject(DateTime.Now);
|
|
|
|
pso.Members.Add(new PSNoteProperty("DayOfWeek", "AValue"));
|
|
|
|
var member = pso.GetFirstPropertyOrDefault(name => name == "DayOfWeek");
|
2019-03-21 02:43:52 +01:00
|
|
|
Assert.NotNull(member);
|
|
|
|
Assert.Equal("DayOfWeek", member.Name);
|
|
|
|
Assert.Equal("AValue", member.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void TestMemberSetIsNotProperty()
|
|
|
|
{
|
2019-03-23 05:52:30 +01:00
|
|
|
var pso = new PSObject(DateTime.Now);
|
2019-03-21 02:43:52 +01:00
|
|
|
var psNoteProperty = new PSNoteProperty("NewMember", "AValue");
|
2019-03-23 05:52:30 +01:00
|
|
|
pso.Members.Add(psNoteProperty);
|
|
|
|
pso.Members.Add(new PSMemberSet("NewMemberSet", new[] { psNoteProperty }));
|
2019-03-21 02:43:52 +01:00
|
|
|
|
2019-03-23 05:52:30 +01:00
|
|
|
var member = pso.GetFirstPropertyOrDefault(name => name == "NewMemberSet");
|
2019-03-21 02:43:52 +01:00
|
|
|
Assert.Null(member);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void TestMemberSet()
|
|
|
|
{
|
2019-03-23 05:52:30 +01:00
|
|
|
var pso = new PSObject(DateTime.Now);
|
2019-03-21 02:43:52 +01:00
|
|
|
var psNoteProperty = new PSNoteProperty("NewMember", "AValue");
|
2019-03-23 05:52:30 +01:00
|
|
|
pso.Members.Add(psNoteProperty);
|
|
|
|
pso.Members.Add(new PSMemberSet("NewMemberSet", new[] { psNoteProperty }));
|
2019-03-21 02:43:52 +01:00
|
|
|
|
2019-03-23 05:52:30 +01:00
|
|
|
var member = pso.Members.FirstOrDefault(name => name == "NewMemberSet");
|
2019-03-21 02:43:52 +01:00
|
|
|
Assert.NotNull(member);
|
|
|
|
Assert.Equal("NewMemberSet", member.Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void TextXmlElementMember()
|
|
|
|
{
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
var root = doc.CreateElement("root");
|
|
|
|
doc.AppendChild(root);
|
|
|
|
var firstChild = doc.CreateElement("elem1");
|
|
|
|
root.AppendChild(firstChild);
|
|
|
|
root.InsertAfter(doc.CreateElement("elem2"), firstChild);
|
|
|
|
|
2019-03-23 05:52:30 +01:00
|
|
|
var pso = new PSObject(root);
|
|
|
|
var member = pso.GetFirstPropertyOrDefault(name => name.StartsWith("elem"));
|
2019-03-21 02:43:52 +01:00
|
|
|
Assert.Equal("elem1", member.Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void TextXmlAttributeMember()
|
|
|
|
{
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
var root = doc.CreateElement("root");
|
|
|
|
doc.AppendChild(root);
|
|
|
|
root.SetAttribute("attr", "value");
|
|
|
|
root.AppendChild(doc.CreateElement("elem"));
|
|
|
|
|
2019-03-23 05:52:30 +01:00
|
|
|
var pso = new PSObject(root);
|
|
|
|
var member = pso.GetFirstPropertyOrDefault(name => name.StartsWith("attr"));
|
2019-03-21 02:43:52 +01:00
|
|
|
Assert.Equal("attr", member.Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
[SkippableFact]
|
|
|
|
public static void TestCimInstanceProperty()
|
|
|
|
{
|
|
|
|
Skip.IfNot(Platform.IsWindows);
|
|
|
|
var iss = InitialSessionState.CreateDefault2();
|
|
|
|
iss.Commands.Add(new SessionStateCmdletEntry("Get-CimInstance", typeof(Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand), null));
|
|
|
|
using (var ps = PowerShell.Create(iss))
|
|
|
|
{
|
|
|
|
ps.AddCommand("Get-CimInstance").AddParameter("ClassName", "Win32_BIOS");
|
|
|
|
var res = ps.Invoke().FirstOrDefault();
|
|
|
|
Assert.NotNull(res);
|
|
|
|
var member = res.GetFirstPropertyOrDefault(name => name == "Name");
|
|
|
|
Assert.NotNull(member);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|