added pattern node, add some tests

This commit is contained in:
reven
2021-11-22 15:31:31 +13:00
parent 54a1957e9b
commit a55c5df49f
14 changed files with 248 additions and 11 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ Plugin.dll
*/obj
*/.vs
*.suo
TestStore/

Binary file not shown.

View File

@@ -25,9 +25,16 @@ namespace FileFlows.BasicNodes.Functions
delegate void LogDelegate(params object[] values);
public override int Execute(NodeParameters args)
{
args.Logger.DLog("Code: ", Environment.NewLine + new string('=', 40) + Environment.NewLine + Code + Environment.NewLine + new string('=', 40));
if (string.IsNullOrEmpty(Code))
return base.Execute(args); // no code, means will run fine... i think... maybe... depends what i do
return -1; // no code, flow cannot continue doesnt know what to do
args.Logger.DLog("Code: ", Environment.NewLine + new string('=', 40) + Environment.NewLine + Code + Environment.NewLine + new string('=', 40));
long fileSize = 0;
var fileInfo = new FileInfo(args.WorkingFile);
if(fileInfo.Exists)
fileSize = fileInfo.Length;
var sb = new StringBuilder();
var log = new
@@ -43,15 +50,19 @@ namespace FileFlows.BasicNodes.Functions
options.MaxStatements(100);
})
.SetValue("Logger", args.Logger)
.SetValue("FileSize", new FileInfo(args.WorkingFile).Length)
//.SetValue("ILog", log.ILog)
.SetValue("FileSize", fileSize)
;
var result = engine.Evaluate(Code).ToObject();
if (result as bool? != true)
args.Result = NodeResult.Failure;
return base.Execute(args);
try
{
var result = int.Parse(engine.Evaluate(Code).ToObject().ToString());
return result;
}
catch (Exception ex)
{
args.Logger.ELog("Failed executing function: " + ex.Message);
return -1;
}
}
}
}

View File

@@ -0,0 +1,38 @@
namespace FileFlows.BasicNodes.Functions
{
using System.ComponentModel;
using System.Text.RegularExpressions;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
public class PatternMatch : Node
{
public override int Inputs => 1;
public override int Outputs => 2;
public override FlowElementType Type => FlowElementType.Logic;
public override string Icon => "fas fa-equals";
[DefaultValue(".*?")]
[RegularExpression(2)]
public string Pattern { get; set; }
delegate void LogDelegate(params object[] values);
public override int Execute(NodeParameters args)
{
if (string.IsNullOrEmpty(Pattern))
return 1; // no pattern, matches everything
try
{
var rgx = new Regex(Pattern);
if (rgx.IsMatch(args.WorkingFile) || rgx.IsMatch(args.FileName))
return 1;
return 0;
}
catch (Exception ex)
{
args.Logger?.ELog("Pattern error: " + ex.Message);
return -1;
}
}
}
}

View File

@@ -0,0 +1,58 @@
#if(DEBUG)
namespace BasicNodes.Tests
{
using FileFlows.BasicNodes.Functions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class FunctionTests
{
FileFlows.Plugin.NodeParameters Args;
[TestInitialize]
public void TestStarting()
{
Args = new FileFlows.Plugin.NodeParameters(@"c:\test\testfile.mkv");
Args.Logger = new TestLogger();
}
[TestMethod]
public void Function_NoCode()
{
Function pm = new Function();
pm.Code = null;
var result = pm.Execute(Args);
Assert.AreEqual(-1, result);
Function pm2 = new Function();
pm2.Code = string.Empty;
result = pm2.Execute(Args);
Assert.AreEqual(-1, result);
}
[TestMethod]
public void Function_BadCode()
{
Function pm = new Function();
pm.Code = "let x = {";
var result = pm.Execute(Args);
Assert.AreEqual(-1, result);
}
[TestMethod]
public void Function_Basic_ReturnInts()
{
for (int i = 0; i < 10; i++)
{
Function pm = new Function();
pm.Code = "return " + i;
var result = pm.Execute(Args);
Assert.AreEqual(i, result);
}
}
}
}
#endif

