Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State machine refactoring #60

Merged
merged 5 commits into from
Nov 6, 2023
Merged
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
49 changes: 24 additions & 25 deletions DbcParserLib.Tests/DbcBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,16 @@ public void TableValuesAreAddedToSignal()
builder.AddSignal(signal);
var testValuesDict = new Dictionary<int, string>() { { 1, "fake" } };

builder.LinkTableValuesToSignal(234, "name1", testValuesDict, "1 fake");
builder.LinkTableValuesToSignal(234, "name1", testValuesDict);
var dbc = builder.Build();

Assert.IsEmpty(dbc.Nodes);
Assert.AreEqual(1, dbc.Messages.Count());
Assert.AreEqual(234, dbc.Messages.First().ID);
Assert.AreEqual("name1", dbc.Messages.First().Signals.First().Name);
Assert.AreEqual(testValuesDict, dbc.Messages.First().Signals.First().ValueTableMap);
Assert.AreEqual("1 fake", dbc.Messages.First().Signals.First().ValueTable);
Assert.AreEqual(1, dbc.Messages.First().Signals.First().ValueTableMap.Keys.First());
Assert.AreEqual("fake", dbc.Messages.First().Signals.First().ValueTableMap.Values.First());
}

[Test]
Expand All @@ -287,15 +288,16 @@ public void TableValuesWithExtendedMessageIdAreAddedToSignal()
builder.AddSignal(signal);
var testValuesDict = new Dictionary<int, string>() { { 1, "fake" } };

builder.LinkTableValuesToSignal(2566896411, "name1", testValuesDict, "1 fake");
builder.LinkTableValuesToSignal(2566896411, "name1", testValuesDict);
var dbc = builder.Build();

Assert.IsEmpty(dbc.Nodes);
Assert.AreEqual(1, dbc.Messages.Count());
Assert.AreEqual(message.ID, dbc.Messages.First().ID);
Assert.AreEqual("name1", dbc.Messages.First().Signals.First().Name);
Assert.AreEqual(testValuesDict, dbc.Messages.First().Signals.First().ValueTableMap);
Assert.AreEqual("1 fake", dbc.Messages.First().Signals.First().ValueTable);
Assert.AreEqual(1, dbc.Messages.First().Signals.First().ValueTableMap.Keys.First());
Assert.AreEqual("fake", dbc.Messages.First().Signals.First().ValueTableMap.Values.First());
}

[Test]
Expand All @@ -308,15 +310,14 @@ public void TableValueIsNotAddedToMissingSignalMessageId()
builder.AddSignal(signal);
var testValuesDict = new Dictionary<int, string>() { { 1, "fake" } };

builder.LinkTableValuesToSignal(235, "name1", testValuesDict, "1 fake");
builder.LinkTableValuesToSignal(235, "name1", testValuesDict);
var dbc = builder.Build();

Assert.IsEmpty(dbc.Nodes);
Assert.AreEqual(1, dbc.Messages.Count());
Assert.AreEqual(234, dbc.Messages.First().ID);
Assert.AreEqual("name1", dbc.Messages.First().Signals.First().Name);
Assert.IsNull(dbc.Messages.First().Signals.First().ValueTableMap);
Assert.IsNull(dbc.Messages.First().Signals.First().ValueTable);
Assert.IsEmpty(dbc.Messages.First().Signals.First().ValueTableMap);
}

[Test]
Expand All @@ -329,15 +330,14 @@ public void TableValueIsNotAddedToMissingSignalName()
builder.AddSignal(signal);
var testValuesDict = new Dictionary<int, string>() { { 1, "fake" } };

builder.LinkTableValuesToSignal(234, "name2", testValuesDict, "1 fake");
builder.LinkTableValuesToSignal(234, "name2", testValuesDict);
var dbc = builder.Build();

Assert.IsEmpty(dbc.Nodes);
Assert.AreEqual(1, dbc.Messages.Count());
Assert.AreEqual(234, dbc.Messages.First().ID);
Assert.AreEqual("name1", dbc.Messages.First().Signals.First().Name);
Assert.IsNull(dbc.Messages.First().Signals.First().ValueTableMap);
Assert.IsNull(dbc.Messages.First().Signals.First().ValueTable);
Assert.IsEmpty(dbc.Messages.First().Signals.First().ValueTableMap);
}

[Test]
Expand All @@ -350,7 +350,7 @@ public void NamedTableValuesAreAddedToSignal()
builder.AddSignal(signal);
var testValuesDict = new Dictionary<int, string>() { { 1, "fake" } };

