"Fossies" - the Fresh Open Source Software Archive

Member "PowerShell-7.2.6/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Member.Tests.ps1" (11 Aug 2022, 9030 Bytes) of package /linux/misc/PowerShell-7.2.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Microsoft PowerShell source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright (c) Microsoft Corporation.
    2 # Licensed under the MIT License.
    3 Describe "Add-Member DRT Unit Tests" -Tags "CI" {
    4 
    5     It "Mandatory parameters should not be null nor empty" {
    6         # when Name is null
    7         { Add-Member -Name $null } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
    8 
    9         # when Name is empty
   10         { Add-Member -Name "" } | Should -Throw -ErrorId "ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
   11 
   12         # when MemberType is null
   13         { Add-Member -MemberType $null } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
   14 
   15         # when MemberType is empty
   16         { Add-Member -MemberType "" } | Should -Throw -ErrorId "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.AddMemberCommand"
   17 
   18         # when InputObject is null
   19         { Add-Member -InputObject $null } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
   20     }
   21 
   22     # It only support on AliasProperty, ScriptProperty, CodeProperty and CodeMethod
   23     It "Should Not Have Value2" {
   24         $memberTypesWhereV1CannotBeNull = "CodeMethod", "MemberSet", "PropertySet", "ScriptMethod", "NoteProperty"
   25         foreach ($memberType in $memberTypesWhereV1CannotBeNull)
   26         {
   27             { Add-Member -InputObject a -MemberType $memberType -Name Name -Value something -SecondValue somethingElse } |
   28                 Should -Throw -ErrorId "Value2ShouldNotBeSpecified,Microsoft.PowerShell.Commands.AddMemberCommand"
   29         }
   30     }
   31 
   32     It "Cannot Add PS Property Or PS Method" {
   33         $membersYouCannotAdd = "Method", "Property", "ParameterizedProperty"
   34         foreach ($member in $membersYouCannotAdd)
   35         {
   36             { Add-Member -InputObject a -MemberType $member -Name Name } | Should -Throw -ErrorId "CannotAddMemberType,Microsoft.PowerShell.Commands.AddMemberCommand"
   37         }
   38 
   39         { Add-Member -InputObject a -MemberType AnythingElse -Name Name } | Should -Throw -ErrorId "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.AddMemberCommand"
   40 
   41     }
   42 
   43     It "Value1 And Value2 Should Not Both Null" {
   44         $memberTypes = "CodeProperty", "ScriptProperty"
   45         foreach ($memberType in $memberTypes)
   46         {
   47             { Add-Member -MemberType $memberType -Name PropertyName -Value $null -SecondValue $null -InputObject a } |
   48                 Should -Throw -ErrorId "Value1AndValue2AreNotBothNull,Microsoft.PowerShell.Commands.AddMemberCommand"
   49         }
   50 
   51     }
   52 
   53     It "Fail to add unexisting type" {
   54         { Add-Member -InputObject a -MemberType AliasProperty -Name Name -Value something -SecondValue unexistingType } |
   55             Should -Throw -ErrorId "InvalidCastFromStringToType,Microsoft.PowerShell.Commands.AddMemberCommand"
   56     }
   57 
   58     It "Successful alias, no type" {
   59         $results = Add-Member -InputObject a -MemberType AliasProperty -Name Cnt -Value Length -PassThru
   60         $results.Cnt | Should -BeOfType Int32
   61         $results.Cnt | Should -Be 1
   62     }
   63 
   64     It "Successful alias, with type" {
   65         $results = Add-Member -InputObject a -MemberType AliasProperty -Name Cnt -Value Length -SecondValue String -PassThru
   66         $results.Cnt | Should -BeOfType String
   67         $results.Cnt | Should -Be '1'
   68     }
   69 
   70     It "CodeProperty Reference Wrong Type" {
   71         { Add-Member -InputObject a -MemberType CodeProperty -Name Name -Value something } |
   72             Should -Throw -ErrorId "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
   73     }
   74 
   75     It "Empty Member Set Null Value1" {
   76         $results = Add-Member -InputObject a -MemberType MemberSet -Name Name -Value $null -PassThru
   77         $results.Length | Should -Be 1
   78         $results.Name.a | Should -BeNullOrEmpty
   79     }
   80 
   81     It "Member Set With 1 Member" {
   82         $members = New-Object System.Collections.ObjectModel.Collection[System.Management.Automation.PSMemberInfo]
   83         $n=New-Object Management.Automation.PSNoteProperty a,1
   84         $members.Add($n)
   85         $r=Add-Member -InputObject a -MemberType MemberSet -Name Name -Value $members -PassThru
   86         $r.Name.a | Should -Be '1'
   87     }
   88 
   89     It "MemberSet With Wrong Type For Value1" {
   90         { Add-Member -InputObject a -MemberType MemberSet -Name Name -Value ImNotACollection } |
   91             Should -Throw -ErrorId "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
   92     }
   93 
   94     It "ScriptMethod Reference Wrong Type" {
   95         { Add-Member -InputObject a -MemberType ScriptMethod -Name Name -Value something } |
   96             Should -Throw -ErrorId "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
   97     }
   98 
   99     It "Add ScriptMethod Success" {
  100         $results = Add-Member -InputObject 'abc' -MemberType ScriptMethod -Name Name -Value {$this.length} -PassThru
  101         $results | Should -BeExactly 'abc'
  102         $results.Name() | Should -Be 3
  103     }
  104 
  105     It "ScriptProperty Reference Wrong Type" {
  106         { Add-Member -InputObject a -MemberType ScriptProperty -Name Name -Value something } |
  107             Should -Throw -ErrorId "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
  108     }
  109 
  110     It "Add ScriptProperty Success" {
  111         Set-Alias ScriptPropertyTestAlias dir
  112         $al=(Get-Alias ScriptPropertyTestAlias)
  113         $al.Description="MyDescription"
  114         $al | Add-Member -MemberType ScriptProperty -Name NewDescription -Value {$this.Description} -SecondValue {$this.Description=$args[0]}
  115         $al.NewDescription | Should -BeExactly 'MyDescription'
  116         $al.NewDescription = "some description"
  117         $al.NewDescription | Should -BeExactly 'some description'
  118     }
  119 
  120     It "Add TypeName MemberSet Success" {
  121         $a = 'string' | Add-Member -MemberType NoteProperty -Name TestNote -Value Any -TypeName MyType -PassThru
  122         $a.PSTypeNames[0] | Should -Be MyType
  123     }
  124 
  125     It "Add TypeName Existing Name Success" {
  126         $a = 'string' | Add-Member -TypeName System.Object -PassThru
  127         $a.PSTypeNames[0] | Should -Be System.Object
  128     }
  129 
  130     It "Add Single Note To Array" {
  131         $a=1,2,3
  132         $a = Add-Member -InputObject $a -MemberType NoteProperty -Name Name -Value Value -PassThru
  133         $a.Name | Should -Be Value
  134     }
  135 
  136     It "Add Multiple Note Members" {
  137         $obj=New-Object psobject
  138         $hash=@{Name='Name';TestInt=1;TestNull=$null}
  139         Add-Member -InputObject $obj $hash
  140         $obj.Name | Should -Be 'Name'
  141         $obj.TestInt | Should -Be 1
  142         $obj.TestNull | Should -BeNullOrEmpty
  143     }
  144 
  145     It "Add Multiple Note With TypeName" {
  146         $obj=New-Object psobject
  147         $hash=@{Name='Name';TestInt=1;TestNull=$null}
  148         $obj = Add-Member -InputObject $obj $hash -TypeName MyType -PassThru
  149         $obj.PSTypeNames[0] | Should -Be MyType
  150     }
  151 
  152     It "Add Multiple Members With Force" {
  153         $obj=New-Object psobject
  154         $hash=@{TestNote='hello'}
  155         $obj | Add-Member -MemberType NoteProperty -Name TestNote -Value 1
  156         $obj | Add-Member $hash -Force
  157         $obj.TestNote | Should -Be 'hello'
  158     }
  159 
  160     It "Simplified Add-Member should support using 'Property' as the NoteProperty member name" {
  161         $results = Add-Member -InputObject a property Any -PassThru
  162         $results.property | Should -BeExactly 'Any'
  163 
  164         $results = Add-Member -InputObject a Method Any -PassThru
  165         $results.Method | Should -BeExactly 'Any'
  166 
  167         $results = Add-Member -InputObject a 23 Any -PassThru
  168         $results.23 | Should -BeExactly 'Any'
  169 
  170         $results = Add-Member -InputObject a 8 np Any -PassThru
  171         $results.np | Should -BeExactly 'Any'
  172 
  173         $results = Add-Member -InputObject a 16 sp {1+1} -PassThru
  174         $results.sp | Should -Be 2
  175     }
  176 
  177     It "Verify Add-Member error message is not empty" {
  178         $object = @(1,2)
  179         Add-Member -InputObject $object "ABC" "Value1"
  180         Add-Member -InputObject $object "ABC" "Value2" -ErrorVariable errorVar -ErrorAction SilentlyContinue
  181         $errorVar.Exception | Should -BeOfType System.InvalidOperationException
  182         $errorVar.Exception.Message | Should -Not -BeNullOrEmpty
  183     }
  184 }
  185 
  186 Describe "Add-Member" -Tags "CI" {
  187 
  188     It "should be able to see a newly added member of an object" {
  189     $o = New-Object psobject
  190     Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"
  191 
  192     $o.proppy | Should -Not -BeNullOrEmpty
  193     $o.proppy | Should -BeExactly "superVal"
  194     }
  195 
  196     It "Should be able to add a member to an object that already has a member in it" {
  197     $o = New-Object psobject
  198     Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"
  199     Add-Member -InputObject $o -MemberType NoteProperty -Name AnotherMember -Value "AnotherValue"
  200 
  201     $o.AnotherMember | Should -Not -BeNullOrEmpty
  202     $o.AnotherMember | Should -BeExactly "AnotherValue"
  203     }
  204 }