summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/tests/ApplicationMessage_test.cpp
blob: 66e7eb6df9936cf1e03f1bb83d50c2840f2c506f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team

#include <QJsonObject>
#include <QTest>

#include <ApplicationMessage.h>
#include <Json.h>

class ApplicationMessageTest : public QObject
{
	Q_OBJECT

  private slots:
	void test_roundTrip_data()
	{
		QTest::addColumn<QString>("command");
		QTest::addColumn<QStringList>("keys");
		QTest::addColumn<QStringList>("values");

		QTest::newRow("empty-args") << "open" << QStringList{} << QStringList{};
		QTest::newRow("single-arg") << "launch" << QStringList{ "instance" } << QStringList{ "main" };
		QTest::newRow("multi-args") << "import"
								  << QStringList{ "path", "mode", "force" }
								  << QStringList{ "/tmp/test", "zip", "true" };
		QTest::newRow("symbols") << "cmd"
							<< QStringList{ "k-1", "k_2" }
							<< QStringList{ "value with spaces", "x=y" };
	}

	void test_roundTrip()
	{
		QFETCH(QString, command);
		QFETCH(QStringList, keys);
		QFETCH(QStringList, values);

		QCOMPARE(keys.size(), values.size());

		ApplicationMessage in;
		in.command = command;
		for (int i = 0; i < keys.size(); ++i)
		{
			in.args.insert(keys.at(i), values.at(i));
		}

		ApplicationMessage out;
		out.parse(in.serialize());

		QCOMPARE(out.command, command);
		QCOMPARE(out.args.size(), keys.size());
		for (int i = 0; i < keys.size(); ++i)
		{
			QCOMPARE(out.args.value(keys.at(i)), values.at(i));
		}
	}

	void test_parseMissingArgs()
	{
		QJsonObject root;
		root.insert("command", "only-command");

		ApplicationMessage msg;
		msg.parse(Json::toText(root));

		QCOMPARE(msg.command, QString("only-command"));
		QVERIFY(msg.args.isEmpty());
	}

	void test_parseInvalidDocumentThrows()
	{
		ApplicationMessage msg;
		bool threw = false;
		try
		{
			msg.parse("{");
		}
		catch (const Json::JsonException&)
		{
			threw = true;
		}
		QVERIFY(threw);
	}

	void test_parseArrayThrows()
	{
		ApplicationMessage msg;
		bool threw = false;
		try
		{
			msg.parse("[1,2,3]");
		}
		catch (const Json::JsonException&)
		{
			threw = true;
		}
		QVERIFY(threw);
	}
};

QTEST_GUILESS_MAIN(ApplicationMessageTest)

#include "ApplicationMessage_test.moc"