Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed numeric comparison with non-int #436 #447

Merged
merged 1 commit into from
Mar 22, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- General improvements:
- Added configuration option `Output.Culture` for setting culture. [#442](https://github.com/Microsoft/PSRule/issues/442)
- Improved handling of fields to allow the input object to be referenced with `.`. [#437](https://github.com/Microsoft/PSRule/issues/437)
- Bug fixes:
- Fixed numeric comparison assertion with non-int types. [#436](https://github.com/Microsoft/PSRule/issues/436)

## v0.15.0

Expand Down
16 changes: 8 additions & 8 deletions docs/concepts/PSRule/en-US/about_PSRule_Assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ Rule 'EndsWith' {
### Greater

The `Greater` assertion method checks the field value is greater than the specified value.
The field value can either be an integer or an array.
The field value can either be an integer, float, array, or string.
When the field value is:

- An integer, a numerical comparison is used.
- An integer or float, a numerical comparison is used.
- An array, the number of elements is compared.
- A string, the length of the string is compared.

Expand Down Expand Up @@ -175,10 +175,10 @@ Rule 'Greater' {
### GreaterOrEqual

The `GreaterOrEqual` assertion method checks the field value is greater or equal to the specified value.
The field value can either be an integer or an array.
The field value can either be an integer, float, array, or string.
When the field value is:

- An integer, a numerical comparison is used.
- An integer or float, a numerical comparison is used.
- An array, the number of elements is compared.
- A string, the length of the string is compared.

Expand Down Expand Up @@ -350,10 +350,10 @@ Rule 'JsonSchema' {
### Less

The `Less` assertion method checks the field value is less than the specified value.
The field value can either be an integer or an array.
The field value can either be an integer, float, array, or string.
When the field value is:

- An integer, a numerical comparison is used.
- An integer or float, a numerical comparison is used.
- An array, the number of elements is compared.
- A string, the length of the string is compared.

Expand Down Expand Up @@ -382,10 +382,10 @@ Rule 'Less' {
### LessOrEqual

The `LessOrEqual` assertion method checks the field value is less or equal to the specified value.
The field value can either be an integer or an array.
The field value can either be an integer, float, array, or string.
When the field value is:

- An integer, a numerical comparison is used.
- An integer or float, a numerical comparison is used.
- An array, the number of elements is compared.
- A string, the length of the string is compared.

Expand Down
59 changes: 51 additions & 8 deletions src/PSRule/Runtime/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ public AssertResult Greater(PSObject inputObject, string field, int value)
GuardField(inputObject, field, false, out object fieldValue, out result))
return result;

if (TryInt(fieldValue, out int actual) || TryStringLength(fieldValue, out actual) || TryArrayLength(fieldValue, out actual))
return actual > value ? Pass() : Fail(ReasonStrings.Greater, actual, value);
if (CompareNumeric(fieldValue, value, out int actual))
return actual > 0 ? Pass() : Fail(ReasonStrings.Greater, actual, value);

return Fail(ReasonStrings.Compare, fieldValue, value);
}
Expand All @@ -293,8 +293,8 @@ public AssertResult GreaterOrEqual(PSObject inputObject, string field, int value
GuardField(inputObject, field, false, out object fieldValue, out result))
return result;

if (TryInt(fieldValue, out int actual) || TryStringLength(fieldValue, out actual) || TryArrayLength(fieldValue, out actual))
return actual >= value ? Pass() : Fail(ReasonStrings.GreaterOrEqual, actual, value);
if (CompareNumeric(fieldValue, value, out int actual))
return actual >= 0 ? Pass() : Fail(ReasonStrings.GreaterOrEqual, actual, value);

return Fail(ReasonStrings.Compare, fieldValue, value);
}
Expand All @@ -307,8 +307,8 @@ public AssertResult Less(PSObject inputObject, string field, int value)
GuardField(inputObject, field, false, out object fieldValue, out result))
return result;

if (TryInt(fieldValue, out int actual) || TryStringLength(fieldValue, out actual) || TryArrayLength(fieldValue, out actual))
return actual < value ? Pass() : Fail(ReasonStrings.Less, actual, value);
if (CompareNumeric(fieldValue, value, out int actual))
return actual < 0 ? Pass() : Fail(ReasonStrings.Less, actual, value);

return Fail(ReasonStrings.Compare, fieldValue, value);
}
Expand All @@ -321,8 +321,8 @@ public AssertResult LessOrEqual(PSObject inputObject, string field, int value)
GuardField(inputObject, field, false, out object fieldValue, out result))
return result;

if (TryInt(fieldValue, out int actual) || TryStringLength(fieldValue, out actual) || TryArrayLength(fieldValue, out actual))
return actual <= value ? Pass() : Fail(ReasonStrings.LessOrEqual, actual, value);
if (CompareNumeric(fieldValue, value, out int actual))
return actual <= 0 ? Pass() : Fail(ReasonStrings.LessOrEqual, actual, value);

return Fail(ReasonStrings.Compare, fieldValue, value);
}
Expand Down Expand Up @@ -371,6 +371,28 @@ private static bool TryInt(object obj, out int value)
return false;
}

