Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 267 additions & 0 deletions DbcParserLib.Tests/PropertiesRelationDefinitionLineParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
using NUnit.Framework;
using DbcParserLib.Model;
using DbcParserLib.Observers;
using DbcParserLib.Parsers;
using Moq;
using System.Collections.Generic;
using System.IO;

namespace DbcParserLib.Tests
{
public class PropertiesRelationDefinitionLineParserTests
{
private MockRepository m_repository;

[SetUp]
public void Setup()
{
m_repository = new MockRepository(MockBehavior.Strict);
}

[TearDown]
public void Teardown()
{
m_repository.VerifyAll();
}

private static List<ILineParser> CreateParser()
{
var observer = new SilentFailureObserver();

return new List<ILineParser>()
{
new PropertiesRelationDefinitionLineParser(observer)
};
}


private static bool ParseLine(
string line,
List<ILineParser> lineParser,
IDbcBuilder builder,
INextLineProvider nextLineProvider)
{
foreach (var parser in lineParser)
{
if (parser.TryParse(line, builder, nextLineProvider))
return true;
}

return false;
}


[Test]
public void IntRelationDefinitionCustomPropertyIsParsedTest()
{
var builderMock = m_repository.Create<IDbcBuilder>();
var nextLineProviderMock = m_repository.Create<INextLineProvider>();

builderMock
.Setup(x => x.AddRelationCustomProperty(
It.IsAny<RelationObjectType>(),
It.IsAny<CustomPropertyDefinition>()))
.Callback<RelationObjectType, CustomPropertyDefinition>((relationType, property) =>
{
Assert.Multiple(() =>
{
Assert.That(relationType, Is.EqualTo(RelationObjectType.NodeSignal));
Assert.That(property.Name, Is.EqualTo("GenSigTimeoutTime"));
Assert.That(property.DataType, Is.EqualTo(CustomPropertyDataType.Integer));

Assert.That(property.IntegerCustomProperty.Minimum, Is.EqualTo(0));
Assert.That(property.IntegerCustomProperty.Maximum, Is.EqualTo(65535));
});
});


var parsers = CreateParser();


Assert.That(
ParseLine(
@"BA_DEF_REL_ BU_SG_REL_ ""GenSigTimeoutTime"" INT 0 65535;",
parsers,
builderMock.Object,
nextLineProviderMock.Object),
Is.True);
}


[Test]
public void HexRelationDefinitionCustomPropertyIsParsedTest()
{
var builderMock = m_repository.Create<IDbcBuilder>();
var nextLineProviderMock = m_repository.Create<INextLineProvider>();

builderMock
.Setup(x => x.AddRelationCustomProperty(
It.IsAny<RelationObjectType>(),
It.IsAny<CustomPropertyDefinition>()))
.Callback<RelationObjectType, CustomPropertyDefinition>((relationType, property) =>
{
Assert.Multiple(() =>
{
Assert.That(relationType, Is.EqualTo(RelationObjectType.NodeMessage));
Assert.That(property.Name, Is.EqualTo("TestHex"));
Assert.That(property.DataType, Is.EqualTo(CustomPropertyDataType.Hex));

Assert.That(property.HexCustomProperty.Minimum, Is.EqualTo(0));
Assert.That(property.HexCustomProperty.Maximum, Is.EqualTo(4095));
});
});


var parsers = CreateParser();


Assert.That(
ParseLine(
@"BA_DEF_REL_ BU_BO_REL_ ""TestHex"" HEX 0 4095;",
parsers,
builderMock.Object,
nextLineProviderMock.Object),
Is.True);
}


[Test]
public void FloatRelationDefinitionCustomPropertyIsParsedTest()
{
var builderMock = m_repository.Create<IDbcBuilder>();
var nextLineProviderMock = m_repository.Create<INextLineProvider>();

builderMock
.Setup(x => x.AddRelationCustomProperty(
It.IsAny<RelationObjectType>(),
It.IsAny<CustomPropertyDefinition>()))
.Callback<RelationObjectType, CustomPropertyDefinition>((relationType, property) =>
{
Assert.Multiple(() =>
{
Assert.That(relationType, Is.EqualTo(RelationObjectType.NodeEnvironment));
Assert.That(property.Name, Is.EqualTo("FloatRelation"));
Assert.That(property.DataType, Is.EqualTo(CustomPropertyDataType.Float));

Assert.That(property.FloatCustomProperty.Minimum, Is.EqualTo(0));
Assert.That(property.FloatCustomProperty.Maximum, Is.EqualTo(10.5));
});
});


var parsers = CreateParser();


Assert.That(
ParseLine(
@"BA_DEF_REL_ BU_EV_REL_ ""FloatRelation"" FLOAT 0 10.5;",
parsers,
builderMock.Object,
nextLineProviderMock.Object),
Is.True);
}


[Test]
public void StringRelationDefinitionCustomPropertyIsParsedTest()
{
var builderMock = m_repository.Create<IDbcBuilder>();
var nextLineProviderMock = m_repository.Create<INextLineProvider>();

builderMock
.Setup(x => x.AddRelationCustomProperty(
It.IsAny<RelationObjectType>(),
It.IsAny<CustomPropertyDefinition>()))
.Callback<RelationObjectType, CustomPropertyDefinition>((relationType, property) =>
{
Assert.Multiple(() =>
{
Assert.That(relationType, Is.EqualTo(RelationObjectType.NodeSignal));
Assert.That(property.Name, Is.EqualTo("RelationString"));
Assert.That(property.DataType, Is.EqualTo(CustomPropertyDataType.String));
});
});


var parsers = CreateParser();


Assert.That(
ParseLine(
@"BA_DEF_REL_ BU_SG_REL_ ""RelationString"" STRING;",
parsers,
builderMock.Object,
nextLineProviderMock.Object),
Is.True);
}


[Test]
public void EnumRelationDefinitionCustomPropertyIsParsedTest()
{
var builderMock = m_repository.Create<IDbcBuilder>();
var nextLineProviderMock = m_repository.Create<INextLineProvider>();

builderMock
.Setup(x => x.AddRelationCustomProperty(
It.IsAny<RelationObjectType>(),
It.IsAny<CustomPropertyDefinition>()))
.Callback<RelationObjectType, CustomPropertyDefinition>((relationType, property) =>
{
Assert.Multiple(() =>
{
Assert.That(relationType, Is.EqualTo(RelationObjectType.NodeSignal));
Assert.That(property.Name, Is.EqualTo("RelationEnum"));
Assert.That(property.DataType, Is.EqualTo(CustomPropertyDataType.Enum));

Assert.That(property.EnumCustomProperty.Values.Length, Is.EqualTo(3));
Assert.That(property.EnumCustomProperty.Values[0], Is.EqualTo("A"));
Assert.That(property.EnumCustomProperty.Values[1], Is.EqualTo("B"));
Assert.That(property.EnumCustomProperty.Values[2], Is.EqualTo("C"));
});
});


var parsers = CreateParser();


Assert.That(
ParseLine(
@"BA_DEF_REL_ BU_SG_REL_ ""RelationEnum"" ENUM ""A"",""B"",""C"";",
parsers,
builderMock.Object,
nextLineProviderMock.Object),
Is.True);
}


[Test]
public void InvalidRelationDefinitionReturnsSyntaxErrorTest()
{
var observer = new Mock<IParseFailureObserver>();

observer
.Setup(x => x.PropertyDefinitionSyntaxError());


var parser = new PropertiesRelationDefinitionLineParser(observer.Object);


var builderMock = m_repository.Create<IDbcBuilder>();
var nextLineProviderMock = m_repository.Create<INextLineProvider>();


Assert.That(
parser.TryParse(
@"BA_DEF_REL_ UNKNOWN ""Invalid"" INT 0 10;",
builderMock.Object,
nextLineProviderMock.Object),
Is.True);


observer.Verify(
x => x.PropertyDefinitionSyntaxError(),
Times.Once);
}
}
}
6 changes: 5 additions & 1 deletion DbcParserLib/Dbc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ public class Dbc
public IEnumerable<EnvironmentVariable> EnvironmentVariables { get; }
public IEnumerable<CustomProperty> GlobalProperties { get; }

