new split line, split line tests, indent tests, separators, instrument struct, Setinstrument test

This commit is contained in:
Tyrel Souza 2022-07-27 00:35:42 -04:00
parent d96091e5c6
commit ad278e13c9
4 changed files with 71 additions and 12 deletions

View File

@ -5,8 +5,9 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace BenchtopParser { namespace BenchtopParser {
public class HardwareCalibrationReport { public class HardwareCalibrationReport
public Dictionary<int, Transducer> Transducers = new(); {
public InstrumentInfo Instrument;
public int Indent = 45; // Default 26 from seeing files, overridden later in SetIndent for safety public int Indent = 45; // Default 26 from seeing files, overridden later in SetIndent for safety
/// Split the string, and clean up whitespace, returns the Name and Value /// Split the string, and clean up whitespace, returns the Name and Value
@ -16,6 +17,12 @@ namespace BenchtopParser {
line[(Indent)..] line[(Indent)..]
}; };
} }
public string[] SplitLine(int indent, string line) {
return new[] {
line[..indent].Trim(),
line[(indent)..]
};
}
public void SetIndent(string line) { public void SetIndent(string line) {
const int endOfCurrentTransducerKey = 10; // "Transducer".Length calculation const int endOfCurrentTransducerKey = 10; // "Transducer".Length calculation
@ -29,11 +36,52 @@ namespace BenchtopParser {
public HardwareCalibrationReport() public HardwareCalibrationReport()
{ {
Instrument = new InstrumentInfo("UNSET", "DATA", "12:00:00", "12/31/1969");
} }
public HardwareCalibrationReport(string fileText) public HardwareCalibrationReport(string fileText)
{ {
const string l0Separator = "===============================================================";
const string l1Separator = "===========================================================";
List<string> documentLines = fileText.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList();
SetInstrumentInfo(documentLines);
if (documentLines[0] != "|| Hardware Calibration Report ||")
{
throw new Exception("Invalid Hardware Calibration Report, please provide a Hardware Calibration Report file data");
}
}
public void SetInstrumentInfo(List<string> documentLines)
{
if (documentLines[0] != "|| Instrument Info ||")
{
throw new Exception(
"Invalid Hardware Calibration Report, please provide a Hardware Calibration Report file data");
}
string instrumentName = SplitLine(39, documentLines[2])[1];
string serialNumber = SplitLine(39, documentLines[3])[1];
string time = SplitLine(39, documentLines[4])[1];
string date = SplitLine(39, documentLines[5])[1];
Instrument = new(instrumentName, serialNumber, time, date);
for (int i = 0; i <= 7; i++)
{
documentLines.RemoveAt(0);
}
} }
} }
public struct InstrumentInfo
{
public InstrumentInfo(string name, string serialNumber, string time, string date)
{
Name = name;
SerialNumber = serialNumber;
Time = Convert.ToDateTime($"{date} {time}");
}
public string Name { get; }
public string SerialNumber { get; }
public DateTime Time { get; }
}
} }

View File

@ -71,10 +71,10 @@ namespace BenchtopParser {
public class ProgramConfig { public class ProgramConfig {
public Dictionary<String, Program> Programs = new(); public Dictionary<String, Program> Programs = new();
public ProgramConfig(string programConfigValue) { public ProgramConfig(string fileText) {
Program? current = null; Program? current = null;
foreach (string line in programConfigValue.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) { foreach (string line in fileText.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) {
List<string> configLine = line.Split("\t").ToList(); List<string> configLine = line.Split("\t").ToList();
if (configLine.Count < 2) { if (configLine.Count < 2) {
throw new Exception("Invalid Program Config, please provide an I28 Program Config file data"); throw new Exception("Invalid Program Config, please provide an I28 Program Config file data");

View File

@ -28,9 +28,9 @@ namespace BenchtopParser {
public TransducerVerify() { public TransducerVerify() {
} }
public TransducerVerify(string transducerVerifyValue) { public TransducerVerify(string fileText) {
const string separator = "==============================================================="; const string separator = "===============================================================";
List<string> documentLines = transducerVerifyValue.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList(); List<string> documentLines = fileText.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList();
// Check if proper document // Check if proper document
string title = documentLines[0]; string title = documentLines[0];

View File

@ -1,24 +1,35 @@
namespace BenchtopParserTests { namespace BenchtopParserTests {
public class HardwareCalibrationReport_Tests { public class HardwareCalibrationReport_Tests {
public String HardwareCalibrationReportValue; public String HardwareCalibrationReportValue;
private List<string> DocumentLines;
[SetUp] [SetUp]
public void Setup() { public void Setup() {
HardwareCalibrationReportValue = File.ReadAllText( HardwareCalibrationReportValue = File.ReadAllText(
Path.Combine(TestContext.CurrentContext.TestDirectory, @"TestFiles\Hardware Calibration Report.txt") Path.Combine(TestContext.CurrentContext.TestDirectory, @"TestFiles\Hardware Calibration Report.txt")
); );
DocumentLines = HardwareCalibrationReportValue.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList();
} }
[Test]
public void Test_HardwareCalibrationReport_SetInstrumentInfo() {
DocumentLines.RemoveRange(7, DocumentLines.Count-8); // trim extra not needed for test
BenchtopParser.HardwareCalibrationReport hcr = new();
Assert.That(hcr.Instrument.Name, Is.EqualTo("UNSET"));
hcr.SetInstrumentInfo(DocumentLines);
Assert.That(hcr.Instrument.Name, Is.EqualTo("Chassis2 Adult"));
}
[Test] [Test]
public void Test_HardwareCalibrationReport() { public void Test_HardwareCalibrationReport() {
BenchtopParser.HardwareCalibrationReport tv = new(HardwareCalibrationReportValue); BenchtopParser.HardwareCalibrationReport hcr = new(HardwareCalibrationReportValue);
Assert.That(tv.Transducers[1].Id, Is.EqualTo(1));
Assert.That(tv.Transducers[1].Name, Is.EqualTo("CTS D34-442 115PSIA"));
} }
[Test] [Test]
public void Test_HardwareCalibrationReport_Throw_InvalidProgram() { public void Test_HardwareCalibrationReport_Throw_InvalidProgram() {
Exception ex = Assert.Throws<Exception>(delegate { new BenchtopParser.HardwareCalibrationReport("broken"); }); Exception ex = Assert.Throws<Exception>(delegate { new BenchtopParser.HardwareCalibrationReport("broken"); });
Assert.That(ex.Message, Is.EqualTo("Invalid Transducer Verify Report, please provide a Transducer Verify Report file data")); Assert.That(ex.Message, Is.EqualTo("Invalid Hardware Calibration Report, please provide a Hardware Calibration Report file data"));
} }
[Test] [Test]