|
| 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 | +} |
0 commit comments