Skip to content

Commit

Permalink
Remove old methods and better style
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillGutyrchik committed Aug 1, 2024
1 parent 88bc675 commit afe1853
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,45 +69,45 @@ public void SetNewValue_and_DisplayText_DisplayParameter()
});
}

[Test]
public void ModifyDevName()
{
string OBJ = nameof(OBJ);
//[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, "");
// 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.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.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);
// deviceManagerMock.Setup(m => m.GetDeviceByIndex(0)).Returns(dev1);
// deviceManagerMock.Setup(m => m.GetDeviceByIndex(1)).Returns(dev2);

var actionParameter = new ActionParameter("action_parameter", "параметр");
// 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());
}
// 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());
//}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -551,71 +551,71 @@ private static object[] SaveAsLuaTableTestCaseSource()
};
}

[TestCase(new int[] { 1, 2, 4, 5, 6 },
new int[] { 3, 4, 5, 6 }, "KOAG", "1", "TANK", "1")]
[TestCase(new int[] { 1, 2, 3, 4 },
new int[] { 5, 6, 3, 4 }, "TANK", "2", "TANK", "1")]
[TestCase(new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 5 }, "TANK", "2", "KOAG", "1")]
public void ModifyDevNames(int[] devIds, int[] expectedDevIds,
string newTechObjectName, int newTechObjectNumber,
string oldTechObjectName, int oldTechObjNumber)
{
EplanDevice.IDeviceManager deviceManager = DeviceManagerMock.DeviceManager;
typeof(Action).GetField("deviceManager",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
.SetValue(null, deviceManager);

var action = new Action(string.Empty, null, string.Empty, null,
null, null);
action.DevicesIndex.AddRange(devIds);

action.ModifyDevNames(newTechObjectName, newTechObjectNumber,
oldTechObjectName, oldTechObjNumber);

Assert.AreEqual(expectedDevIds, action.DevicesIndex);
}

[TestCase(new int[] { 1, 2, 3, 4 }, new int[] { 5, 6, 3, 4 }, 2, 1, "TANK")]
[TestCase(new int[] { 5, 4, 6, 3 }, new int[] { 1, 4, 2, 3 }, 2, 1, "TANK")]
[TestCase(new int[] { 1, 2, 3, 4, 8 }, new int[] { 5, 6, 3, 4, 7 }, 2, -1, "TANK")]
[TestCase(new int[] { 8 }, new int[] { 8 }, 2, 1, "TANK")]
public void ModifyDevNames(int[] devIds, int[] expectedDevIds,
int newTechObjectN, int oldTechObjectN, string techObjectName)
{
EplanDevice.IDeviceManager deviceManager = DeviceManagerMock.DeviceManager;
typeof(Action).GetField("deviceManager",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
.SetValue(null, deviceManager);

var action = new Action(string.Empty, null, string.Empty, null,
null, null);
action.DevicesIndex.AddRange(devIds);

action.ModifyDevNames(newTechObjectN, oldTechObjectN,
techObjectName);

Assert.AreEqual(expectedDevIds, action.DevicesIndex);
}

[TestCase(new int[] { 1, 2, 3, 4 }, new int[] { 5, 6, 3, 4 }, 2, "TANK")]
[TestCase(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, 3, "TANK")]
public void ModifyDevNames_CheckGenericUpdating(int[] devIds,
int[] expectedDevIds, int newObjID, string techObjectName)
{
EplanDevice.IDeviceManager deviceManager = DeviceManagerMock.DeviceManager;
typeof(Action).GetField("deviceManager",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
.SetValue(null, deviceManager);

var action = new Action(string.Empty, null, string.Empty, null,
null, null);
action.DevicesIndex.AddRange(devIds);

action.ModifyDevNames(newObjID, -1, techObjectName);

CollectionAssert.AreEqual(expectedDevIds, action.DevicesIndex);
}
//[TestCase(new int[] { 1, 2, 4, 5, 6 },
// new int[] { 3, 4, 5, 6 }, "KOAG", "1", "TANK", "1")]
//[TestCase(new int[] { 1, 2, 3, 4 },
// new int[] { 5, 6, 3, 4 }, "TANK", "2", "TANK", "1")]
//[TestCase(new int[] { 1, 2, 3, 4 },
// new int[] { 1, 2, 5 }, "TANK", "2", "KOAG", "1")]
//public void ModifyDevNames(int[] devIds, int[] expectedDevIds,
// string newTechObjectName, int newTechObjectNumber,
// string oldTechObjectName, int oldTechObjNumber)
//{
// EplanDevice.IDeviceManager deviceManager = DeviceManagerMock.DeviceManager;
// typeof(Action).GetField("deviceManager",
// System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
// .SetValue(null, deviceManager);

// var action = new Action(string.Empty, null, string.Empty, null,
// null, null);
// action.DevicesIndex.AddRange(devIds);

// action.ModifyDevNames(newTechObjectName, newTechObjectNumber,
// oldTechObjectName, oldTechObjNumber);

// Assert.AreEqual(expectedDevIds, action.DevicesIndex);
//}

//[TestCase(new int[] { 1, 2, 3, 4 }, new int[] { 5, 6, 3, 4 }, 2, 1, "TANK")]
//[TestCase(new int[] { 5, 4, 6, 3 }, new int[] { 1, 4, 2, 3 }, 2, 1, "TANK")]
//[TestCase(new int[] { 1, 2, 3, 4, 8 }, new int[] { 5, 6, 3, 4, 7 }, 2, -1, "TANK")]
//[TestCase(new int[] { 8 }, new int[] { 8 }, 2, 1, "TANK")]
//public void ModifyDevNames(int[] devIds, int[] expectedDevIds,
// int newTechObjectN, int oldTechObjectN, string techObjectName)
//{
// EplanDevice.IDeviceManager deviceManager = DeviceManagerMock.DeviceManager;
// typeof(Action).GetField("deviceManager",
// System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
// .SetValue(null, deviceManager);

// var action = new Action(string.Empty, null, string.Empty, null,
// null, null);
// action.DevicesIndex.AddRange(devIds);

// action.ModifyDevNames(newTechObjectN, oldTechObjectN,
// techObjectName);

// Assert.AreEqual(expectedDevIds, action.DevicesIndex);
//}

//[TestCase(new int[] { 1, 2, 3, 4 }, new int[] { 5, 6, 3, 4 }, 2, "TANK")]
//[TestCase(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, 3, "TANK")]
//public void ModifyDevNames_CheckGenericUpdating(int[] devIds,
// int[] expectedDevIds, int newObjID, string techObjectName)
//{
// EplanDevice.IDeviceManager deviceManager = DeviceManagerMock.DeviceManager;
// typeof(Action).GetField("deviceManager",
// System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
// .SetValue(null, deviceManager);

// var action = new Action(string.Empty, null, string.Empty, null,
// null, null);
// action.DevicesIndex.AddRange(devIds);

// action.ModifyDevNames(newObjID, -1, techObjectName);

// CollectionAssert.AreEqual(expectedDevIds, action.DevicesIndex);
//}

[TestCase(new int[] { 1, 3, 5, 7, 9 })]
[TestCase(new int[0])]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,64 @@ namespace TechObjectTests
{
public class EquipmentTest
{
[Test]
public void ModifyDevNames()
{
V OBJ1V1 = new V(nameof(OBJ1V1), "=OBJ1+V1", string.Empty, 1, "OBJ", 1, string.Empty);
V OBJ2V2 = new V(nameof(OBJ2V2), "=OBJ2+V2", string.Empty, 2, "OBJ", 2, string.Empty);
V OBJ3V1 = new V(nameof(OBJ3V1), "=OBJ3+V1", string.Empty, 1, "OBJ", 3, string.Empty);
V OBJ3V2 = new V(nameof(OBJ3V2), "=OBJ3+V2", string.Empty, 2, "OBJ", 3, string.Empty);
V OBJ3V3 = new V(nameof(OBJ3V3), "=OBJ3+V3", string.Empty, 3, "OBJ", 3, string.Empty);
//[Test]
//public void ModifyDevNames()
//{
// V OBJ1V1 = new V(nameof(OBJ1V1), "=OBJ1+V1", string.Empty, 1, "OBJ", 1, string.Empty);
// V OBJ2V2 = new V(nameof(OBJ2V2), "=OBJ2+V2", string.Empty, 2, "OBJ", 2, string.Empty);
// V OBJ3V1 = new V(nameof(OBJ3V1), "=OBJ3+V1", string.Empty, 1, "OBJ", 3, string.Empty);
// V OBJ3V2 = new V(nameof(OBJ3V2), "=OBJ3+V2", string.Empty, 2, "OBJ", 3, string.Empty);
// V OBJ3V3 = new V(nameof(OBJ3V3), "=OBJ3+V3", string.Empty, 3, "OBJ", 3, string.Empty);

IODevice CAP = new V(string.Empty, string.Empty, StaticHelper.CommonConst.Cap, 1, string.Empty, 1, string.Empty);
// IODevice CAP = new V(string.Empty, string.Empty, StaticHelper.CommonConst.Cap, 1, string.Empty, 1, string.Empty);

var deviceManagerMock = new Mock<IDeviceManager>();
// var deviceManagerMock = new Mock<IDeviceManager>();

deviceManagerMock.Setup(dm => dm.GetDevice(It.IsAny<string>())).Returns(CAP);
// deviceManagerMock.Setup(dm => dm.GetDevice(It.IsAny<string>())).Returns(CAP);

deviceManagerMock.Setup(dm => dm.GetDevice(nameof(OBJ1V1))).Returns(OBJ1V1);
deviceManagerMock.Setup(dm => dm.GetDevice(nameof(OBJ2V2))).Returns(OBJ2V2);
deviceManagerMock.Setup(dm => dm.GetDevice(nameof(OBJ3V1))).Returns(OBJ3V1);
deviceManagerMock.Setup(dm => dm.GetDevice(nameof(OBJ3V2))).Returns(OBJ3V2);
deviceManagerMock.Setup(dm => dm.GetDevice(nameof(OBJ3V3))).Returns(OBJ3V3);
// deviceManagerMock.Setup(dm => dm.GetDevice(nameof(OBJ1V1))).Returns(OBJ1V1);
// deviceManagerMock.Setup(dm => dm.GetDevice(nameof(OBJ2V2))).Returns(OBJ2V2);
// deviceManagerMock.Setup(dm => dm.GetDevice(nameof(OBJ3V1))).Returns(OBJ3V1);
// deviceManagerMock.Setup(dm => dm.GetDevice(nameof(OBJ3V2))).Returns(OBJ3V2);
// deviceManagerMock.Setup(dm => dm.GetDevice(nameof(OBJ3V3))).Returns(OBJ3V3);

deviceManagerMock.Setup(dm => dm.GetDeviceIndex(nameof(OBJ1V1))).Returns(0);
deviceManagerMock.Setup(dm => dm.GetDeviceIndex(nameof(OBJ2V2))).Returns(1);
deviceManagerMock.Setup(dm => dm.GetDeviceIndex(nameof(OBJ3V1))).Returns(2);
deviceManagerMock.Setup(dm => dm.GetDeviceIndex(nameof(OBJ3V2))).Returns(3);
deviceManagerMock.Setup(dm => dm.GetDeviceIndex(nameof(OBJ3V3))).Returns(4);
// deviceManagerMock.Setup(dm => dm.GetDeviceIndex(nameof(OBJ1V1))).Returns(0);
// deviceManagerMock.Setup(dm => dm.GetDeviceIndex(nameof(OBJ2V2))).Returns(1);
// deviceManagerMock.Setup(dm => dm.GetDeviceIndex(nameof(OBJ3V1))).Returns(2);
// deviceManagerMock.Setup(dm => dm.GetDeviceIndex(nameof(OBJ3V2))).Returns(3);
// deviceManagerMock.Setup(dm => dm.GetDeviceIndex(nameof(OBJ3V3))).Returns(4);

deviceManagerMock.Setup(dm => dm.GetDeviceByIndex(0)).Returns(OBJ1V1);
deviceManagerMock.Setup(dm => dm.GetDeviceByIndex(1)).Returns(OBJ2V2);
deviceManagerMock.Setup(dm => dm.GetDeviceByIndex(2)).Returns(OBJ3V1);
deviceManagerMock.Setup(dm => dm.GetDeviceByIndex(3)).Returns(OBJ3V2);
deviceManagerMock.Setup(dm => dm.GetDeviceByIndex(4)).Returns(OBJ3V3);
// deviceManagerMock.Setup(dm => dm.GetDeviceByIndex(0)).Returns(OBJ1V1);
// deviceManagerMock.Setup(dm => dm.GetDeviceByIndex(1)).Returns(OBJ2V2);
// deviceManagerMock.Setup(dm => dm.GetDeviceByIndex(2)).Returns(OBJ3V1);
// deviceManagerMock.Setup(dm => dm.GetDeviceByIndex(3)).Returns(OBJ3V2);
// deviceManagerMock.Setup(dm => dm.GetDeviceByIndex(4)).Returns(OBJ3V3);

deviceManagerMock.Setup(dm => dm.GetDeviceByEplanName(It.IsAny<string>())).Returns(OBJ3V3);
// deviceManagerMock.Setup(dm => dm.GetDeviceByEplanName(It.IsAny<string>())).Returns(OBJ3V3);


var equipment = new Equipment(
new TechObject.TechObject(string.Empty, GetN => 1, 3, 2,
"OBJ", -1, string.Empty, string.Empty, new BaseTechObject()));
// var equipment = new Equipment(
// new TechObject.TechObject(string.Empty, GetN => 1, 3, 2,
// "OBJ", -1, string.Empty, string.Empty, new BaseTechObject()));

var equipmentParameter = new EquipmentParameter("devs", "устройства", "");
typeof(BaseParameter).GetField("deviceManager",
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.NonPublic)
.SetValue(null, deviceManagerMock.Object);
// var equipmentParameter = new EquipmentParameter("devs", "устройства", "");
// typeof(BaseParameter).GetField("deviceManager",
// System.Reflection.BindingFlags.Static |
// System.Reflection.BindingFlags.NonPublic)
// .SetValue(null, deviceManagerMock.Object);

typeof(Equipment).GetProperty("deviceManager",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Static)
.SetValue(null, deviceManagerMock.Object);
// typeof(Equipment).GetProperty("deviceManager",
// System.Reflection.BindingFlags.NonPublic |
// System.Reflection.BindingFlags.Static)
// .SetValue(null, deviceManagerMock.Object);

equipment.AddItems(new List<BaseParameter>() { equipmentParameter });
// equipment.AddItems(new List<BaseParameter>() { equipmentParameter });

equipment.SetEquipmentValue("devs", "OBJ1V1 OBJ2V2 OBJ3V3");
// equipment.SetEquipmentValue("devs", "OBJ1V1 OBJ2V2 OBJ3V3");

equipment.ModifyDevNames();
// equipment.ModifyDevNames();

Assert.AreEqual($"{nameof(OBJ3V1)} {nameof(OBJ3V2)} {nameof(OBJ3V3)}", equipmentParameter.Value);
}
// Assert.AreEqual($"{nameof(OBJ3V1)} {nameof(OBJ3V2)} {nameof(OBJ3V3)}", equipmentParameter.Value);
//}
}
}
13 changes: 6 additions & 7 deletions src/Device/DeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,11 +1167,10 @@ public IDevice GetModifiedDevice(IDevice device, IDevModifyOptions options)
{
if (ModifyMixproof(device, options) is IDevice mixproof)
{
return mixproof.Description == CommonConst.Cap ? null : mixproof;
return mixproof.Description == CommonConst.Cap ? null : mixproof;
// null - удаляем привязку устройства
}

IDevice res = null;

if (options.NumberModified &&
options.OldTechObjectNumber != 0 && // Не модифицировать устройства в типовых объектах
device.Description != CommonConst.Cap &&
Expand All @@ -1183,21 +1182,21 @@ public IDevice GetModifiedDevice(IDevice device, IDevModifyOptions options)
// Изменяем номер объекта в устройстве в соответствии с изменениями объекта или для типовых объектов:
// ( 1 -> 2 ) : OBJ[1]V1 -> OBJ[2]V1
// ( -1 -> 1, 2,... ) : OBJ[x]V1 -> OBJ[1]V1, OBJ[2]V1, ... - для типовых объектов
res = GetDeviceByEplanName($"{device.ObjectName}{options.NewTechObjectNumber}{device.DeviceDesignation}");
return GetDeviceByEplanName($"{device.ObjectName}{options.NewTechObjectNumber}{device.DeviceDesignation}");
}
else if (device.ObjectNumber == options.NewTechObjectNumber)
{
// Инверсионное изменение номера объекта: когда устройство имеет номер объекта равный новому номеру объекта
// ( 1 -> 2 ) : OBJ[2]V1 -> OBJ[1]V1
res = GetDeviceByEplanName($"{device.ObjectName}{options.OldTechObjectNumber}{device.DeviceDesignation}");
return GetDeviceByEplanName($"{device.ObjectName}{options.OldTechObjectNumber}{device.DeviceDesignation}");
}
}
else if (options.NameModified && device.ObjectName == options.OldTechObjectName)
{
res = GetDeviceByEplanName($"{options.NewTechObjectName}{device.ObjectNumber}{device.DeviceDesignation}");
return GetDeviceByEplanName($"{options.NewTechObjectName}{device.ObjectNumber}{device.DeviceDesignation}");
}

return res?.Description == CommonConst.Cap ? null : res;
return device;
}

/// <summary>
Expand Down
Loading

0 comments on commit afe1853

Please sign in to comment.