mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Add calculator unit testing
This commit is contained in:
parent
684fafdfdd
commit
6841ad5410
3 changed files with 89 additions and 1 deletions
|
|
@ -39,6 +39,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Calculator\Flow.Launcher.Plugin.Calculator.csproj" />
|
||||
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Explorer\Flow.Launcher.Plugin.Explorer.csproj" />
|
||||
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Program\Flow.Launcher.Plugin.Program.csproj" />
|
||||
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Url\Flow.Launcher.Plugin.Url.csproj" />
|
||||
|
|
|
|||
86
Flow.Launcher.Test/Plugins/CalculatorTest.cs
Normal file
86
Flow.Launcher.Test/Plugins/CalculatorTest.cs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Flow.Launcher.Plugin.Calculator;
|
||||
using Mages.Core;
|
||||
using NUnit.Framework;
|
||||
using NUnit.Framework.Legacy;
|
||||
|
||||
namespace Flow.Launcher.Test.Plugins
|
||||
{
|
||||
[TestFixture]
|
||||
public class CalculatorPluginTest
|
||||
{
|
||||
private readonly Main _plugin;
|
||||
|
||||
public CalculatorPluginTest()
|
||||
{
|
||||
_plugin = new Main();
|
||||
|
||||
var settingField = typeof(Main).GetField("_settings", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (settingField == null)
|
||||
Assert.Fail("Could not find field '_settings' on Flow.Launcher.Plugin.Calculator.Main");
|
||||
settingField.SetValue(_plugin, new Settings
|
||||
{
|
||||
ShowErrorMessage = false // Make sure we return the empty results when error occurs
|
||||
});
|
||||
|
||||
var engineField = typeof(Main).GetField("MagesEngine", BindingFlags.NonPublic | BindingFlags.Static);
|
||||
if (engineField == null)
|
||||
Assert.Fail("Could not find static field 'MagesEngine' on Flow.Launcher.Plugin.Calculator.Main");
|
||||
engineField.SetValue(null, new Engine(new Configuration
|
||||
{
|
||||
Scope = new Dictionary<string, object>
|
||||
{
|
||||
{ "e", Math.E }, // e is not contained in the default mages engine
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
// Basic operations
|
||||
[TestCase(@"1+1", "2")]
|
||||
[TestCase(@"2-1", "1")]
|
||||
[TestCase(@"2*2", "4")]
|
||||
[TestCase(@"4/2", "2")]
|
||||
[TestCase(@"2^3", "8")]
|
||||
// Decimal places
|
||||
[TestCase(@"10/3", "3.3333333333")]
|
||||
// Parentheses
|
||||
[TestCase(@"(1+2)*3", "9")]
|
||||
[TestCase(@"2^(1+2)", "8")]
|
||||
// Functions
|
||||
[TestCase(@"pow(2,3)", "8")]
|
||||
[TestCase(@"min(1,-1,-2)", "-2")]
|
||||
[TestCase(@"max(1,-1,-2)", "1")]
|
||||
[TestCase(@"sqrt(16)", "4")]
|
||||
[TestCase(@"sin(pi)", "0")]
|
||||
[TestCase(@"cos(0)", "1")]
|
||||
[TestCase(@"tan(0)", "0")]
|
||||
[TestCase(@"log(100)", "2")]
|
||||
[TestCase(@"ln(e)", "1")]
|
||||
[TestCase(@"abs(-5)", "5")]
|
||||
// Constants
|
||||
[TestCase(@"pi", "3.1415926536")]
|
||||
// Complex expressions
|
||||
[TestCase(@"(2+3)*sqrt(16)-log(100)/ln(e)", "19")]
|
||||
[TestCase(@"sin(pi/2)+cos(0)+tan(0)", "2")]
|
||||
// Error handling (should return empty result)
|
||||
[TestCase(@"10/0", "")]
|
||||
[TestCase(@"sqrt(-1)", "")]
|
||||
[TestCase(@"log(0)", "")]
|
||||
[TestCase(@"invalid_expression", "")]
|
||||
public void CalculatorTest(string expression, string result)
|
||||
{
|
||||
ClassicAssert.AreEqual(GetCalculationResult(expression), result);
|
||||
}
|
||||
|
||||
private string GetCalculationResult(string expression)
|
||||
{
|
||||
var results = _plugin.Query(new Plugin.Query()
|
||||
{
|
||||
Search = expression
|
||||
});
|
||||
return results.Count > 0 ? results[0].Title : string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -112,7 +112,8 @@ namespace Flow.Launcher.Plugin.Calculator
|
|||
Title = newResult,
|
||||
IcoPath = IcoPath,
|
||||
Score = 300,
|
||||
SubTitle = Localize.flowlauncher_plugin_calculator_copy_number_to_clipboard(),
|
||||
// Check context nullability for unit testing
|
||||
SubTitle = Context == null ? string.Empty : Localize.flowlauncher_plugin_calculator_copy_number_to_clipboard(),
|
||||
CopyText = newResult,
|
||||
Action = c =>
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue