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

basic expander refactor #11089

Merged
merged 4 commits into from
Jan 17, 2025
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
59 changes: 30 additions & 29 deletions src/Build.UnitTests/Evaluation/ExpanderFunction_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
Expand All @@ -11,6 +11,7 @@

using Xunit;
using Xunit.Abstractions;
using ParseArgs = Microsoft.Build.Evaluation.Expander.ArgumentParser;

namespace Microsoft.Build.Engine.UnitTests.Evaluation
{
Expand All @@ -25,39 +26,39 @@ public class ExpanderFunction_Tests
[Fact]
public void TryConvertToIntGivenNull()
{
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(null, out int actual).ShouldBeFalse();
ParseArgs.TryConvertToInt(null, out int actual).ShouldBeFalse();
actual.ShouldBe(0);
}

[Fact]
public void TryConvertToIntGivenDouble()
{
const double value = 10.0;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue();
ParseArgs.TryConvertToInt(value, out int actual).ShouldBeTrue();
actual.ShouldBe(10);
}

[Fact]
public void TryConvertToIntGivenLong()
{
const long value = 10;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue();
ParseArgs.TryConvertToInt(value, out int actual).ShouldBeTrue();
actual.ShouldBe(10);
}

[Fact]
public void TryConvertToIntGivenInt()
{
const int value = 10;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue();
ParseArgs.TryConvertToInt(value, out int actual).ShouldBeTrue();
actual.ShouldBe(10);
}

[Fact]
public void TryConvertToIntGivenString()
{
const string value = "10";
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue();
ParseArgs.TryConvertToInt(value, out int actual).ShouldBeTrue();
actual.ShouldBe(10);
}

Expand All @@ -66,7 +67,7 @@ public void TryConvertToIntGivenDoubleWithIntMinValue()
{
const int expected = int.MinValue;
const double value = expected;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue();
ParseArgs.TryConvertToInt(value, out int actual).ShouldBeTrue();
actual.ShouldBe(expected);
}

Expand All @@ -75,31 +76,31 @@ public void TryConvertToIntGivenDoubleWithIntMaxValue()
{
const int expected = int.MaxValue;
const double value = expected;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue();
ParseArgs.TryConvertToInt(value, out int actual).ShouldBeTrue();
actual.ShouldBe(expected);
}

[Fact]
public void TryConvertToIntGivenDoubleWithLessThanIntMinValue()
{
const double value = int.MinValue - 1.0;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeFalse();
ParseArgs.TryConvertToInt(value, out int actual).ShouldBeFalse();
actual.ShouldBe(0);
}

[Fact]
public void TryConvertToIntGivenDoubleWithGreaterThanIntMaxValue()
{
const double value = int.MaxValue + 1.0;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeFalse();
ParseArgs.TryConvertToInt(value, out int actual).ShouldBeFalse();
actual.ShouldBe(0);
}

[Fact]
public void TryConvertToIntGivenLongWithGreaterThanIntMaxValue()
{
const long value = int.MaxValue + 1L;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeFalse();
ParseArgs.TryConvertToInt(value, out int actual).ShouldBeFalse();
actual.ShouldBe(0);
}

Expand All @@ -108,39 +109,39 @@ public void TryConvertToIntGivenLongWithGreaterThanIntMaxValue()
[Fact]
public void TryConvertToLongGivenNull()
{
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(null, out long actual).ShouldBeFalse();
ParseArgs.TryConvertToLong(null, out long actual).ShouldBeFalse();
actual.ShouldBe(0);
}

[Fact]
public void TryConvertToLongGivenDouble()
{
const double value = 10.0;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue();
ParseArgs.TryConvertToLong(value, out long actual).ShouldBeTrue();
actual.ShouldBe(10);
}

[Fact]
public void TryConvertToLongGivenLong()
{
const long value = 10;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue();
ParseArgs.TryConvertToLong(value, out long actual).ShouldBeTrue();
actual.ShouldBe(10);
}

[Fact]
public void TryConvertToLongGivenInt()
{
const int value = 10;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue();
ParseArgs.TryConvertToLong(value, out long actual).ShouldBeTrue();
actual.ShouldBe(10);
}

[Fact]
public void TryConvertToLongGivenString()
{
const string value = "10";
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue();
ParseArgs.TryConvertToLong(value, out long actual).ShouldBeTrue();
actual.ShouldBe(10);
}

Expand All @@ -149,7 +150,7 @@ public void TryConvertToLongGivenDoubleWithLongMinValue()
{
const long expected = long.MinValue;
const double value = expected;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue();
ParseArgs.TryConvertToLong(value, out long actual).ShouldBeTrue();
actual.ShouldBe(expected);
}

Expand All @@ -159,14 +160,14 @@ public void TryConvertToLongGivenDoubleWithLongMaxValueShouldNotThrow()
// An OverflowException should not be thrown from TryConvertToLong().
// Convert.ToInt64(double) has a defect and will throw an OverflowException
// for values >= (long.MaxValue - 511) and <= long.MaxValue.
_ = Should.NotThrow(() => Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong((double)long.MaxValue, out _));
_ = Should.NotThrow(() => ParseArgs.TryConvertToLong((double)long.MaxValue, out _));
}

