-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from Uight/Add-support-for-keyword-BO_TX_BU_
Add support for keyword BO_TX_BU_
- Loading branch information
Showing
9 changed files
with
325 additions
and
0 deletions.
There are no files selected for viewing
231 changes: 231 additions & 0 deletions
231
DbcParserLib.Tests/ExtraMessageTransmitterLineParserTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
using DbcParserLib.Observers; | ||
using NUnit.Framework; | ||
using System.Linq; | ||
|
||
namespace DbcParserLib.Tests | ||
{ | ||
[TestFixture] | ||
public class ExtraMessageTransmitterLineParserTests | ||
{ | ||
[Test] | ||
public void ParseOneExtraTransmitters() | ||
{ | ||
var dbcString = @" | ||
BO_ 200 TestMessage: 1 TestTransmitter | ||
SG_ Test : 0|8@1+ (0.1,0) [0|0] """" DBG | ||
BO_TX_BU_ 200 : Transmitter2;"; | ||
|
||
var failureObserver = new SimpleFailureObserver(); | ||
Parser.SetParsingFailuresObserver(failureObserver); | ||
var dbc = Parser.Parse(dbcString); | ||
var errorList = failureObserver.GetErrorList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(errorList, Has.Count.EqualTo(0)); | ||
Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.SelectMany(m => m.Signals).Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.Count, Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.First(), Is.EqualTo("Transmitter2")); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void ParseTwoExtraTransmitters() | ||
{ | ||
var dbcString = @" | ||
BO_ 200 TestMessage: 1 TestTransmitter | ||
SG_ Test : 0|8@1+ (0.1,0) [0|0] """" DBG | ||
BO_TX_BU_ 200 : Transmitter2,Transmitter3;"; | ||
|
||
|
||
var failureObserver = new SimpleFailureObserver(); | ||
Parser.SetParsingFailuresObserver(failureObserver); | ||
var dbc = Parser.Parse(dbcString); | ||
var errorList = failureObserver.GetErrorList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(errorList, Has.Count.EqualTo(0)); | ||
Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.SelectMany(m => m.Signals).Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.Count, Is.EqualTo(2)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.First(), Is.EqualTo("Transmitter2")); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.Last(), Is.EqualTo("Transmitter3")); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void MissingLineTerminationParsingErrorIsObserved() | ||
{ | ||
var dbcString = @"BO_TX_BU_ 200 : Transmitter2"; | ||
|
||
var failureObserver = new SimpleFailureObserver(); | ||
Parser.SetParsingFailuresObserver(failureObserver); | ||
var dbc = Parser.Parse(dbcString); | ||
var errorList = failureObserver.GetErrorList(); | ||
|
||
Assert.That(errorList, Has.Count.EqualTo(1)); | ||
} | ||
|
||
[Test] | ||
public void LineTerminationWithLeadingSpace() | ||
{ | ||
var dbcString = @" | ||
BO_ 200 TestMessage: 1 TestTransmitter | ||
SG_ Test : 0|8@1+ (0.1,0) [0|0] """" DBG | ||
BO_TX_BU_ 200 : Transmitter2 ;"; | ||
|
||
var failureObserver = new SimpleFailureObserver(); | ||
Parser.SetParsingFailuresObserver(failureObserver); | ||
var dbc = Parser.Parse(dbcString); | ||
var errorList = failureObserver.GetErrorList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(errorList, Has.Count.EqualTo(0)); | ||
Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.SelectMany(m => m.Signals).Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.Count, Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.First(), Is.EqualTo("Transmitter2")); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void LineTerminationWithLeadingSpaceMultiDefinition() | ||
{ | ||
var dbcString = @" | ||
BO_ 200 TestMessage: 1 TestTransmitter | ||
SG_ Test : 0|8@1+ (0.1,0) [0|0] """" DBG | ||
BO_TX_BU_ 200 : Transmitter2, Transmitter3 ;"; | ||
|
||
var failureObserver = new SimpleFailureObserver(); | ||
Parser.SetParsingFailuresObserver(failureObserver); | ||
var dbc = Parser.Parse(dbcString); | ||
var errorList = failureObserver.GetErrorList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(errorList, Has.Count.EqualTo(0)); | ||
Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.SelectMany(m => m.Signals).Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.Count, Is.EqualTo(2)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.First(), Is.EqualTo("Transmitter2")); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.Last(), Is.EqualTo("Transmitter3")); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void ParseTwoExtraTransmittersLessSpaces() | ||
{ | ||
var dbcString = @" | ||
BO_ 200 TestMessage: 1 TestTransmitter | ||
SG_ Test : 0|8@1+ (0.1,0) [0|0] """" DBG | ||
BO_TX_BU_ 200:Transmitter2,Transmitter3;"; | ||
|
||
|
||
var failureObserver = new SimpleFailureObserver(); | ||
Parser.SetParsingFailuresObserver(failureObserver); | ||
var dbc = Parser.Parse(dbcString); | ||
var errorList = failureObserver.GetErrorList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(errorList, Has.Count.EqualTo(0)); | ||
Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.SelectMany(m => m.Signals).Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.Count, Is.EqualTo(2)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.First(), Is.EqualTo("Transmitter2")); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.Last(), Is.EqualTo("Transmitter3")); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void ParseTwoExtraTransmittersMoreSpaces() | ||
{ | ||
var dbcString = @" | ||
BO_ 200 TestMessage: 1 TestTransmitter | ||
SG_ Test : 0|8@1+ (0.1,0) [0|0] """" DBG | ||
BO_TX_BU_ 200 : Transmitter2 , Transmitter3 ;"; | ||
|
||
|
||
var failureObserver = new SimpleFailureObserver(); | ||
Parser.SetParsingFailuresObserver(failureObserver); | ||
var dbc = Parser.Parse(dbcString); | ||
var errorList = failureObserver.GetErrorList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(errorList, Has.Count.EqualTo(0)); | ||
Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.SelectMany(m => m.Signals).Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.Count, Is.EqualTo(2)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.First(), Is.EqualTo("Transmitter2")); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.Last(), Is.EqualTo("Transmitter3")); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void ParsingErrorIsObserved() | ||
{ | ||
var dbcString = @"BO_TX_BU_ 200 ; Transmitter2, Transmitter3;"; | ||
|
||
var failureObserver = new SimpleFailureObserver(); | ||
Parser.SetParsingFailuresObserver(failureObserver); | ||
var dbc = Parser.Parse(dbcString); | ||
var errorList = failureObserver.GetErrorList(); | ||
|
||
Assert.That(errorList, Has.Count.EqualTo(1)); | ||
} | ||
|
||
[Test] | ||
public void ParseExtraTransmittersDuplicateErrorIsObserved() | ||
{ | ||
var dbcString = @" | ||
BO_ 200 TestMessage: 1 TestTransmitter | ||
SG_ Test : 0|8@1+ (0.1,0) [0|0] """" DBG | ||
BO_TX_BU_ 200 : Transmitter2 , Transmitter2 ;"; | ||
|
||
|
||
var failureObserver = new SimpleFailureObserver(); | ||
Parser.SetParsingFailuresObserver(failureObserver); | ||
var dbc = Parser.Parse(dbcString); | ||
var errorList = failureObserver.GetErrorList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(errorList, Has.Count.EqualTo(1)); | ||
Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.SelectMany(m => m.Signals).Count(), Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.Count, Is.EqualTo(1)); | ||
Assert.That(dbc.Messages.First().AdditionalTransmitters.First(), Is.EqualTo("Transmitter2")); | ||
}); | ||
} | ||
|
||
public void ParseExtraTransmittersMessageNotFoundErrorIsObserved() | ||
{ | ||
var dbcString = @" | ||
BO_ 200 TestMessage: 1 TestTransmitter | ||
SG_ Test : 0|8@1+ (0.1,0) [0|0] """" DBG | ||
BO_TX_BU_ 201 : Transmitter2, Transmitter3 ;"; | ||
|
||
var failureObserver = new SimpleFailureObserver(); | ||
Parser.SetParsingFailuresObserver(failureObserver); | ||
var dbc = Parser.Parse(dbcString); | ||
var errorList = failureObserver.GetErrorList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(errorList, Has.Count.EqualTo(1)); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using DbcParserLib.Observers; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace DbcParserLib.Parsers | ||
{ | ||
internal class ExtraMessageTransmitterLineParser : ILineParser | ||
{ | ||
private const string MessageId = "Id"; | ||
private const string TransmitterGroup = "Transmitter"; | ||
private const string ExtraMessageTransmitterLineStarter = "BO_TX_BU_ "; | ||
|
||
private readonly string m_extraTransmitterRegex = $@"BO_TX_BU_ (?<{MessageId}>\d+)\s*:\s*(?<{TransmitterGroup}>(\s*(?:[a-zA-Z_][\w]*)\s*(?:,)?)+);"; | ||
|
||
private readonly IParseFailureObserver m_observer; | ||
|
||
public ExtraMessageTransmitterLineParser(IParseFailureObserver observer) | ||
{ | ||
m_observer = observer; | ||
} | ||
|
||
public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) | ||
{ | ||
if (line.TrimStart().StartsWith(ExtraMessageTransmitterLineStarter) == false) | ||
{ | ||
return false; | ||
} | ||
|
||
var match = Regex.Match(line, m_extraTransmitterRegex); | ||
if (match.Success) | ||
{ | ||
var messageId = uint.Parse(match.Groups[MessageId].Value, CultureInfo.InvariantCulture); | ||
var transmitters = new List<string>(); | ||
|
||
foreach (var transmitter in match.Groups[TransmitterGroup].Value.Trim().Split(',')) | ||
{ | ||
var transmitterClean = transmitter.Trim(); | ||
if (transmitters.Contains(transmitterClean)) | ||
{ | ||
m_observer.ExtraMessageTransmittersDuplicate(messageId, transmitterClean); | ||
continue; | ||
} | ||
transmitters.Add(transmitterClean); | ||
} | ||
|
||
builder.AddMessageAdditionalTransmitters(messageId, transmitters.ToArray()); | ||
} | ||
else | ||
{ | ||
m_observer.ExtraMessageTransmittersSyntaxError(); | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |