plcsharp/Tests/PLCSharp.Tests/ControllerRegressionTests.cs

62 lines
2.1 KiB
C#

using PLCSharp.VVMs.MotionController;
using System.Reflection;
namespace PLCSharp.Tests;
public class ControllerRegressionTests
{
private sealed class RecordingSdk : SMC
{
public List<(string Operation, ushort Axis)> Calls { get; } = [];
public Axis.AxisParams? Saved { get; private set; }
public override void Stop(ushort axisNo) => Calls.Add(("stop", axisNo));
public override void Reset(ushort axisNo) => Calls.Add(("reset", axisNo));
public override short Power(ushort axisNo, bool onoff)
{
Calls.Add((onoff ? "on" : "off", axisNo));
return 0;
}
public override short Save(ushort axisNo, Axis.AxisParams axisParams)
{
Calls.Add(("save", axisNo));
Saved = axisParams;
return 0;
}
}
private static Controller CreateController(RecordingSdk sdk)
{
var controller = new Controller();
typeof(Controller).GetField("SDK", BindingFlags.Instance | BindingFlags.NonPublic)!
.SetValue(controller, sdk);
return controller;
}
[Theory]
[InlineData(1)]
[InlineData(7)]
public void CommandsUseConfiguredAxisNumber(int number)
{
var sdk = new RecordingSdk();
var controller = CreateController(sdk);
var axis = new Axis { AxisNo = (ushort)number, ALM = true };
var other = new Axis { AxisNo = 0 };
controller.Axes = [axis, other];
other.MotionFlow.Step = 12;
axis.MotionFlow.Step = 101;
controller.Stop(axis);
Assert.Equal(0, axis.MotionFlow.Step);
axis.MotionFlow.Step = 2;
controller.PowerOff(axis);
Assert.Equal(0, axis.MotionFlow.Step);
axis.MotionFlow.Step = 21;
controller.PowerOn(axis);
Assert.Equal(0, axis.MotionFlow.Step);
controller.Save(axis);
Assert.Same(axis.Params, sdk.Saved);
Assert.Equal(12, other.MotionFlow.Step);
Assert.Equal(new[] { "stop", "reset", "off", "on", "save" }, sdk.Calls.Select(c => c.Operation));
Assert.All(sdk.Calls, c => Assert.Equal(axis.AxisNo, c.Axis));
}
}