SSPC-Tester/SSPCTester.Tests/DaqChannelOptionsNormalizerTests.cs

78 lines
2.7 KiB
C#
Raw Normal View History

using System.Text.Json;
using SSPCTester.Devices.Config;
using SSPCTester.Devices.Interfaces;
namespace SSPCTester.Tests;
public sealed class DaqChannelOptionsNormalizerTests
{
[Fact]
public void Normalize_ReturnsExactlyEightOrderedChannels()
{
var channels = new[]
{
new DaqChannelOptions { PhysicalChannel = 3, Role = nameof(DaqChannelRole.InputCurrent), Name = "Iin" },
new DaqChannelOptions { PhysicalChannel = 0, Role = nameof(DaqChannelRole.OutputVoltage), Name = "Vout" },
new DaqChannelOptions { PhysicalChannel = 12, Role = nameof(DaqChannelRole.OutputCurrent), Name = "Ignored" }
};
var normalized = DaqChannelOptionsNormalizer.Normalize(channels);
Assert.Equal(8, normalized.Count);
Assert.Equal(Enumerable.Range(0, 8), normalized.Select(x => x.PhysicalChannel));
Assert.Equal(nameof(DaqChannelRole.OutputVoltage), normalized[0].Role);
Assert.Equal(nameof(DaqChannelRole.Unconfigured), normalized[1].Role);
Assert.Equal(nameof(DaqChannelRole.InputCurrent), normalized[3].Role);
Assert.Equal("CH1", normalized[1].Name);
}
[Fact]
public void Normalize_ReplacesInvalidRoleAndPreservesCalibrationFields()
{
var channels = new[]
{
new DaqChannelOptions
{
PhysicalChannel = 0,
Role = "bad-role",
Name = "Vout",
Scale = 2.5,
Offset = -0.1,
IsCalibrated = true
}
};
var normalized = DaqChannelOptionsNormalizer.Normalize(channels);
Assert.Equal(nameof(DaqChannelRole.Unconfigured), normalized[0].Role);
Assert.Equal("Vout", normalized[0].Name);
Assert.Equal(2.5, normalized[0].Scale);
Assert.Equal(-0.1, normalized[0].Offset);
Assert.True(normalized[0].IsCalibrated);
}
[Fact]
public void NormalizedChannels_SerializeRoleAndCalibrationValues()
{
var options = new DeviceOptions();
options.Daq.Channels = DaqChannelOptionsNormalizer.Normalize(new[]
{
new DaqChannelOptions
{
PhysicalChannel = 0,
Role = nameof(DaqChannelRole.OutputVoltage),
Name = "输出电压",
Scale = 1,
IsCalibrated = true
}
});
string json = JsonSerializer.Serialize(new { Devices = options });
Assert.Contains("\"PhysicalChannel\":0", json);
Assert.Contains("\"Role\":\"OutputVoltage\"", json);
Assert.Contains("\"IsCalibrated\":true", json);
Assert.Contains("\"PhysicalChannel\":7", json);
}
}