builder.AddNamedValueTable("aTableName", testValuesDict, "1 fake");
builder.AddNamedValueTable("aTableName", testValuesDict);

builder.LinkNamedTableToSignal(234, "name1", "aTableName");
var dbc = builder.Build();
Expand All @@ -360,7 +360,8 @@ public void NamedTableValuesAreAddedToSignal()
Assert.AreEqual(234, dbc.Messages.First().ID);
Assert.AreEqual("name1", dbc.Messages.First().Signals.First().Name);
Assert.AreEqual(testValuesDict, dbc.Messages.First().Signals.First().ValueTableMap);
Assert.AreEqual("1 fake", dbc.Messages.First().Signals.First().ValueTable);
Assert.AreEqual(1, dbc.Messages.First().Signals.First().ValueTableMap.Keys.First());
Assert.AreEqual("fake", dbc.Messages.First().Signals.First().ValueTableMap.Values.First());
}

[Test]
Expand All @@ -373,7 +374,7 @@ public void NamedTableValueIsNotAddedToMissingSignalMessageId()
builder.AddSignal(signal);
var testValuesDict = new Dictionary<int, string>() { { 1, "fake" } };

builder.AddNamedValueTable("aTableName", testValuesDict, "1 fake");
builder.AddNamedValueTable("aTableName", testValuesDict);

builder.LinkNamedTableToSignal(235, "name1", "aTableName");
var dbc = builder.Build();
Expand All @@ -382,8 +383,7 @@ public void NamedTableValueIsNotAddedToMissingSignalMessageId()
Assert.AreEqual(1, dbc.Messages.Count());
Assert.AreEqual(234, dbc.Messages.First().ID);
Assert.AreEqual("name1", dbc.Messages.First().Signals.First().Name);
Assert.IsNull(dbc.Messages.First().Signals.First().ValueTableMap);
Assert.IsNull(dbc.Messages.First().Signals.First().ValueTable);
Assert.IsEmpty(dbc.Messages.First().Signals.First().ValueTableMap);
}

[Test]
Expand All @@ -396,7 +396,7 @@ public void NamedTableValueIsNotAddedToMissingSignalName()
builder.AddSignal(signal);
var testValuesDict = new Dictionary<int, string>() { { 1, "fake" } };

builder.AddNamedValueTable("aTableName", testValuesDict, "1 fake");
builder.AddNamedValueTable("aTableName", testValuesDict);

builder.LinkNamedTableToSignal(234, "name2", "aTableName");
var dbc = builder.Build();
Expand All @@ -405,8 +405,7 @@ public void NamedTableValueIsNotAddedToMissingSignalName()
Assert.AreEqual(1, dbc.Messages.Count());
Assert.AreEqual(234, dbc.Messages.First().ID);
Assert.AreEqual("name1", dbc.Messages.First().Signals.First().Name);
Assert.IsNull(dbc.Messages.First().Signals.First().ValueTableMap);
Assert.IsNull(dbc.Messages.First().Signals.First().ValueTable);
Assert.IsEmpty(dbc.Messages.First().Signals.First().ValueTableMap);
}

[Test]
Expand All @@ -425,7 +424,7 @@ public void NamedTableValueIsNotAddedIfTableNameDoesNotExist()
Assert.AreEqual(1, dbc.Messages.Count());
Assert.AreEqual(234, dbc.Messages.First().ID);
Assert.AreEqual("name1", dbc.Messages.First().Signals.First().Name);
Assert.IsNull(dbc.Messages.First().Signals.First().ValueTable);
Assert.IsEmpty(dbc.Messages.First().Signals.First().ValueTableMap);
}

[Test]
Expand All @@ -434,9 +433,9 @@ public void NamedTableValueThatAreNotUsedDoNotHarmOnBuild()
var testValuesDict = new Dictionary<int, string>() { { 1, "fake" } };
var builder = new DbcBuilder(new SilentFailureObserver());

builder.AddNamedValueTable("aTableName", testValuesDict, "1 fake");
builder.AddNamedValueTable("aTableName2", testValuesDict, "1 fake");
builder.AddNamedValueTable("aTableName3", testValuesDict, "1 fake");
builder.AddNamedValueTable("aTableName", testValuesDict);
builder.AddNamedValueTable("aTableName2", testValuesDict);
builder.AddNamedValueTable("aTableName3", testValuesDict);
var dbc = builder.Build();

