From 60b4851753542356156dc18ebc587350724ec91a Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 29 Oct 2022 14:46:10 +0300 Subject: [PATCH] Fix reading json. --- src/libs/vtest/abstracttest.cpp | 29 ++++---- src/libs/vtest/abstracttest.h | 66 ++++++++++++------- src/test/ValentinaTest/tst_vabstractpiece.cpp | 14 ++-- src/test/ValentinaTest/tst_vpiece.cpp | 31 +++++---- 4 files changed, 88 insertions(+), 52 deletions(-) diff --git a/src/libs/vtest/abstracttest.cpp b/src/libs/vtest/abstracttest.cpp index d65cd908b..776b9de71 100644 --- a/src/libs/vtest/abstracttest.cpp +++ b/src/libs/vtest/abstracttest.cpp @@ -118,13 +118,16 @@ void AbstractTest::PassmarkDataFromJson(const QString &json, VPiecePassmarkData try { - auto previousSAPoint = PointFromJson(passmarkData[QStringLiteral("previousSAPoint")].toObject()); + VSAPoint previousSAPoint; + PointFromJson(passmarkData[QStringLiteral("previousSAPoint")].toObject(), previousSAPoint); data.previousSAPoint = previousSAPoint; - auto passmarkSAPoint = PointFromJson(passmarkData[QStringLiteral("passmarkSAPoint")].toObject()); + VSAPoint passmarkSAPoint; + PointFromJson(passmarkData[QStringLiteral("passmarkSAPoint")].toObject(), passmarkSAPoint); data.passmarkSAPoint = passmarkSAPoint; - auto nextSAPoint = PointFromJson(passmarkData[QStringLiteral("nextSAPoint")].toObject()); + VSAPoint nextSAPoint; + PointFromJson(passmarkData[QStringLiteral("nextSAPoint")].toObject(), nextSAPoint); data.nextSAPoint = nextSAPoint; } catch (const VException &e) @@ -198,7 +201,7 @@ void AbstractTest::PassmarkShapeFromJson(const QString &json, QVector &s QString type; AbstractTest::ReadStringValue(lineObject, typeKey, type); - if (type != typeid(QLineF).name()) + if (type != QLatin1String("QLineF")) { const QString error = QStringLiteral("Invalid json file '%1'. Unexpected class '%2'.") .arg(json, lineObject[typeKey].toString()); @@ -213,7 +216,7 @@ void AbstractTest::PassmarkShapeFromJson(const QString &json, QVector &s void AbstractTest::ComparePathsDistance(const QVector &ekv, const QVector &ekvOrig) const { // Begin comparison - QCOMPARE(ekv.size(), ekvOrig.size());// First check if sizes equal + QCOMPARE(ekv.size(), ekvOrig.size());// First check if sizes are equal const qreal testAccuracy = MmToPixel(1.); for (int i=0; i < ekv.size(); i++) @@ -526,7 +529,7 @@ void AbstractTest::ReadPointValue(const QJsonObject &itemObject, const QString & { if (itemObject.contains(attribute)) { - value = PointFromJson(itemObject[attribute].toObject()); + PointFromJson(itemObject[attribute].toObject(), value); } else { @@ -728,8 +731,11 @@ void AbstractTest::ReadDoubleValue(const QJsonObject &itemObject, const QString //--------------------------------------------------------------------------------------------------------------------- auto AbstractTest::QLineFromJson(const QJsonObject &itemObject) -> QLineF { - return {PointFromJson(itemObject[QStringLiteral("p1")].toObject()), - PointFromJson(itemObject[QStringLiteral("p2")].toObject())}; + QPointF p1; + QPointF p2; + PointFromJson(itemObject[QStringLiteral("p1")].toObject(), p1); + PointFromJson(itemObject[QStringLiteral("p2")].toObject(), p2); + return {p1, p2}; } //--------------------------------------------------------------------------------------------------------------------- @@ -808,9 +814,9 @@ void AbstractTest::DBFromJson(const QJsonObject &dbObject, QSharedPointer(itemObject); + VPointF point; + PointFromJson(itemObject, point); data->UpdateGObject(point.id(), new VPointF(point)); break; } diff --git a/src/libs/vtest/abstracttest.h b/src/libs/vtest/abstracttest.h index ed9b2c92b..3f513392e 100644 --- a/src/libs/vtest/abstracttest.h +++ b/src/libs/vtest/abstracttest.h @@ -127,7 +127,7 @@ protected: static auto ReadPointData(const QJsonObject &pointObject) -> T; template - static auto PointFromJson(const QJsonObject &pointObject) -> T; + static auto PointFromJson(const QJsonObject &pointObject, T &point) -> void; auto QLineFromJson(const QJsonObject &itemObject) -> QLineF; void SplineFromJson(const QJsonObject &itemObject, QSharedPointer &data); @@ -151,13 +151,16 @@ inline auto AbstractTest::VectorFromJson(const QString &json) -> QVector TestRoot(vectorObject, vectorKey, json); QJsonArray vectorArray = vectorObject[vectorKey].toArray(); - QVector vector(vectorArray.size()); + QVector vector; + vector.reserve(vectorArray.size()); for (auto && item : vectorArray) { try { - vector.append(PointFromJson(item.toObject())); + T point; + PointFromJson(item.toObject(), point); + vector.append(point); } catch (const VException &e) { @@ -177,7 +180,34 @@ inline void AbstractTest::CheckClassType(const QJsonObject &itemObject) QString type; AbstractTest::ReadStringValue(itemObject, typeKey, type); - if (type != typeid(T).name()) + const QStringList types + { + QStringLiteral("QPointF"), // 0 + QStringLiteral("VLayoutPoint"), // 1 + QStringLiteral("VRawSAPoint"), // 2 + QStringLiteral("VSAPoint"), // 3 + }; + + bool res = false; + switch (types.indexOf(type)) + { + case 0: + res = (typeid(T) == typeid(QPointF)); + break; + case 1: + res = (typeid(T) == typeid(VLayoutPoint)); + break; + case 2: + res = (typeid(T) == typeid(VRawSAPoint)); + break; + case 3: + res = (typeid(T) == typeid(VSAPoint)); + break; + default: + break; + } + + if (not res) { throw VException(QStringLiteral("Unexpected class '%2'.").arg(itemObject[typeKey].toString())); } @@ -201,18 +231,16 @@ inline auto AbstractTest::ReadPointData(const QJsonObject &pointObject) -> T //--------------------------------------------------------------------------------------------------------------------- template -inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> T +inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject, T &point) -> void { CheckClassType(pointObject); - return ReadPointData(pointObject); + point = ReadPointData(pointObject); } //--------------------------------------------------------------------------------------------------------------------- template<> -inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VPointF +inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject, VPointF &point) -> void { - CheckClassType(pointObject); - vidtype id = NULL_ID; AbstractTest::ReadDoubleValue(pointObject, QStringLiteral("id"), id); @@ -231,10 +259,8 @@ inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VPoin qreal y = 0; AbstractTest::ReadDoubleValue(pointObject, QChar('y'), y); - VPointF point(x, y, name, mx, my); + point = VPointF(x, y, name, mx, my); point.setId(id); - - return point; } //--------------------------------------------------------------------------------------------------------------------- @@ -256,19 +282,19 @@ inline auto AbstractTest::ReadPointData(const QJsonObject &pointObject) -> VLayo //--------------------------------------------------------------------------------------------------------------------- template<> -inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VLayoutPoint +inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject, VLayoutPoint &point) -> void { CheckClassType(pointObject); - return ReadPointData(pointObject); + point = ReadPointData(pointObject); } //--------------------------------------------------------------------------------------------------------------------- template<> -inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VSAPoint +inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject, VSAPoint &point) -> void { CheckClassType(pointObject); - VSAPoint point(ReadPointData(pointObject)); + point = VSAPoint(ReadPointData(pointObject)); qreal saBefore; AbstractTest::ReadDoubleValue(pointObject, QStringLiteral("saBefore"), saBefore, QStringLiteral("-1")); @@ -282,23 +308,19 @@ inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VSAPo AbstractTest::ReadDoubleValue(pointObject, QStringLiteral("angle"), angleType, QString::number(static_cast(PieceNodeAngle::ByLength))); point.SetAngleType(angleType); - - return point; } //--------------------------------------------------------------------------------------------------------------------- template<> -inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VRawSAPoint +inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject, VRawSAPoint &point) -> void { CheckClassType(pointObject); - VRawSAPoint point(ReadPointData(pointObject)); + point = VRawSAPoint(ReadPointData(pointObject)); bool loopPoint; AbstractTest::ReadBooleanValue(pointObject, QStringLiteral("loopPoint"), loopPoint, QStringLiteral("0")); point.SetLoopPoint(loopPoint); - - return point; } #endif // ABSTRACTTEST_H diff --git a/src/test/ValentinaTest/tst_vabstractpiece.cpp b/src/test/ValentinaTest/tst_vabstractpiece.cpp index 9ef493550..76bda6036 100644 --- a/src/test/ValentinaTest/tst_vabstractpiece.cpp +++ b/src/test/ValentinaTest/tst_vabstractpiece.cpp @@ -47,7 +47,7 @@ void TST_VAbstractPiece::EquidistantRemoveLoop_data() QTest::addColumn("width"); QTest::addColumn>("ekvOrig"); - auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output, qreal width) + auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output, qreal width) { QVector inputPoints = AbstractTest::VectorFromJson(input); QVector outputPoints = AbstractTest::VectorFromJson(output); @@ -322,7 +322,7 @@ void TST_VAbstractPiece::LayoutAllowanceRemoveLoop_data() QTest::addColumn("width"); QTest::addColumn>("ekvOrig"); - auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output, qreal width) + auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output, qreal width) { QVector inputPoints = AbstractTest::VectorFromJson(input); QVector outputPoints = AbstractTest::VectorFromJson(output); @@ -385,7 +385,7 @@ void TST_VAbstractPiece::RawPathRemoveLoop_data() const QTest::addColumn>("path"); QTest::addColumn>("expect"); - auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output) + auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output) { QVector inputPoints = AbstractTest::VectorFromJson(input); QVector outputPoints = AbstractTest::VectorFromJson(output); @@ -875,7 +875,7 @@ void TST_VAbstractPiece::BrokenDetailEquidistant_data() QTest::addColumn("width"); QTest::addColumn>("ekvOrig"); - auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output, qreal width) + auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output, qreal width) { QVector inputPoints = AbstractTest::VectorFromJson(input); QVector outputPoints = AbstractTest::VectorFromJson(output); @@ -979,7 +979,7 @@ void TST_VAbstractPiece::EquidistantAngleType_data() QTest::addColumn("width"); QTest::addColumn>("ekvOrig"); - auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output, qreal width) + auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output, qreal width) { QVector inputPoints = AbstractTest::VectorFromJson(input); QVector outputPoints = AbstractTest::VectorFromJson(output); @@ -1128,7 +1128,7 @@ void TST_VAbstractPiece::TestCorrectEquidistantPoints_data() QTest::addColumn>("before"); QTest::addColumn>("expect"); - auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output) + auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output) { QVector inputPoints = AbstractTest::VectorFromJson(input); QVector outputPoints = AbstractTest::VectorFromJson(output); @@ -1280,7 +1280,7 @@ void TST_VAbstractPiece::IsAllowanceValid_data() const QTest::addColumn>("allowance"); QTest::addColumn("valid"); - auto ASSERT_TEST_CASE = [this](const char *title, const QString &base, const QString &allowance, bool valid) + auto ASSERT_TEST_CASE = [](const char *title, const QString &base, const QString &allowance, bool valid) { QVector basePoints = AbstractTest::VectorFromJson(base); QVector allowancePoints = AbstractTest::VectorFromJson(allowance); diff --git a/src/test/ValentinaTest/tst_vpiece.cpp b/src/test/ValentinaTest/tst_vpiece.cpp index 67fd62c78..3de24777b 100644 --- a/src/test/ValentinaTest/tst_vpiece.cpp +++ b/src/test/ValentinaTest/tst_vpiece.cpp @@ -44,21 +44,28 @@ TST_VPiece::TST_VPiece(QObject *parent) //--------------------------------------------------------------------------------------------------------------------- void TST_VPiece::Issue620() { - // See file /src/app/share/collection/bugs/Issue_#620.vit - // Check main path - const Unit unit = Unit::Cm; - QSharedPointer data(new VContainer(nullptr, &unit, VContainer::UniqueNamespace())); - VAbstractValApplication::VApp()->SetPatternUnits(unit); + try + { + // See file /src/app/share/collection/bugs/Issue_#620.vit + // Check main path + const Unit unit = Unit::Cm; + QSharedPointer data(new VContainer(nullptr, &unit, VContainer::UniqueNamespace())); + VAbstractValApplication::VApp()->SetPatternUnits(unit); - VPiece detail; - AbstractTest::PieceFromJson(QStringLiteral("://Issue_620/input.json"), detail, data); + VPiece detail; + AbstractTest::PieceFromJson(QStringLiteral("://Issue_620/input.json"), detail, data); - QVector pointsEkv; - CastTo(detail.MainPathPoints(data.data()), pointsEkv); - QVector origPoints = AbstractTest::VectorFromJson(QStringLiteral("://Issue_620/output.json")); + QVector pointsEkv; + CastTo(detail.MainPathPoints(data.data()), pointsEkv); + QVector origPoints = AbstractTest::VectorFromJson(QStringLiteral("://Issue_620/output.json")); - // Begin comparison - ComparePathsDistance(pointsEkv, origPoints); + // Begin comparison + ComparePathsDistance(pointsEkv, origPoints); + } + catch (const VException &e) + { + QFAIL(qUtf8Printable(e.ErrorMessage())); + } } //---------------------------------------------------------------------------------------------------------------------