From ad2305bbb32e7c8203294e264a8d8a013b8cf3cc Mon Sep 17 00:00:00 2001 From: Jan Oltmann Date: Mon, 25 Sep 2023 16:23:12 +0200 Subject: [PATCH 1/2] Updated enum parsing tests Updated tests to check for correct parsing of enum definitions --- .../PropertiesLineParserTests.cs | 52 ++++++++++++++++--- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/DbcParserLib.Tests/PropertiesLineParserTests.cs b/DbcParserLib.Tests/PropertiesLineParserTests.cs index 0fd42cd..4a32838 100644 --- a/DbcParserLib.Tests/PropertiesLineParserTests.cs +++ b/DbcParserLib.Tests/PropertiesLineParserTests.cs @@ -99,23 +99,59 @@ public void StringDefinitionCustomPropertyIsParsedTest() [Test] public void EnumDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var dbcBuilderMock = m_repository.Create(); + var nextLineProviderMock = m_repository.Create(); + + dbcBuilderMock.Setup(mock => mock.AddCustomProperty(It.IsAny(), It.IsAny())) + .Callback((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(), It.IsAny())) + .Callback((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(); + var nextLineProviderMock = m_repository.Create(); + + dbcBuilderMock.Setup(mock => mock.AddCustomProperty(It.IsAny(), It.IsAny())) + .Callback((_, 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(), It.IsAny())) + .Callback((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] From 31bf7063e50c6cf003858cf281738e83bc66ace5 Mon Sep 17 00:00:00 2001 From: Jan Oltmann Date: Mon, 25 Sep 2023 16:28:54 +0200 Subject: [PATCH 2/2] Fixed parsing of enum definitions Fixed the regex for parsing enum definitions, which got broken by a change related to EFeru#41. --- DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs b/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs index ab0f3fb..896a553 100644 --- a/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs +++ b/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs @@ -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; @@ -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;