Assert.IsEmpty(dbc.Nodes);
Expand All @@ -449,9 +448,9 @@ public void NamedTablesWithSameNameAreManaged()
var testValuesDict = new Dictionary<int, string>() { { 1, "fake" } };
var builder = new DbcBuilder(new SilentFailureObserver());

builder.AddNamedValueTable("aTableName", testValuesDict, "1 fake");
builder.AddNamedValueTable("aTableName", testValuesDict, "1 fake");
builder.AddNamedValueTable("aTableName", testValuesDict, "1 fake");
builder.AddNamedValueTable("aTableName", testValuesDict);
builder.AddNamedValueTable("aTableName", testValuesDict);
builder.AddNamedValueTable("aTableName", testValuesDict);
var dbc = builder.Build();

Assert.IsEmpty(dbc.Nodes);
Expand Down
98 changes: 97 additions & 1 deletion DbcParserLib.Tests/ExtensionMethodsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using System.Collections.Generic;
using NUnit.Framework;
using DbcParserLib.Model;

namespace DbcParserLib.Tests
Expand Down Expand Up @@ -133,5 +134,100 @@ public void EmptyMessageIsNotMultiplexedTest()
var message = new Message();
Assert.IsFalse(message.IsMultiplexed());
}

[TestCase("1 \"First\" 2 \"Second\" 3 \"Third\"")]
[TestCase("1 \"First with spaces\" 2 \" Second \" 3 \"T h i r d\"")]
[TestCase("1 \"First with spaces\" 2 \" \" 3 \"\"")]
[TestCase("1 \"1\" 2 \" 2 \" 3 \" 3\"")]
public void FsmNoErrorTest(string text)
{
var operation = text.TryParseToDict(out _);
Assert.IsTrue(operation);
}

[TestCase("1 \"First 2 \"Second\" 3 \"Third\"")]
[TestCase("1 First 2 \"Second\" 3 \"Third\"")]
[TestCase("1 \"First\" 2 Second\" 3 \"Third\"")]
[TestCase("One \"First with spaces\" 2 \" Second \"")]
[TestCase("1 \"First\" 2 Second\" 3 \"Third\" 4")]
[TestCase("1 \"First\" 2 Second\" 3 \"Third")]
[TestCase("1 \"First\", 2 Second\", 3 \"Third")]
public void FsmWithErrorTest(string text)
{
var operation = text.TryParseToDict(out _);
Assert.IsFalse(operation);
}

[Test]
public void FsmNoSpacesParsedTest()
{
var text = "1 \"First\" 2 \"Second\" 3 \"Third\"";
var operation = text.TryParseToDict(out var dict);
var expectedDict = new Dictionary<int, string>()
{
{ 1, "First" },
{ 2, "Second" },
{ 3, "Third" }
};

Assert.IsTrue(operation);
Assert.AreEqual(expectedDict, dict);
}

[Test]
public void FsmWithSpacesParsedTest()
{
var text = "1 \"First with spaces\" 2 \" Second \" 3 \" T h i r d \"";
var operation = text.TryParseToDict(out var dict);
var expectedDict = new Dictionary<int, string>()
{
{ 1, "First with spaces" },
{ 2, " Second " },
{ 3, " T h i r d " }
};

Assert.IsTrue(operation);
Assert.AreEqual(expectedDict, dict);
}

[Test]
public void FsmWithEmptyStringParsedTest()
{
var text = "1 \"\" 2 \" \"";
var operation = text.TryParseToDict(out var dict);
var expectedDict = new Dictionary<int, string>()
{
{ 1, "" },
{ 2, " " }
};

Assert.IsTrue(operation);
Assert.AreEqual(expectedDict, dict);
}

[Test]
public void FsmWithIntegerStringParsedTest()
{
var text = "1 \"1\" 2 \"2\"";
var operation = text.TryParseToDict(out var dict);
var expectedDict = new Dictionary<int, string>()
{
{ 1, "1" },
{ 2, "2" }
};

Assert.IsTrue(operation);
Assert.AreEqual(expectedDict, dict);
}

[Test]
public void FsmErrorTest()
{
var text = "1 \"First with spaces\" 2 \" Second \" 3 T h i r d \"";
var operation = text.TryParseToDict(out var dict);

Assert.IsFalse(operation);
Assert.IsNull(dict);
}
}
}
54 changes: 21 additions & 33 deletions DbcParserLib.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using System.Linq;
using System.IO;
using DbcParserLib.Model;

