plcsharp/Tests/PLCSharp.Tests/AxisRenameTests.cs

73 lines
3.4 KiB
C#

using PLCSharp.Models;
using PLCSharp.VVMs.MotionController;
using PLCSharp.VVMs.Recipe;
using PLCSharp.VVMs.Workflows;
using System.IO;
namespace PLCSharp.Tests;
public class AxisRenameTests
{
[Fact]
public void RenameUpdatesReferencesAcrossRecipesAndRejectsStalePointFiles()
{
var root = Directory.CreateTempSubdirectory();
try
{
using var p = ProjectDirectory.New();
p.Recipes.Add(new Recipe { Name = "B" });
var axis = new Axis { Name = "X" };
p.Data.AddRange(axis, new Axis { Name = "Y" });
foreach (var recipe in p.Recipes)
{
p.Data.Add(new AxisPoint { RecipeID = recipe.ID, Name = "Home", AxisUName = "X" });
p.Data.Add(new InterpolationGroup { RecipeID = recipe.ID, AxisYName = "X" });
var flow = new Workflow { RecipeID = recipe.ID, Name = "Run" };
flow.Code = $$"""
using System.Linq;
using PLCSharp.Models;
using PLCSharp.Core.Common;
public class Extension_{{flow.ID:N}} {
public void Run(GlobalModel m, FlowModel f) {
_ = m.ControllersModel.Axes.FirstOrDefault(a => a.Name == "X");
_ = m.ControllersModel.Controller.Axes.FirstOrDefault(a => a.Name == f.Step.ToString());
_ = m.GetTask("X");
}
}
""";
p.Flows(recipe.ID).Add(flow);
}
p.Save(root.FullName);
var original = Directory.GetFiles(root.FullName, "*", SearchOption.AllDirectories).ToDictionary(f => f, File.ReadAllBytes);
Assert.ThrowsAny<IOException>(() => p.PlanAxisRename(axis.ID, "Y"));
var draft = p.PlanAxisRename(axis.ID, "横移轴");
Assert.True(draft.Compiled);
Assert.Equal(2, draft.Scripts.Count);
Assert.Equal(2, draft.ManualReferences.Count);
Assert.Equal(4, draft.PointReferences.Count);
p.ApplyRename(draft);
using (var reopened = ProjectDirectory.Open(root.FullName))
{
Assert.Equal("横移轴", reopened.Data.Axes.Find(axis.ID)!.Name);
Assert.All(reopened.Data.AxisPoints, pt => Assert.Equal("横移轴", pt.AxisUName));
Assert.All(reopened.Data.InterpolationGroups, g => Assert.Equal("横移轴", g.AxisYName));
foreach (var r in reopened.Recipes)
{
var script = Assert.Single(reopened.Flows(r.ID)).Code;
Assert.Contains("a.Name == \"横移轴\"", script);
Assert.Contains("GetTask(\"X\")", script);
}
}
p.ApplyRename(p.PlanUndo(Guid.NewGuid(), draft));
Assert.All(original, file => Assert.Equal(file.Value, File.ReadAllBytes(file.Key)));
var stale = p.PlanAxisRename(axis.ID, "横移轴");
var points = Path.Combine(root.FullName, "configuration", "points.json");
File.AppendAllText(points, "\n");
Assert.ThrowsAny<IOException>(() => p.ApplyRename(stale));
Assert.Equal("X", axis.Name);
Assert.All(original.Where(f => f.Key != points), file => Assert.Equal(file.Value, File.ReadAllBytes(file.Key)));
}
finally { root.Delete(true); }
}
}