forked from ncalc/ncalc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support "Old" string concat behavior
Prior to v5.1, NCalc would treat + as string concatenation if the first argument was a string. Later versions changed this logic and while they did add options to control it, no combination of those options restored the old behavior. The Tech Software fork exists to allow callers to opt-in to the old behavior.
- Loading branch information
Showing
7 changed files
with
60 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace NCalc.Tests; | ||
|
||
[Trait("Category", "UseOldStringConcatBehavior")] | ||
public class UseOldStringConcatBehaviorTests | ||
{ | ||
[Theory] | ||
[InlineData("'to' + 'to'", "toto")] | ||
[InlineData("'one' + 2", "one2")] | ||
[InlineData("'one' + 2.1", "one2.1")] | ||
[InlineData("'1' + '2'", "12")] | ||
[InlineData("'1.1' + '2'", "1.12")] | ||
[InlineData("'1' + '2.2'", "12.2")] | ||
[InlineData("1 + 2", 3)] | ||
[InlineData("1.5 + 2.5", 4.0)] | ||
[InlineData("1 + '2'", 3.0)] | ||
[InlineData("1.5 + '2.5'", 4.0)] | ||
[InlineData("'10' + 'mA - ' + (10 + 20) + 'mA'", "10mA - 30mA")] | ||
public void ShouldUseStringConcatenationIfFirstValueIsAString(string expression, object expected) | ||
{ | ||
Assert.Equal(expected, new Expression(expression, ExpressionOptions.UseOldStringConcatBehavior).Evaluate()); | ||
} | ||
|
||
[Theory] | ||
[InlineData("2 + 'one'")] | ||
[InlineData("2.1 + 'one'")] | ||
public void ShouldThrowIfFirstValueIsNumericAndSecondIsNot(string expression) | ||
{ | ||
Assert.Throws<FormatException>(() | ||
=> new Expression(expression, ExpressionOptions.UseOldStringConcatBehavior).Evaluate()); | ||
} | ||
} |