Test frameworks consolidated (#12672)

This commit is contained in:
Davide Giacometti
2021-08-16 15:25:06 +02:00
committed by GitHub
parent c3a51f9227
commit e96c0da265
53 changed files with 1018 additions and 924 deletions

View File

@@ -2,23 +2,24 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using NUnit.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
{
[TestFixture]
[TestClass]
public class BracketHelperTests
{
[TestCase(null)]
[TestCase("")]
[TestCase("\t \r\n")]
[TestCase("none")]
[TestCase("()")]
[TestCase("(())")]
[TestCase("()()")]
[TestCase("(()())")]
[TestCase("([][])")]
[TestCase("([(()[])[](([]()))])")]
[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow("\t \r\n")]
[DataRow("none")]
[DataRow("()")]
[DataRow("(())")]
[DataRow("()()")]
[DataRow("(()())")]
[DataRow("([][])")]
[DataRow("([(()[])[](([]()))])")]
public void IsBracketComplete_TestValid_WhenCalled(string input)
{
// Arrange
@@ -30,11 +31,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.IsTrue(result);
}
[TestCase("((((", "only opening brackets")]
[TestCase("]]]", "only closing brackets")]
[TestCase("([)(])", "inner bracket mismatch")]
[TestCase(")(", "opening and closing reversed")]
[TestCase("(]", "mismatch in bracket type")]
[DataTestMethod]
[DataRow("((((", "only opening brackets")]
[DataRow("]]]", "only closing brackets")]
[DataRow("([)(])", "inner bracket mismatch")]
[DataRow(")(", "opening and closing reversed")]
[DataRow("(]", "mismatch in bracket type")]
public void IsBracketComplete_TestInvalid_WhenCalled(string input, string invalidReason)
{
// Arrange

View File

@@ -3,39 +3,43 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using NUnit.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
{
[TestFixture]
[TestClass]
public class ExtendedCalculatorParserTests
{
[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void InputValid_ThrowError_WhenCalledNullOrEmpty(string input)
{
// Act
Assert.Catch<ArgumentNullException>(() => CalculateHelper.InputValid(input));
Assert.ThrowsException<ArgumentNullException>(() => CalculateHelper.InputValid(input));
}
[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void Interpret_ThrowError_WhenCalledNullOrEmpty(string input)
{
// Arrange
var engine = new CalculateEngine();
// Act
Assert.Catch<ArgumentNullException>(() => engine.Interpret(input));
Assert.ThrowsException<ArgumentNullException>(() => engine.Interpret(input));
}
[TestCase("42")]
[TestCase("test")]
[TestCase("pi(2)")] // Incorrect input, constant is being treated as a function.
[TestCase("e(2)")]
[DataTestMethod]
[DataRow("42")]
[DataRow("test")]
[DataRow("pi(2)")] // Incorrect input, constant is being treated as a function.
[DataRow("e(2)")]
public void Interpret_NoResult_WhenCalled(string input)
{
// Arrange
@@ -48,25 +52,32 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.AreEqual(default(CalculateResult), result);
}
[TestCase("2 * 2", 4D)]
[TestCase("-2 ^ 2", 4D)]
[TestCase("-(2 ^ 2)", -4D)]
[TestCase("2 * pi", 6.28318530717959D)]
[TestCase("round(2 * pi)", 6D)]
[TestCase("1 == 2", default(double))]
[TestCase("pi * ( sin ( cos ( 2)))", -1.26995475603563D)]
[TestCase("5.6/2", 2.8D)]
[TestCase("123 * 4.56", 560.88D)]
[TestCase("1 - 9.0 / 10", 0.1D)]
[TestCase("0.5 * ((2*-395.2)+198.2)", -296.1D)]
[TestCase("2+2.11", 4.11D)]
[TestCase("8.43 + 4.43 - 12.86", 0D)]
[TestCase("8.43 + 4.43 - 12.8", 0.06D)]
[TestCase("exp(5)", 148.413159102577D)]
[TestCase("e^5", 148.413159102577D)]
[TestCase("e*2", 5.43656365691809D)]
[TestCase("log(e)", 1D)]
[TestCase("cosh(0)", 1D)]
private static IEnumerable<object[]> Interpret_NoErrors_WhenCalledWithRounding_Data =>
new[]
{
new object[] { "2 * 2", 4M },
new object[] { "-2 ^ 2", 4M },
new object[] { "-(2 ^ 2)", -4M },
new object[] { "2 * pi", 6.28318530717959M },
new object[] { "round(2 * pi)", 6M },
new object[] { "1 == 2", default(decimal) },
new object[] { "pi * ( sin ( cos ( 2)))", -1.26995475603563M },
new object[] { "5.6/2", 2.8M },
new object[] { "123 * 4.56", 560.88M },
new object[] { "1 - 9.0 / 10", 0.1M },
new object[] { "0.5 * ((2*-395.2)+198.2)", -296.1M },
new object[] { "2+2.11", 4.11M },
new object[] { "8.43 + 4.43 - 12.86", 0M },
new object[] { "8.43 + 4.43 - 12.8", 0.06M },
new object[] { "exp(5)", 148.413159102577M },
new object[] { "e^5", 148.413159102577M },
new object[] { "e*2", 5.43656365691809M },
new object[] { "log(e)", 1M },
new object[] { "cosh(0)", 1M },
};
[DataTestMethod]
[DynamicData(nameof(Interpret_NoErrors_WhenCalledWithRounding_Data))]
public void Interpret_NoErrors_WhenCalledWithRounding(string input, decimal expectedResult)
{
// Arrange
@@ -81,9 +92,16 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.AreEqual(CalculateEngine.Round(expectedResult), result.RoundedResult);
}
[TestCase("0.100000000000000000000", 0.00776627963145224D)] // BUG: Because data structure
[TestCase("0.200000000000000000000000", 0.000000400752841041379D)] // BUG: Because data structure
[TestCase("123 456", 56088D)] // BUG: Framework accepts ' ' as multiplication
private static IEnumerable<object[]> Interpret_QuirkOutput_WhenCalled_Data =>
new[]
{
new object[] { "0.100000000000000000000", 0.00776627963145224M }, // BUG: Because data structure
new object[] { "0.200000000000000000000000", 0.000000400752841041379M }, // BUG: Because data structure
new object[] { "123 456", 56088M }, // BUG: Framework accepts ' ' as multiplication
};
[DynamicData(nameof(Interpret_QuirkOutput_WhenCalled_Data))]
[DataTestMethod]
public void Interpret_QuirkOutput_WhenCalled(string input, decimal expectedResult)
{
// Arrange
@@ -98,9 +116,16 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.AreEqual(expectedResult, result.Result);
}
[TestCase("4.5/3", 1.5D, "nl-NL")]
[TestCase("4.5/3", 1.5D, "en-EN")]
[TestCase("4.5/3", 1.5D, "de-DE")]
private static IEnumerable<object[]> Interpret_DifferentCulture_WhenCalled_Data =>
new[]
{
new object[] { "4.5/3", 1.5M, "nl-NL" },
new object[] { "4.5/3", 1.5M, "en-EN" },
new object[] { "4.5/3", 1.5M, "de-DE" },
};
[DataTestMethod]
[DynamicData(nameof(Interpret_DifferentCulture_WhenCalled_Data))]
public void Interpret_DifferentCulture_WhenCalled(string input, decimal expectedResult, string cultureName)
{
// Arrange
@@ -115,23 +140,24 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.AreEqual(CalculateEngine.Round(expectedResult), result.RoundedResult);
}
[TestCase("ceil(2 * (pi ^ 2))", true)]
[TestCase("((1 * 2)", false)]
[TestCase("(1 * 2)))", false)]
[TestCase("abcde", false)]
[TestCase("1 + 2 +", false)]
[TestCase("1+2*", false)]
[TestCase("1 && 3 &&", false)]
[TestCase("sqrt( 36)", true)]
[TestCase("max 4", false)]
[TestCase("sin(0)", true)]
[TestCase("sinh(1)", true)]
[TestCase("tanh(0)", true)]
[TestCase("artanh(pi/2)", true)]
[TestCase("cosh", false)]
[TestCase("cos", false)]
[TestCase("abs", false)]
[TestCase("1+1.1e3", true)]
[DataTestMethod]
[DataRow("ceil(2 * (pi ^ 2))", true)]
[DataRow("((1 * 2)", false)]
[DataRow("(1 * 2)))", false)]
[DataRow("abcde", false)]
[DataRow("1 + 2 +", false)]
[DataRow("1+2*", false)]
[DataRow("1 && 3 &&", false)]
[DataRow("sqrt( 36)", true)]
[DataRow("max 4", false)]
[DataRow("sin(0)", true)]
[DataRow("sinh(1)", true)]
[DataRow("tanh(0)", true)]
[DataRow("artanh(pi/2)", true)]
[DataRow("cosh", false)]
[DataRow("cos", false)]
[DataRow("abs", false)]
[DataRow("1+1.1e3", true)]
public void InputValid_TestValid_WhenCalled(string input, bool valid)
{
// Arrange
@@ -143,9 +169,10 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.AreEqual(valid, result);
}
[TestCase("1-1")]
[TestCase("sin(0)")]
[TestCase("sinh(0)")]
[DataTestMethod]
[DataRow("1-1")]
[DataRow("sin(0)")]
[DataRow("sinh(0)")]
public void Interpret_MustReturnResult_WhenResultIsZero(string input)
{
// Arrange
@@ -157,14 +184,21 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(0.0, result.Result);
Assert.AreEqual(0.0M, result.Result);
}
[TestCase("factorial(5)", 120)]
[TestCase("sign(-2)", -1)]
[TestCase("sign(2)", +1)]
[TestCase("abs(-2)", 2)]
[TestCase("abs(2)", 2)]
private static IEnumerable<object[]> Interpret_MustReturnExpectedResult_WhenCalled_Data =>
new[]
{
new object[] { "factorial(5)", 120M },
new object[] { "sign(-2)", -1M },
new object[] { "sign(2)", +1M },
new object[] { "abs(-2)", 2M },
new object[] { "abs(2)", 2M },
};
[DataTestMethod]
[DynamicData(nameof(Interpret_MustReturnExpectedResult_WhenCalled_Data))]
public void Interpret_MustReturnExpectedResult_WhenCalled(string input, decimal expectedResult)
{
// Arrange

View File

@@ -10,9 +10,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.13.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.5" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.PowerToys.Run.Plugin.Calculator\Microsoft.PowerToys.Run.Plugin.Calculator.csproj" />

View File

@@ -4,15 +4,16 @@
using System;
using System.Globalization;
using NUnit.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
{
[TestFixture]
[TestClass]
public class NumberTranslatorTests
{
[TestCase(null, "en-US")]
[TestCase("de-DE", null)]
[DataTestMethod]
[DataRow(null, "en-US")]
[DataRow("de-DE", null)]
public void Create_ThrowError_WhenCalledNullOrEmpty(string sourceCultureName, string targetCultureName)
{
// Arrange
@@ -20,12 +21,13 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
CultureInfo targetCulture = targetCultureName != null ? new CultureInfo(targetCultureName) : null;
// Act
Assert.Catch<ArgumentNullException>(() => NumberTranslator.Create(sourceCulture, targetCulture));
Assert.ThrowsException<ArgumentNullException>(() => NumberTranslator.Create(sourceCulture, targetCulture));
}
[TestCase("en-US", "en-US")]
[TestCase("en-EN", "en-US")]
[TestCase("de-DE", "en-US")]
[DataTestMethod]
[DataRow("en-US", "en-US")]
[DataRow("en-EN", "en-US")]
[DataRow("de-DE", "en-US")]
public void Create_WhenCalled(string sourceCultureName, string targetCultureName)
{
// Arrange
@@ -39,18 +41,20 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.IsNotNull(translator);
}
[TestCase(null)]
[DataTestMethod]
[DataRow(null)]
public void Translate_ThrowError_WhenCalledNull(string input)
{
// Arrange
var translator = NumberTranslator.Create(new CultureInfo("de-DE"), new CultureInfo("en-US"));
// Act
Assert.Catch<ArgumentNullException>(() => translator.Translate(input));
Assert.ThrowsException<ArgumentNullException>(() => translator.Translate(input));
}
[TestCase("")]
[TestCase(" ")]
[DataTestMethod]
[DataRow("")]
[DataRow(" ")]
public void Translate_WhenCalledEmpty(string input)
{
// Arrange
@@ -63,11 +67,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.AreEqual(input, result);
}
[TestCase("2,0 * 2", "2.0 * 2")]
[TestCase("4 * 3,6 + 9", "4 * 3.6 + 9")]
[TestCase("5,2+6", "5.2+6")]
[TestCase("round(2,5)", "round(2.5)")]
[TestCase("3,3333", "3.3333")]
[DataTestMethod]
[DataRow("2,0 * 2", "2.0 * 2")]
[DataRow("4 * 3,6 + 9", "4 * 3.6 + 9")]
[DataRow("5,2+6", "5.2+6")]
[DataRow("round(2,5)", "round(2.5)")]
[DataRow("3,3333", "3.3333")]
public void Translate_NoErrors_WhenCalled(string input, string expectedResult)
{
// Arrange
@@ -81,11 +86,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.AreEqual(expectedResult, result);
}
[TestCase("2.0 * 2", "2,0 * 2")]
[TestCase("4 * 3.6 + 9", "4 * 3,6 + 9")]
[TestCase("5.2+6", "5,2+6")]
[TestCase("round(2.5)", "round(2,5)")]
[TestCase("3.3333", "3,3333")]
[DataTestMethod]
[DataRow("2.0 * 2", "2,0 * 2")]
[DataRow("4 * 3.6 + 9", "4 * 3,6 + 9")]
[DataRow("5.2+6", "5,2+6")]
[DataRow("round(2.5)", "round(2,5)")]
[DataRow("3.3333", "3,3333")]
public void TranslateBack_NoErrors_WhenCalled(string input, string expectedResult)
{
// Arrange
@@ -99,10 +105,11 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.AreEqual(expectedResult, result);
}
[TestCase(".", ",", "2,000,000", "2000000")]
[TestCase(".", ",", "2,000,000.6", "2000000.6")]
[TestCase(",", ".", "2.000.000", "2000000")]
[TestCase(",", ".", "2.000.000,6", "2000000.6")]
[DataTestMethod]
[DataRow(".", ",", "2,000,000", "2000000")]
[DataRow(".", ",", "2,000,000.6", "2000000.6")]
[DataRow(",", ".", "2.000.000", "2000000")]
[DataRow(",", ".", "2.000.000,6", "2000000.6")]
public void Translate_RemoveNumberGroupSeparator_WhenCalled(string decimalSeparator, string groupSeparator, string input, string expectedResult)
{
// Arrange