plcsharp/Tests/PLCSharp.Tests/ObjectRenameTests.cs

82 lines
4.4 KiB
C#

using PLCSharp.Models;
using PLCSharp.VVMs.GlobalVariables;
using PLCSharp.VVMs.Homepage;
using PLCSharp.VVMs.MotionController;
using PLCSharp.VVMs.Robots;
using PLCSharp.VVMs.Vision;
using PLCSharp.VVMs.Workflows;
using System.IO;
namespace PLCSharp.Tests;
public class ObjectRenameTests
{
[Fact]
public void SixRemainingKindsCompileSaveAndUndoAsOneSequence()
{
var root = Directory.CreateTempSubdirectory();
try
{
using var project = ProjectDirectory.New();
var recipe = project.Recipes.Single();
recipe.Name = "Product";
var other = new PLCSharp.VVMs.Recipe.Recipe { Name = "Other" };
project.Recipes.Add(other);
var robot = new Robot { Name = "Robot" };
var matrix = new Matrix { RecipeID = recipe.ID, Name = "Grid" };
var grid = new RobotMatrix { RecipeID = recipe.ID, RobotID = robot.ID, Name = "Grid" };
var group = new InterpolationGroup { RecipeID = recipe.ID, Name = "Path" };
var system = new SystemVariable { RecipeID = recipe.ID, Name = "Transform" };
var control = new CustomControl { RecipeID = recipe.ID, Name = "Display" };
var function = new VisionFunction { RecipeID = recipe.ID, Name = "Inspect", ControlName = "Display" };
var step = new VisionFlow { Type = VisionFlowType.坐标转换 };
step.StringParams["TransformMat"] = system.Name;
function.VisionFlows.Add(step);
var display = new VisionFlow { Type = VisionFlowType.显示图像到主页 };
display.StringParams["CustomControlName"] = control.Name;
function.VisionFlows.Add(display);
project.Data.AddRange(robot, matrix, grid, group, system, control, function);
var flow = new Workflow { RecipeID = recipe.ID, Name = "Check" };
flow.Code = $$"""
using System.Linq;
using PLCSharp.Models;
using PLCSharp.Core.Common;
public class Extension_{{flow.ID:N}} {
public void Run(GlobalModel model, FlowModel flow) {
_ = model.CurrentRecipe.Name == "Product";
_ = model.ControllersModel.Matrices.First(m => m.Name == "Grid");
_ = model.GetRobot("Robot").RobotMatrices.First(m => m.Name == "Grid");
_ = model.ControllersModel.InterpolationGroups.First(g => g.Name == "Path");
_ = model.VariablesModel.SystemVariables.First(v => v.Name == "Transform");
_ = model.GetCustomControl("Display");
_ = model.GetCustomControl(flow.Step.ToString());
}
}
""";
project.Flows(recipe.ID).Add(flow);
project.Save(root.FullName);
var initial = Directory.GetFiles(root.FullName, "*", SearchOption.AllDirectories).ToDictionary(p => p, File.ReadAllBytes);
var history = new Stack<ProjectRename>();
Assert.ThrowsAny<IOException>(() => project.PlanObjectRename("Recipes", recipe.ID, other.Name));
foreach (var (kind, id) in new[] { ("Recipes", recipe.ID), ("Matrices", matrix.ID), ("RobotMatrices", grid.ID),
("InterpolationGroups", group.ID), ("SystemVariables", system.ID), ("CustomControls", control.ID) })
{
var plan = project.PlanObjectRename(kind, id, kind);
Assert.True(plan.Compiled);
Assert.Single(plan.Scripts);
if (kind == "CustomControls") Assert.Single(plan.ManualReferences);
project.ApplyRename(plan);
Assert.Contains($"\"{kind}\"", flow.Code);
history.Push(plan);
}
Assert.Equal("SystemVariables", step.StringParams["TransformMat"]);
Assert.Equal("CustomControls", function.ControlName);
Assert.Equal("CustomControls", display.StringParams["CustomControlName"]);
while (history.TryPop(out var saved)) project.ApplyRename(project.PlanUndo(Guid.NewGuid(), saved));
Assert.Equal(initial.Keys.Order(), Directory.GetFiles(root.FullName, "*", SearchOption.AllDirectories).Order());
foreach (var (path, bytes) in initial) Assert.Equal(bytes, File.ReadAllBytes(path));
}
finally { root.Delete(true); }
}
}