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 {
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 programConfigValue) {
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 programConfigValue.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,32 +7,30 @@ using System.Threading.Tasks;
namespace BenchtopParser {
public class TransducerVerify {
public Dictionary<int, Transducer> transducers = new();
public int indent = 26; // Default 26 from seeing files, overridden later in SetIndent for safety
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() {
return;
}
public TransducerVerify(String transducer_verify_value) {
string separator = "===============================================================";
List<String> documentLines = transducer_verify_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList();
public TransducerVerify(string transducer_verify_value) {
const string separator = "===============================================================";
List<string> documentLines = transducer_verify_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList();
// Check if proper document
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");
}
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

@ -15,12 +15,12 @@ namespace BenchtopParserTests {
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

@ -11,8 +11,8 @@ namespace BenchtopParserTests {
[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"));
Assert.That(tv.Transducers[1].Id, Is.EqualTo(1));
Assert.That(tv.Transducers[1].Name, Is.EqualTo("CTS D34-442 115PSIA"));
}
[Test]
@ -25,13 +25,13 @@ namespace BenchtopParserTests {
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));
Assert.That(tv.Indent, Is.EqualTo(26));
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");
Assert.That(tv.indent, Is.EqualTo(11));
Assert.That(tv.Indent, Is.EqualTo(11));
}
[Test]