ScaleTestMatzen: Add more tests
This commit is contained in:
@ -44,6 +44,8 @@ namespace Elwig.Helpers.Weighing {
|
|||||||
} else {
|
} else {
|
||||||
throw new ArgumentException("Unsupported scheme");
|
throw new ArgumentException("Unsupported scheme");
|
||||||
}
|
}
|
||||||
|
Stream.WriteTimeout = 250;
|
||||||
|
Stream.ReadTimeout = 11000;
|
||||||
|
|
||||||
if (empty != null) {
|
if (empty != null) {
|
||||||
var parts = empty.Split(':');
|
var parts = empty.Split(':');
|
||||||
@ -105,7 +107,7 @@ namespace Elwig.Helpers.Weighing {
|
|||||||
line = await reader.ReadLineAsync();
|
line = await reader.ReadLineAsync();
|
||||||
if (LogPath != null) await File.AppendAllTextAsync(LogPath, $"{line}\r\n");
|
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");
|
throw new IOException("Invalid response from scale");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,29 +8,45 @@ namespace Tests.WeighingTests {
|
|||||||
private SystecScale? Scale;
|
private SystecScale? Scale;
|
||||||
|
|
||||||
private static (string, bool) ScaleHandler(string req, int weight, string? error, int identNr) {
|
private static (string, bool) ScaleHandler(string req, int weight, string? error, int identNr) {
|
||||||
bool incr = false;
|
var modes = error?.Split(';') ?? [];
|
||||||
if (!req.StartsWith('<') || !req.EndsWith('>'))
|
var overloaded = modes.Contains("overloaded");
|
||||||
return ("<31>\r\n", incr);
|
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];
|
req = req[1..^1];
|
||||||
|
|
||||||
|
bool incr;
|
||||||
if (req.Length > 3) {
|
if (req.Length > 3) {
|
||||||
return ("<32>\r\n", incr);
|
return ("<32>\r\n", false);
|
||||||
} else if (req.StartsWith("RN")) {
|
} else if (req.StartsWith("RN")) {
|
||||||
incr = true;
|
incr = true;
|
||||||
} else if (req.StartsWith("RM")) {
|
} else if (req.StartsWith("RM")) {
|
||||||
incr = false;
|
incr = false;
|
||||||
} else {
|
} else {
|
||||||
return ("<32>\r\n", incr);
|
return ("<32>\r\n", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
var modes = error?.Split(';') ?? [];
|
if (overloaded) {
|
||||||
if (weight == 0) {
|
return ("<12>\r\n", false);
|
||||||
|
} else if (weight == 0) {
|
||||||
incr = false;
|
incr = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool moving = modes.Contains("moving");
|
if (moving && incr)
|
||||||
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 ("<13>\r\n", false);
|
||||||
return ($"<{data}{Elwig.Helpers.Utils.CalcCrc16Modbus(data),8}>\r\n", incr);
|
|
||||||
|
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]
|
[OneTimeSetUp]
|
||||||
@ -89,5 +105,43 @@ namespace Tests.WeighingTests {
|
|||||||
Date = Utils.Today, Time = Utils.Time,
|
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