Files
lmxopcua/tests/ZB.MOM.WW.OtOpcUa.Driver.S7.Tests/SymbolImport/S7DriverFactoryAddImportTests.cs
2026-04-26 06:32:18 -04:00

96 lines
3.1 KiB
C#

using System.IO;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.S7.SymbolImport;
namespace ZB.MOM.WW.OtOpcUa.Driver.S7.Tests.SymbolImport;
/// <summary>
/// Coverage for <c>S7DriverFactoryExtensions.AddTiaCsvImport</c> and
/// <c>AddAwlImport</c>. The extension methods are thin wrappers around the importers
/// so the surface area to test is the merge behaviour + result propagation.
/// </summary>
[Trait("Category", "Unit")]
public sealed class S7DriverFactoryAddImportTests
{
[Fact]
public void AddTiaCsvImport_concatenates_imported_tags_onto_existing_options()
{
var path = Path.Combine(Path.GetTempPath(), $"tia-{Guid.NewGuid():N}.csv");
File.WriteAllText(path, """
Name,Path,Data type,Logical address,Comment,Hmi accessible
Imported1,T,Int,%MW0,desc,True
Imported2,T,Real,%MD4,desc,True
""");
try
{
var existing = new S7TagDefinition("PreExisting", "MW10", S7DataType.Int16);
var options = new S7DriverOptions
{
Host = "192.168.1.10",
Tags = [existing],
};
var updated = options.AddTiaCsvImport(path, out var result);
result.ParsedCount.ShouldBe(2);
updated.Tags.Count.ShouldBe(3);
updated.Tags[0].Name.ShouldBe("PreExisting");
updated.Tags[1].Name.ShouldBe("Imported1");
updated.Tags[2].Name.ShouldBe("Imported2");
// Other options fields propagate untouched.
updated.Host.ShouldBe("192.168.1.10");
}
finally
{
try { File.Delete(path); } catch { /* best-effort */ }
}
}
[Fact]
public void AddTiaCsvImportWithResult_returns_options_and_result_tuple()
{
var path = Path.Combine(Path.GetTempPath(), $"tia-{Guid.NewGuid():N}.csv");
File.WriteAllText(path, """
Name,Path,Data type,Logical address,Comment,Hmi accessible
T,P,Int,%MW0,d,True
""");
try
{
var (updated, result) = new S7DriverOptions { Host = "h" }.AddTiaCsvImportWithResult(path);
updated.Tags.Count.ShouldBe(1);
result.ParsedCount.ShouldBe(1);
}
finally
{
try { File.Delete(path); } catch { /* best-effort */ }
}
}
[Fact]
public void AddAwlImport_appends_var_global_declarations()
{
var path = Path.Combine(Path.GetTempPath(), $"awl-{Guid.NewGuid():N}.awl");
File.WriteAllText(path, """
VAR_GLOBAL
Speed : INT;
Pressure : REAL;
END_VAR
""");
try
{
var options = new S7DriverOptions { Host = "h", Tags = [] };
var updated = options.AddAwlImport(path, out var result);
result.ParsedCount.ShouldBe(2);
updated.Tags.Count.ShouldBe(2);
updated.Tags[0].Address.ShouldBe("MW0");
updated.Tags[1].Address.ShouldBe("MD2");
}
finally
{
try { File.Delete(path); } catch { /* best-effort */ }
}
}
}