Jetbrains Rider suggested changes

This commit is contained in:
Tyrel Souza 2022-07-26 20:13:20 -04:00
parent e20a064fa9
commit 2a6d30bf50
5 changed files with 70 additions and 89 deletions

View File

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

View File

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

View File

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

View File

@ -15,12 +15,12 @@ namespace BenchtopParserTests {
Assert.IsNotNull(programConfigs); Assert.IsNotNull(programConfigs);
// Check that group collecting works - // Check that group collecting works -
Assert.That(programConfigs.programs["P1"].I["3"].value, Is.EqualTo("Unassigned")); 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"].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"].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"].Unit, Is.EqualTo("mbar"));
Assert.That(programConfigs.programs["P1"].P["Minimum Pressure"].type, Is.EqualTo("f")); Assert.That(programConfigs.Programs["P1"].P["Minimum Pressure"].Type, Is.EqualTo("f"));
} }
[Test] [Test]

View File

@ -11,8 +11,8 @@ namespace BenchtopParserTests {
[Test] [Test]
public void Test_TransducerVerify() { public void Test_TransducerVerify() {
BenchtopParser.TransducerVerify tv = new(transducer_verify_value); BenchtopParser.TransducerVerify tv = new(transducer_verify_value);
Assert.That(tv.transducers[1].id, Is.EqualTo(1)); Assert.That(tv.Transducers[1].Id, Is.EqualTo(1));
Assert.That(tv.transducers[1].name, Is.EqualTo("CTS D34-442 115PSIA")); Assert.That(tv.Transducers[1].Name, Is.EqualTo("CTS D34-442 115PSIA"));
} }
[Test] [Test]
@ -25,13 +25,13 @@ namespace BenchtopParserTests {
public void Test_TransducerVerify_SetIndent() { public void Test_TransducerVerify_SetIndent() {
BenchtopParser.TransducerVerify tv = new(); BenchtopParser.TransducerVerify tv = new();
tv.SetIndent(1, "Transducer 1 CTS D34-442 115PSIA"); tv.SetIndent(1, "Transducer 1 CTS D34-442 115PSIA");
Assert.That(tv.indent, Is.EqualTo(26)); Assert.That(tv.Indent, Is.EqualTo(26));
tv.SetIndent(1, "Transducer 1 CTS D34-442 115PSIA"); tv.SetIndent(1, "Transducer 1 CTS D34-442 115PSIA");
Assert.That(tv.indent, Is.EqualTo(20)); Assert.That(tv.Indent, Is.EqualTo(20));
tv.SetIndent(1, "Transducer 10CTS D34-442 115PSIA"); tv.SetIndent(1, "Transducer 10CTS D34-442 115PSIA");
Assert.That(tv.indent, Is.EqualTo(11)); Assert.That(tv.Indent, Is.EqualTo(11));
} }
[Test] [Test]