Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unification of modify device name #1411

Merged
merged 11 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions EasyEplanner.Tests/EplanDevice.Test/DeviceManager.Test.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using EplanDevice;
using Moq;
using NUnit.Framework;
using StaticHelper;
using System;
using System.Windows.Forms;
using TechObject;

namespace Tests.EplanDevices
{
Expand Down Expand Up @@ -46,6 +50,28 @@ public void SetUpDevices()
var C_THLD = new C("OBJ1C2", "+OBJ1-C2", string.Empty, 2, "OBJ", 1);
C_THLD.SetSubType("C_THLD");
devManager.Devices.Add(C_THLD);


// .GetModifiedDevice()
var mixproof_1 = new V("NOOBJ1V111", "+NOOBJ1-V111", "", 111, "NOOBJ", 1, "");
mixproof_1.SetSubType("V_AS_MIXPROOF");
devManager.Devices.Add(mixproof_1);

var mixproof_2 = new V("NOOBJ1V112", "+NOOBJ1-V112", "", 112, "NOOBJ", 1, "");
mixproof_2.SetSubType("V_AS_MIXPROOF");
devManager.Devices.Add(mixproof_2);

var dev_1 = new V("OBJ1V1", "+OBJ1-V1", "", 1, "OBJ", 1, "");
devManager.Devices.Add(dev_1);

var dev_2 = new V("OBJ2V1", "+OBJ2-V1", "", 1, "OBJ", 2, "");
devManager.Devices.Add(dev_2);

var dev_3 = new V("OTHER1V1", "+OTHER1-V1", "", 1, "OTHER", 1, "");
devManager.Devices.Add(dev_3);

var dev_4 = new V("OTHER2V1", "+OTHER2-V1", "", 1, "OTHER", 2, "");
devManager.Devices.Add(dev_4);
}

[TestCase("+LINE1-V2", false)]
Expand Down Expand Up @@ -203,5 +229,56 @@ public void CheckControllerIOProperties_EmptyAllowedDevice()
Assert.AreEqual(string.Empty, res);
}


[TestCaseSource(nameof(GetModifiedDevice_TestCaseSource))]
public void GetModifiedDevice(
string oldObjName, int oldTechNumber,
string newObjName, int newTechNumber,
string modify, string expectedModified,
bool isCap)
{
var manager = DeviceManager.GetInstance();

var options = Mock.Of<IDevModifyOptions>(o =>
o.IsUnit == true &&
o.NameModified == (oldObjName != newObjName) &&
o.NumberModified == (oldTechNumber != newTechNumber) &&
o.OldTechObjectName == oldObjName &&
o.OldTechObjectNumber == oldTechNumber &&
o.NewTechObjectName == newObjName &&
o.NewTechObjectNumber == newTechNumber);

var modified = manager.GetModifiedDevice(
manager.GetDeviceByEplanName(modify),
options);

Assert.Multiple(() =>
{
Assert.AreEqual(expectedModified, modified?.Name);
Assert.AreEqual(isCap, modified?.Description == CommonConst.Cap);
});
}

private static readonly object[] GetModifiedDevice_TestCaseSource = new object[]
{
new object[] { "OBJ", 11, "OBJ", 12, "NOOBJ1V111", "NOOBJ1V112", false },
new object[] { "OBJ", 11, "OBJ", 13, "NOOBJ1V111", null, false },
new object[] { "OBJ", 11, "OBJECT", 11, "NOOBJ1V111", "NOOBJ1V111", false },
new object[] { "OBJ", 12, "OBJ", 11, "NOOBJ1V111", "NOOBJ1V111", false },

new object[] { "OBJ", 1, "OBJ", 2, "OBJ1V1", "OBJ2V1", false },
new object[] { "OBJ", 2, "OBJ", 3, "OBJ2V1", "OBJ3V1", true },
new object[] { "OBJ", 1, "OBJ", 2, "OBJ2V1", "OBJ1V1", false },
new object[] { "OBJ", 3, "OBJ", 2, "OBJ1V1", "OBJ1V1", false },

new object[] { "OBJ", 1, "OTHER", 1, "OBJ1V1", "OTHER1V1", false },
new object[] { "OBJ", 1, "OTHER", 1, "OTHER1V1", "OTHER1V1", false },

new object[] { "OBJ", -1, "OBJ", 1, "OBJ2V1", "OBJ1V1", false },
new object[] { "OBJ", -1, "OBJ", 2, "OBJ2V1", "OBJ2V1", false },
new object[] { "OBJ", -1, "OBJ", 3, "OBJ2V1", "OBJ3V1", true },

new object[] { "OTHER", 1, "OTHER", 1, "OTHER1V1", "OTHER1V1", false },
};
}
}
70 changes: 55 additions & 15 deletions EasyEplanner.Tests/TechObject.Test/Base.Test/BaseParameter.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,17 +439,41 @@ public void SaveToPrgLua_CheckActiveBoolParameter(string value, string prefix, s
Assert.AreEqual(expected, res);
}

