-
-
Notifications
You must be signed in to change notification settings - Fork 615
Expand file tree
/
Copy pathCalculatorTest.cs
More file actions
203 lines (174 loc) · 7.45 KB
/
Copy pathCalculatorTest.cs
File metadata and controls
203 lines (174 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Reflection;
using Flow.Launcher.Plugin.Calculator;
using Flow.Launcher.Plugin.Calculator.Storage;
using Mages.Core;
using NUnit.Framework;
using NUnit.Framework.Legacy;
namespace Flow.Launcher.Test.Plugins
{
[TestFixture]
public class CalculatorPluginTest
{
private readonly Main _plugin;
private readonly Settings _settings = new()
{
DecimalSeparator = DecimalSeparator.UseSystemLocale,
MaxDecimalPlaces = 10,
ShowErrorMessage = false, // Make sure we return the empty results when error occurs
UseThousandsSeparator = true, // Default value
EnableHistory = true,
};
private readonly Engine _engine = new(new Configuration
{
Scope = new Dictionary<string, object>
{
{ "e", Math.E }, // e is not contained in the default mages engine
}
});
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, _settings);
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, _engine);
SetHistory(new History());
}
[SetUp]
public void SetUp()
{
SetHistory(new History());
}
[Test]
public void ThousandsSeparatorTest_Enabled()
{
_settings.UseThousandsSeparator = true;
_settings.DecimalSeparator = DecimalSeparator.Dot;
var result = GetCalculationResult("1000+234");
// When thousands separator is enabled, the result should contain a separator
// Since decimal separator is dot, thousands separator should be comma
ClassicAssert.AreEqual("1,234", result);
_settings.DecimalSeparator = DecimalSeparator.Comma;
var result2 = GetCalculationResult("1000+234");
// When thousands separator is enabled, the result should contain a separator
// Since decimal separator is comma, thousands separator should be dot
ClassicAssert.AreEqual("1.234", result2);
}
[Test]
public void ThousandsSeparatorTest_Disabled()
{
_settings.UseThousandsSeparator = false;
_settings.DecimalSeparator = DecimalSeparator.UseSystemLocale;
var result = GetCalculationResult("1000+234");
ClassicAssert.AreEqual("1234", result);
}
[Test]
public void ThousandsSeparatorTest_LargeNumber()
{
_settings.UseThousandsSeparator = false;
_settings.DecimalSeparator = DecimalSeparator.UseSystemLocale;
var result = GetCalculationResult("1000000+234567");
ClassicAssert.AreEqual("1234567", result);
}
[Test]
public void CalculationHistory_IsStoredWhenEnabled()
{
_settings.EnableHistory = true;
_plugin.Query(new Plugin.Query { Search = "1+1" });
WaitForHistoryDebounce();
ClassicAssert.AreEqual(1, GetHistory().Items.Count);
ClassicAssert.AreEqual("1+1", GetHistory().Items[0].Query);
ClassicAssert.AreEqual("2", GetHistory().Items[0].CopyText);
ClassicAssert.IsNotEmpty(GetHistory().Items[0].SubTitle);
}
[Test]
public void CalculationHistory_IsNotReturnedWhenDisabled()
{
_settings.EnableHistory = true;
_plugin.Query(new Plugin.Query { Search = "1+1" });
WaitForHistoryDebounce();
_settings.EnableHistory = false;
var results = _plugin.Query(new Plugin.Query { Search = "2+2" });
ClassicAssert.AreEqual(1, results.Count);
ClassicAssert.AreEqual("4", results[0].Title);
}
// 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.0000000000")]
[TestCase(@"cos(0)", "1")]
[TestCase(@"tan(0)", "0")]
[TestCase(@"log10(100)", "2")]
[TestCase(@"log(100)", "2")]
[TestCase(@"log2(8)", "3")]
[TestCase(@"ln(e)", "1")]
[TestCase(@"abs(-5)", "5")]
// Constants
[TestCase(@"pi", "3.1415926536")]
// Complex expressions
[TestCase(@"(2+3)*sqrt(16)-log(100)/ln(e)", "18")]
[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)
{
_settings.UseThousandsSeparator = false;
_settings.DecimalSeparator = DecimalSeparator.Dot;
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;
}
private void WaitForHistoryDebounce()
{
var flushPendingItemMethod = typeof(History).GetMethod("FlushPendingItem", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.That(flushPendingItemMethod, Is.Not.Null,
"Could not find method 'FlushPendingItem' on Flow.Launcher.Plugin.Calculator.Storage.History");
flushPendingItemMethod!.Invoke(GetHistory(), null);
}
private History GetHistory()
{
var historyProperty = typeof(Main).GetProperty("History", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.That(historyProperty, Is.Not.Null,
"Could not find property 'History' on Flow.Launcher.Plugin.Calculator.Main");
var history = historyProperty!.GetValue(_plugin) as History;
Assert.That(history, Is.Not.Null,
"Property 'History' on Flow.Launcher.Plugin.Calculator.Main' was not a calculator History instance");
return history!;
}
private void SetHistory(History history)
{
var historyProperty = typeof(Main).GetProperty("History", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.That(historyProperty, Is.Not.Null,
"Could not find property 'History' on Flow.Launcher.Plugin.Calculator.Main");
historyProperty!.SetValue(_plugin, history);
}
}
}