Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
New-Object.Tests.ps1169 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "New-Object" -Tags "CI" {4    It "Support 'ComObject' parameter on platforms" {5        if ($IsLinux -or $IsMacOS ) {6            { New-Object -ComObject "Shell.Application" } | Should -Throw -ErrorId "NamedParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand"7        } else {8            # It works on NanoServer and IoT too9            (Get-Command "New-Object").Parameters.ContainsKey("ComObject") | Should -BeTrue10        }11    }12 13    It "should create an object with 4 fields" {14        $o = New-Object psobject15        $val = $o.GetType()16 17        $val.IsPublic       | Should -Not -BeNullOrEmpty18        $val.Name           | Should -Not -BeNullOrEmpty19        $val.IsSerializable | Should -Not -BeNullOrEmpty20        $val.BaseType       | Should -Not -BeNullOrEmpty21 22        $val.IsPublic       | Should -BeTrue23        $val.IsSerializable | Should -BeFalse24        $val.Name           | Should -Be 'PSCustomObject'25        $val.BaseType       | Should -Be 'System.Object'26    }27 28    It "should create an object with using Property switch" {29        $hash = @{30            FirstVal = 'test1'31            SecondVal = 'test2'32        }33        $o = New-Object psobject -Property $hash34 35        $o.FirstVal     | Should -Be 'test1'36        $o.SecondVal    | Should -Be 'test2'37    }38 39    It "should create a .Net object with using ArgumentList switch" {40        $o = New-Object -TypeName System.Version -ArgumentList "1.2.3.4"41        $o.GetType() | Should -Be ([System.Version])42 43        $o      | Should -BeExactly "1.2.3.4"44    }45}46 47Describe "New-Object DRT basic functionality" -Tags "CI" {48    It "New-Object with int array should work"{49        $result = New-Object -TypeName int[] -Arg 1050        $result.Count | Should -Be 1051    }52 53    It "New-Object with char should work"{54        $result = New-Object -TypeName char55        $result.Count | Should -Be 156        $defaultChar = [char]057        ([char]$result) | Should -Be $defaultChar58    }59 60    It "New-Object with default Coordinates should work"{61        $result = New-Object -TypeName System.Management.Automation.Host.Coordinates62        $result.Count | Should -Be 163        $result.X | Should -Be 064        $result.Y | Should -Be 065    }66 67    It "New-Object with specified Coordinates should work"{68        $result = New-Object -TypeName System.Management.Automation.Host.Coordinates -ArgumentList 1,269        $result.Count | Should -Be 170        $result.X | Should -Be 171        $result.Y | Should -Be 272    }73 74    It "New-Object with Employ should work"{75        if(-not ([System.Management.Automation.PSTypeName]'Employee').Type)76        {77            Add-Type -TypeDefinition "public class Employee{public Employee(string firstName,string lastName,int yearsInMS){FirstName = firstName;LastName=lastName;YearsInMS = yearsInMS;}public string FirstName;public string LastName;public int YearsInMS;}"78        }79        $result = New-Object -TypeName Employee -ArgumentList "Mary", "Soe", 1180        $result.Count | Should -Be 181        $result.FirstName | Should -BeExactly "Mary"82        $result.LastName | Should -BeExactly "Soe"83        $result.YearsInMS | Should -Be 1184    }85 86    It "New-Object with invalid type should throw Exception"{87        $e = { New-Object -TypeName LiarType -ErrorAction Stop } | Should -Throw -ErrorId "TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand" -PassThru88        $e.CategoryInfo | Should -Match "PSArgumentException"89    }90 91    It "New-Object with invalid argument should throw Exception"{92        $e = { New-Object -TypeName System.Management.Automation.PSVariable -ArgumentList "A", 1, None, "asd" -ErrorAction Stop } |93	        Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" -PassThru94        $e.CategoryInfo | Should -Match "MethodException"95    }96 97    It "New-Object with abstract class should throw Exception"{98        Add-Type -TypeDefinition "public abstract class AbstractEmployee{public AbstractEmployee(){}}"99        $e = { New-Object -TypeName AbstractEmployee -ErrorAction Stop } |100		Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" -PassThru101        $e.CategoryInfo | Should -Match "MethodInvocationException"102    }103 104    It "New-Object with bad argument for class constructor should throw Exception"{105        if(-not ([System.Management.Automation.PSTypeName]'Employee').Type)106        {107            Add-Type -TypeDefinition "public class Employee{public Employee(string firstName,string lastName,int yearsInMS){FirstName = firstName;LastName=lastName;YearsInMS = yearsInMS;}public string FirstName;public string LastName;public int YearsInMS;}"108        }109        $e = { New-Object -TypeName Employee -ArgumentList 11 -ErrorAction Stop } | Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" -PassThru110        $e.CategoryInfo | Should -Match "MethodException"111    }112 113    #This case will throw "Execution OK" now, just mark as pending now114    It "New-Object with not init class constructor should throw Exception" -Pending{115        if(-not ([System.Management.Automation.PSTypeName]'Employee').Type)116        {117           Add-Type -TypeDefinition "public class Employee{public Employee(string firstName,string lastName,int yearsInMS){FirstName = firstName;LastName=lastName;YearsInMS = yearsInMS;}public string FirstName;public string LastName;public int YearsInMS;}"118        }119        { New-Object -TypeName Employee -ErrorAction Stop } | Should -Throw -ErrorId "CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand"120    }121 122    It "New-Object with Private Nested class should throw Exception"{123        Add-Type -TypeDefinition "public class WeirdEmployee{public WeirdEmployee(){}private class PrivateNestedWeirdEmployee{public PrivateNestedWeirdEmployee(){}}}"124        $e = { New-Object -TypeName WeirdEmployee+PrivateNestedWeirdEmployee -ErrorAction Stop } | Should -Throw -ErrorId "TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand" -PassThru125        $e.CategoryInfo | Should -Match "PSArgumentException"126    }127 128    It "New-Object with TypeName and Property parameter should work"{129        $result = New-Object -TypeName PSObject -Property @{foo=123}130        $result.foo | Should -Be 123131    }132}133 134try135{136    $defaultParamValues = $PSDefaultParameterValues.Clone()137    $PSDefaultParameterValues["it:skip"] = ![System.Management.Automation.Platform]::IsWindowsDesktop138 139    Describe "New-Object COM functionality" -Tags "CI" {140        $testCases = @(141            @{142                Name   = 'Microsoft.Update.AutoUpdate'143                Property = 'Settings'144                Type = 'Object'145            }146            @{147                Name   = 'Microsoft.Update.SystemInfo'148                Property = 'RebootRequired'149                Type = 'Bool'150            }151        )152 153        It "Should be able to create <Name> with property <Property> of Type <Type>" -TestCases $testCases {154            param($Name, $Property, $Type)155            $comObject = New-Object -ComObject $name156            $comObject.$Property | Should -Not -BeNullOrEmpty157            $comObject.$Property | Should -BeOfType $Type158        }159 160        It "Should fail with correct error when creating a COM object that dose not exist" {161            {New-Object -ComObject 'doesnotexist'} | Should -Throw -ErrorId 'NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand'162        }163    }164}165finally166{167    $global:PSdefaultParameterValues = $defaultParamValues168}169