From 2655552f56055596fb2a1abf37ba71a7b2fe1463 Mon Sep 17 00:00:00 2001 From: Fredrik Eilertsen Date: Tue, 5 Apr 2022 14:20:34 +0200 Subject: [PATCH 1/3] Add parameter argument to ArgumentExceptions --- Solutions/Ais.Net/Ais/Net/NmeaLineParser.cs | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Solutions/Ais.Net/Ais/Net/NmeaLineParser.cs b/Solutions/Ais.Net/Ais/Net/NmeaLineParser.cs index 043375d..deda3c1 100644 --- a/Solutions/Ais.Net/Ais/Net/NmeaLineParser.cs +++ b/Solutions/Ais.Net/Ais/Net/NmeaLineParser.cs @@ -47,7 +47,7 @@ public NmeaLineParser(ReadOnlySpan line, bool throwWhenTagBlockContainsUnk if (tagBlockEndIndex < 0) { - throw new ArgumentException("Invalid data. Unclosed tag block"); + throw new ArgumentException("Invalid data. Unclosed tag block", nameof(line)); } this.TagBlockAsciiWithoutDelimiters = line.Slice(1, tagBlockEndIndex); @@ -76,12 +76,12 @@ public NmeaLineParser(ReadOnlySpan line, bool throwWhenTagBlockContainsUnk // So that's 18 characters before we get to any payload. if (this.Sentence.Length < 18) { - throw new ArgumentException("Invalid data. The message appears to be missing some characters - it may have been corrupted or truncated."); + throw new ArgumentException("Invalid data. The message appears to be missing some characters - it may have been corrupted or truncated.", nameof(line)); } if (this.Sentence[0] != ExclamationMark) { - throw new ArgumentException("Invalid data. Expected '!' at sentence start"); + throw new ArgumentException("Invalid data. Expected '!' at sentence start", nameof(line)); } byte talkerFirstChar = this.Sentence[1]; @@ -99,19 +99,19 @@ public NmeaLineParser(ReadOnlySpan line, bool throwWhenTagBlockContainsUnk (byte)'S' => TalkerId.LimitedBaseStation, (byte)'T' => TalkerId.TransmittingStation, (byte)'X' => TalkerId.RepeaterStation, - _ => throw new ArgumentException("Invalid data. Unrecognized talker id - cannot start with " + talkerFirstChar), + _ => throw new ArgumentException("Invalid data. Unrecognized talker id - cannot start with " + talkerFirstChar, nameof(line)), }, (byte)'B' => talkerSecondChar switch { (byte)'S' => TalkerId.DeprecatedBaseStation, - _ => throw new ArgumentException("Invalid data. Unrecognized talker id - cannot end with " + talkerSecondChar), + _ => throw new ArgumentException("Invalid data. Unrecognized talker id - cannot end with " + talkerSecondChar, nameof(line)), }, (byte)'S' => talkerSecondChar switch { (byte)'A' => TalkerId.PhysicalShoreStation, - _ => throw new ArgumentException("Invalid data. Unrecognized talker id - cannot end with " + talkerSecondChar), + _ => throw new ArgumentException("Invalid data. Unrecognized talker id - cannot end with " + talkerSecondChar, nameof(line)), }, _ => throw new ArgumentException("Invalid data. Unrecognized talker id"), @@ -126,12 +126,12 @@ public NmeaLineParser(ReadOnlySpan line, bool throwWhenTagBlockContainsUnk } else { - throw new ArgumentException("Invalid data. Unrecognized origin in AIS talker ID - must be VDM or VDO"); + throw new ArgumentException("Invalid data. Unrecognized origin in AIS talker ID - must be VDM or VDO", nameof(line)); } if (this.Sentence[6] != (byte)',') { - throw new ArgumentException("Invalid data. Talker ID must be followed by ','"); + throw new ArgumentException("Invalid data. Talker ID must be followed by ','", nameof(line)); } ReadOnlySpan remainingFields = this.Sentence.Slice(7); @@ -148,7 +148,7 @@ public NmeaLineParser(ReadOnlySpan line, bool throwWhenTagBlockContainsUnk if (nextComma > 1) { - throw new ArgumentException("Invalid data. Channel code must be only one character"); + throw new ArgumentException("Invalid data. Channel code must be only one character", nameof(line)); } this.ChannelCode = nextComma == 0 ? default : (char)remainingFields[0]; @@ -158,7 +158,7 @@ public NmeaLineParser(ReadOnlySpan line, bool throwWhenTagBlockContainsUnk if (nextComma < 0 || remainingFields.Length <= (nextComma + 1) || !char.IsDigit((char)remainingFields[nextComma + 1])) { - throw new ArgumentException("Invalid data. Payload padding field not present - the message may have been corrupted or truncated"); + throw new ArgumentException("Invalid data. Payload padding field not present - the message may have been corrupted or truncated", nameof(line)); } this.Payload = remainingFields.Slice(0, nextComma); @@ -167,7 +167,7 @@ public NmeaLineParser(ReadOnlySpan line, bool throwWhenTagBlockContainsUnk if (remainingFields.Length < 4) { - throw new ArgumentException("Invalid data. Payload checksum not present - the message may have been corrupted or truncated"); + throw new ArgumentException("Invalid data. Payload checksum not present - the message may have been corrupted or truncated", nameof(line)); } this.Padding = (uint)GetSingleDigitField(ref remainingFields, true); From 813a38f70d89e3ab856edd24c225898d96802c26 Mon Sep 17 00:00:00 2001 From: Fredrik Eilertsen Date: Thu, 7 Apr 2022 14:21:13 +0200 Subject: [PATCH 2/3] Add xml comment for ArgumentException --- Solutions/Ais.Net/Ais/Net/NmeaLineParser.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Solutions/Ais.Net/Ais/Net/NmeaLineParser.cs b/Solutions/Ais.Net/Ais/Net/NmeaLineParser.cs index deda3c1..c0e0047 100644 --- a/Solutions/Ais.Net/Ais/Net/NmeaLineParser.cs +++ b/Solutions/Ais.Net/Ais/Net/NmeaLineParser.cs @@ -35,6 +35,7 @@ public NmeaLineParser(ReadOnlySpan line) /// Ignore non-standard and unsupported tag block field types. Useful when working with /// data sources that add non-standard fields. /// + /// contains unparsable data. public NmeaLineParser(ReadOnlySpan line, bool throwWhenTagBlockContainsUnknownFields) { this.Line = line; From 04e2f39c4a8776c73d3636ded12e17ceacd72c37 Mon Sep 17 00:00:00 2001 From: Fredrik Eilertsen Date: Tue, 19 Apr 2022 14:36:57 +0200 Subject: [PATCH 3/3] Add parameter to feature specs --- .../Net/Specs/NmeaStreamParserSpecsSteps.cs | 10 +- .../Net/Specs/ParsePayloadSpecs.feature.cs | 30 +-- .../Net/Specs/SentenceLayerSpecs.feature.cs | 174 ++++-------------- 3 files changed, 52 insertions(+), 162 deletions(-) diff --git a/Solutions/Ais.Net.Specs/Ais/Net/Specs/NmeaStreamParserSpecsSteps.cs b/Solutions/Ais.Net.Specs/Ais/Net/Specs/NmeaStreamParserSpecsSteps.cs index f4f829a..b13c5a3 100644 --- a/Solutions/Ais.Net.Specs/Ais/Net/Specs/NmeaStreamParserSpecsSteps.cs +++ b/Solutions/Ais.Net.Specs/Ais/Net/Specs/NmeaStreamParserSpecsSteps.cs @@ -124,7 +124,7 @@ public void ThenTheLineErrorReportShouldIncludeAnExceptionReportingThatTheExpect Assert.IsInstanceOf(call.Error); var e = (ArgumentException)call.Error; - Assert.AreEqual("Invalid data. Expected '!' at sentence start", e.Message); + Assert.AreEqual("Invalid data. Expected '!' at sentence start (Parameter 'line')", e.Message); } [Then("the message error report (.*) should include an exception reporting that the message appears to be incomplete")] @@ -134,7 +134,7 @@ public void ThenTheMessageErrorReportShouldIncludeAnExceptionReportingThatTheMes Assert.IsInstanceOf(call.Error); var e = (ArgumentException)call.Error; - Assert.AreEqual("Invalid data. The message appears to be missing some characters - it may have been corrupted or truncated.", e.Message); + Assert.AreEqual("Invalid data. The message appears to be missing some characters - it may have been corrupted or truncated. (Parameter 'line')", e.Message); } [Then("the message error report (.*) should include an exception reporting that the padding is missing")] @@ -144,7 +144,7 @@ public void ThenTheMessageErrorReportShouldIncludeAnExceptionReportingThatThePad Assert.IsInstanceOf(call.Error); var e = (ArgumentException)call.Error; - Assert.AreEqual("Invalid data. Payload padding field not present - the message may have been corrupted or truncated", e.Message); + Assert.AreEqual("Invalid data. Payload padding field not present - the message may have been corrupted or truncated (Parameter 'line')", e.Message); } [Then("the message error report (.*) should include an exception reporting that the checksum is missing")] @@ -154,7 +154,7 @@ public void ThenTheMessageErrorReportShouldIncludeAnExceptionReportingThatTheChe Assert.IsInstanceOf(call.Error); var e = (ArgumentException)call.Error; - Assert.AreEqual("Invalid data. Payload checksum not present - the message may have been corrupted or truncated", e.Message); + Assert.AreEqual("Invalid data. Payload checksum not present - the message may have been corrupted or truncated (Parameter 'line')", e.Message); } [Then("the message error report (.*) should include an exception reporting that the expected exclamation mark is missing")] @@ -164,7 +164,7 @@ public void ThenTheMessageErrorReportShouldIncludeAnExceptionReportingThatTheExp Assert.IsInstanceOf(call.Error); var e = (ArgumentException)call.Error; - Assert.AreEqual("Invalid data. Expected '!' at sentence start", e.Message); + Assert.AreEqual("Invalid data. Expected '!' at sentence start (Parameter 'line')", e.Message); } [Then("the message error report (.*) should include an exception reporting that an unrecognized field is present")] diff --git a/Solutions/Ais.Net.Specs/Ais/Net/Specs/ParsePayloadSpecs.feature.cs b/Solutions/Ais.Net.Specs/Ais/Net/Specs/ParsePayloadSpecs.feature.cs index 5364de0..2116566 100644 --- a/Solutions/Ais.Net.Specs/Ais/Net/Specs/ParsePayloadSpecs.feature.cs +++ b/Solutions/Ais.Net.Specs/Ais/Net/Specs/ParsePayloadSpecs.feature.cs @@ -26,7 +26,7 @@ public partial class ParsePayloadSpecsFeature private TechTalk.SpecFlow.ITestRunner testRunner; - private string[] _featureTags = ((string[])(null)); + private static string[] featureTags = ((string[])(null)); #line 1 "ParsePayloadSpecs.feature" #line hidden @@ -36,7 +36,7 @@ public virtual void FeatureSetup() { testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner(); TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "Ais/Net/Specs", "ParsePayloadSpecs", " In order process AIS messages from an nm4 file\r\n As a developer\r\n I wan" + - "t the NmeaPayloadParser to be able report the message type", ProgrammingLanguage.CSharp, ((string[])(null))); + "t the NmeaPayloadParser to be able report the message type", ProgrammingLanguage.CSharp, featureTags); testRunner.OnFeatureStart(featureInfo); } @@ -48,28 +48,28 @@ public virtual void FeatureTearDown() } [NUnit.Framework.SetUpAttribute()] - public virtual void TestInitialize() + public void TestInitialize() { } [NUnit.Framework.TearDownAttribute()] - public virtual void TestTearDown() + public void TestTearDown() { testRunner.OnScenarioEnd(); } - public virtual void ScenarioInitialize(TechTalk.SpecFlow.ScenarioInfo scenarioInfo) + public void ScenarioInitialize(TechTalk.SpecFlow.ScenarioInfo scenarioInfo) { testRunner.OnScenarioInitialize(scenarioInfo); testRunner.ScenarioContext.ScenarioContainer.RegisterInstanceAs(NUnit.Framework.TestContext.CurrentContext); } - public virtual void ScenarioStart() + public void ScenarioStart() { testRunner.OnScenarioStart(); } - public virtual void ScenarioCleanup() + public void ScenarioCleanup() { testRunner.CollectScenarioErrors(); } @@ -88,28 +88,18 @@ public virtual void ScenarioCleanup() [NUnit.Framework.TestCaseAttribute("B3o8B<00F8:0h694gOtbgwqUoP06", "0", "18", null)] [NUnit.Framework.TestCaseAttribute("H000000000000000000000000000", "0", "24", null)] [NUnit.Framework.TestCaseAttribute("H3m9b308tL5