private static bool TryLong(object obj, out long value)
{
if (obj is long lvalue)
{
value = lvalue;
return true;
}
value = 0;
return false;
}

private static bool TryFloat(object obj, out float value)
{
if (obj is float fvalue)
{
value = fvalue;
return true;
}
value = 0;
return false;
}

private static bool TryStringLength(object obj, out int value)
{
if (obj is string s)
Expand All @@ -393,6 +415,27 @@ private static bool TryArrayLength(object obj, out int value)
return false;
}

private static bool CompareNumeric(object obj, int value, out int actual)
{
if (TryInt(obj, out int ivalue) || TryStringLength(obj, out ivalue) || TryArrayLength(obj, out ivalue))
{
actual = ivalue.CompareTo(value);
return true;
}
if (TryLong(obj, out long lvalue))
{
actual = lvalue.CompareTo(value);
return true;
}
if (TryFloat(obj, out float fvalue))
{
actual = fvalue.CompareTo(value);
return true;
}
actual = 0;
return false;
}

/// <summary>
/// Fails if the value is null.
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions src/PSRule/Runtime/AssertResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ public bool Complete()

// Continue
for (var i = 0; _Reason != null && i < _Reason.Count; i++)
{
RunspaceContext.CurrentThread.WriteReason(_Reason[i]);
}

return Result;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/PSRule.Tests/AssertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ public void Assertion()
Assert.Equal("Alternate reason", actual1.ToString());
}

[Fact]
public void WithinRollupBlock()
{
SetContext();
var assert = GetAssertionHelper();
var actual1 = PSRule.Runtime.RuleConditionResult.Create(new object[] { PSObject.AsPSObject(assert.Create(true, "Test reason")), PSObject.AsPSObject(assert.Create(false, "Test reason")) });
Assert.True(actual1.AnyOf());
Assert.False(actual1.AllOf());

var actual2 = PSRule.Runtime.RuleConditionResult.Create(new object[] { assert.Create(true, "Test reason"), assert.Create(false, "Test reason") });
Assert.True(actual2.AnyOf());
Assert.False(actual2.AllOf());
}

[Fact]
public void HasJsonSchema()
{
Expand Down Expand Up @@ -335,6 +349,7 @@ private static void SetContext()
{
var context = PipelineContext.New(new Configuration.PSRuleOption(), null, null, null, null);
context.ExecutionScope = ExecutionScope.Condition;
new RunspaceContext(context, null);
}

private static PSObject GetObject(params (string name, object value)[] properties)
Expand Down
2 changes: 2 additions & 0 deletions tests/PSRule.Tests/FromFile.Rule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ Rule 'AllOfTest' {
AllOf {
$True
$True
$Assert.Greater(3, '.', 2)
}
}

Expand All @@ -372,6 +373,7 @@ Rule 'AnyOfTest' {
$True
$False
$False
$Assert.Greater(3, '.', 2)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PSRule.Tests/PSRule.Assert.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Describe 'PSRule assertions' -Tag 'Assert' {
}

It 'With self field' {
$result = @($testObject | Invoke-PSRule -Path $ruleFilePath -Name 'Assert.Self' -Outcome All -WarningAction SilentlyContinue -Verbose);
$result = @($testObject | Invoke-PSRule -Path $ruleFilePath -Name 'Assert.Self' -Outcome All -WarningAction SilentlyContinue);
$result | Should -Not -BeNullOrEmpty;
$result.Length | Should -Be 2;

Expand Down