Add frozen TikZRefinery demo baseline and build pipeline
This commit is contained in:
@@ -0,0 +1,262 @@
|
||||
#include "controller/tkrEditorController.h"
|
||||
#include "model/tkrTikzDocument.h"
|
||||
#include "ui/tkrDiagramScene.h"
|
||||
#include "ui/tkrMainWindow.h"
|
||||
|
||||
#include <QAction>
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QGraphicsRectItem>
|
||||
#include <QGraphicsView>
|
||||
#include <QFileDialog>
|
||||
#include <QFontDatabase>
|
||||
#include <QImage>
|
||||
#include <QPainter>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QStatusBar>
|
||||
#include <QTimer>
|
||||
#include <cmath>
|
||||
|
||||
using namespace TikzRefinery;
|
||||
|
||||
namespace {
|
||||
|
||||
void require(bool bCondition, const char *pMessage)
|
||||
{
|
||||
if (!bCondition) {
|
||||
qFatal("%s", pMessage);
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray readCorpus(const QString &strName)
|
||||
{
|
||||
QFile fileSource(QStringLiteral(TKR_CORPUS_DIRECTORY) + QLatin1Char('/') + strName);
|
||||
require(fileSource.open(QIODevice::ReadOnly), "Cannot open corpus fixture");
|
||||
const QByteArray baSource = fileSource.readAll();
|
||||
require(fileSource.error() == QFileDevice::NoError, "Cannot read corpus fixture");
|
||||
return baSource;
|
||||
}
|
||||
|
||||
void checkCorpus(const QString &strName, qreal rRightFirstX, const QString &strEvidenceDirectory)
|
||||
{
|
||||
const QString strPath = QStringLiteral(TKR_CORPUS_DIRECTORY) + QLatin1Char('/') + strName;
|
||||
const QByteArray baSource = readCorpus(strName);
|
||||
tkrTikzDocument document;
|
||||
tkrDiagramScene diagramScene;
|
||||
tkrEditorController controller(document, diagramScene);
|
||||
QString strStatus;
|
||||
QObject::connect(&controller, &tkrEditorController::statusChanged,
|
||||
[&strStatus](const QString &strMessage) { strStatus = strMessage; });
|
||||
require(controller.openDocument(strPath).bSucceeded, "Corpus open failed");
|
||||
require(document.sourceBytes() == baSource && !document.hasUnsavedChanges(), "Open altered bytes");
|
||||
require(document.elements().size() == 2 && diagramScene.items().size() == 2,
|
||||
"Expected exactly two rectangles; bounding path must not become an item");
|
||||
require(strStatus.contains(QStringLiteral("Displayed 2 element(s)"))
|
||||
&& strStatus.contains(QStringLiteral("drag a corner to resize")), "Display status is misleading");
|
||||
require(diagramScene.sceneRect() == QRectF(0, 0, 700, 500), "Fixed comparison extent changed");
|
||||
for (int iElement = 0; iElement < 2; ++iElement) {
|
||||
const ElementRecord &elementRecord = document.elements().at(iElement);
|
||||
const qreal rFirstX = iElement == 0 ? 0.0 : rRightFirstX;
|
||||
require(elementRecord.elementKind == ElementKind::Rectangle
|
||||
&& elementRecord.uElementId == static_cast<quint64>(iElement + 1)
|
||||
&& elementRecord.uDocumentRevision == document.revision(), "Element identity mismatch");
|
||||
require(elementRecord.ptFirstTikzPosition == QPointF(rFirstX, 0)
|
||||
&& elementRecord.ptSecondTikzPosition == QPointF(rFirstX + 1, 1), "Corpus geometry mismatch");
|
||||
const SourceRange rangesCoordinates[] = {elementRecord.rangeFirstX, elementRecord.rangeFirstY,
|
||||
elementRecord.rangeSecondX, elementRecord.rangeSecondY};
|
||||
const qreal rExpected[] = {rFirstX, 0, rFirstX + 1, 1};
|
||||
for (int iCoordinate = 0; iCoordinate < 4; ++iCoordinate) {
|
||||
const SourceRange &rangeSource = rangesCoordinates[iCoordinate];
|
||||
bool bConverted = false;
|
||||
const qreal rActual = baSource.mid(rangeSource.nStart, rangeSource.nLength).toDouble(&bConverted);
|
||||
require(bConverted && rActual == rExpected[iCoordinate], "Numeric byte range mismatch");
|
||||
}
|
||||
QGraphicsRectItem *pRectangle = nullptr;
|
||||
for (QGraphicsItem *pItem : diagramScene.items()) {
|
||||
if (pItem->data(0).toULongLong() == elementRecord.uElementId) {
|
||||
pRectangle = qgraphicsitem_cast<QGraphicsRectItem *>(pItem);
|
||||
}
|
||||
}
|
||||
require(pRectangle && pRectangle->rect() == QRectF(200 + 100 * rFirstX, 200, 100, 100),
|
||||
"Scene rectangle origin/size mismatch");
|
||||
require(std::abs(pRectangle->pen().widthF() - 0.6 * 100 * 2.54 / 72.27) < 1e-10,
|
||||
"TeX point width mismatch");
|
||||
require(!pRectangle->pen().isCosmetic() && pRectangle->brush().style() == Qt::NoBrush,
|
||||
"Rectangle appearance mismatch");
|
||||
require(!pRectangle->flags().testFlag(QGraphicsItem::ItemIsMovable)
|
||||
&& pRectangle->flags().testFlag(QGraphicsItem::ItemIsSelectable), "Selection/scene-controlled movement mismatch");
|
||||
}
|
||||
if (!strEvidenceDirectory.isEmpty()) {
|
||||
QImage imageCanvas(700, 500, QImage::Format_ARGB32_Premultiplied);
|
||||
imageCanvas.fill(Qt::white);
|
||||
QPainter painter(&imageCanvas);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
diagramScene.render(&painter, QRectF(0, 0, 700, 500), QRectF(0, 0, 700, 500));
|
||||
painter.end();
|
||||
require(imageCanvas.save(strEvidenceDirectory + QLatin1Char('/')
|
||||
+ strName.chopped(4) + QStringLiteral("-canvas.png")), "Cannot save display evidence");
|
||||
}
|
||||
// Failed refresh must clear both the scene and stored records, even if the
|
||||
// model still contains records from a prior successful interpretation.
|
||||
ElementRecord elementUnsupported = document.elements().first();
|
||||
elementUnsupported.elementKind = static_cast<ElementKind>(99);
|
||||
tkrCoordinateMapper mapper;
|
||||
require(!diagramScene.displayElements({document.elements().first(), elementUnsupported}, mapper).bSucceeded
|
||||
&& diagramScene.items().isEmpty(), "Display failure left partial items");
|
||||
document.loadSource("\\draw (0cm,0cm) to (1cm,1cm);", QStringLiteral("unsupported.tex"));
|
||||
elementUnsupported.uDocumentRevision = document.revision();
|
||||
require(document.setParsedElements({elementUnsupported}, document.revision()).bSucceeded,
|
||||
"Cannot prepare stale-record regression");
|
||||
require(!controller.refreshParsedDocument().bSucceeded && document.elements().isEmpty()
|
||||
&& diagramScene.items().isEmpty(), "Parse failure retained stale records/items");
|
||||
}
|
||||
|
||||
void checkByteRangesAndVariants()
|
||||
{
|
||||
tkrTikzParserExplicit parser;
|
||||
const QByteArray baSource = QByteArray::fromHex("efbbbf25c3a90d0a")
|
||||
+ "% \\draw (99cm,99cm) rectangle (100cm,100cm);\r\n"
|
||||
"\\begin{tikzpicture}\r\n"
|
||||
"\\draw[solid, line width = 0.6pt, black]\r\n"
|
||||
" (+2.50cm, % corner\r\n -.5cm) rectangle (-1.cm, +1.25cm); % keep\r\n"
|
||||
"\\end{tikzpicture}\r\n";
|
||||
const ParseResult parseResult = parser.parse(baSource, 42);
|
||||
require(parseResult.bSafeToEdit && parseResult.vecElements.size() == 1, "Whitespace/comments/options rejected");
|
||||
const ElementRecord &elementRecord = parseResult.vecElements.first();
|
||||
require(elementRecord.ptFirstTikzPosition == QPointF(2.5, -0.5)
|
||||
&& elementRecord.ptSecondTikzPosition == QPointF(-1, 1.25), "Signed/reversed corners mismatch");
|
||||
const SourceRange rangesCoordinates[] = {elementRecord.rangeFirstX, elementRecord.rangeFirstY,
|
||||
elementRecord.rangeSecondX, elementRecord.rangeSecondY};
|
||||
const QByteArray baExpected[] = {"+2.50", "-.5", "-1.", "+1.25"};
|
||||
for (int iCoordinate = 0; iCoordinate < 4; ++iCoordinate) {
|
||||
require(baSource.mid(rangesCoordinates[iCoordinate].nStart, rangesCoordinates[iCoordinate].nLength)
|
||||
== baExpected[iCoordinate], "UTF-8/CRLF/BOM shifted byte ranges");
|
||||
}
|
||||
tkrTikzDocument document;
|
||||
document.loadSource(baSource, QStringLiteral("ranges.tex"));
|
||||
require(document.applyCoordinateEdits({{elementRecord.rangeFirstX, "+2.50", "3.75", document.revision()}})
|
||||
.bSucceeded, "Parsed range could not drive guarded byte replacement");
|
||||
QByteArray baExpectedSource = baSource;
|
||||
baExpectedSource.replace(baSource.indexOf("+2.50"), 5, "3.75");
|
||||
require(document.sourceBytes() == baExpectedSource, "Parsed range changed unrelated bytes");
|
||||
const ParseResult parseDefault = parser.parse("\\draw (0cm,0cm) rectangle (1cm,1cm);", 1);
|
||||
require(parseDefault.bSafeToEdit && parseDefault.vecElements.first().rLineWidthPoints == 0.4,
|
||||
"Default rectangle width rejected");
|
||||
tkrDiagramScene diagramScene;
|
||||
tkrCoordinateMapper mapper;
|
||||
require(mapper.configure(QPointF(10, 20), 50).bSucceeded, "Cannot configure alternate mapping");
|
||||
require(diagramScene.displayElements(parseResult.vecElements, mapper).bSucceeded, "Reversed corners not displayed");
|
||||
const QGraphicsRectItem *pRectangle = qgraphicsitem_cast<QGraphicsRectItem *>(diagramScene.items().first());
|
||||
require(pRectangle && pRectangle->rect() == QRectF(-40, -42.5, 175, 87.5), "Alternate mapping/reversed rectangle mismatch");
|
||||
}
|
||||
|
||||
void checkUnsupportedSyntax()
|
||||
{
|
||||
tkrTikzParserExplicit parser;
|
||||
const QByteArray baValid = "\\draw (0cm,0cm) rectangle (1cm,1cm);";
|
||||
const QList<QByteArray> listUnsupported = {
|
||||
"", "% only a comment", "\\path[use as bounding box] (-2cm,-2cm) rectangle (5cm,3cm);",
|
||||
"\\draw (0,0) rectangle (1,1);", "\\draw (0mm,0cm) rectangle (1cm,1cm);",
|
||||
"\\draw (0cm,0cm) rectangle +(1cm,1cm);", "\\draw (0cm,0cm) rectangle (1+1cm,1cm);",
|
||||
"\\draw (A) rectangle (B);", "\\draw (0cm,0cm) to (1cm,1cm);",
|
||||
"\\draw[->] (0cm,0cm) rectangle (1cm,1cm);", "\\draw[fill=red] (0cm,0cm) rectangle (1cm,1cm);",
|
||||
"\\draw[scale=2] (0cm,0cm) rectangle (1cm,1cm);", "\\draw[custom] (0cm,0cm) rectangle (1cm,1cm);",
|
||||
"\\draw[line width=0pt] (0cm,0cm) rectangle (1cm,1cm);",
|
||||
"\\draw[line width=0.6pt,line width=1pt] (0cm,0cm) rectangle (1cm,1cm);",
|
||||
"\\draw (0cm,0cm) rectangle (0cm,1cm);", baValid.chopped(1),
|
||||
"\\foreach \\x in {1,2} {" + baValid + "}", "\\def\\box{" + baValid + "}",
|
||||
"\\begin{scope}[shift={(1cm,0cm)}]" + baValid + "\\end{scope}",
|
||||
"\\begin{tikzpicture}[scale=2]" + baValid + "\\end{tikzpicture}",
|
||||
"\\tikzset{every path/.style={scale=2}}" + baValid,
|
||||
baValid + "\\node at (0cm,0cm) {text};", baValid + "\\input{other.tex}",
|
||||
baValid + "\\path[use as bounding box] (-2cm,-2cm) rectangle (5cm,3cm);",
|
||||
"\\path[use as bounding box] (0cm,0cm) rectangle (1cm,1cm);" + baValid,
|
||||
"\\path[use as bounding box] (-2cm,-2cm) rectangle (5cm,3cm);"
|
||||
"\\path[use as bounding box] (-2cm,-2cm) rectangle (5cm,3cm);" + baValid,
|
||||
QByteArray::fromHex("c2a0") + baValid
|
||||
};
|
||||
for (const QByteArray &baUnsupported : listUnsupported) {
|
||||
const ParseResult parseResult = parser.parse(baUnsupported, 5);
|
||||
require(!parseResult.bSafeToEdit && parseResult.vecElements.isEmpty()
|
||||
&& !parseResult.comparisonExtent.bIsPresent && !parseResult.listDiagnostics.isEmpty(),
|
||||
"Unsupported input produced a partial/successful interpretation");
|
||||
}
|
||||
QByteArray baAlteredPreamble = readCorpus(QStringLiteral("boxes-initial.tex"));
|
||||
baAlteredPreamble.replace("\\usepackage{tikz}", "\\usepackage{tikz}\n\\tikzset{every picture/.style={scale=2}}");
|
||||
require(!parser.parse(baAlteredPreamble, 1).bSafeToEdit, "Altered preamble accepted");
|
||||
}
|
||||
|
||||
void checkWindowOpen(const QString &strEvidenceDirectory)
|
||||
{
|
||||
tkrMainWindow mainWindow;
|
||||
mainWindow.show();
|
||||
QAction *pOpenAction = nullptr;
|
||||
QAction *pSaveAction = nullptr;
|
||||
for (QAction *pAction : mainWindow.findChildren<QAction *>()) {
|
||||
if (pAction->shortcut() == QKeySequence(QKeySequence::Open)) {
|
||||
pOpenAction = pAction;
|
||||
} else if (pAction->shortcut() == QKeySequence(QKeySequence::Save)) {
|
||||
pSaveAction = pAction;
|
||||
}
|
||||
}
|
||||
require(pOpenAction && pSaveAction, "Window file actions missing");
|
||||
QTimer timerDialog;
|
||||
QObject::connect(&timerDialog, &QTimer::timeout, [&timerDialog]() {
|
||||
for (QWidget *pWidget : QApplication::topLevelWidgets()) {
|
||||
QFileDialog *pDialog = qobject_cast<QFileDialog *>(pWidget);
|
||||
if (pDialog && pDialog->isVisible()) {
|
||||
pDialog->selectFile(QStringLiteral(TKR_CORPUS_DIRECTORY "/boxes-initial.tex"));
|
||||
QMetaObject::invokeMethod(pDialog, "accept", Qt::QueuedConnection);
|
||||
timerDialog.stop();
|
||||
}
|
||||
}
|
||||
});
|
||||
QTimer timerTimeout;
|
||||
timerTimeout.setSingleShot(true);
|
||||
QObject::connect(&timerTimeout, &QTimer::timeout, []() { qFatal("Window open test timed out"); });
|
||||
timerTimeout.start(10000);
|
||||
timerDialog.start(20);
|
||||
pOpenAction->trigger(); // Exercise the actual window Open -> controller -> view wiring.
|
||||
timerTimeout.stop();
|
||||
QApplication::processEvents();
|
||||
const QGraphicsView *pView = mainWindow.findChild<QGraphicsView *>();
|
||||
const QPlainTextEdit *pSourceView = mainWindow.findChild<QPlainTextEdit *>();
|
||||
require(pView && pView->scene()->items().size() == 2, "Window canvas did not receive rectangles");
|
||||
require(pSourceView && pSourceView->isReadOnly()
|
||||
&& pSourceView->toPlainText().contains(QStringLiteral("rectangle (3cm,1cm)")),
|
||||
"Window source view did not load");
|
||||
require(!pSaveAction->isEnabled() && !mainWindow.isWindowModified(), "Display dirtied window");
|
||||
require(mainWindow.statusBar()->currentMessage().contains(QStringLiteral("Displayed 2 element(s)")),
|
||||
"Window display status missing");
|
||||
if (!strEvidenceDirectory.isEmpty()) {
|
||||
require(mainWindow.grab().save(strEvidenceDirectory + QStringLiteral("/rectangle-window-2026-10-21.png")),
|
||||
"Cannot save window evidence");
|
||||
}
|
||||
mainWindow.close();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int iArgumentCount, char *pArguments[])
|
||||
{
|
||||
QApplication::setAttribute(Qt::AA_DontUseNativeDialogs);
|
||||
QApplication application(iArgumentCount, pArguments);
|
||||
#ifdef Q_OS_WIN
|
||||
// The offscreen platform does not always enumerate Windows fonts. Load an
|
||||
// existing system font explicitly so retained window evidence is readable.
|
||||
const int iFontId = QFontDatabase::addApplicationFont(
|
||||
qEnvironmentVariable("SystemRoot") + QStringLiteral("/Fonts/segoeui.ttf"));
|
||||
if (iFontId >= 0) {
|
||||
QApplication::setFont(QFont(QFontDatabase::applicationFontFamilies(iFontId).first(), 9));
|
||||
}
|
||||
#endif
|
||||
const QString strEvidenceDirectory = iArgumentCount > 1 ? QString::fromLocal8Bit(pArguments[1]) : QString();
|
||||
checkCorpus(QStringLiteral("boxes-initial.tex"), 2, strEvidenceDirectory);
|
||||
checkCorpus(QStringLiteral("boxes-touching.tex"), 1, strEvidenceDirectory);
|
||||
checkByteRangesAndVariants();
|
||||
checkUnsupportedSyntax();
|
||||
checkWindowOpen(strEvidenceDirectory);
|
||||
qInfo("Rectangle parsing, byte ranges, controller and scene checks passed. Gestures and product acceptance remain untested.");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user