From 97ab7714fe20ad540cf0ec9503e92d82f2cf5a4e Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 1 Dec 2022 18:05:57 +0100 Subject: [PATCH] Base: fix InventorBuilder and add more unit tests --- src/Base/Builder3D.cpp | 24 ++- tests/CMakeLists.txt | 2 + tests/src/InventorBuilder.cpp | 267 +++++++++++++++++++++++++++++++++- 3 files changed, 275 insertions(+), 18 deletions(-) diff --git a/src/Base/Builder3D.cpp b/src/Base/Builder3D.cpp index bb32456d08..e03a99a3c1 100644 --- a/src/Base/Builder3D.cpp +++ b/src/Base/Builder3D.cpp @@ -184,19 +184,16 @@ void NodeItem::writeField(const char* field, const std::vector& rgb, I return; if (rgb.size() == 1) { - out.increaseIndent(); out.write() << field << " " << rgb[0].red() << " " << rgb[0].green() << " " << rgb[0].blue() << '\n'; - out.decreaseIndent(); } else { - out.increaseIndent(); out.write() << field << " [\n"; out.increaseIndent(); for (auto it : rgb) { out.write() << it.red() << " " << it.green() << " " << it.blue() << '\n'; } out.decreaseIndent(); - out.decreaseIndent(); + out.write() << "]\n"; } } @@ -206,19 +203,16 @@ void NodeItem::writeField(const char* field, const std::vector& value, In return; if (value.size() == 1) { - out.increaseIndent(); out.write() << field << " " << value[0] << '\n'; - out.decreaseIndent(); } else { - out.increaseIndent(); out.write() << field << " [\n"; out.increaseIndent(); for (auto it : value) { out.write() << it << '\n'; } out.decreaseIndent(); - out.decreaseIndent(); + out.write() << "]\n"; } } @@ -231,9 +225,9 @@ LabelItem::LabelItem(const std::string& text) : text(text) void LabelItem::write(InventorOutput& out) const { - out.write("Label { \n"); + out.write("Label {\n"); out.write() << " label \"" << text << "\"\n"; - out.write("} \n"); + out.write("}\n"); } // ----------------------------------------------------------------------------- @@ -245,9 +239,9 @@ InfoItem::InfoItem(const std::string& text) : text(text) void InfoItem::write(InventorOutput& out) const { - out.write("Info { \n"); + out.write("Info {\n"); out.write() << " string \"" << text << "\"\n"; - out.write("} \n"); + out.write("}\n"); } // ----------------------------------------------------------------------------- @@ -259,9 +253,9 @@ BaseColorItem::BaseColorItem(const ColorRGB& rgb) : rgb(rgb) void BaseColorItem::write(InventorOutput& out) const { - out.write("BaseColor { \n"); + out.write("BaseColor {\n"); out.write() << " rgb " << rgb.red() << " " << rgb.green() << " " << rgb.blue() << '\n'; - out.write("} \n"); + out.write("}\n"); } // ----------------------------------------------------------------------------- @@ -428,7 +422,7 @@ void DrawStyleItem::write(InventorOutput& out) const out.write() << " style " << style.styleAsString() << '\n'; out.write() << " pointSize " << style.pointSize << '\n'; out.write() << " lineWidth " << style.lineWidth << '\n'; - out.write() << " linePattern " << style.linePattern << '\n'; + out.write() << " linePattern " << style.patternAsString() << '\n'; out.write() << "}\n"; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e2596bd904..7ddf4a9452 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -34,11 +34,13 @@ set(CMAKE_AUTOMOC ON) include_directories( ${QtGui_INCLUDE_DIRS} ${QtTest_INCLUDE_DIRS} + ${COIN3D_INCLUDE_DIRS} ) # ------------------------------------------------------ set (InventorBuilder_LIBS + ${COIN3D_LIBRARIES} FreeCADBase ) diff --git a/tests/src/InventorBuilder.cpp b/tests/src/InventorBuilder.cpp index 27cd47c5ac..29aa9c0a3b 100644 --- a/tests/src/InventorBuilder.cpp +++ b/tests/src/InventorBuilder.cpp @@ -1,6 +1,10 @@ #include +#include #include #include +#include +#include +#include class testInventorBuilder : public QObject { @@ -15,10 +19,17 @@ public: { } + SoNode* loadBuffer(const std::string& buffer) + { + SoInput in; + in.setBuffer((void *)buffer.c_str(), buffer.size()); + return SoDB::readAll(&in); + } + private Q_SLOTS: void initTestCase() { - + SoDB::init(); } void initTestCase_data() { @@ -26,7 +37,7 @@ private Q_SLOTS: } void cleanupTestCase() { - + SoDB::finish(); } void init() @@ -39,17 +50,267 @@ private Q_SLOTS: output.str(std::string()); } + // Must be the very first test! void test_Output() { QCOMPARE(output.str().c_str(), "#Inventor V2.1 ascii \n\n"); } + void test_Invalid() + { + SoNode* node = loadBuffer("Hello, World"); + QCOMPARE(node, nullptr); + } + + void test_MaterialBinding_data() + { + QTest::addColumn("result"); + QTest::newRow("MaterialBinding") << "MaterialBinding { value OVERALL } \n"; + } + void test_MaterialBinding() { + QFETCH(QString, result); + Base::MaterialBindingItem item{Base::MaterialBinding{}}; builder.addNode(item); + QString string = QString::fromStdString(output.str()); - QCOMPARE(output.str().c_str(), "MaterialBinding { value OVERALL } \n"); + QCOMPARE(string, result); + } + + void test_Label_data() + { + auto result = +R"(Label { + label "FreeCAD" +} +)"; + QTest::addColumn("result"); + QTest::newRow("Label") << result; + } + + void test_Label() + { + QFETCH(QString, result); + + Base::LabelItem item{"FreeCAD"}; + builder.addNode(item); + QString string = QString::fromStdString(output.str()); + + QCOMPARE(string, result); + } + + void test_Info_data() + { + auto result = +R"(Info { + string "FreeCAD" +} +)"; + QTest::addColumn("result"); + QTest::newRow("Label") << result; + } + + void test_Info() + { + QFETCH(QString, result); + + Base::InfoItem item{"FreeCAD"}; + builder.addNode(item); + QString string = QString::fromStdString(output.str()); + + QCOMPARE(string, result); + } + + void test_BaseColor_data() + { + auto result = +R"(BaseColor { + rgb 0.21 0.3 0.4 +} +)"; + QTest::addColumn("result"); + QTest::newRow("BaseColor") << result; + } + + void test_BaseColor() + { + QFETCH(QString, result); + + Base::BaseColorItem item{Base::ColorRGB{0.21F, 0.3F, 0.4F}}; + builder.addNode(item); + QString string = QString::fromStdString(output.str()); + + QCOMPARE(string, result); + } + + void test_Material_data() + { + auto result = +R"(Material { + diffuseColor 1 0 0 +} +)"; + QTest::addColumn("result"); + QTest::newRow("Material") << result; + } + + void test_Material() + { + QFETCH(QString, result); + + Base::MaterialItem item; + item.setDiffuseColor({Base::ColorRGB{1,0,0}}); + builder.addNode(item); + QString string = QString::fromStdString(output.str()); + + QCOMPARE(string, result); + } + + void test_Materials_data() + { + auto result = +R"(Material { + diffuseColor [ + 1 0 0 + 0 1 0 + 0 0 1 + ] +} +)"; + QTest::addColumn("result"); + QTest::newRow("Material") << result; + } + + void test_Materials() + { + QFETCH(QString, result); + + Base::MaterialItem item; + item.setDiffuseColor({Base::ColorRGB{1,0,0}, + Base::ColorRGB{0,1,0}, + Base::ColorRGB{0,0,1}}); + builder.addNode(item); + QString string = QString::fromStdString(output.str()); + + QCOMPARE(string, result); + + SoNode* node = loadBuffer(output.str()); + QVERIFY(node != nullptr); + QVERIFY(node->getRefCount() == 0); + } + + void test_DrawStyle_data() + { + auto result = +R"(DrawStyle { + style FILLED + pointSize 2 + lineWidth 2 + linePattern 0xffff +} +)"; + QTest::addColumn("result"); + QTest::newRow("DrawStyle") << result; + } + + void test_DrawStyle() + { + QFETCH(QString, result); + + Base::DrawStyleItem item{Base::DrawStyle{}}; + builder.addNode(item); + QString string = QString::fromStdString(output.str()); + + QCOMPARE(string, result); + } + + void test_ShapeHints_data() + { + auto result = +R"(ShapeHints { + creaseAngle 0.5 +} +)"; + QTest::addColumn("result"); + QTest::newRow("ShapeHints") << result; + } + + void test_ShapeHints() + { + QFETCH(QString, result); + + Base::ShapeHintsItem item{0.5F}; + builder.addNode(item); + QString string = QString::fromStdString(output.str()); + + QCOMPARE(string, result); + } + + void test_PolygonOffset_data() + { + auto result = +R"(PolygonOffset { + factor 1 + units 1 + styles FILLED + on TRUE +} +)"; + QTest::addColumn("result"); + QTest::newRow("PolygonOffset") << result; + } + + void test_PolygonOffset() + { + QFETCH(QString, result); + + Base::PolygonOffsetItem item{Base::PolygonOffset{}}; + builder.addNode(item); + QString string = QString::fromStdString(output.str()); + + QCOMPARE(string, result); + } + + void test_PointSet_data() + { + auto result = "PointSet { }\n"; + QTest::addColumn("result"); + QTest::newRow("PointSet") << result; + } + + void test_PointSet() + { + QFETCH(QString, result); + + Base::PointSetItem item; + builder.addNode(item); + QString string = QString::fromStdString(output.str()); + + QCOMPARE(string, result); + } + + void test_LineItem() + { + Base::Line3f line; + Base::DrawStyle style; + Base::LineItem item{line, style}; + builder.addNode(item); + + SoNode* node = loadBuffer(output.str()); + QVERIFY(node != nullptr); + } + + void test_PointItem() + { + Base::Vector3f pnt; + Base::DrawStyle style; + Base::PointItem item{pnt, style}; + builder.addNode(item); + + SoNode* node = loadBuffer(output.str()); + QVERIFY(node != nullptr); } private: