Files
qelectrotech-source-mirror/tests/qttest/tst_diagramcontext.cpp
T
ispyisail 82fc56c5e6 Fix Project properties > OK marking the project modified with no edit
Pressing OK in Projet > Propriétés du projet without changing anything
marked the project modified and rewrote its defaults for new folios.
Besides the Page Num placeholder (fixed separately), four causes:

- Title block custom fields: since #271 the dialog offers the
  template's variables for filling in; those left empty came back as
  part of the context. Drop the ones not in the stored context.
- DiagramContext::remove() left the key's show flag behind, so a
  context with a key removed never equalled one without it.
- Guides: applyConf() marked the project modified whether or not the
  guides changed. Compare them like every other setting.
- Wire dash size and cross-reference offsets: the spin boxes cannot
  show the stored default (dash size 1 under a minimum of 2; offset 0
  under a minimum of 40 shown as "Default"), so the clamped value came
  back. Keep the stored value unless the box shows something else, as
  #1204 did for the wire panel.

Checked in the GUI (with the Page Num fix as well) on grafcet,
schema_indus and Projet_vierge: OK with no edit then Ctrl+S saves the
same file as Ctrl+S alone; master writes dash-size 1 -> 2, a
cross-reference offset 0 -> 40, and on Projet_vierge 22 empty custom
fields. New test for DiagramContext::remove() fails without the fix.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 21:04:54 +13:00

91 lines
2.7 KiB
C++

// SPDX-License-Identifier: GPL-2.0-or-later
#include <QtTest>
#include "diagramcontext.h"
#include "qetapp.h"
QString QETApp::m_interface_language;
/**
DiagramContext::fromXml() has two readers: QDom, for projects, and
pugixml, for element definitions in the collection. Both must give the
values the next save writes -- stray spaces around real content trimmed,
accented characters kept.
*/
class tst_diagramcontext : public QObject
{
Q_OBJECT
static QByteArray xml(const QString &value)
{
return QStringLiteral(
"<elementInformations>"
"<elementInformation name=\"v\" show=\"1\">%1</elementInformation>"
"</elementInformations>")
.arg(value)
.toUtf8();
}
static QString fromDom(const QByteArray &data)
{
QDomDocument doc;
if (!doc.setContent(data)) return QStringLiteral("<parse error>");
DiagramContext dc;
dc.fromXml(doc.documentElement(), QStringLiteral("elementInformation"));
return dc.value(QStringLiteral("v")).toString();
}
static QString fromPugi(const QByteArray &data)
{
pugi::xml_document doc;
if (!doc.load_buffer(data.constData(), size_t(data.size())))
return QStringLiteral("<parse error>");
DiagramContext dc;
dc.fromXml(doc.document_element(), QStringLiteral("elementInformation"));
return dc.value(QStringLiteral("v")).toString();
}
private slots:
void bothReadersAgree_data()
{
QTest::addColumn<QString>("value");
QTest::addColumn<QString>("expected");
QTest::newRow("plain") << "PRISE" << "PRISE";
QTest::newRow("stray spaces") << " PRISE " << "PRISE";
QTest::newRow("accents") << "Armoire façade été" << "Armoire façade été";
QTest::newRow("accents and stray spaces") << " Moteur à cage " << "Moteur à cage";
QTest::newRow("non-Latin") << "Двигатель 電機" << "Двигатель 電機";
}
void bothReadersAgree()
{
QFETCH(QString, value);
QFETCH(QString, expected);
QCOMPARE(fromDom(xml(value)), expected);
QCOMPARE(fromPugi(xml(value)), expected);
}
// remove() forgets the key entirely: a context with a key added
// then removed equals one that never had it. remove() used to keep
// the key's show flag, so the two never compared equal, and
// Project properties > OK saw a change where there was none.
void removeForgetsTheKey()
{
DiagramContext edited;
edited.addValue(QStringLiteral("author"), QStringLiteral("N.V."));
edited.addValue(QStringLiteral("01-numprojet"), QString());
edited.remove(QStringLiteral("01-numprojet"));
DiagramContext stored;
stored.addValue(QStringLiteral("author"), QStringLiteral("N.V."));
QCOMPARE(edited.keys(), stored.keys());
QVERIFY(edited == stored);
}
};
QTEST_APPLESS_MAIN(tst_diagramcontext)
#include "tst_diagramcontext.moc"