added true|false matching to matches

This commit is contained in:
John Andrews
2024-04-16 14:39:19 +12:00
parent 186c27dc35
commit 7e9a96c762
2 changed files with 108 additions and 6 deletions

View File

@@ -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;
}
}

View File

@@ -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);
}
}