Skip to content

Commit a08d7cc

Browse files
Jack251970jjw24
authored andcommitted
Merge pull request #3971 from dcog989/calculator-min-fix
Backout 'smart' digit separation & Fix Mages functions for Calculator plugin
1 parent af92e74 commit a08d7cc

File tree

10 files changed

+397
-181
lines changed

10 files changed

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

Plugins/Flow.Launcher.Plugin.Calculator/Languages/en.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:system="clr-namespace:System;assembly=mscorlib">
55

66
<system:String x:Key="flowlauncher_plugin_calculator_plugin_name">Calculator</system:String>
7-
<system:String x:Key="flowlauncher_plugin_calculator_plugin_description">Perform mathematical calculations (including hexadecimal values). Use ',' or '.' as thousand separator or decimal place.</system:String>
7+
<system:String x:Key="flowlauncher_plugin_calculator_plugin_description">Perform mathematical calculations, including hex values and advanced functions such as 'min(1,2,3)', 'sqrt(123)' and 'cos(123)'.</system:String>
88
<system:String x:Key="flowlauncher_plugin_calculator_not_a_number">Not a number (NaN)</system:String>
99
<system:String x:Key="flowlauncher_plugin_calculator_expression_not_complete">Expression wrong or incomplete (Did you forget some parentheses?)</system:String>
1010
<system:String x:Key="flowlauncher_plugin_calculator_copy_number_to_clipboard">Copy this number to the clipboard</system:String>
@@ -15,4 +15,5 @@
1515
<system:String x:Key="flowlauncher_plugin_calculator_decimal_separator_dot">Dot (.)</system:String>
1616
<system:String x:Key="flowlauncher_plugin_calculator_max_decimal_places">Max. decimal places</system:String>
1717
<system:String x:Key="flowlauncher_plugin_calculator_failed_to_copy">Copy failed, please try later</system:String>
18+
<system:String x:Key="flowlauncher_plugin_calculator_show_error_message">Show error message when calculation fails</system:String>
1819
</ResourceDictionary>

0 commit comments

Comments
 (0)