| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | |
| 6 | #include "core/commandqueue.h" |
| 7 | #include <QString> |
| 8 | |
| 9 | namespace Tests::CommandQueue { |
| 10 | |
| 11 | class CommandQueueTestAdapter : public ::CommandQueue |
| 12 | { |
| 13 | public: |
| 14 | using ::CommandQueue::CommandQueue; |
| 15 | |
| 16 | using ::CommandQueue::add; |
| 17 | using ::CommandQueue::commands; |
| 18 | using ::CommandQueue::hasCommandQueuedFor; |
| 19 | using ::CommandQueue::toRawData; |
| 20 | }; |
| 21 | |
| 22 | TEST_CASE("CommandQueue tests", "[CommandQueue]") |
| 23 | { |
| 24 | CommandQueueTestAdapter ts({"multi-cmd-file"}); |
| 25 | |
| 26 | SECTION("Initially, is empty") |
| 27 | { |
| 28 | REQUIRE(ts.commands().size() == 0); |
| 29 | |
| 30 | SECTION("Commands can be added") |
| 31 | { |
| 32 | ts.add({"path", "value"}); |
| 33 | |
| 34 | REQUIRE(ts.commands().size() == 1); |
| 35 | |
| 36 | std::pair<std::string, std::string> cmd{"path", "value"}; |
| 37 | REQUIRE(ts.commands().front() == cmd); |
| 38 | |
| 39 | SECTION("Queuing an already queued command has no effect") |
| 40 | { |
| 41 | ts.add({"path", "value"}); |
| 42 | |
| 43 | REQUIRE(ts.commands().size() == 1); |
| 44 | } |
| 45 | |
| 46 | SECTION("Queuing a command for a multi-command file inserts the command " |
| 47 | "just after the last queued command for that file") |
| 48 | { |
| 49 | ts.add({"multi-cmd-file", "1"}); |
| 50 | ts.add({"other", "value"}); |
| 51 | ts.add({"multi-cmd-file", "2"}); |
| 52 | |
| 53 | auto &commands = ts.commands(); |
| 54 | REQUIRE(commands.size() == 4); |
| 55 | |
| 56 | REQUIRE(commands[0].first == "path"); |
| 57 | REQUIRE(commands[0].second == "value"); |
| 58 | REQUIRE(commands[1].first == "multi-cmd-file"); |
| 59 | REQUIRE(commands[1].second == "1"); |
| 60 | REQUIRE(commands[2].first == "multi-cmd-file"); |
| 61 | REQUIRE(commands[2].second == "2"); |
| 62 | REQUIRE(commands[3].first == "other"); |
| 63 | REQUIRE(commands[3].second == "value"); |
| 64 | } |
| 65 | |
| 66 | SECTION( |
| 67 | "Queuing a command for a non multi-command file removes the last " |
| 68 | "queued command for that file and inserts the new command at the end") |
| 69 | { |
| 70 | ts.add({"other", "value"}); |
| 71 | |
| 72 | auto &commands = ts.commands(); |
| 73 | REQUIRE(commands.size() == 2); |
| 74 | |
| 75 | REQUIRE(commands[0].first == "path"); |
| 76 | REQUIRE(commands[0].second == "value"); |
| 77 | REQUIRE(commands[1].first == "other"); |
| 78 | REQUIRE(commands[1].second == "value"); |
| 79 | |
| 80 | ts.add({"path", "value1"}); |
| 81 | |
| 82 | REQUIRE(commands[0].first == "other"); |
| 83 | REQUIRE(commands[0].second == "value"); |
| 84 | REQUIRE(commands[1].first == "path"); |
| 85 | REQUIRE(commands[1].second == "value1"); |
| 86 | } |
| 87 | |
| 88 | SECTION("Commands are transformed into raw data...") |
| 89 | { |
| 90 | auto data = ts.toRawData(); |
| 91 | |
| 92 | REQUIRE(data == QByteArray("path\0value\0", 11)); |
| 93 | |
| 94 | SECTION("The queue is cleared") |
| 95 | { |
| 96 | REQUIRE(ts.commands().size() == 0); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | } // namespace Tests::CommandQueue |