From 7e9a96c762bc2e2c025bff00b76713642f5cb867 Mon Sep 17 00:00:00 2001 From: John Andrews Date: Tue, 16 Apr 2024 14:39:19 +1200 Subject: [PATCH] added true|false matching to matches --- BasicNodes/Functions/Matches.cs | 32 ++++++++++--- BasicNodes/Tests/MatchesTests.cs | 82 ++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 6 deletions(-) diff --git a/BasicNodes/Functions/Matches.cs b/BasicNodes/Functions/Matches.cs index 3f38c239..524d9c8e 100644 --- a/BasicNodes/Functions/Matches.cs +++ b/BasicNodes/Functions/Matches.cs @@ -45,27 +45,47 @@ public class Matches : Node output++; try { - var value = args.ReplaceVariables(match.Key, stripMissing: true); + object value; + if (Regex.IsMatch(match.Key, @"\{[\w\d\.-]+\}") && + args.Variables.TryGetValue(match.Key[1..^1], out var varValue)) + value = varValue; + else + value = args.ReplaceVariables(match.Key, stripMissing: true); + string strValue = value?.ToString() ?? string.Empty; + if (GeneralHelper.IsRegex(match.Value)) { - if (Regex.IsMatch(value, match.Value, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)) + if (Regex.IsMatch(strValue, match.Value, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)) { args.Logger?.ILog($"Match found '{match.Value}' = {value}"); return output; } } - if (match.Value == value) + if (Regex.IsMatch(match.Value ??string.Empty, "^(true|1)$", RegexOptions.IgnoreCase) && + Regex.IsMatch(strValue, "^(true|1)$", RegexOptions.IgnoreCase)) { - args.Logger?.ILog($"Match found '{match.Value}' = {value}"); + args.Logger?.ILog($"Match found '{match.Value}' = {strValue}"); + return output; + } + if (Regex.IsMatch(match.Value ??string.Empty, "^(false|0)$", RegexOptions.IgnoreCase) && + Regex.IsMatch(strValue, "^(false|0)$", RegexOptions.IgnoreCase)) + { + args.Logger?.ILog($"Match found '{match.Value}' = {strValue}"); + return output; + } + + if (match.Value == strValue) + { + args.Logger?.ILog($"Match found '{match.Value}' = {strValue}"); return output; } if (MathHelper.IsMathOperation(match.Value)) { - if (MathHelper.IsTrue(value, match.Value)) + if (MathHelper.IsTrue(strValue, match.Value)) { - args.Logger?.ILog($"Match found '{match.Value}' = {value}"); + args.Logger?.ILog($"Match found '{match.Value}' = {strValue}"); return output; } } diff --git a/BasicNodes/Tests/MatchesTests.cs b/BasicNodes/Tests/MatchesTests.cs index 3a29fb95..6bbef868 100644 --- a/BasicNodes/Tests/MatchesTests.cs +++ b/BasicNodes/Tests/MatchesTests.cs @@ -86,6 +86,88 @@ public class MatchesTests var log = logger.ToString(); Assert.AreEqual(2, result); } + + [TestMethod] + public void Matches_True() + { + var logger = new TestLogger(); + Matches ele = new (); + ele.MatchConditions = new() + { + new("{file.Name}", "triggerthis"), + new("{file.Deleted}", "true"), + new("{file.Name}", "TriggerThis"), + }; + var args = new FileFlows.Plugin.NodeParameters(null, logger, + false, string.Empty, new LocalFileService()); + args.Variables["file.Deleted"] = true; + + var result = ele.Execute(args); + var log = logger.ToString(); + Assert.AreEqual(2, result); + } + + + [TestMethod] + public void Matches_True_1() + { + var logger = new TestLogger(); + Matches ele = new (); + ele.MatchConditions = new() + { + new("{file.Name}", "triggerthis"), + new("{file.Deleted}", "true"), + new("{file.Name}", "TriggerThis"), + }; + var args = new FileFlows.Plugin.NodeParameters(null, logger, + false, string.Empty, new LocalFileService()); + args.Variables["file.Deleted"] = 1; + + var result = ele.Execute(args); + var log = logger.ToString(); + Assert.AreEqual(2, result); + } + + [TestMethod] + public void Matches_False() + { + var logger = new TestLogger(); + Matches ele = new (); + ele.MatchConditions = new() + { + new("{file.Name}", "triggerthis"), + new("{file.Deleted}", "false"), + new("{file.Name}", "TriggerThis"), + }; + var args = new FileFlows.Plugin.NodeParameters(null, logger, + false, string.Empty, new LocalFileService()); + args.Variables["file.Deleted"] = false; + + var result = ele.Execute(args); + var log = logger.ToString(); + Assert.AreEqual(2, result); + } + + + [TestMethod] + public void Matches_False_0() + { + var logger = new TestLogger(); + Matches ele = new (); + ele.MatchConditions = new() + { + new("{file.Name}", "triggerthis"), + new("{file.Deleted}", "false"), + new("{file.Name}", "TriggerThis"), + }; + var args = new FileFlows.Plugin.NodeParameters(null, logger, + false, string.Empty, new LocalFileService()); + args.Variables["file.Deleted"] = 0; + + var result = ele.Execute(args); + var log = logger.ToString(); + Assert.AreEqual(2, result); + } }