From ad278e13c9165fd02f3c6bf484e3149969b2f0cd Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Wed, 27 Jul 2022 00:35:42 -0400 Subject: [PATCH] new split line, split line tests, indent tests, separators, instrument struct, Setinstrument test --- BenchtopParser/HardwareCalibrationReport.cs | 56 +++++++++++++++++-- BenchtopParser/ProgramConfig.cs | 4 +- BenchtopParser/TransducerVerify.cs | 4 +- .../HardwareCalibrationReport_Tests.cs | 19 +++++-- 4 files changed, 71 insertions(+), 12 deletions(-) diff --git a/BenchtopParser/HardwareCalibrationReport.cs b/BenchtopParser/HardwareCalibrationReport.cs index 1c1e3b0..bb1b533 100644 --- a/BenchtopParser/HardwareCalibrationReport.cs +++ b/BenchtopParser/HardwareCalibrationReport.cs @@ -5,8 +5,9 @@ using System.Text; using System.Threading.Tasks; namespace BenchtopParser { - public class HardwareCalibrationReport { - public Dictionary Transducers = new(); + public class HardwareCalibrationReport + { + public InstrumentInfo Instrument; 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 @@ -16,6 +17,12 @@ namespace BenchtopParser { line[(Indent)..] }; } + public string[] SplitLine(int indent, string line) { + return new[] { + line[..indent].Trim(), + line[(indent)..] + }; + } public void SetIndent(string line) { const int endOfCurrentTransducerKey = 10; // "Transducer".Length calculation @@ -29,11 +36,52 @@ namespace BenchtopParser { public HardwareCalibrationReport() { - + Instrument = new InstrumentInfo("UNSET", "DATA", "12:00:00", "12/31/1969"); } public HardwareCalibrationReport(string fileText) { - + const string l0Separator = "==============================================================="; + const string l1Separator = "==========================================================="; + + List 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 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; } + } } diff --git a/BenchtopParser/ProgramConfig.cs b/BenchtopParser/ProgramConfig.cs index 4705daf..fb20d3c 100644 --- a/BenchtopParser/ProgramConfig.cs +++ b/BenchtopParser/ProgramConfig.cs @@ -71,10 +71,10 @@ namespace BenchtopParser { public class ProgramConfig { public Dictionary Programs = new(); - public ProgramConfig(string programConfigValue) { + public ProgramConfig(string fileText) { 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 configLine = line.Split("\t").ToList(); if (configLine.Count < 2) { throw new Exception("Invalid Program Config, please provide an I28 Program Config file data"); diff --git a/BenchtopParser/TransducerVerify.cs b/BenchtopParser/TransducerVerify.cs index 5ad0663..e89f050 100644 --- a/BenchtopParser/TransducerVerify.cs +++ b/BenchtopParser/TransducerVerify.cs @@ -28,9 +28,9 @@ namespace BenchtopParser { public TransducerVerify() { } - public TransducerVerify(string transducerVerifyValue) { + public TransducerVerify(string fileText) { const string separator = "==============================================================="; - List documentLines = transducerVerifyValue.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList(); + List documentLines = fileText.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList(); // Check if proper document string title = documentLines[0]; diff --git a/BenchtopParserTests/HardwareCalibrationReport_Tests.cs b/BenchtopParserTests/HardwareCalibrationReport_Tests.cs index 5f1b04e..445868c 100644 --- a/BenchtopParserTests/HardwareCalibrationReport_Tests.cs +++ b/BenchtopParserTests/HardwareCalibrationReport_Tests.cs @@ -1,24 +1,35 @@ namespace BenchtopParserTests { public class HardwareCalibrationReport_Tests { public String HardwareCalibrationReportValue; + private List DocumentLines; [SetUp] public void Setup() { HardwareCalibrationReportValue = File.ReadAllText( 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] public void Test_HardwareCalibrationReport() { - BenchtopParser.HardwareCalibrationReport tv = new(HardwareCalibrationReportValue); - Assert.That(tv.Transducers[1].Id, Is.EqualTo(1)); - Assert.That(tv.Transducers[1].Name, Is.EqualTo("CTS D34-442 115PSIA")); + BenchtopParser.HardwareCalibrationReport hcr = new(HardwareCalibrationReportValue); } [Test] public void Test_HardwareCalibrationReport_Throw_InvalidProgram() { Exception ex = Assert.Throws(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]