ScaleTestMatzen: Add more tests
This commit is contained in:
@ -44,6 +44,8 @@ namespace Elwig.Helpers.Weighing {
|
||||
} else {
|
||||
throw new ArgumentException("Unsupported scheme");
|
||||
}
|
||||
Stream.WriteTimeout = 250;
|
||||
Stream.ReadTimeout = 11000;
|
||||
|
||||
if (empty != null) {
|
||||
var parts = empty.Split(':');
|
||||
@ -105,7 +107,7 @@ namespace Elwig.Helpers.Weighing {
|
||||
line = await reader.ReadLineAsync();
|
||||
if (LogPath != null) await File.AppendAllTextAsync(LogPath, $"{line}\r\n");
|
||||
}
|
||||
if (line == null || line.Length < 4 || !line.StartsWith("<") || !line.EndsWith(">")) {
|
||||
if (line == null || line.Length < 4 || !line.StartsWith('<') || !line.EndsWith('>')) {
|
||||
throw new IOException("Invalid response from scale");
|
||||
}
|
||||
|
||||
|
@ -8,29 +8,45 @@ namespace Tests.WeighingTests {
|
||||
private SystecScale? Scale;
|
||||
|
||||
private static (string, bool) ScaleHandler(string req, int weight, string? error, int identNr) {
|
||||
bool incr = false;
|
||||
if (!req.StartsWith('<') || !req.EndsWith('>'))
|
||||
return ("<31>\r\n", incr);
|
||||
var modes = error?.Split(';') ?? [];
|
||||
var overloaded = modes.Contains("overloaded");
|
||||
var moving = modes.Contains("moving");
|
||||
var invalid = modes.Contains("invalid");
|
||||
var crc = modes.Contains("crc");
|
||||
var unit = modes.Contains("unit");
|
||||
|
||||
if (invalid) {
|
||||
return ("abcd\r\n", false);
|
||||
} else if (!req.StartsWith('<') || !req.EndsWith('>')) {
|
||||
return ("<31>\r\n", false);
|
||||
}
|
||||
req = req[1..^1];
|
||||
|
||||
bool incr;
|
||||
if (req.Length > 3) {
|
||||
return ("<32>\r\n", incr);
|
||||
return ("<32>\r\n", false);
|
||||
} else if (req.StartsWith("RN")) {
|
||||
incr = true;
|
||||
} else if (req.StartsWith("RM")) {
|
||||
incr = false;
|
||||
} else {
|
||||
return ("<32>\r\n", incr);
|
||||
return ("<32>\r\n", false);
|
||||
}
|
||||
|
||||
var modes = error?.Split(';') ?? [];
|
||||
if (weight == 0) {
|
||||
if (overloaded) {
|
||||
return ("<12>\r\n", false);
|
||||
} else if (weight == 0) {
|
||||
incr = false;
|
||||
}
|
||||
|
||||
bool moving = modes.Contains("moving");
|
||||
string data = $"00{(moving ? 1 : 0)}0{DateTime.Now:dd.MM.yyHH:mm}{(incr ? identNr : 0),4}1{weight,8}{0,8}{weight,8}kg {1,3}";
|
||||
return ($"<{data}{Elwig.Helpers.Utils.CalcCrc16Modbus(data),8}>\r\n", incr);
|
||||
if (moving && incr)
|
||||
return ("<13>\r\n", false);
|
||||
|
||||
string data = $"00{(moving ? 1 : 0)}0{DateTime.Now:dd.MM.yyHH:mm}{(incr ? identNr : 0),4}1" +
|
||||
$"{weight,8}{0,8}{weight,8}{(unit ? "lb" : "kg")} {1,3}";
|
||||
ushort checksum = Elwig.Helpers.Utils.CalcCrc16Modbus(data);
|
||||
if (crc) checksum += 10;
|
||||
return ($"<{data}{checksum,8}>\r\n", incr);
|
||||
}
|
||||
|
||||
[OneTimeSetUp]
|
||||
@ -89,5 +105,43 @@ namespace Tests.WeighingTests {
|
||||
Date = Utils.Today, Time = Utils.Time,
|
||||
}));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_03_Moving() {
|
||||
Mock!.Weight = 1_000;
|
||||
Mock!.Error = "moving";
|
||||
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
|
||||
Assert.That(ex.Message, Contains.Substring("Waage in Bewegung"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_04_Overloaded() {
|
||||
Mock!.Weight = 10_000;
|
||||
Mock!.Error = "overloaded";
|
||||
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
|
||||
Assert.That(ex.Message, Contains.Substring("Waage in Überlast"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_05_InvalidResponse() {
|
||||
Mock!.Weight = 1_000;
|
||||
Mock!.Error = "invalid";
|
||||
Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_06_InvalidCrc() {
|
||||
Mock!.Weight = 1_000;
|
||||
Mock!.Error = "crc";
|
||||
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
|
||||
Assert.That(ex.Message, Contains.Substring("Invalid CRC16 checksum"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_07_InvalidUnit() {
|
||||
Mock!.Weight = 1_000;
|
||||
Mock!.Error = "unit";
|
||||
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user