public VectorExtensions VectorExtensions { get; }

public Dbc(IEnumerable<Node> nodes, IEnumerable<Message> messages, IEnumerable<EnvironmentVariable> environmentVariables,
IEnumerable<CustomProperty> globalProperties)
IEnumerable<CustomProperty> globalProperties, VectorExtensions vectorExtensions = null)
{
Nodes = nodes;
Messages = messages;
EnvironmentVariables = environmentVariables;
GlobalProperties = globalProperties;
//TODO: Null Reference check
VectorExtensions = vectorExtensions;
}
}
}
75 changes: 74 additions & 1 deletion DbcParserLib/DbcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ internal class DbcBuilder : IDbcBuilder
{CustomPropertyObjectType.Signal, new Dictionary<string, CustomPropertyDefinition>()},
{CustomPropertyObjectType.Environment, new Dictionary<string, CustomPropertyDefinition>()},
{CustomPropertyObjectType.Global, new Dictionary<string, CustomPropertyDefinition>()},

};

private readonly IList<RelationCustomPropertyDefinition> m_relationCustomProperties = new List<RelationCustomPropertyDefinition>();

private readonly IList<RelationCustomPropertyValue> m_relationCustomPropertyValues = new List<RelationCustomPropertyValue>();


private Message m_currentMessage;

