Compare commits

...

5 Commits

Author SHA1 Message Date
Tyrel Souza ad278e13c9 new split line, split line tests, indent tests, separators, instrument struct, Setinstrument test 2022-07-27 00:35:42 -04:00
Tyrel Souza d96091e5c6 Added HardwareCalibrationReport.cs and Tests. Indent and Split tests 2022-07-26 23:12:24 -04:00
Tyrel Souza f8b773da1c more Rider changes 2022-07-26 22:33:45 -04:00
Tyrel Souza 2a6d30bf50 Jetbrains Rider suggested changes 2022-07-26 20:13:20 -04:00
Tyrel Souza e20a064fa9 testing SetIndent and SplitLine 2022-07-26 02:04:19 -04:00
8 changed files with 280 additions and 112 deletions

View File

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BenchtopParser {
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
public string[] SplitLine(string line) {
return new[] {
line[..Indent].Trim(),
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
int leadingSpaces = line.TakeWhile(c => c == ' ').Count();
string restOfLine = line[(leadingSpaces + endOfCurrentTransducerKey)..];
int additionalSpaces = restOfLine.TakeWhile(c => c == ' ').Count();
Indent = leadingSpaces + endOfCurrentTransducerKey + additionalSpaces;
string endOfLine = line[Indent..];
Console.Write(endOfLine);
}
public HardwareCalibrationReport()
{
Instrument = new InstrumentInfo("UNSET", "DATA", "12:00:00", "12/31/1969");
}
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

@ -7,31 +7,24 @@ using System.Threading.Tasks;
namespace BenchtopParser {
public class Program {
public String? ProgramNumber { get; set; }
public string? ProgramNumber { get; set; }
// Config Groups. TODO: rename to w/e the heck they are.
public Dictionary<String, Configuration> A = new();
public Dictionary<String, Configuration> I = new();
public Dictionary<String, Configuration> M = new();
public Dictionary<String, Configuration> O = new();
public Dictionary<String, Configuration> P = new();
public Dictionary<String, Configuration> R = new();
public Dictionary<String, Configuration> S = new();
public Dictionary<String, Configuration> T = new();
public Dictionary<String, Configuration> U = new();
public Dictionary<String, Configuration> X = new();
public string Name() {
if (M != null && M["Name"] != null && M["Name"].value != null) {
return M["Name"].value;
}
return "";
}
public Dictionary<string, Configuration> A = new();
public Dictionary<string, Configuration> I = new();
public Dictionary<string, Configuration> M = new();
public Dictionary<string, Configuration> O = new();
public Dictionary<string, Configuration> P = new();
public Dictionary<string, Configuration> R = new();
public Dictionary<string, Configuration> S = new();
public Dictionary<string, Configuration> T = new();
public Dictionary<string, Configuration> U = new();
public Dictionary<string, Configuration> X = new();
public void AddToGroup(String group, Configuration config) {
// UGLY - but helpful later.
// TODO: Find out what the config names are and MAKE A MAP
string name = config.name;
string name = config.Name;
if (name == null) {
throw new Exception("Name is null, adding before Config has set name");
}
@ -53,36 +46,36 @@ namespace BenchtopParser {
}
public class Configuration {
public String name { get; set; }
public String? value { get; set; }
public String? unit { get; set; }
public string Name { get; set; }
public string? Value { get; set; }
public string? Unit { get; set; }
public String? type { get; set; } //possible values are: a, c, f, i. Which I presume are Array, Characters, Float, Integer
public string? Type { get; set; } //possible values are: a, c, f, i. Which I presume are Array, Characters, Float, Integer
public Configuration(string name, string? value, string? unit, string? type) {
this.name = name;
this.value = value;
this.unit = unit;
this.type = type;
Name = name;
Value = value;
Unit = unit;
Type = type;
}
public Configuration(List<String> config, String data) {
String[] nameValue = data.Split("=");
name = nameValue[0].Trim();
value = nameValue[1].Trim();
public Configuration(List<string> config, string data) {
string[] nameValue = data.Split("=");
Name = nameValue[0].Trim();
Value = nameValue[1].Trim();
unit = config[1].Trim();
type = config[2].Trim();
Unit = config[1].Trim();
Type = config[2].Trim();
}
}
public class ProgramConfig {
public Dictionary<String, Program> programs = new();
public ProgramConfig(String program_config_value) {
public Dictionary<String, Program> Programs = new();
public ProgramConfig(string fileText) {
Program? current = null;
foreach (string line in program_config_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) {
List<String> configLine = line.Split("\t").ToList();
foreach (string line in fileText.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) {
List<string> configLine = line.Split("\t").ToList();
if (configLine.Count < 2) {
throw new Exception("Invalid Program Config, please provide an I28 Program Config file data");
}
@ -100,8 +93,8 @@ namespace BenchtopParser {
}
if (configLine[0].StartsWith("Stop,")) {
// Short circuit early when stop comes.
if (current != null && current.ProgramNumber != null) {
programs.Add(current.ProgramNumber, current);
if (current?.ProgramNumber != null) {
Programs.Add(current.ProgramNumber, current);
}
current = null;
// TODO: What if multiple configs how to handle start and stop?
@ -120,16 +113,14 @@ namespace BenchtopParser {
string value = config[1];
string[] programnumber_group_data = value.Split('\\'); // snake case as its three values
string data = programnumber_group_data[2];
string[] programnumberGroupData = value.Split('\\'); // snake case as its three values
string data = programnumberGroupData[2];
if (data.StartsWith("=")) { continue; /* comment line */ }
Configuration lineConfig = new(configLine, data);
// Deal with this group logic
if (current != null) {
current.AddToGroup(programnumber_group_data[1], lineConfig);
}
current?.AddToGroup(programnumberGroupData[1], lineConfig);
}
// TODO: What if multiple configs how to handle start and stop?
}

View File

@ -7,28 +7,30 @@ using System.Threading.Tasks;
namespace BenchtopParser {
public class TransducerVerify {
public Dictionary<int, Transducer> transducers = new();
public int indent = 26;
public Dictionary<int, Transducer> Transducers = new();
public int Indent = 26; // Default 26 from seeing files, overridden later in SetIndent for safety
/// Split the string, and clean up whitespace, returns the Name and Value
public String[] SplitLine(String line) {
public string[] SplitLine(string line) {
return new[] {
line[..indent].TrimEnd(),
line[(indent + 1)..]
line[..Indent].TrimEnd(),
line[(Indent + 1)..]
};
}
public void SetIndent(int id, String line) {
public void SetIndent(int id, string line) {
int endOfCurrentTransducerKey = $"Transducer {id}".Length;
string restOfLine = line.Substring(endOfCurrentTransducerKey);
string restOfLine = line[endOfCurrentTransducerKey..];
int additionalSpaces = restOfLine.TakeWhile(c => c == ' ').Count();
// length 1 based, remove 1, then add additional spaces
this.indent = endOfCurrentTransducerKey - 1 + additionalSpaces;
Indent = endOfCurrentTransducerKey - 1 + additionalSpaces;
}
public TransducerVerify(String transducer_verify_value) {
string separator = "===============================================================";
List<String> documentLines = transducer_verify_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList();
public TransducerVerify() {
}
public TransducerVerify(string fileText) {
const string separator = "===============================================================";
List<string> documentLines = fileText.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList();
// Check if proper document
string title = documentLines[0];
@ -37,53 +39,43 @@ namespace BenchtopParser {
throw new Exception("Invalid Transducer Verify Report, please provide a Transducer Verify Report file data");
}
Dictionary<String, int> countDict = documentLines.GroupBy(p => p).ToDictionary(p => p.Key, q => q.Count());
int numTransducers = countDict[separator];
Dictionary<string, int> countDict = documentLines.GroupBy(p => p).ToDictionary(p => p.Key, q => q.Count());
int currentTransducer = 0; // ONE BASED - will add 1 later at check step, but first transducer is 1.
// Find the the indexes of each transducer start and stop
for (int i = 0; i < documentLines.Count(); i++) {
string line = documentLines[i];
foreach (var line in documentLines)
{
if (line == $"TRANSDUCER{currentTransducer + 1}") {
currentTransducer++;
continue; // Skip TITLE line and separator
}
if (line == separator) {
continue;
}
if (line == "") {
if (line is separator or "") {
// end of current transducer
continue;
}
// Add new Transducer if not present
if (transducers != null && !this.transducers.ContainsKey(currentTransducer)) {
transducers.Add(currentTransducer, new Transducer(currentTransducer));
if (!Transducers.ContainsKey(currentTransducer)) {
Transducers.Add(currentTransducer, new Transducer(currentTransducer));
// Calculating indent
SetIndent(currentTransducer, line);
transducers[currentTransducer].name = SplitLine(line)[1];
Transducers[currentTransducer].Name = SplitLine(line)[1];
continue;
}
// Add transducer parameters
string[] _line = SplitLine(line);
if (transducers != null) {
transducers[currentTransducer].parameters[_line[0]] = _line[1];
} else {
throw new Exception("Config list is null, something broke");
}
string[] lineParts = SplitLine(line);
Transducers[currentTransducer].Parameters[lineParts[0]] = lineParts[1];
}
}
}
public class Transducer {
public int id;
public String? name;
public Dictionary<String, String> parameters = new();
public int Id;
public string? Name;
public Dictionary<string, string> Parameters = new();
public Transducer(int id) {
this.id = id;
Id = id;
}
}
}

View File

@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AltCover" Version="8.3.838" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />

View File

@ -0,0 +1,75 @@
namespace BenchtopParserTests {
public class HardwareCalibrationReport_Tests {
public String HardwareCalibrationReportValue;
private List<string> 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 hcr = new(HardwareCalibrationReportValue);
}
[Test]
public void Test_HardwareCalibrationReport_Throw_InvalidProgram() {
Exception ex = Assert.Throws<Exception>(delegate { new BenchtopParser.HardwareCalibrationReport("broken"); });
Assert.That(ex.Message, Is.EqualTo("Invalid Hardware Calibration Report, please provide a Hardware Calibration Report file data"));
}
[Test]
public void Test_HardwareCalibrationReport_SetIndent() {
BenchtopParser.HardwareCalibrationReport tv = new();
tv.SetIndent(" Transducer Custom");
Assert.That(tv.Indent, Is.EqualTo(45));
tv.SetIndent(" Transducer Custom");
Assert.That(tv.Indent, Is.EqualTo(41));
tv.SetIndent(" TransducerCustom");
Assert.That(tv.Indent, Is.EqualTo(12));
}
[Test]
public void Test_HardwareCalibrationReport_SplitLine() {
BenchtopParser.HardwareCalibrationReport tv = new();
string[] split;
string context = " Transducer Custom";
tv.SetIndent(context);
split = tv.SplitLine(context);
Assert.That(tv.Indent, Is.EqualTo(45));
Assert.That(split[0], Is.EqualTo("Transducer"));
Assert.That(split[1], Is.EqualTo("Custom"));
context = " Transducer Custom";
tv.SetIndent(context);
split = tv.SplitLine(context);
Assert.That(tv.Indent, Is.EqualTo(41));
Assert.That(split[0], Is.EqualTo("Transducer"));
Assert.That(split[1], Is.EqualTo("Custom"));
context = " TransducerCustom";
tv.SetIndent(context);
split = tv.SplitLine(context);
Assert.That(tv.Indent, Is.EqualTo(12));
Assert.That(split[0], Is.EqualTo("Transducer"));
Assert.That(split[1], Is.EqualTo("Custom"));
}
}
}

View File

@ -1,26 +1,26 @@
namespace BenchtopParserTests {
public class Program_Config_Tests {
public String program_config_value;
public class ProgramConfig_Tests {
public String ProgramConfigValue;
[SetUp]
public void Setup() {
program_config_value = File.ReadAllText(
ProgramConfigValue = File.ReadAllText(
Path.Combine(TestContext.CurrentContext.TestDirectory, @"TestFiles\Program Config.txt")
);
}
[Test]
public void Test_ProgramConfig() {
BenchtopParser.ProgramConfig programConfigs = new(program_config_value);
BenchtopParser.ProgramConfig programConfigs = new(ProgramConfigValue);
Assert.IsNotNull(programConfigs);
// Check that group collecting works -
Assert.That(programConfigs.programs["P1"].I["3"].value, Is.EqualTo("Unassigned"));
Assert.That(programConfigs.programs["P1"].O["3"].value, Is.EqualTo("Test Passed"));
Assert.That(programConfigs.Programs["P1"].I["3"].Value, Is.EqualTo("Unassigned"));
Assert.That(programConfigs.Programs["P1"].O["3"].Value, Is.EqualTo("Test Passed"));
Assert.That(programConfigs.programs["P1"].P["Minimum Pressure"].value, Is.EqualTo("95.000"));
Assert.That(programConfigs.programs["P1"].P["Minimum Pressure"].unit, Is.EqualTo("mbar"));
Assert.That(programConfigs.programs["P1"].P["Minimum Pressure"].type, Is.EqualTo("f"));
Assert.That(programConfigs.Programs["P1"].P["Minimum Pressure"].Value, Is.EqualTo("95.000"));
Assert.That(programConfigs.Programs["P1"].P["Minimum Pressure"].Unit, Is.EqualTo("mbar"));
Assert.That(programConfigs.Programs["P1"].P["Minimum Pressure"].Type, Is.EqualTo("f"));
}
[Test]

View File

@ -0,0 +1,46 @@
namespace BenchtopParserTests {
public class TransducerVerify_Tests {
public String TransducerVerifyValue;
[SetUp]
public void Setup() {
TransducerVerifyValue = File.ReadAllText(
Path.Combine(TestContext.CurrentContext.TestDirectory, @"TestFiles\Transducer Verify.txt")
);
}
[Test]
public void Test_TransducerVerify() {
BenchtopParser.TransducerVerify tv = new(TransducerVerifyValue);
Assert.That(tv.Transducers[1].Id, Is.EqualTo(1));
Assert.That(tv.Transducers[1].Name, Is.EqualTo("CTS D34-442 115PSIA"));
}
[Test]
public void Test_TransducerVerify_Throw_InvalidProgram() {
Exception ex = Assert.Throws<Exception>(delegate { new BenchtopParser.TransducerVerify("broken"); });
Assert.That(ex.Message, Is.EqualTo("Invalid Transducer Verify Report, please provide a Transducer Verify Report file data"));
}
[Test]
public void Test_TransducerVerify_SetIndent() {
BenchtopParser.TransducerVerify tv = new();
tv.SetIndent(1, "Transducer 1 CTS D34-442 115PSIA");
Assert.That(tv.Indent, Is.EqualTo(26));
tv.SetIndent(1, "Transducer 1 CTS D34-442 115PSIA");
Assert.That(tv.Indent, Is.EqualTo(20));
tv.SetIndent(1, "Transducer 10CTS D34-442 115PSIA");
Assert.That(tv.Indent, Is.EqualTo(11));
}
[Test]
public void Test_TransducerVerify_SplitLine() {
BenchtopParser.TransducerVerify tv = new();
string[] split;
split = tv.SplitLine("Instrument Pressure 1 0.000 psig");
Assert.That(split[0], Is.EqualTo("Instrument Pressure 1"));
Assert.That(split[1], Is.EqualTo("0.000 psig"));
}
}
}

View File

@ -1,24 +0,0 @@
namespace BenchtopParserTests {
public class Transducer_Verify_Tests {
public String transducer_verify_value;
[SetUp]
public void Setup() {
transducer_verify_value = File.ReadAllText(
Path.Combine(TestContext.CurrentContext.TestDirectory, @"TestFiles\Transducer Verify.txt")
);
}
[Test]
public void Test_TransducerVerify() {
BenchtopParser.TransducerVerify tv = new(transducer_verify_value);
Assert.That(tv.transducers[1].id, Is.EqualTo(1));
Assert.That(tv.transducers[1].name, Is.EqualTo("CTS D34-442 115PSIA"));
}
[Test]
public void Test_TransducerVerify_Throw_InvalidProgram() {
Exception ex = Assert.Throws<Exception>(delegate { new BenchtopParser.TransducerVerify("broken"); });
Assert.That(ex.Message, Is.EqualTo("Invalid Transducer Verify Report, please provide a Transducer Verify Report file data"));
}
}
}