plcsharp/Tests/PLCSharp.Tests/VisionRenameTests.cs

54 lines
2.5 KiB
C#

using PLCSharp.Models;
using PLCSharp.VVMs.Vision;
using PLCSharp.VVMs.Workflows;
using System.IO;
namespace PLCSharp.Tests;
public class VisionRenameTests
{
[Fact]
public void OnlyRecipeVisionLookupsAreRewrittenAndUndoRestoresFiles()
{
var root = Directory.CreateTempSubdirectory();
try
{
using var p = ProjectDirectory.New();
var vision = new VisionFunction { RecipeID = p.DefaultRecipeId, Name = "检查" };
p.Data.AddRange(vision, new VisionFunction { RecipeID = vision.RecipeID, Name = "复检" });
var caller = new Workflow { Name = "调度", RecipeID = p.DefaultRecipeId };
caller.Code = $$"""
using System.Linq;
using PLCSharp.Models;
using PLCSharp.Core.Common;
public class Extension_{{caller.ID:N}} {
public void Run(GlobalModel m, FlowModel f) {
const string original = "检查";
_ = m.GetVisionFunction(original);
_ = m.VisionsModel.VisionFunctions.FirstOrDefault(v => v.Name == "检查");
_ = m.GetVisionFunction(f.Step.ToString());
_ = m.GetTask("检查");
}
}
""";
p.Flows(vision.RecipeID).Add(caller);
p.Save(root.FullName);
Assert.ThrowsAny<IOException>(() => p.PlanVisionRename(vision.ID, "复检"));
var original = Directory.GetFiles(root.FullName, "*", SearchOption.AllDirectories).ToDictionary(f => f, File.ReadAllBytes);
var plan = p.PlanVisionRename(vision.ID, "装配检查");
Assert.True(plan.Compiled);
Assert.Equal("dynamic", (string?)Assert.Single(plan.ManualReferences)!["confidence"]);
p.ApplyRename(plan);
Assert.Contains("GetVisionFunction(\"装配检查\")", caller.Code);
Assert.Contains("v.Name == \"装配检查\"", caller.Code);
Assert.Contains("GetTask(\"检查\")", caller.Code);
using (var reopened = ProjectDirectory.Open(root.FullName))
Assert.Equal("装配检查", reopened.Data.VisionFunctions.Find(vision.ID)!.Name);
p.ApplyRename(p.PlanUndo(Guid.NewGuid(), plan));
Assert.Equal("检查", vision.Name);
Assert.All(original, file => Assert.Equal(file.Value, File.ReadAllBytes(file.Key)));
}
finally { root.Delete(true); }
}
}