plcsharp/Tests/PLCSharp.Tests/DeviceRenameTests.cs

65 lines
3.0 KiB
C#

using PLCSharp.Models;
using PLCSharp.VVMs.Connects;
using PLCSharp.VVMs.Cylinders;
using PLCSharp.VVMs.Robots;
using PLCSharp.VVMs.Workflows;
using System.IO;
namespace PLCSharp.Tests;
public class DeviceRenameTests
{
[Fact]
public void SameNamedHardwareIsScopedByTypeAndNamedParametersBindToTheConnection()
{
var root = Directory.CreateTempSubdirectory();
try
{
using var p = ProjectDirectory.New();
var cylinder = new Cylinder { Name = "Common", InA = "Common" };
var robot = new Robot { Name = "Common" };
var connect = new Connect { Name = "Common" };
p.Data.AddRange(cylinder, robot, connect, new Robot { Name = "Existing" });
var flow = new Workflow { RecipeID = p.DefaultRecipeId, Name = "Inspect" };
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) {
const string label = "Common";
_ = model.CylindersModel.Cylinders.FirstOrDefault(c => c.Name == label);
_ = model.GetRobot(label);
_ = model.RobotModel.Robots.FirstOrDefault(r => r.Name == label);
_ = model.GetConnect(label);
model.Connects.ApplyDataItemToServer(item: null, connectName: label);
_ = model.Connects.Connects.FirstOrDefault(c => c.Name == flow.Step.ToString());
}
}
""";
p.Flows(flow.RecipeID).Add(flow);
p.Save(root.FullName);
var before = File.ReadAllBytes(Path.Combine(root.FullName, "configuration", "hardware.json"));
Assert.ThrowsAny<IOException>(() => p.PlanDeviceRename("Robots", robot.ID, "Existing"));
var plans = new Stack<ProjectRename>();
foreach (var (kind, id) in new[] { ("Cylinders", cylinder.ID), ("Robots", robot.ID), ("Connects", connect.ID) })
{
var plan = p.PlanDeviceRename(kind, id, kind);
Assert.True(plan.Compiled);
p.ApplyRename(plan);
plans.Push(plan);
}
Assert.Contains("c.Name == \"Cylinders\"", flow.Code);
Assert.Contains("GetRobot(\"Robots\")", flow.Code);
Assert.Contains("r.Name == \"Robots\"", flow.Code);
Assert.Contains("GetConnect(\"Connects\")", flow.Code);
Assert.Contains("connectName: \"Connects\"", flow.Code);
Assert.Single(plans.Peek().ManualReferences);
Assert.Equal("Common", cylinder.InA);
while (plans.TryPop(out var plan)) p.ApplyRename(p.PlanUndo(Guid.NewGuid(), plan));
Assert.Equal(before, File.ReadAllBytes(Path.Combine(root.FullName, "configuration", "hardware.json")));
}
finally { root.Delete(true); }
}
}