From 61441715c10ab5b4fb9c9d91e771e2cf38266b7d Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Wed, 20 Apr 2016 00:57:31 -0700 Subject: [PATCH 1/3] Add Unit Test for Add-Member --- test/powershell/Add-Member.Tests.ps1 | 284 +++++++++++++++++++++++++++ 1 file changed, 284 insertions(+) diff --git a/test/powershell/Add-Member.Tests.ps1 b/test/powershell/Add-Member.Tests.ps1 index 31bae7d335..e082fa8f20 100644 --- a/test/powershell/Add-Member.Tests.ps1 +++ b/test/powershell/Add-Member.Tests.ps1 @@ -1,3 +1,287 @@ +Describe "Add-Member DRT Unit Tests" -Tags DRT{ + + It "Mandatory parameters should not be null nor empty" { + # when Name is null + try + { + Add-Member -Name $null + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand" + } + + # when Name is empty + try + { + Add-Member -Name "" + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand" + } + + # when MemberType is null + try + { + Add-Member -MemberType $null + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand" + } + + # when MemberType is empty + try + { + Add-Member -MemberType "" + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.AddMemberCommand" + } + + # when InputObject is null + try + { + Add-Member -InputObject $null + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand" + } + } + + # It only support on AliasProperty, ScriptProperty, CodeProperty and CodeMethod + It "Should Not Have Value2" { + $memberTypesWhereV1CannotBeNull = "CodeMethod", "MemberSet", "PropertySet", "ScriptMethod", "NoteProperty" + foreach ($memberType in $memberTypesWhereV1CannotBeNull) + { + try + { + Add-Member -InputObject a $memberType Name something somethingElse + Throw "Execution OK" + } + catch{ + $_.FullyQualifiedErrorId | Should Be "Value2ShouldNotBeSpecified,Microsoft.PowerShell.Commands.AddMemberCommand" + } + } + } + + # Blocked by Issue #874. + It "Cannot Add PS Property Or PS Method" -Skip:$true { + $membersYouCannotAdd = "Method", "Property", "ParameterizedProperty", "AnythingElse" + foreach ($member in $membersYouCannotAdd) + { + try + { + Add-Member -InputObject a $member Name + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "" + + } + } + + } + + It "Value1 And Value2 Should Not Both Null" { + $memberTypes = "CodeProperty", "ScriptProperty" + foreach ($memberType in $memberTypes) + { + try + { + Add-Member $memberType PropertyName $null $null -InputObject a + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "Value1AndValue2AreNotBothNull,Microsoft.PowerShell.Commands.AddMemberCommand" + } + } + + } + + It "Fail to add unexisting type" { + try + { + Add-Member -InputObject a AliasProperty Name something unexistingType + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "InvalidCastFromStringToType,Microsoft.PowerShell.Commands.AddMemberCommand" + } + } + + It "Successful alias, no type" { + $results = Add-Member -InputObject a AliasProperty Cnt Length -passthru + $results.Cnt.GetType().Name | Should Be 'Int32' + $results.Cnt | Should Be 1 + } + + It "Successful alias, with type" { + $results = add-member -InputObject a AliasProperty Cnt Length String -passthru + $results.Cnt.GetType().Name | Should Be 'String' + $results.Cnt | Should Be '1' + } + + It "CodeProperty Reference Wrong Type" { + try + { + add-member -InputObject a CodeProperty Name something + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand" + } + } + + It "Empty Member Set Null Value1" { + $results = add-member -InputObject a MemberSet Name $null -passthru + $results.Length | Should Be 1 + $results.Name.a | Should BeNullOrEmpty + } + + It "Member Set With 1 Member" { + $members = new-object System.Collections.ObjectModel.Collection[System.Management.Automation.PSMemberInfo] + $n=new-object Management.Automation.PSNoteProperty a,1 + $members.Add($n) + $r=add-member -InputObject a MemberSet Name $members -passthru + $r.Name.a | Should Be '1' + } + + It "MemberSet With Wrong Type For Value1" { + try + { + add-member -InputObject a MemberSet Name ImNotACollection + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand" + } + } + + # Blocked by Issue #875. + It "PropertySet With Wrong Type For Value1" -Skip:$true { + try + { + add-member -InputObject a PropertySet Name ImNotACollection + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand" + } + } + + It "ScriptMethod Reference Wrong Type" { + try + { + add-member -InputObject a ScriptMethod Name something + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand" + } + } + + It "Add ScriptMethod Success" { + $results = add-member -InputObject 'abc' ScriptMethod Name {$this.length} -passthru + $results | Should Be abc + $results.Name() | Should Be 3 + } + + It "ScriptProperty Reference Wrong Type" { + try + { + add-member -InputObject a ScriptProperty Name something + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand" + } + } + + It "Add ScriptProperty Success" { + set-alias ScriptPropertyTestAlias dir + $al=(get-alias ScriptPropertyTestAlias) + $al.Description="MyDescription" + $al | add-member ScriptProperty NewDescription {$this.Description} {$this.Description=$args[0]} -passthru + $al.NewDescription | Should Be 'MyDescription' + $al.NewDescription = "some description" + $al.NewDescription | Should Be 'some description' + } + + It "Add TypeName MemberSet Success" { + $a = 'string' | add-member NoteProperty TestNote Any -TypeName MyType -passthru + $a.PSTypeNames[0] | Should Be MyType + } + + It "Add TypeName Existing Name Success" { + $a = 'string' | add-member -TypeName System.Object -passthru + $a.PSTypeNames[0] | Should Be System.Object + } + + It "Add Single Note To Array" { + $a=1,2,3 + $a = add-member -InputObject $a Name Value -Passthru + $a.Name | Should Be Value + } + + It "Add Multiple Note Members" { + $obj=new-object psobject + $hash=@{Name='Name';TestInt=1;TestNull=$null} + add-member -InputObject $obj $hash -Passthru + $obj.Name | Should Be 'Name' + $obj.TestInt | Should Be 1 + $obj.TestNull | Should BeNullOrEmpty + } + + It "Add Multiple Note With TypeName" { + $obj=new-object psobject + $hash=@{Name='Name';TestInt=1;TestNull=$null} + $obj = add-member -InputObject $obj $hash -TypeName MyType -Passthru + $obj.PSTypeNames[0] | Should Be MyType + } + + It "Add Multiple Members With Force" { + $obj=new-object psobject + $hash=@{TestNote='hello'} + $obj | add-member TestNote 1 + $obj | add-member $hash -force -Passthru + $obj.TestNote | Should Be 'hello' + } + + It "Simplified Add-Member should support using 'Property' as the NoteProperty member name" { + $results = add-member -InputObject a property Any -passthru + $results.property | Should Be 'Any' + + $results = add-member -InputObject a Method Any -passthru + $results.Method | Should Be 'Any' + + $results = add-member -InputObject a 23 Any -passthru + $results.23 | Should Be 'Any' + + $results = add-member -InputObject a 8 np Any -passthru + $results.np | Should Be 'Any' + + $results = add-member -InputObject a 16 sp {1+1} -passthru + $results.sp | Should Be 2 + } +} + Describe "Add-Member" { It "should be able to see a newly added member of an object" { From 6a0dc423f4f0c4cadf0b62b8bfb56ac111553d2d Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Wed, 20 Apr 2016 21:34:23 -0700 Subject: [PATCH 2/3] Fixed the CR issues --- test/powershell/Add-Member.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Add-Member.Tests.ps1 b/test/powershell/Add-Member.Tests.ps1 index e082fa8f20..dfd8e68574 100644 --- a/test/powershell/Add-Member.Tests.ps1 +++ b/test/powershell/Add-Member.Tests.ps1 @@ -74,7 +74,7 @@ } # Blocked by Issue #874. - It "Cannot Add PS Property Or PS Method" -Skip:$true { + It "Cannot Add PS Property Or PS Method" -Pending { $membersYouCannotAdd = "Method", "Property", "ParameterizedProperty", "AnythingElse" foreach ($member in $membersYouCannotAdd) { @@ -172,7 +172,7 @@ } # Blocked by Issue #875. - It "PropertySet With Wrong Type For Value1" -Skip:$true { + It "PropertySet With Wrong Type For Value1" -Pending { try { add-member -InputObject a PropertySet Name ImNotACollection From fc1d89a3733446fdad795316d965ffd8bee69347 Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Fri, 22 Apr 2016 01:44:22 -0700 Subject: [PATCH 3/3] Fix the issues of CR for Add-Member --- test/powershell/Add-Member.Tests.ps1 | 68 +++++++++++++--------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/test/powershell/Add-Member.Tests.ps1 b/test/powershell/Add-Member.Tests.ps1 index dfd8e68574..b78f8c7dd9 100644 --- a/test/powershell/Add-Member.Tests.ps1 +++ b/test/powershell/Add-Member.Tests.ps1 @@ -64,7 +64,7 @@ { try { - Add-Member -InputObject a $memberType Name something somethingElse + Add-Member -InputObject a -memberType $memberType -Name Name -Value something -SecondValue somethingElse Throw "Execution OK" } catch{ @@ -73,22 +73,31 @@ } } - # Blocked by Issue #874. - It "Cannot Add PS Property Or PS Method" -Pending { - $membersYouCannotAdd = "Method", "Property", "ParameterizedProperty", "AnythingElse" + It "Cannot Add PS Property Or PS Method" { + $membersYouCannotAdd = "Method", "Property", "ParameterizedProperty" foreach ($member in $membersYouCannotAdd) { try { - Add-Member -InputObject a $member Name + Add-Member -InputObject a -memberType $member -Name Name Throw "Execution OK" } catch { - $_.FullyQualifiedErrorId | Should Be "" + $_.FullyQualifiedErrorId | Should Be "CannotAddMemberType,Microsoft.PowerShell.Commands.AddMemberCommand" } } + + try + { + Add-Member -InputObject a -memberType AnythingElse -Name Name + Throw "Execution OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.AddMemberCommand" + } } @@ -98,7 +107,7 @@ { try { - Add-Member $memberType PropertyName $null $null -InputObject a + Add-Member -memberType $memberType -Name PropertyName -Value $null -SecondValue $null -InputObject a Throw "Execution OK" } catch @@ -112,7 +121,7 @@ It "Fail to add unexisting type" { try { - Add-Member -InputObject a AliasProperty Name something unexistingType + Add-Member -InputObject a -MemberType AliasProperty -Name Name -Value something -SecondValue unexistingType Throw "Execution OK" } catch @@ -122,13 +131,13 @@ } It "Successful alias, no type" { - $results = Add-Member -InputObject a AliasProperty Cnt Length -passthru + $results = Add-Member -InputObject a -MemberType AliasProperty -Name Cnt -Value Length -passthru $results.Cnt.GetType().Name | Should Be 'Int32' $results.Cnt | Should Be 1 } It "Successful alias, with type" { - $results = add-member -InputObject a AliasProperty Cnt Length String -passthru + $results = add-member -InputObject a -MemberType AliasProperty -Name Cnt -Value Length -SecondValue String -passthru $results.Cnt.GetType().Name | Should Be 'String' $results.Cnt | Should Be '1' } @@ -136,7 +145,7 @@ It "CodeProperty Reference Wrong Type" { try { - add-member -InputObject a CodeProperty Name something + add-member -InputObject a -MemberType CodeProperty -Name Name -Value something Throw "Execution OK" } catch @@ -146,7 +155,7 @@ } It "Empty Member Set Null Value1" { - $results = add-member -InputObject a MemberSet Name $null -passthru + $results = add-member -InputObject a -MemberType MemberSet -Name Name -Value $null -passthru $results.Length | Should Be 1 $results.Name.a | Should BeNullOrEmpty } @@ -155,27 +164,14 @@ $members = new-object System.Collections.ObjectModel.Collection[System.Management.Automation.PSMemberInfo] $n=new-object Management.Automation.PSNoteProperty a,1 $members.Add($n) - $r=add-member -InputObject a MemberSet Name $members -passthru + $r=add-member -InputObject a -MemberType MemberSet -Name Name -Value $members -passthru $r.Name.a | Should Be '1' } It "MemberSet With Wrong Type For Value1" { try { - add-member -InputObject a MemberSet Name ImNotACollection - Throw "Execution OK" - } - catch - { - $_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand" - } - } - - # Blocked by Issue #875. - It "PropertySet With Wrong Type For Value1" -Pending { - try - { - add-member -InputObject a PropertySet Name ImNotACollection + add-member -InputObject a -MemberType MemberSet -Name Name -Value ImNotACollection Throw "Execution OK" } catch @@ -187,7 +183,7 @@ It "ScriptMethod Reference Wrong Type" { try { - add-member -InputObject a ScriptMethod Name something + add-member -InputObject a -MemberType ScriptMethod -Name Name -Value something Throw "Execution OK" } catch @@ -197,7 +193,7 @@ } It "Add ScriptMethod Success" { - $results = add-member -InputObject 'abc' ScriptMethod Name {$this.length} -passthru + $results = add-member -InputObject 'abc' -MemberType ScriptMethod -Name Name -Value {$this.length} -passthru $results | Should Be abc $results.Name() | Should Be 3 } @@ -205,7 +201,7 @@ It "ScriptProperty Reference Wrong Type" { try { - add-member -InputObject a ScriptProperty Name something + add-member -InputObject a -MemberType ScriptProperty -Name Name -Value something Throw "Execution OK" } catch @@ -218,14 +214,14 @@ set-alias ScriptPropertyTestAlias dir $al=(get-alias ScriptPropertyTestAlias) $al.Description="MyDescription" - $al | add-member ScriptProperty NewDescription {$this.Description} {$this.Description=$args[0]} -passthru + $al | add-member -MemberType ScriptProperty -Name NewDescription -Value {$this.Description} -SecondValue {$this.Description=$args[0]} $al.NewDescription | Should Be 'MyDescription' $al.NewDescription = "some description" $al.NewDescription | Should Be 'some description' } It "Add TypeName MemberSet Success" { - $a = 'string' | add-member NoteProperty TestNote Any -TypeName MyType -passthru + $a = 'string' | add-member -MemberType NoteProperty -Name TestNote -Value Any -TypeName MyType -passthru $a.PSTypeNames[0] | Should Be MyType } @@ -236,14 +232,14 @@ It "Add Single Note To Array" { $a=1,2,3 - $a = add-member -InputObject $a Name Value -Passthru + $a = Add-Member -InputObject $a -MemberType NoteProperty -Name Name -Value Value -PassThru $a.Name | Should Be Value } It "Add Multiple Note Members" { $obj=new-object psobject $hash=@{Name='Name';TestInt=1;TestNull=$null} - add-member -InputObject $obj $hash -Passthru + add-member -InputObject $obj $hash $obj.Name | Should Be 'Name' $obj.TestInt | Should Be 1 $obj.TestNull | Should BeNullOrEmpty @@ -259,8 +255,8 @@ It "Add Multiple Members With Force" { $obj=new-object psobject $hash=@{TestNote='hello'} - $obj | add-member TestNote 1 - $obj | add-member $hash -force -Passthru + $obj | Add-Member -MemberType NoteProperty -Name TestNote -Value 1 + $obj | add-member $hash -force $obj.TestNote | Should Be 'hello' }