[TestCase("NORM1DEV2", "NORM", 2, "NORM2DEV2")]
[TestCase("NORM1DEV2", "OTHER", 1, "OTHER1DEV2")]
public void ModifyDevNames(
string source,
string newName, int newNumber,
string expected)
{
var parameter = new ActiveParameter("LuaName", "Name");
parameter.SetNewValue(source);

var options = Mock.Of<IDevModifyOptions>(o =>
o.NewTechObjectName == newName &&
o.NewTechObjectNumber == newNumber);

parameter.ModifyDevNames(options);

Assert.AreEqual(expected, parameter.Value);
}

private EplanDevice.IDeviceManager GetMoqForSetValuesAndDisplayTextTest()
{
string stubDev1Name = "STUB1DEV1";
string stubDev2Name = "STUB1DEV2";
string normDev1Name = "NORM1DEV1";
string normDev2Name = "NORM1DEV2";
string norm_1_dev_1_name = "NORM1DEV1";
string norm_1_dev_2_name = "NORM1DEV2";
string norm_2_dev_2_name = "NORM2DEV2";
string other_1_dev_2_name = "OTHER1DEV2";


int normDev1Index = 1;
int norm_1_dev_1_index = 1;
int stubDev1Index = 2;
int normDev2Index = 3;
int norm_1_dev_2_index = 3;
int stubDev2Index = 4;
int norm_2_dev_2_index = 5;
int other_1_dev_2_index = 6;

var stubDevice1 = Mock.Of<EplanDevice.IDevice>(
dev => dev.Name == stubDev1Name &&
Expand All @@ -458,29 +482,45 @@ private EplanDevice.IDeviceManager GetMoqForSetValuesAndDisplayTextTest()
dev => dev.Name == stubDev2Name &&
dev.Description == StaticHelper.CommonConst.Cap);

var okDevice1 = Mock.Of<EplanDevice.IDevice>(
dev => dev.Name == normDev1Name &&
var norm_1_dev_1 = Mock.Of<EplanDevice.IDevice>(
dev => dev.Name == norm_1_dev_1_name &&
dev.Description == "Description 1");
var okDevice2 = Mock.Of<EplanDevice.IDevice>(
dev => dev.Name == normDev2Name &&
var norm_1_dev_2 = Mock.Of<EplanDevice.IDevice>(
dev => dev.Name == norm_1_dev_2_name &&
dev.Description == "Description 2");

var norm_2_dev_2 = Mock.Of<EplanDevice.IDevice>(
dev => dev.Name == norm_2_dev_2_name &&
dev.Description == "Description");

var other_1_dev_2 = Mock.Of<EplanDevice.IDevice>(
dev => dev.Name == other_1_dev_2_name &&
dev.Description == "Description");

var deviceManagerMock = Mock.Of<EplanDevice.IDeviceManager>(
d => d.GetDeviceByEplanName(stubDev1Name) == stubDevice1 &&
d.GetDeviceByEplanName(It.IsAny<string>()) == stubDevice1 &&
d.GetDeviceByEplanName(stubDev2Name) == stubDevice2 &&
d.GetDeviceByEplanName(normDev1Name) == okDevice1 &&
d.GetDeviceByEplanName(normDev2Name) == okDevice2 &&
d.GetDeviceByEplanName(norm_1_dev_1_name) == norm_1_dev_1 &&
d.GetDeviceByEplanName(norm_1_dev_2_name) == norm_1_dev_2 &&
d.GetDeviceByEplanName(norm_2_dev_2_name) == norm_2_dev_2 &&
d.GetDeviceByEplanName(other_1_dev_2_name) == other_1_dev_2 &&
d.GetDeviceByIndex(It.IsAny<int>()) == stubDevice1 &&
d.GetDeviceByIndex(normDev1Index) == okDevice1 &&
d.GetDeviceByIndex(normDev2Index) == okDevice2 &&
d.GetDeviceByIndex(norm_1_dev_1_index) == norm_1_dev_1 &&
d.GetDeviceByIndex(norm_1_dev_2_index) == norm_1_dev_2 &&
d.GetDeviceByIndex(stubDev1Index) == stubDevice1 &&
d.GetDeviceByIndex(stubDev2Index) == stubDevice2 &&
d.GetDeviceByIndex(norm_2_dev_2_index) == norm_2_dev_2 &&
d.GetDeviceByIndex(other_1_dev_2_index) == other_1_dev_2 &&
d.GetDeviceIndex(It.IsAny<string>()) == -1 &&
d.GetDeviceIndex(normDev1Name) == normDev1Index &&
d.GetDeviceIndex(normDev2Name) == normDev2Index &&
d.GetDeviceIndex(norm_1_dev_1_name) == norm_1_dev_1_index &&
d.GetDeviceIndex(norm_1_dev_2_name) == norm_1_dev_2_index &&
d.GetDeviceIndex(stubDev1Name) == stubDev1Index &&
d.GetDeviceIndex(stubDev2Name) == stubDev2Index);
d.GetDeviceIndex(stubDev2Name) == stubDev2Index &&
d.GetDeviceIndex(norm_2_dev_2_name) == norm_2_dev_2_index &&
d.GetDeviceIndex(other_1_dev_2_name) == other_1_dev_2_index &&
d.GetModifiedDevice(norm_1_dev_2, It.Is<IDevModifyOptions>(o => o.NewTechObjectNumber == 2)) == norm_2_dev_2 &&
d.GetModifiedDevice(norm_1_dev_2, It.Is<IDevModifyOptions>(o => o.NewTechObjectName == "OTHER")) == other_1_dev_2);

return deviceManagerMock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,46 +68,5 @@ public void SetNewValue_and_DisplayText_DisplayParameter()
Assert.IsNull(actionParameter.Parameter);
});
}

