Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should update these tests to use:

{ ...scriptblock... } | Should -Throw -ErrorId "...fully qualified error id..."

}

# when Name is empty
Expand All @@ -22,7 +22,7 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
}

# when MemberType is null
Expand All @@ -33,7 +33,7 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
}

# when MemberType is empty
Expand All @@ -44,7 +44,7 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.AddMemberCommand"
}

# when InputObject is null
Expand All @@ -55,7 +55,7 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}

Expand All @@ -70,7 +70,7 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
Throw "Execution OK"
}
catch{
$_.FullyQualifiedErrorId | Should Be "Value2ShouldNotBeSpecified,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "Value2ShouldNotBeSpecified,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}
}
Expand All @@ -86,7 +86,7 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "CannotAddMemberType,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "CannotAddMemberType,Microsoft.PowerShell.Commands.AddMemberCommand"

}
}
Expand All @@ -98,7 +98,7 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.AddMemberCommand"
}

}
Expand All @@ -114,7 +114,7 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "Value1AndValue2AreNotBothNull,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "Value1AndValue2AreNotBothNull,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}

Expand All @@ -128,20 +128,20 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "InvalidCastFromStringToType,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "InvalidCastFromStringToType,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}

It "Successful alias, no type" {
$results = Add-Member -InputObject a -MemberType AliasProperty -Name Cnt -Value Length -passthru
$results.Cnt | Should BeOfType Int32
$results.Cnt | Should Be 1
$results.Cnt | Should -BeOfType Int32
$results.Cnt | Should -Be 1
}

It "Successful alias, with type" {
$results = add-member -InputObject a -MemberType AliasProperty -Name Cnt -Value Length -SecondValue String -passthru
$results.Cnt | Should BeOfType String
$results.Cnt | Should Be '1'
$results.Cnt | Should -BeOfType String
$results.Cnt | Should -Be '1'
}

It "CodeProperty Reference Wrong Type" {
Expand All @@ -152,22 +152,22 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}

It "Empty Member Set Null Value1" {
$results = add-member -InputObject a -MemberType MemberSet -Name Name -Value $null -passthru
$results.Length | Should Be 1
$results.Name.a | Should BeNullOrEmpty
$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 -MemberType MemberSet -Name Name -Value $members -passthru
$r.Name.a | Should Be '1'
$r.Name.a | Should -Be '1'
}

It "MemberSet With Wrong Type For Value1" {
Expand All @@ -178,7 +178,7 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}

Expand All @@ -190,14 +190,14 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}

It "Add ScriptMethod Success" {
$results = add-member -InputObject 'abc' -MemberType ScriptMethod -Name Name -Value {$this.length} -passthru
$results | Should Be abc
$results.Name() | Should Be 3
$results | Should -Be abc
$results.Name() | Should -Be 3
}

It "ScriptProperty Reference Wrong Type" {
Expand All @@ -208,7 +208,7 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
$_.FullyQualifiedErrorId | Should -Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}

Expand All @@ -217,74 +217,74 @@ Describe "Add-Member DRT Unit Tests" -Tags "CI" {
$al=(get-alias ScriptPropertyTestAlias)
$al.Description="MyDescription"
$al | add-member -MemberType ScriptProperty -Name NewDescription -Value {$this.Description} -SecondValue {$this.Description=$args[0]}
$al.NewDescription | Should Be 'MyDescription'
$al.NewDescription | Should -Be 'MyDescription'
$al.NewDescription = "some description"
$al.NewDescription | Should Be 'some description'
$al.NewDescription | Should -Be 'some description'
}

It "Add TypeName MemberSet Success" {
$a = 'string' | add-member -MemberType NoteProperty -Name TestNote -Value Any -TypeName MyType -passthru
$a.PSTypeNames[0] | Should Be MyType
$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
$a.PSTypeNames[0] | Should -Be System.Object
}

It "Add Single Note To Array" {
$a=1,2,3
$a = Add-Member -InputObject $a -MemberType NoteProperty -Name Name -Value Value -PassThru
$a.Name | Should Be Value
$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
$obj.Name | Should Be 'Name'
$obj.TestInt | Should Be 1
$obj.TestNull | Should BeNullOrEmpty
$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
$obj.PSTypeNames[0] | Should -Be MyType
}

