Skip to content

Commit

Permalink
Merge pull request #46 from Brainy0207/fix-enum-parse
Browse files Browse the repository at this point in the history
#41 Fixed enum definition parser repeating last value twice
  • Loading branch information
Adhara3 authored Sep 26, 2023
2 parents 0290c32 + 31bf706 commit 89aee40
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
52 changes: 44 additions & 8 deletions DbcParserLib.Tests/PropertiesLineParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,59 @@ public void StringDefinitionCustomPropertyIsParsedTest()
[Test]
public void EnumDefinitionCustomPropertyIsParsedTest()
{
var builder = new DbcBuilder(new SilentFailureObserver());
var dbcBuilderMock = m_repository.Create<IDbcBuilder>();
var nextLineProviderMock = m_repository.Create<INextLineProvider>();

dbcBuilderMock.Setup(mock => mock.AddCustomProperty(It.IsAny<CustomPropertyObjectType>(), It.IsAny<CustomPropertyDefinition>()))
.Callback<CustomPropertyObjectType, CustomPropertyDefinition>((objectType, customProperty) =>
{
Assert.AreEqual("AttributeName", customProperty.Name);
Assert.AreEqual(CustomPropertyDataType.Enum, customProperty.DataType);
Assert.AreEqual(3, customProperty.EnumCustomProperty.Values.Length);
Assert.AreEqual("Val1", customProperty.EnumCustomProperty.Values[0]);
Assert.AreEqual("Val2", customProperty.EnumCustomProperty.Values[1]);
Assert.AreEqual("Val3", customProperty.EnumCustomProperty.Values[2]);
});

dbcBuilderMock.Setup(mock => mock.AddCustomPropertyDefaultValue(It.IsAny<string>(), It.IsAny<string>()))
.Callback<string, string>((propertyName, value) =>
{
Assert.AreEqual("AttributeName", propertyName);
Assert.AreEqual("Val2", value);
});

var customPropertyLineParsers = CreateParser();
var nextLineProvider = new NextLineProvider(new StringReader(string.Empty));
Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" ENUM ""Val1"",""Val2"",""Val3"";", customPropertyLineParsers, builder, nextLineProvider));
Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""Val2"";", customPropertyLineParsers, builder, nextLineProvider));
Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" ENUM ""Val1"",""Val2"",""Val3"";", customPropertyLineParsers, dbcBuilderMock.Object, nextLineProviderMock.Object));
Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""Val2"";", customPropertyLineParsers, dbcBuilderMock.Object, nextLineProviderMock.Object));
}

[Test]
public void EnumDefinitionCustomPropertyMoreWhiteSpaceIsParsedTest()
{
var builder = new DbcBuilder(new SilentFailureObserver());
var dbcBuilderMock = m_repository.Create<IDbcBuilder>();
var nextLineProviderMock = m_repository.Create<INextLineProvider>();

dbcBuilderMock.Setup(mock => mock.AddCustomProperty(It.IsAny<CustomPropertyObjectType>(), It.IsAny<CustomPropertyDefinition>()))
.Callback<CustomPropertyObjectType, CustomPropertyDefinition>((_, customProperty) =>
{
Assert.AreEqual("AttributeName", customProperty.Name);
Assert.AreEqual(CustomPropertyDataType.Enum, customProperty.DataType);
Assert.AreEqual(3, customProperty.EnumCustomProperty.Values.Length);
Assert.AreEqual("Val1", customProperty.EnumCustomProperty.Values[0]);
Assert.AreEqual("Val2", customProperty.EnumCustomProperty.Values[1]);
Assert.AreEqual("Val3", customProperty.EnumCustomProperty.Values[2]);
});

dbcBuilderMock.Setup(mock => mock.AddCustomPropertyDefaultValue(It.IsAny<string>(), It.IsAny<string>()))
.Callback<string, string>((propertyName, value) =>
{
Assert.AreEqual("AttributeName", propertyName);
Assert.AreEqual("Val2", value);
});

var customPropertyLineParsers = CreateParser();
var nextLineProvider = new NextLineProvider(new StringReader(string.Empty));
Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" ENUM ""Val1"",""Val2"",""Val3"" ;", customPropertyLineParsers, builder, nextLineProvider));
Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""Val2"";", customPropertyLineParsers, builder, nextLineProvider));
Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" ENUM ""Val1"",""Val2"",""Val3"" ;", customPropertyLineParsers, dbcBuilderMock.Object, nextLineProviderMock.Object));
Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""Val2"";", customPropertyLineParsers, dbcBuilderMock.Object, nextLineProviderMock.Object));
}

[Test]
Expand Down
7 changes: 4 additions & 3 deletions DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ internal class PropertiesDefinitionLineParser : ILineParser
{
private const string PropertiesDefinitionLineStarter = "BA_DEF_ ";
private const string PropertiesDefinitionDefaultLineStarter = "BA_DEF_DEF_ ";
private const string PropertyDefinitionParsingRegex = @"BA_DEF_(?:\s+(BU_|BO_|SG_|EV_))?\s+""([a-zA-Z_][\w]*)""\s+(?:(?:(INT|HEX)\s+(-?\d+)\s+(-?\d+))|(?:(FLOAT)\s+([\d\+\-eE.]+)\s+([\d\+\-eE.]+))|(STRING)|(?:(ENUM)\s+((?:""[^""]*"",+)*(""[^""]*""))))\s*;";
// language=regex
private const string PropertyDefinitionParsingRegex = @"BA_DEF_(?:\s+(BU_|BO_|SG_|EV_))?\s+""([a-zA-Z_][\w]*)""\s+(?:(?:(INT|HEX)\s+(-?\d+)\s+(-?\d+))|(?:(FLOAT)\s+([\d\+\-eE.]+)\s+([\d\+\-eE.]+))|(STRING)|(?:(ENUM)\s+((?:""[^""]*"",+)*(?:""[^""]*""))))\s*;";
// language=regex
private const string PropertyDefinitionDefaultParsingRegex = @"BA_DEF_DEF_\s+""([a-zA-Z_][\w]*)""\s+(-?\d+|[\d\+\-eE.]+|""[^""]*"")\s*;";

private readonly IParseFailureObserver m_observer;
Expand Down Expand Up @@ -93,10 +95,9 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
else if (match.Groups[10].Value.StartsWith("ENUM"))
{
dataType = CustomPropertyDataType.Enum;
var enumDefinition = match.Groups[11].Value + match.Groups[12].Value;
customProperty.EnumCustomProperty = new EnumCustomPropertyDefinition
{
Values = enumDefinition.Replace("\"", "").Split(',')
Values = match.Groups[11].Value.Replace("\"", "").Split(',')
};
}
customProperty.DataType = dataType;
Expand Down

0 comments on commit 89aee40

Please sign in to comment.