[Test]
public void ModifyDevName()
{
string OBJ = nameof(OBJ);

var deviceManagerMock = new Mock<IDeviceManager>();
var dev1 = new V($"{OBJ}1V1", $"+{OBJ}1-V1", "", 1, OBJ, 1, "");
var dev2 = new V($"{OBJ}2V1", $"+{OBJ}2-V1", "", 1, OBJ, 2, "");

deviceManagerMock.Setup(m => m.GetDeviceByEplanName($"{OBJ}1V1")).Returns(dev1);
deviceManagerMock.Setup(m => m.GetDeviceByEplanName($"{OBJ}2V1")).Returns(dev2);

deviceManagerMock.Setup(m => m.GetDeviceIndex($"{OBJ}1V1")).Returns(0);
deviceManagerMock.Setup(m => m.GetDeviceIndex($"{OBJ}2V1")).Returns(1);

deviceManagerMock.Setup(m => m.GetDeviceByIndex(0)).Returns(dev1);
deviceManagerMock.Setup(m => m.GetDeviceByIndex(1)).Returns(dev2);

var actionParameter = new ActionParameter("action_parameter", "параметр");

typeof(BaseParameter).GetField("deviceManager",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
.SetValue(null, deviceManagerMock.Object);

Assert.Multiple(() =>
{
actionParameter.SetNewValue($"{OBJ}1V1");
actionParameter.ModifyDevName(1, 2, OBJ);
Assert.AreEqual($"{OBJ}2V1",actionParameter.Value);


actionParameter.SetNewValue($"{OBJ}1V1");
actionParameter.ModifyDevName(2, -1, OBJ);
Assert.AreEqual($"{OBJ}2V1", actionParameter.Value);
});

typeof(BaseParameter).GetField("deviceManager",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
.SetValue(null, DeviceManager.GetInstance());
}
}
}
34 changes: 34 additions & 0 deletions EasyEplanner.Tests/TechObject.Test/DevModifyOptions.Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechObject;

