Skip to content

Commit

Permalink
basic refactor of expander
Browse files Browse the repository at this point in the history
  • Loading branch information
SimaTian committed Dec 16, 2024
1 parent 3a63bca commit 9a3b07b
Show file tree
Hide file tree
Showing 5 changed files with 1,443 additions and 1,360 deletions.
58 changes: 30 additions & 28 deletions src/Build.UnitTests/Evaluation/ExpanderFunction_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

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


namespace Microsoft.Build.Engine.UnitTests.Evaluation
{
Expand All @@ -25,39 +27,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 +68,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 +77,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 +110,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 +151,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 +161,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 +179,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 +194,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 +219,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 +269,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

0 comments on commit 9a3b07b

Please sign in to comment.