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