Files
PowerShell/test/powershell/Language/Parser/ParameterBinding.Tests.ps1
T

96 lines
3.1 KiB
PowerShell
Raw Normal View History

2016-07-19 13:52:03 -07:00
2016-07-19 17:21:32 -07:00
Describe 'Argument transformation attribute on optional argument with explicit $null' -Tags "CI" {
$tdefinition = @'
2016-07-19 13:52:03 -07:00
using System;
using System.Management.Automation;
using System.Reflection;
namespace MSFT_1407291
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class AddressTransformationAttribute : ArgumentTransformationAttribute
{
public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
{
return (ulong) 42;
}
}
[Cmdlet(VerbsLifecycle.Invoke, "CSharpCmdletTakesUInt64")]
[OutputType(typeof(System.String))]
public class Cmdlet1 : PSCmdlet
{
[Parameter(Mandatory = false)]
[AddressTransformation]
public ulong Address { get; set; }
protected override void ProcessRecord()
{
WriteObject(Address);
}
}
[Cmdlet(VerbsLifecycle.Invoke, "CSharpCmdletTakesObject")]
[OutputType(typeof(System.String))]
public class Cmdlet2 : PSCmdlet
{
[Parameter(Mandatory = false)]
[AddressTransformation]
public object Address { get; set; }
protected override void ProcessRecord()
{
WriteObject(Address ?? "passed in null");
}
}
}
'@
2016-07-19 17:21:32 -07:00
$mod = Add-Type -PassThru -TypeDefinition $tdefinition -refer mscorlib,System.Management.Automation
2016-07-19 13:52:03 -07:00
Import-Module $mod[0].Assembly
function Invoke-ScriptFunctionTakesObject
{
param([MSFT_1407291.AddressTransformation()]
[Parameter(Mandatory = $false)]
[object]$Address = "passed in null")
return $Address
}
function Invoke-ScriptFunctionTakesUInt64
{
param([MSFT_1407291.AddressTransformation()]
[Parameter(Mandatory = $false)]
[Uint64]$Address = 11)
return $Address
}
2016-07-19 17:21:32 -07:00
It "Script function takes object" {
Invoke-ScriptFunctionTakesObject | Should Be 42
}
It "Script function takes uint64" {
Invoke-ScriptFunctionTakesUInt64 | Should Be 42
}
it "csharp cmdlet takes object" {
Invoke-CSharpCmdletTakesObject | Should Be "passed in null"
}
it "csharp cmdlet takes uint64" {
Invoke-CSharpCmdletTakesUInt64 | Should Be 0
}
2016-07-19 13:52:03 -07:00
2016-07-19 17:21:32 -07:00
it "script function takes object when parameter is null" {
Invoke-ScriptFunctionTakesObject -Address $null | Should Be 42
}
it "script function takes unit64 when parameter is null" {
Invoke-ScriptFunctionTakesUInt64 -Address $null | Should Be 42
}
it "script csharp cmdlet takes object when parameter is null" {
Invoke-CSharpCmdletTakesObject -Address $null | Should Be 42
}
it "script csharp cmdlet takes uint64 when parameter is null" {
Invoke-CSharpCmdletTakesUInt64 -Address $null | Should Be 42
}
2016-07-19 13:52:03 -07:00
}