Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Implement tests for PSD cos parsing.

Wolthera van Hövell tot Westerflier46980106911c
diff --git a/libs/psdutils/tests/CMakeLists.txt b/libs/psdutils/tests/CMakeLists.txt
index eea444fc2d..935829e268 100644
--- a/libs/psdutils/tests/CMakeLists.txt
+++ b/libs/psdutils/tests/CMakeLists.txt
@@ -5,5 +5,6 @@ set(PSD_TEST_LIBS kritapsdutils kritatestsdk)
kis_add_tests(
psd_utils_test.cpp
compression_test.cpp
+ psd_cos_parser_test.cpp
NAME_PREFIX "kritapsdutils-"
LINK_LIBRARIES ${PSD_TEST_LIBS})
diff --git a/libs/psdutils/tests/psd_cos_parser_test.cpp b/libs/psdutils/tests/psd_cos_parser_test.cpp
new file mode 100644
index 0000000000..b8bb9c462d
--- /dev/null
+++ b/libs/psdutils/tests/psd_cos_parser_test.cpp
@@ -0,0 +1,68 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "psd_cos_parser_test.h"
+#include "cos/kis_cos_parser.h"
+#include "cos/kis_cos_writer.h"
+
+void psd_cos_parser_test::test_roundtrip_cos_struct_data()
+{
+ QTest::addColumn<QVariantHash>("doc");
+
+ QTest::addRow("empty") << QVariantHash ();
+ QTest::addRow("map") << QVariantHash ({{"/Map", QVariantHash ({{"/A", "a"}, {"/B", "b"}})}});
+ QTest::addRow("array") << QVariantHash ({{"/List", QVariantList ({1, 2, 3, 4})}});
+ QTest::addRow("string") << QVariantHash ({{"/A", "the (quick) \r brown fox"}, {"/B", "jumps over \rthe la\\zy dog."}});
+ QTest::addRow("int") << QVariantHash ({{"/A", 2}, {"/B", 12}});
+ QTest::addRow("double") << QVariantHash ({{"/A", 0.5}, {"/B", 0.001}});
+ QTest::addRow("bool") << QVariantHash ({{"/A", true}, {"/B", false}});
+}
+
+void psd_cos_parser_test::test_roundtrip_cos_struct()
+{
+ QFETCH(QVariantHash, doc);
+ KisCosWriter w;
+ QByteArray data = w.writeCosFromVariantHash(doc);
+
+ KisCosParser p;
+ QVariantHash newDoc = p.parseCosToJson(&data);
+ if (doc != newDoc) {
+ qDebug() << doc;
+ qDebug() << newDoc;
+ }
+ QVERIFY2(doc == newDoc, "Roundtrip failed");
+}
+
+void psd_cos_parser_test::test_parse_cos_struct_data()
+{
+ QTest::addColumn<QByteArray>("data");
+ QTest::addColumn<QVariantHash>("reference");
+
+ QTest::addRow("empty") << QByteArray("<<>>") << QVariantHash ();
+ QTest::addRow("bool") << QByteArray("<< /Ligatures true /DLigatures false >>")
+ << QVariantHash ({{"/Ligatures", true}, {"/DLigatures", false}});
+ QTest::addRow("int") << QByteArray("<< /FigureStyle 0 /PreHyphen 2 >>")
+ << QVariantHash ({{"/FigureStyle", 0}, {"/PreHyphen", 2}});
+ QTest::addRow("string") << QByteArray("\n\n<<\n\t/CloseDoubleQuote (\xFE\xFF \x1D)\n>>\n")
+ << QVariantHash({{"/CloseDoubleQuote", "”"}});
+ QTest::addRow("array of floats") << QByteArray("<< /Zone 36.0 /WordSpacing [ .8 1.0 1.33 ] /After -.5 >>")
+ << QVariantHash ({{"/Zone", 36.0}, {"/WordSpacing", QVariantList({0.8, 1.0, 1.33})}, {"/After", -0.5}});
+}
+
+void psd_cos_parser_test::test_parse_cos_struct()
+{
+ QFETCH(QByteArray, data);
+ QFETCH(QVariantHash, reference);
+
+ KisCosParser p;
+ QVariantHash newDoc = p.parseCosToJson(&data);
+ if (reference != newDoc) {
+ qDebug() << reference;
+ qDebug() << newDoc;
+ }
+ QVERIFY2(reference == newDoc, "Parsing failed");
+}
+
+SIMPLE_TEST_MAIN(psd_cos_parser_test)
diff --git a/libs/psdutils/tests/psd_cos_parser_test.h b/libs/psdutils/tests/psd_cos_parser_test.h
new file mode 100644
index 0000000000..680eccc091
--- /dev/null
+++ b/libs/psdutils/tests/psd_cos_parser_test.h
@@ -0,0 +1,23 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef PSD_COS_PARSER_TEST_H
+#define PSD_COS_PARSER_TEST_H
+
+#include <simpletest.h>
+
+class psd_cos_parser_test : public QObject
+{
+ Q_OBJECT
+private Q_SLOTS:
+
+ void test_roundtrip_cos_struct_data();
+ void test_roundtrip_cos_struct();
+
+ void test_parse_cos_struct_data();
+ void test_parse_cos_struct();
+};
+
+#endif // PSD_COS_PARSER_TEST_H