namespace TechObjectTests
{
public class DevModifyOptionsTest
{
[Test]
public void Constructor()
{

var techObject = new TechObject.TechObject("", getN => 2, 2, 2, "OBJ", -1, "", "",
new BaseTechObject() { S88Level = (int)BaseTechObjectManager.ObjectType.Unit });
var options = new DevModifyOptions(techObject, "OBJ", 1);

Assert.Multiple(() =>
{
Assert.IsTrue(options.IsUnit);
Assert.IsTrue(options.NumberModified);
Assert.IsFalse(options.NameModified);
Assert.AreEqual("OBJ", options.OldTechObjectName);
Assert.AreEqual("OBJ", options.NewTechObjectName);
Assert.AreEqual(1, options.OldTechObjectNumber);
Assert.AreEqual(2, options.NewTechObjectNumber);
});
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ public void CreateGenericGroup_FromManyTechObjects()
Assert.AreEqual(expT1Step.GetStepName(), steps[1].GetStepName());
CollectionAssert.AreEqual(new[] { "TANK1V1", "DO1" }, actionOnDevices.DevicesNames);
CollectionAssert.AreEqual(new[] { "TANK1V1" }, actionDelayOnDevices.SubActions[0].SubActions[0].DevicesNames);
CollectionAssert.AreEqual(new[] { "TANK1V1" }, actionDelayOnDevices.SubActions[0].SubActions[0].DevicesNames);
});
}

Expand All @@ -503,28 +502,31 @@ private void SetupDeviceManagerMock()
var TANK1V1 = new V("TANK1V1", "+TANK1-V1", "desc", 1, "TANK", 1, "");
var TANK2V1 = new V("TANK2V1", "+TANK2-V1", "desc", 1, "TANK", 2, "");


deviceManagerMock.Setup(m => m.GetDeviceByEplanName(It.IsAny<string>())).Returns(capDevice);
deviceManagerMock.Setup(m => m.GetDeviceByEplanName("LS1")).Returns(LS1);
deviceManagerMock.Setup(m => m.GetDeviceByEplanName("TANK1V1")).Returns(TANK1V1);
deviceManagerMock.Setup(m => m.GetDeviceByEplanName("TANK2V1")).Returns(TANK2V1);
deviceManagerMock.Setup(m => m.GetDeviceByEplanName("DO1")).Returns(DO1);
deviceManagerMock.Setup(m => m.GetDeviceByEplanName(It.IsAny<string>())).Returns(capDevice);

deviceManagerMock.Setup(m => m.GetDevice("LS1")).Returns(LS1);
deviceManagerMock.Setup(m => m.GetDevice("TANK1V1")).Returns(TANK1V1);
deviceManagerMock.Setup(m => m.GetDevice("TANK2V1")).Returns(TANK2V1);
deviceManagerMock.Setup(m => m.GetDevice("DO1")).Returns(DO1);

deviceManagerMock.Setup(m => m.GetDeviceIndex("LS1")).Returns(0);
deviceManagerMock.Setup(m => m.GetDeviceIndex("TANK1V1")).Returns(1);
deviceManagerMock.Setup(m => m.GetDeviceIndex("TANK2V1")).Returns(2);
deviceManagerMock.Setup(m => m.GetDeviceIndex("DO1")).Returns(3);

deviceManagerMock.Setup(m => m.GetDeviceIndex(LS1)).Returns(0);
deviceManagerMock.Setup(m => m.GetDeviceIndex(TANK1V1)).Returns(1);
deviceManagerMock.Setup(m => m.GetDeviceIndex(TANK2V1)).Returns(2);
deviceManagerMock.Setup(m => m.GetDeviceIndex(DO1)).Returns(3);

deviceManagerMock.Setup(m => m.GetDeviceByIndex(0)).Returns(LS1);
deviceManagerMock.Setup(m => m.GetDeviceByIndex(1)).Returns(TANK1V1);
deviceManagerMock.Setup(m => m.GetDeviceByIndex(2)).Returns(TANK2V1);
deviceManagerMock.Setup(m => m.GetDeviceByIndex(3)).Returns(DO1);

deviceManagerMock.Setup(m => m.GetModifiedDevice(TANK1V1, It.IsAny<IDevModifyOptions>())).Returns(TANK1V1);
deviceManagerMock.Setup(m => m.GetModifiedDevice(TANK2V1, It.IsAny<IDevModifyOptions>())).Returns(TANK1V1);
deviceManagerMock.Setup(m => m.GetModifiedDevice(LS1, It.IsAny<IDevModifyOptions>())).Returns(LS1);
deviceManagerMock.Setup(m => m.GetModifiedDevice(DO1, It.IsAny<IDevModifyOptions>())).Returns(DO1);

typeof(BaseParameter).GetField("deviceManager",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
Expand Down
Loading