Skip to content

Commit 6841ad5

Browse files
committed
Add calculator unit testing
1 parent 684fafd commit 6841ad5

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

Flow.Launcher.Test/Flow.Launcher.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
</ItemGroup>
4040

4141
<ItemGroup>
42+
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Calculator\Flow.Launcher.Plugin.Calculator.csproj" />
4243
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Explorer\Flow.Launcher.Plugin.Explorer.csproj" />
4344
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Program\Flow.Launcher.Plugin.Program.csproj" />
4445
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Url\Flow.Launcher.Plugin.Url.csproj" />
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using Flow.Launcher.Plugin.Calculator;
5+
using Mages.Core;
6+
using NUnit.Framework;
7+
using NUnit.Framework.Legacy;
8+
9+
namespace Flow.Launcher.Test.Plugins
10+
{
11+
[TestFixture]
12+
public class CalculatorPluginTest
13+
{
14+
private readonly Main _plugin;
15+
16+
public CalculatorPluginTest()
17+
{
18+
_plugin = new Main();
19+
20+
var settingField = typeof(Main).GetField("_settings", BindingFlags.NonPublic | BindingFlags.Instance);
21+
if (settingField == null)
22+
Assert.Fail("Could not find field '_settings' on Flow.Launcher.Plugin.Calculator.Main");
23+
settingField.SetValue(_plugin, new Settings
24+
{
25+
ShowErrorMessage = false // Make sure we return the empty results when error occurs
26+
});
27+
28+
var engineField = typeof(Main).GetField("MagesEngine", BindingFlags.NonPublic | BindingFlags.Static);
29+
if (engineField == null)
30+
Assert.Fail("Could not find static field 'MagesEngine' on Flow.Launcher.Plugin.Calculator.Main");
31+
engineField.SetValue(null, new Engine(new Configuration
32+
{
33+
Scope = new Dictionary<string, object>
34+
{
35+
{ "e", Math.E }, // e is not contained in the default mages engine
36+
}
37+
}));
38+
}
39+
40+
// Basic operations
41+
[TestCase(@"1+1", "2")]
42+
[TestCase(@"2-1", "1")]
43+
[TestCase(@"2*2", "4")]
44+
[TestCase(@"4/2", "2")]
45+
[TestCase(@"2^3", "8")]
46+
// Decimal places
47+
[TestCase(@"10/3", "3.3333333333")]
48+
// Parentheses
49+
[TestCase(@"(1+2)*3", "9")]
50+
[TestCase(@"2^(1+2)", "8")]
51+
// Functions
52+
[TestCase(@"pow(2,3)", "8")]
53+
[TestCase(@"min(1,-1,-2)", "-2")]
54+
[TestCase(@"max(1,-1,-2)", "1")]
55+
[TestCase(@"sqrt(16)", "4")]
56+
[TestCase(@"sin(pi)", "0")]
57+
[TestCase(@"cos(0)", "1")]
58+
[TestCase(@"tan(0)", "0")]
59+
[TestCase(@"log(100)", "2")]
60+
[TestCase(@"ln(e)", "1")]
61+
[TestCase(@"abs(-5)", "5")]
62+
// Constants
63+
[TestCase(@"pi", "3.1415926536")]
64+
// Complex expressions
65+
[TestCase(@"(2+3)*sqrt(16)-log(100)/ln(e)", "19")]
66+
[TestCase(@"sin(pi/2)+cos(0)+tan(0)", "2")]
67+
// Error handling (should return empty result)
68+
[TestCase(@"10/0", "")]
69+
[TestCase(@"sqrt(-1)", "")]
70+
[TestCase(@"log(0)", "")]
71+
[TestCase(@"invalid_expression", "")]
72+
public void CalculatorTest(string expression, string result)
73+
{
74+
ClassicAssert.AreEqual(GetCalculationResult(expression), result);
75+
}
76+
77+
private string GetCalculationResult(string expression)
78+
{
79+
var results = _plugin.Query(new Plugin.Query()
80+
{
81+
Search = expression
82+
});
83+
return results.Count > 0 ? results[0].Title : string.Empty;
84+
}
85+
}
86+
}

Plugins/Flow.Launcher.Plugin.Calculator/Main.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public List<Result> Query(Query query)
112112
Title = newResult,
113113
IcoPath = IcoPath,
114114
Score = 300,
115-
SubTitle = Localize.flowlauncher_plugin_calculator_copy_number_to_clipboard(),
115+
// Check context nullability for unit testing
116+
SubTitle = Context == null ? string.Empty : Localize.flowlauncher_plugin_calculator_copy_number_to_clipboard(),
116117
CopyText = newResult,
117118
Action = c =>
118119
{

0 commit comments

Comments
 (0)