namespace DbcParserLib.Tests
Expand Down Expand Up @@ -71,7 +71,7 @@ public void CheckSignalPropertiesTest()

var signal = targetMessage.Signals.FirstOrDefault(x => x.Name.Equals("UI_camBlockBlurThreshold"));
Assert.IsNotNull(signal);
Assert.AreEqual(0, signal.IsSigned);
Assert.AreEqual(DbcValueType.Unsigned, signal.ValueType);
Assert.AreEqual(11, signal.StartBit);
Assert.AreEqual(6, signal.Length);
Assert.AreEqual(0.01587, signal.Factor);
Expand All @@ -93,7 +93,7 @@ public void CheckOtherSignalPropertiesTest()

var signal = targetMessage.Signals.FirstOrDefault(x => x.Name.Equals("DI_torqueMotor"));
Assert.IsNotNull(signal);
Assert.AreEqual(1, signal.IsSigned);
Assert.AreEqual(DbcValueType.Signed, signal.ValueType);
Assert.AreEqual("Nm", signal.Unit);
Assert.AreEqual(13, signal.Length);
Assert.AreEqual(0.25, signal.Factor);
Expand All @@ -103,34 +103,6 @@ public void CheckOtherSignalPropertiesTest()
Assert.AreEqual(1, signal.Receiver.Length);
}

[Test]
public void CheckTableValuesSignalPropertiesTest()
{
var dbc = Parser.ParseFromPath(MainDbcFilePath);

var targetMessage = dbc.Messages.FirstOrDefault(x => x.ID == 264);
Assert.IsNotNull(targetMessage);

Assert.AreEqual(7, targetMessage.Signals.Count);

var signal = targetMessage.Signals.FirstOrDefault(x => x.Name.Equals("DI_soptState"));
Assert.IsNotNull(signal);
Assert.IsFalse(string.IsNullOrWhiteSpace(signal.ValueTable));
Assert.AreEqual(132, signal.ValueTable.Length);

var lineCount = 0;
using (var reader = new StringReader(signal.ValueTable))
{
while (reader.Peek() > -1)
{
reader.ReadLine();
++lineCount;
}
}

Assert.AreEqual(6, lineCount);
}

[Test]
public void SignalCommentIsProperlyAppliedWhenMultipleSignalsShareSameNameTest()
{
Expand Down Expand Up @@ -253,14 +225,22 @@ public void NamedValTableIsAppliedTest()

VAL_ 1043 withNamedTable DI_aebLockState ; ";

var expectedValueTableMap = new Dictionary<int, string>()
{
{ 3, "AEB_LOCK_STATE_SNA" },
{ 2, "AEB_LOCK_STATE_UNUSED" },
{ 1, "AEB_LOCK_STATE_UNLOCKED" },
{ 0, "AEB_LOCK_STATE_LOCKED"}
};

var dbc = Parser.Parse(dbcString);

Assert.AreEqual(1, dbc.Messages.Count());
Assert.AreEqual(0, dbc.Nodes.Count());

var signal = dbc.Messages.Single().Signals.Single();
Assert.IsNotNull(signal);
Assert.AreEqual(107, signal.ValueTable.Length);
Assert.AreEqual(expectedValueTableMap, signal.ValueTableMap);
}

[Test]
Expand All @@ -273,14 +253,22 @@ public void ExplicitValTableIsAppliedTest()

VAL_ 1043 withNamedTable 3 ""AEB_LOCK_STATE_SNA"" 2 ""AEB_LOCK_STATE_UNUSED"" 1 ""AEB_LOCK_STATE_UNLOCKED"" 0 ""AEB_LOCK_STATE_LOCKED"" ;";

var expectedValueTableMap = new Dictionary<int, string>()
{
{ 3, "AEB_LOCK_STATE_SNA" },
{ 2, "AEB_LOCK_STATE_UNUSED" },
{ 1, "AEB_LOCK_STATE_UNLOCKED" },
{ 0, "AEB_LOCK_STATE_LOCKED"}
};

var dbc = Parser.Parse(dbcString);

Assert.AreEqual(1, dbc.Messages.Count());
Assert.AreEqual(0, dbc.Nodes.Count());

var signal = dbc.Messages.Single().Signals.Single();
Assert.IsNotNull(signal);
Assert.AreEqual(107, signal.ValueTable.Length);
Assert.AreEqual(expectedValueTableMap, signal.ValueTableMap);
}

[Test]
Expand Down
Loading
Loading