File indexing completed on 2024-12-01 04:35:22

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "commandstest.h"
0008 #include "commands/commands.h"
0009 #include "ruqola_autotest_helper.h"
0010 
0011 QTEST_GUILESS_MAIN(CommandsTest)
0012 
0013 CommandsTest::CommandsTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 void CommandsTest::shouldHaveDefaultValue()
0019 {
0020     Commands r;
0021     QVERIFY(r.commands().isEmpty());
0022     QVERIFY(r.isEmpty());
0023 }
0024 
0025 void CommandsTest::shouldLoadCommands_data()
0026 {
0027     QTest::addColumn<QString>("name");
0028     QTest::addColumn<int>("commandsCount");
0029 
0030     QTest::addRow("command1") << QStringLiteral("command1") << 0;
0031     QTest::addRow("command2") << QStringLiteral("command2") << 25;
0032     QTest::addRow("command3") << QStringLiteral("command3") << 3;
0033 }
0034 
0035 void CommandsTest::shouldLoadCommands()
0036 {
0037     QFETCH(QString, name);
0038     QFETCH(int, commandsCount);
0039     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/commands/") + name + QLatin1String(".json");
0040     const QJsonObject obj = AutoTestHelper::loadJsonObject(originalJsonFile);
0041 
0042     Commands r;
0043     r.parseCommands(obj);
0044     QCOMPARE(r.commandsCount(), commandsCount);
0045 }
0046 
0047 void CommandsTest::shouldLoadPermissions()
0048 {
0049     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/commands/command3.json");
0050     const QJsonObject obj = AutoTestHelper::loadJsonObject(originalJsonFile);
0051 
0052     Commands r;
0053     r.parseCommands(obj);
0054     QCOMPARE(r.commandsCount(), 3);
0055 
0056     QVector<Command> result;
0057     {
0058         Command d;
0059         d.setCommandName(QStringLiteral("/slackbridge-import"));
0060         result.append(std::move(d));
0061     }
0062     {
0063         Command d;
0064         d.setCommandName(QStringLiteral("/archive"));
0065         d.setDescription(QStringLiteral("Archive"));
0066         d.setPermissions({QStringLiteral("archive-room")});
0067         d.setParams(QStringLiteral("#channel"));
0068         result.append(std::move(d));
0069     }
0070     {
0071         Command d;
0072         d.setCommandName(QStringLiteral("/leave"));
0073         d.setDescription(QStringLiteral("Leave_the_current_channel"));
0074         d.setPermissions({{QStringLiteral("leave-c")}, {QStringLiteral("leave-p")}});
0075         result.append(std::move(d));
0076     }
0077 
0078     const bool equalResult = r.commands() == result;
0079     if (!equalResult) {
0080         qDebug() << " Expected " << result;
0081         qDebug() << " result   " << r.commands();
0082     }
0083     QVERIFY(equalResult);
0084 }
0085 
0086 #include "moc_commandstest.cpp"