8000 Add support for ValidateRangeKind to ParameterMetadata.GetProxyAttributeData by indented-automation · Pull Request #9059 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion src/System.Management.Automation/engine/Attributes.cs
< 8000 template class="js-line-alert-template">
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,12 @@ public sealed class ValidateRangeAttribute : ValidateEnumeratedArgumentsAttribut
/// </summary>
private Type _promotedType;

ValidateRangeKind? _rangeKind;
/// <summary>
/// Gets the name of the predefined range.
/// </summary>
internal ValidateRangeKind? RangeKind { get => _rangeKind; }

private ValidateRangeKind? _rangeKind;

/// <summary>
/// Validates that each parameter argument falls in the range
Expand Down
44 changes: 31 additions & 13 deletions src/System.Management.Automation/engine/TypeMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ internal bool IsMatchingType(PSTypeName psTypeName)
private const string ParameterSetNameFormat = "ParameterSetName='{0}'";
private const string AliasesFormat = @"{0}[Alias({1})]";
private const string ValidateLengthFormat = @"{0}[ValidateLength({1}, {2})]";
private const string ValidateRangeRangeKindFormat = @"{0}[ValidateRange([System.Management.Automation.ValidateRangeKind]::{1})]";
private const string ValidateRangeFloatFormat = @"{0}[ValidateRange({1:R}, {2:R})]";
private const string ValidateRangeFormat = @"{0}[ValidateRange({1}, {2})]";
private const string ValidatePatternFormat = "{0}[ValidatePattern('{1}')]";
Expand Down Expand Up @@ -899,31 +900,48 @@ private string GetProxyAttributeData(Attribute attrib, string prefix)
ValidateLengthAttribute validLengthAttrib = attrib as ValidateLengthAttribute;
if (validLengthAttrib != null)
{
result = string.Format(CultureInfo.InvariantCulture,
result = string.Format(
CultureInfo.InvariantCulture,
ValidateLengthFormat, prefix,
validLengthAttrib.MinLength, validLengthAttrib.MaxLength);
validLengthAttrib.MinLength,
validLengthAttrib.MaxLength);
return result;
}

ValidateRangeAttribute validRangeAttrib = attrib as ValidateRangeAttribute;
if (validRangeAttrib != null)
{
Type rangeType = validRangeAttrib.MinRange.GetType();
string format;

if (rangeType == typeof(float) || rangeType == typeof(double))
if (validRangeAttrib.RangeKind.HasValue)
{
format = ValidateRangeFloatFormat;
result = string.Format(
CultureInfo.InvariantCulture,
ValidateRangeRangeKindFormat,
prefix,
validRangeAttrib.RangeKind.ToString());
return result;
}
else
{
format = ValidateRangeFormat;
}
Type rangeType = validRangeAttrib.MinRange.GetType();
string format;

result = string.Format(CultureInfo.InvariantCulture,
format, prefix,
validRangeAttrib.MinRange, validRangeAttrib.MaxRange);
return result;
if (rangeType == typeof(float) || rangeType == typeof(double))
{
format = ValidateRangeFloatFormat;
}
else
{
format = ValidateRangeFormat;
}

result = string.Format(
CultureInfo.InvariantCulture,
format,
prefix,
validRangeAttrib.MinRange,
validRangeAttrib.MaxRange);
return result;
}
}

AllowNullAttribute allowNullAttrib = attrib as AllowNullAttribute;
Expand Down
60 changes: 60 additions & 0 deletions test/powershell/engine/Basic/ProxyCommand.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Describe 'ProxyCommand Tests' -Tag 'CI' {
BeforeAll {
$testCases = @(
@{ Name = 'ValidateLengthAttribute'; ParamBlock = '[ValidateLength(1, 10)][int]${Parameter}' }
@{ Name = 'ValidateRangeAttribute with Minimum and Maximum'; ParamBlock = '[ValidateRange(1, 10)][int]${Parameter}' }
@{ Name = 'ValidateRangeAttribute with RangeKind'; ParamBlock = '[ValidateRange([System.Management.Automation.ValidateRangeKind]::Positive)][int]${Parameter}' }
@{ Name = 'AllowNullAttribute'; ParamBlock = '[AllowNull()][int]${Parameter}' }
@{ Name = 'AllowEmptyStringAttribute'; ParamBlock = '[AllowEmptyString()][int]${Parameter}' }
@{ Name = 'AllowEmptyCollectionAttribute'; ParamBlock = '[AllowEmptyCollection()][int]${Parameter}' }
@{ Name = 'ValidatePatternAttribute'; ParamBlock = '[ValidatePattern(''.*'')][int]${Parameter}' }
@{ Name = 'ValidateCountAttribute'; ParamBlock = '[ValidateCount(1, 10)][int]${Parameter}' }
@{ Name = 'ValidateNotNullAttribute'; ParamBlock = '[ValidateNotNull()][int]${Parameter}' }
@{ Name = 'ValidateNotNullOrEmptyAttribute'; ParamBlock = '[ValidateNotNullOrEmpty()][int]${Parameter}' }
@{ Name = 'ValidateSetAttribute with explicit set'; ParamBlock = '[ValidateSet(''1'',''10'')][int]${Parameter}' }
@{ Name = 'PSTypeNameAttribute'; ParamBlock = '[PSTypeName(''TypeName'')][int]${Parameter}' }
)
}

Context 'GetParamBlock method' {
AfterAll {
Remove-Item function:testProxyCommandFunction -ErrorAction SilentlyContinue
}

It 'Generates a param block when <Name> is used' -TestCases $testCases {
param (
$Name,
$ParamBlock
)

$functionDefinition = 'param ( {0} )' -f $ParamBlock
Set-Item -Path function:testProxyCommandFunction -Value $functionDefinition

$generatedParamBlock = [System.Management.Automation.ProxyCommand]::GetParamBlock(
(Get-Command testProxyCommandFunction)
)
$generatedParamBlock = $generatedParamBlock -split '\r?\n' -replace '^ *' -join ''

$generatedParamBlock | Should -Be $ParamBlock
}

It 'Generates a param block when ValidateScriptAttribute is used' {
param (
$Name,
$ParamBlock
)

$functionDefinition = 'param ( [ValidateScript({ $true })][int]${Parameter} )'
Set-Item -Path function:testProxyCommandFunction -Value $functionDefinition
$generatedParamBlock = [System.Management.Automation.ProxyCommand]::GetParamBlock(
(Get-Command testProxyCommandFunction)
)
$generatedParamBlock = $generatedParamBlock -split '\r?\n' -replace '^ *' -join ''

$generatedParamBlock | Should -Be '[ValidateScript({ $true })][int]${Parameter}'
}
}
}
0