public DbcBuilder(IParseFailureObserver observer)
Expand Down Expand Up @@ -341,6 +347,64 @@ public void AddSignalExtendedMultiplexingInfo(uint messageId, string signalName,
{
// ToDo: Define what to do with the data
}
public void AddRelationCustomProperty(RelationObjectType relationType, CustomPropertyDefinition property)
{
var existing = m_relationCustomProperties
.FirstOrDefault(x =>
x.RelationType == relationType &&
x.Property.Name == property.Name);

if (existing != null)
{
m_observer.DuplicatedProperty(property.Name);
return;
}

m_relationCustomProperties.Add( new RelationCustomPropertyDefinition(relationType, property));
}

public void AddRelationCustomPropertyDefaultValue(string propertyName, string value, bool isNumeric)
{
//TODO: Need Tests
var property = m_relationCustomProperties
.FirstOrDefault(x =>
x.Property.Name == propertyName);

if (property == null)
{
m_observer.PropertyNameNotFound(propertyName);
return;
}

property.Property.SetCustomPropertyDefaultValue(
value,
isNumeric);
}

public void AddRelationCustomPropertyValue(string propertyName, string relationType, string nodeName, uint messageId, string signalName, string value)
{
//TODO: Need Tests
var propertyExists = m_relationCustomProperties.Any(x =>
x.Property.Name == propertyName);


if (!propertyExists)
{
m_observer.PropertyNameNotFound(propertyName);
return;
}

//TODO: Better Enum parsing
m_relationCustomPropertyValues.Add(
new RelationCustomPropertyValue(
propertyName,
(RelationObjectType)Enum.Parse(typeof(RelationObjectType), relationType),
nodeName,
messageId,
signalName,
value));
}


private bool TryGetValueMessageSignal(uint messageId, string signalName, out Signal signal)
{
Expand Down Expand Up @@ -479,8 +543,17 @@ public Dbc Build()
//}
//return new Dbc(nodes, messages, environmentVariables);

return new Dbc(m_nodes.ToArray(), m_messages.Values.ToArray(), m_environmentVariables.Values.ToArray(), m_globalCustomProperties.Values);
return new Dbc(
m_nodes.ToArray(),
m_messages.Values.ToArray(),
m_environmentVariables.Values.ToArray(),
m_globalCustomProperties.Values,
new VectorExtensions(
m_relationCustomProperties,
m_relationCustomPropertyValues));
}


}

internal class NodeEqualityComparer : IEqualityComparer<Node>
Expand Down
2 changes: 1 addition & 1 deletion DbcParserLib/DbcParserLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>netstandard2.0;net462;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net462;net6.0;net8.0;net10.0</TargetFrameworks>
<Authors>Emanuel Feru</Authors>
<Description>DbcParserLib is a .NET dbc file parser library for CAN bus network signals. This library can also be used to pack and unpack CAN signals.</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
Loading