It "Add Multiple Members With Force" {
$obj=new-object psobject
$hash=@{TestNote='hello'}
$obj | Add-Member -MemberType NoteProperty -Name TestNote -Value 1
$obj | add-member $hash -force
$obj.TestNote | Should Be 'hello'
$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.property | Should -Be 'Any'

$results = add-member -InputObject a Method Any -passthru
$results.Method | Should Be 'Any'
$results.Method | Should -Be 'Any'

$results = add-member -InputObject a 23 Any -passthru
$results.23 | Should Be 'Any'
$results.23 | Should -Be 'Any'

$results = add-member -InputObject a 8 np Any -passthru
$results.np | Should Be 'Any'
$results.np | Should -Be 'Any'

$results = add-member -InputObject a 16 sp {1+1} -passthru
$results.sp | Should Be 2
$results.sp | Should -Be 2
}

It "Verify Add-Member error message is not empty" {
$object = @(1,2)
Add-Member -InputObject $object "ABC" "Value1"
Add-Member -InputObject $object "ABC" "Value2" -ErrorVariable errorVar -ErrorAction SilentlyContinue
$errorVar.Exception | Should BeOfType "System.InvalidOperationException"
$errorVar.Exception.Message | Should Not BeNullOrEmpty
$errorVar.Exception | Should -BeOfType "System.InvalidOperationException"
$errorVar.Exception.Message | Should -Not -BeNullOrEmpty
}
}

Expand All @@ -294,16 +294,16 @@ Describe "Add-Member" -Tags "CI" {
$o = New-Object psobject
Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"

$o.proppy | Should Not BeNullOrEmpty
$o.proppy | Should Be "superVal"
$o.proppy | Should -Not -BeNullOrEmpty
$o.proppy | Should -Be "superVal"
}

It "Should be able to add a member to an object that already has a member in it" {
$o = New-Object psobject
Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"
Add-Member -InputObject $o -MemberType NoteProperty -Name AnotherMember -Value "AnotherValue"

$o.AnotherMember | Should Not BeNullOrEmpty
$o.AnotherMember | Should Be "AnotherValue"
$o.AnotherMember | Should -Not -BeNullOrEmpty
$o.AnotherMember | Should -Be "AnotherValue"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,33 @@ Describe "Add-Type" -Tags "CI" {
}

It "Public 'Language' enumeration contains all members" {
[Enum]::GetNames("Microsoft.PowerShell.Commands.Language") -join "," | Should Be "CSharp,VisualBasic"
[Enum]::GetNames("Microsoft.PowerShell.Commands.Language") -join "," | Should -Be "CSharp,VisualBasic"
}

It "Should not throw given a simple class definition" {
{ Add-Type -TypeDefinition "public static class foo { }" } | Should Not Throw
{ Add-Type -TypeDefinition "public static class foo { }" } | Should -Not -Throw
}

It "Can use System.Management.Automation.CmdletAttribute" {
$code = @"
[System.Management.Automation.Cmdlet("Get", "Thing", ConfirmImpact = System.Management.Automation.ConfirmImpact.High, SupportsPaging = true)]
public class AttributeTest$guid {}
"@
Add-Type -TypeDefinition $code -PassThru | Should Not Be $null
Add-Type -TypeDefinition $code -PassThru | Should -Not -BeNullOrEmpty
}

It "Can load TPA assembly System.Runtime.Serialization.Primitives.dll" {
Add-Type -AssemblyName 'System.Runtime.Serialization.Primitives' -PassThru | Should Not Be $null
Add-Type -AssemblyName 'System.Runtime.Serialization.Primitives' -PassThru | Should -Not -BeNullOrEmpty
}

It "Can compile C# files" {

{ [Test.AddType.BasicTest1]::Add1(1, 2) } | Should Throw
{ [Test.AddType.BasicTest2]::Add2(3, 4) } | Should Throw
{ [Test.AddType.BasicTest1]::Add1(1, 2) } | Should -Throw
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should never have just Should -Throw, please get the error being thrown and add -ErrorId. We want to prevent the test from passing if it throws for a different reason than what is being tested (like out of memory, for example).

{ [Test.AddType.BasicTest2]::Add2(3, 4) } | Should -Throw

Add-Type -Path $codeFile1,$codeFile2

{ [Test.AddType.BasicTest1]::Add1(1, 2) } | Should Not Throw
{ [Test.AddType.BasicTest2]::Add2(3, 4) } | Should Not Throw
{ [Test.AddType.BasicTest1]::Add1(1, 2) } | Should -Not -Throw
{ [Test.AddType.BasicTest2]::Add2(3, 4) } | Should -Not -Throw
}
}
Loading