[WindowsFullFrameworkOnlyFact]
public void TryConvertToLongGivenDoubleWithLongMaxValueFramework()
{
const long longMaxValue = long.MaxValue;
bool result = Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong((double)longMaxValue, out long actual);
bool result = ParseArgs.TryConvertToLong((double)longMaxValue, out long actual);

// Because of loss of precision, long.MaxValue will not 'round trip' from long to double to long.
result.ShouldBeFalse();
Expand All @@ -177,7 +178,7 @@ public void TryConvertToLongGivenDoubleWithLongMaxValueFramework()
public void TryConvertToLongGivenDoubleWithLongMaxValueDotNet()
{
const long longMaxValue = long.MaxValue;
bool result = Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong((double)longMaxValue, out long actual);
bool result = ParseArgs.TryConvertToLong((double)longMaxValue, out long actual);

// Testing on macOS 12 on Apple Silicon M1 Pro produces different result.
result.ShouldBeTrue();
Expand All @@ -192,23 +193,23 @@ public void TryConvertToLongGivenDoubleWithVeryLargeLongValue()
const long veryLargeLong = long.MaxValue - 512;
const double value = veryLargeLong;
const long expected = 9223372036854774784L;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue();
ParseArgs.TryConvertToLong(value, out long actual).ShouldBeTrue();
actual.ShouldBe(expected);
}

[Fact]
public void TryConvertToLongGivenDoubleWithLessThanLongMinValue()
{
const double value = -92233720368547758081D;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeFalse();
ParseArgs.TryConvertToLong(value, out long actual).ShouldBeFalse();
actual.ShouldBe(0);
}

[Fact]
public void TryConvertToLongGivenDoubleWithGreaterThanLongMaxValue()
{
const double value = (double)long.MaxValue + long.MaxValue;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeFalse();
ParseArgs.TryConvertToLong(value, out long actual).ShouldBeFalse();
actual.ShouldBe(0);
}

Expand All @@ -217,39 +218,39 @@ public void TryConvertToLongGivenDoubleWithGreaterThanLongMaxValue()
[Fact]
public void TryConvertToDoubleGivenNull()
{
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(null, out double actual).ShouldBeFalse();
ParseArgs.TryConvertToDouble(null, out double actual).ShouldBeFalse();
actual.ShouldBe(0);
}

[Fact]
public void TryConvertToDoubleGivenDouble()
{
const double value = 10.0;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(value, out double actual).ShouldBeTrue();
ParseArgs.TryConvertToDouble(value, out double actual).ShouldBeTrue();
actual.ShouldBe(10.0);
}

[Fact]
public void TryConvertToDoubleGivenLong()
{
const long value = 10;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(value, out double actual).ShouldBeTrue();
ParseArgs.TryConvertToDouble(value, out double actual).ShouldBeTrue();
actual.ShouldBe(10.0);
}

[Fact]
public void TryConvertToDoubleGivenInt()
{
const int value = 10;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(value, out double actual).ShouldBeTrue();
ParseArgs.TryConvertToDouble(value, out double actual).ShouldBeTrue();
actual.ShouldBe(10.0);
}

[Fact]
public void TryConvertToDoubleGivenString()
{
const string value = "10";
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(value, out double actual).ShouldBeTrue();
ParseArgs.TryConvertToDouble(value, out double actual).ShouldBeTrue();
actual.ShouldBe(10.0);
}

Expand All @@ -267,7 +268,7 @@ public void TryConvertToDoubleGivenStringAndLocale()
// The invariant culture should be used and "1,2" should be 12.0 not 1.2.
var cultureEnglishSouthAfrica = CultureInfo.CreateSpecificCulture("en-ZA");
currentThread.CurrentCulture = cultureEnglishSouthAfrica;
Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(value, out double actual).ShouldBeTrue();
ParseArgs.TryConvertToDouble(value, out double actual).ShouldBeTrue();
actual.ShouldBe(12.0);
}
finally
Expand Down
Loading