This commit is contained in:
deeaitch committed 2026-10-14 10:32:44 -04:00
1 parent 0b05152bcf
commit d87c100398
9 files changed
+265 -65

No files matched your search

+100
View File
@@ -75,6 +75,9 @@ private Q_SLOTS:
void duplicateFrames();
void duplicateSignals();
void distinctDefinitions();
void comments();
void invalidComments_data();
void invalidComments();
void integration();
void invalidSignals();
void validSignals();
@@ -489,6 +492,103 @@ void DbcParserTest::distinctDefinitions() {
QCOMPARE(extended.definition->comment, std::string("Extended"));
}
void DbcParserTest::comments() {
Fixture fixture;
fixture.Write(
"CM_\tBO_\t1\t\"First\";\r\n"
"CM_\tSG_\t1\tState\t\" Line one \r\n"
"\r\n"
"\tBO_ is text; \\\"quoted\\\" and \\\\slash\"\t;\r\n"
"CM_ BO_ 2147483649 \"Extended\";\r\n"
"CM_ SG_ 2147483649 State \"Extended signal\";\r\n"
"BO_ 1 Standard: 1 ECU\r\n"
"CM_ BO_ 1 \"Last\";\r\n"
"SG_ State : 0|8@1+ (1,0) [0|255] \"\" ECU\r\n"
"BO_ 2147483649 Extended: 1 ECU\r\n"
"SG_ State : 0|8@1+ (1,0) [0|255] \"\" ECU");
const DbcParseResult result = DbcParser().ParseFileWithDiagnostics(fixture.path.string());
QVERIFY(!result.HasErrors());
QVERIFY(result.diagnostics.empty());
QCOMPARE(result.database.frames.size(), std::size_t(2));
QCOMPARE(result.database.frames[0].comment, std::string("Last"));
QCOMPARE(result.database.frames[0].signals[0].comment,
std::string(" Line one \n\n\tBO_ is text; \"quoted\" and \\slash"));
QCOMPARE(result.database.frames[1].comment, std::string("Extended"));
QCOMPARE(result.database.frames[1].signals[0].comment, std::string("Extended signal"));
const std::string ids[][2] = {
{"0", "0"},
{"2047", "2047"},
{"2048", "2147485696"},
{"536870911", "2684354559"},
{"2684354559", "536870911"}};
for (const std::string(&id)[2]: ids) {
fixture.Write("CM_ BO_ " + id[0] + " \"Frame\";\nCM_ SG_ " + id[0] +
" State \"Signal\";\nBO_ " + id[1] + " Frame: 1 ECU\n"
"SG_ State : 0|8@1+ (1,0) [0|255] \"\" ECU\n");
const DbcDatabase database = DbcParser().ParseFile(fixture.path.string());
QCOMPARE(database.frames[0].comment, std::string("Frame"));
QCOMPARE(database.frames[0].signals[0].comment, std::string("Signal"));
}
fixture.Write("CM_ BO_ 1 \"Line one\nLine two\";\n"
"BO_ 1 Frame: 0 ECU\nUNKNOWN_ value\nBO_ bad Frame: 0 ECU\n");
const DbcParseResult following = DbcParser().ParseFileWithDiagnostics(fixture.path.string());
QVERIFY(following.HasErrors());
QCOMPARE(following.diagnostics.size(), std::size_t(2));
QCOMPARE(following.diagnostics[0].lineNumber, std::size_t(4));
QCOMPARE(following.diagnostics[1].lineNumber, std::size_t(5));
}
void DbcParserTest::invalidComments_data() {
QTest::addColumn<QString>("statement");
const char *ids[] = {"-1", "+1", "1x", "0x1", "4294967296", "536870912",
"1073741824", "2684354560", "4294967295"};
for (const char *id: ids) {
const std::string frameRow = std::string("frame-id-") + id;
const std::string signalRow = std::string("signal-id-") + id;
QTest::newRow(frameRow.c_str()) << QString::fromStdString("CM_ BO_ " + std::string(id) + " \"Text\";");
QTest::newRow(signalRow.c_str()) << QString::fromStdString("CM_ SG_ " + std::string(id) + " State \"Text\";");
}
QTest::newRow("missing-id") << QStringLiteral("CM_ BO_");
QTest::newRow("missing-signal") << QStringLiteral("CM_ SG_ 1 \"Text\";");
QTest::newRow("missing-quote") << QStringLiteral("CM_ BO_ 1 Text;");
QTest::newRow("unclosed") << QStringLiteral("CM_ SG_ 1 State \"Text\n More");
QTest::newRow("invalid-escape") << QStringLiteral("CM_ BO_ 1 \"bad\\q\";");
QTest::newRow("dangling-escape") << QStringLiteral("CM_ BO_ 1 \"bad\\");
QTest::newRow("missing-semicolon") << QStringLiteral("CM_ SG_ 1 State \"Text\"");
QTest::newRow("trailing-text") << QStringLiteral("CM_ BO_ 1 \"Text\"; garbage");
QTest::newRow("extra-header") << QStringLiteral("CM_ BO_ 1 extra \"Text\";");
QTest::newRow("extra-string") << QStringLiteral("CM_ BO_ 1 \"Text\" \"More\";");
QTest::newRow("missing-frame") << QStringLiteral("CM_ BO_ 2 \"Text\";");
QTest::newRow("missing-signal-frame") << QStringLiteral("CM_ SG_ 2 State \"Text\";");
QTest::newRow("missing-signal-reference") << QStringLiteral("CM_ SG_ 1 Missing \"Text\";");
QTest::newRow("different-frame-type") << QStringLiteral("CM_ BO_ 2147483649 \"Text\";");
QTest::newRow("missing-multiline-reference") << QStringLiteral("CM_ SG_ 1 Missing \"Text\nMore\";");
}
void DbcParserTest::invalidComments() {
QFETCH(QString, statement);
Fixture fixture;
fixture.Write("BO_ 1 Frame: 1 ECU\nSG_ State : 0|8@1+ (1,0) [0|255] \"\" ECU\n\n" +
statement.toStdString());
const DbcParseResult result = DbcParser().ParseFileWithDiagnostics(fixture.path.string());
QVERIFY(result.HasErrors());
QVERIFY(result.database.frames.empty());
QCOMPARE(result.diagnostics.size(), std::size_t(1));
const DbcDiagnostic &error = result.diagnostics[0];
QCOMPARE(error.severity, DbcDiagnosticSeverity::Error);
QCOMPARE(error.code, DbcDiagnosticCode::InvalidStatement);
QCOMPARE(error.filePath, fixture.path.string());
QCOMPARE(error.lineNumber, std::size_t(4));
QVERIFY(!error.message.empty());
try {
DbcParser().ParseFile(fixture.path.string());
QFAIL("Malformed or unresolved comment must throw.");
} catch (const std::runtime_error &exception) {
QVERIFY(std::string(exception.what()).find(fixture.path.string() + ":4: ") == 0U);
}
}
void DbcParserTest::integration() {
Fixture fixture;
fixture.Write(