View File

@@ -0,0 +1,49 @@
#if(DEBUG)
namespace BasicNodes.Tests
{
using FileFlows.BasicNodes.Functions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class PatternMatchTests
{
[TestMethod]
public void PatternMatch_Extension()
{
PatternMatch pm = new PatternMatch();
pm.Pattern = @"\.mkv$";
var args = new FileFlows.Plugin.NodeParameters(@"c:\test\testfile.mkv");
args.SetWorkingFile($@"c:\temp\{Guid.NewGuid().ToString()}.mkv", dontDelete: true);
var result = pm.Execute(args);
Assert.AreEqual(1, result);
}
[TestMethod]
public void PatternMatch_NotMatch()
{
PatternMatch pm = new PatternMatch();
pm.Pattern = @"\.mkv$";
var args = new FileFlows.Plugin.NodeParameters(@"c:\test\testfile.avi");
args.SetWorkingFile($@"c:\temp\{Guid.NewGuid().ToString()}.avi", dontDelete: true);
var result = pm.Execute(args);
Assert.AreEqual(0, result);
}
[TestMethod]
public void PatternMatch_BadExpression()
{
PatternMatch pm = new PatternMatch();
pm.Pattern = @"[-$";
var args = new FileFlows.Plugin.NodeParameters(@"c:\test\testfile.avi");
args.SetWorkingFile($@"c:\temp\{Guid.NewGuid().ToString()}.avi", dontDelete: true);
var result = pm.Execute(args);
Assert.AreEqual(-1, result);
}
}
}
#endif

View File

@@ -0,0 +1,29 @@
namespace BasicNodes.Tests
{
using FileFlows.Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
internal class TestLogger : ILogger
{
public void DLog(params object[] args)
{
}
public void ELog(params object[] args)
{
}
public void ILog(params object[] args)
{
}
public void WLog(params object[] args)
{
}
}
}

Binary file not shown.

Binary file not shown.

28
FileFlowsPlugins.sln Normal file
View File

@@ -0,0 +1,28 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicNodes", "BasicNodes\BasicNodes.csproj", "{7AE24315-9FE7-429F-83D9-C989CFF5420D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VideoNodes", "VideoNodes\VideoNodes.csproj", "{CF96D3D1-1D8B-47F7-BEA7-BB238F7A566A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7AE24315-9FE7-429F-83D9-C989CFF5420D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7AE24315-9FE7-429F-83D9-C989CFF5420D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7AE24315-9FE7-429F-83D9-C989CFF5420D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7AE24315-9FE7-429F-83D9-C989CFF5420D}.Release|Any CPU.Build.0 = Release|Any CPU
{CF96D3D1-1D8B-47F7-BEA7-BB238F7A566A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF96D3D1-1D8B-47F7-BEA7-BB238F7A566A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF96D3D1-1D8B-47F7-BEA7-BB238F7A566A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF96D3D1-1D8B-47F7-BEA7-BB238F7A566A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

23
Plugin.deps.json Normal file
View File

@@ -0,0 +1,23 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v6.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v6.0": {
"Plugin/1.0.0": {
"runtime": {
"Plugin.dll": {}
}
}
}
},
"libraries": {
"Plugin/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}

BIN
Plugin.pdb Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -1,12 +1,12 @@
[
{
"Name": "BasicNodes",
"Version": "0.0.1.3",
"Version": "0.0.1.4",
"Package": "https://github.com/revenz/FileFlowsPlugins/blob/master/Builds/BasicNodes.zip?raw=true"
},
{
"Name": "VideoNodes",
"Version": "0.0.1.3",
"Version": "0.0.1.4",
"Package": "https://github.com/revenz/FileFlowsPlugins/blob/master/Builds/VideoNodes.zip?raw=true"
}
]