Skip to content

Commit

Permalink
#491 - Add Temperature and Mass Units support to ToNumber function. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 authored Feb 15, 2022
1 parent b46dbb7 commit 920cce5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,9 @@ public virtual ResultTypes Analyze(ToNumber exp)
{
ResultTypes.Undefined or
ResultTypes.AngleNumber or
ResultTypes.PowerNumber
ResultTypes.PowerNumber or
ResultTypes.TemperatureNumber or
ResultTypes.MassNumber
=> ResultTypes.Number,
_ => ResultTypes.AngleNumber.ThrowFor(result),
};
Expand Down
2 changes: 2 additions & 0 deletions xFunc.Maths/Expressions/Units/ToNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public override object Execute(ExpressionParameters? parameters)
{
AngleValue angleValue => angleValue.Angle,
PowerValue powerValue => powerValue.Value,
TemperatureValue temperatureValue => temperatureValue.Value,
MassValue massValue => massValue.Value,
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
12 changes: 12 additions & 0 deletions xFunc.Tests/Analyzers/TypeAnalyzerTests/StandardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,18 @@ public void TestPowerToNumber()
Test(new ToNumber(PowerValue.Watt(10).AsExpression()), ResultTypes.Number);
}

[Fact]
public void TestTemperatureToNumber()
{
Test(new ToNumber(TemperatureValue.Celsius(10).AsExpression()), ResultTypes.Number);
}

[Fact]
public void TestMassToNumber()
{
Test(new ToNumber(MassValue.Gram(10).AsExpression()), ResultTypes.Number);
}

[Fact]
public void TestToNumberException()
{
Expand Down
20 changes: 20 additions & 0 deletions xFunc.Tests/Expressions/Units/ToNumberTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ public void ExecutePowerTest()
Assert.Equal(expected, actual);
}

[Fact]
public void ExecuteTemperatureTest()
{
var exp = new ToNumber(TemperatureValue.Celsius(10).AsExpression());
var actual = exp.Execute();
var expected = new NumberValue(10.0);

Assert.Equal(expected, actual);
}

[Fact]
public void ExecuteMassTest()
{
var exp = new ToNumber(MassValue.Gram(10).AsExpression());
var actual = exp.Execute();
var expected = new NumberValue(10.0);

Assert.Equal(expected, actual);
}

[Fact]
public void ExecuteBoolTest()
{
Expand Down

0 comments on commit 920cce5

Please sign in to comment.