File: TestCMDLineTerm.cc

package info (click to toggle)
j4-dmenu-desktop 3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,180 kB
  • sloc: cpp: 6,282; python: 473; sh: 81; makefile: 8
file content (140 lines) | stat: -rw-r--r-- 6,304 bytes parent folder | download
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// This file is part of j4-dmenu-desktop.
//
// j4-dmenu-desktop is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// j4-dmenu-desktop is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with j4-dmenu-desktop.  If not, see <http://www.gnu.org/licenses/>.
//

// custom_term_assembler() is the only function that can be reasonably tested
// here. Testing other terminal integrations here would require actually
// executing the relevant terminal emulators with the output of
// *_term_assembler(). This is done in Bats tests.

#include <catch2/catch_test_macros.hpp>

#include <string>
#include <vector>

#include "CMDLineTerm.hh"

using namespace CMDLineTerm::assembler_functions;
using vec = std::vector<std::string>;

TEST_CASE("Test custom term assembler", "[CMDLineTerm]") {
    REQUIRE(custom_term_assembler({"ignored"}, "command", "Ignored") ==
            vec{"command"});
    REQUIRE(custom_term_assembler({"ignored"}, "command arg", "Ignored") ==
            vec{"command", "arg"});
    // arg and arg2 should **not** be split into separate arguments (as
    // j4-dmenu-desktop(1) manpage mandates). Only the space character is a
    // valid argument separator.
    REQUIRE(custom_term_assembler({"ignored"}, "command arg\targ2",
                                  "Ignored") == vec{"command", "arg\targ2"});
}

TEST_CASE("Test leading and trailing whitespace in custom term assembler",
          "[CMDLineTerm]") {
    // Leading and trailing whitespace in --term shall be ignored.
    REQUIRE(custom_term_assembler({"ignored"}, "command ", "Ignored") ==
            vec{"command"});
    REQUIRE(custom_term_assembler({"ignored"}, "command         ", "Ignored") ==
            vec{"command"});
    REQUIRE(custom_term_assembler({"ignored"}, " command", "Ignored") ==
            vec{"command"});
    REQUIRE(custom_term_assembler({"ignored"}, "         command", "Ignored") ==
            vec{"command"});

    REQUIRE(custom_term_assembler({"ignored"}, "command arg ", "Ignored") ==
            vec{"command", "arg"});
    REQUIRE(custom_term_assembler({"ignored"}, "command arg  ", "Ignored") ==
            vec{"command", "arg"});
    REQUIRE(custom_term_assembler({"ignored"}, " command arg", "Ignored") ==
            vec{"command", "arg"});
    REQUIRE(custom_term_assembler({"ignored"}, "  command arg", "Ignored") ==
            vec{"command", "arg"});
    REQUIRE(custom_term_assembler({"ignored"}, "  command arg  ", "Ignored") ==
            vec{"command", "arg"});
}

TEST_CASE("Test multiple consecutive spaces in custom term assembler",
          "[CMDLineTerm]") {
    REQUIRE(custom_term_assembler({"ignored"}, "command arg arg2", "Ignored") ==
            vec{"command", "arg", "arg2"});
    REQUIRE(custom_term_assembler({"ignored"}, " command  arg    arg2",
                                  "Ignored") == vec{"command", "arg", "arg2"});
    REQUIRE(custom_term_assembler({"ignored"}, " command  arg    arg2   ",
                                  "Ignored") == vec{"command", "arg", "arg2"});
}

TEST_CASE("Test escaping in custom term assembler", "[CMDLineTerm]") {
    REQUIRE(custom_term_assembler({"ignored"}, R"(\\)", "Ignored") ==
            vec{R"(\)"});
    REQUIRE(custom_term_assembler({"ignored"}, R"(\\\\)", "Ignored") ==
            vec{R"(\\)"});
    REQUIRE(custom_term_assembler({"ignored"}, R"( arg --te\\s\\t\\)",
                                  "Ignored") == vec{"arg", R"(--te\s\t\)"});

    REQUIRE(custom_term_assembler({"ignored"}, R"(\{name})", "Ignored") ==
            vec{"{name}"});
    REQUIRE(custom_term_assembler({"ignored"}, R"(\{{name})", "MyName") ==
            vec{"{MyName"});

    REQUIRE(custom_term_assembler({"ignored"}, R"(a b\ c d\ {name}- d\ \ e)",
                                  "MyName") ==
            vec{"a", "b c", "d MyName-", "d  e"});
}

TEST_CASE("Test desktop app name in custom term assembler", "[CMDLineTerm]") {
    using namespace std::string_literals;

    // Random name with special characters. Special characters shouldn't be
    // parsed and should be passed verbatim.
    const char *name = "Myna m\t e\n:";
    REQUIRE(custom_term_assembler({"ignored"}, "{name}", name) == vec{name});
    REQUIRE(custom_term_assembler({"ignored"}, "  {name}  ", name) ==
            vec{name});
    REQUIRE(custom_term_assembler({"ignored"}, "command --title {name}",
                                  name) == vec{"command", "--title", name});
    REQUIRE(custom_term_assembler({"ignored"}, "command --title={name}",
                                  name) == vec{"command", "--title="s + name});
    REQUIRE(custom_term_assembler({"ignored"}, " -->{name}<-- ", name) ==
            vec{"-->"s + name + "<--"});
}

TEST_CASE("Test cmdline in custom term handler", "[CMDLineTerm]") {
    REQUIRE(custom_term_assembler({"program"}, "printf {cmdline@}",
                                  "Ignored") == vec{"printf", "program"});

    vec input{"!@#$%^&*{}", "''''''''''", "'", "!?$ > /dev/null"};

    vec transformed{"command", "-e"};
    transformed.insert(transformed.cend(), input.cbegin(), input.cend());
    transformed.push_back("-x");

    REQUIRE(
        custom_term_assembler(input, "command -e {cmdline@} -x", "Ignored") ==
        vec{"command", "-e", "!@#$%^&*{}", "''''''''''", "'", "!?$ > /dev/null",
            "-x"});
}

TEST_CASE("Test placeholders after {cmdline@} (#179)", "[CMDLineTerm]") {
    const char *term = "alacritty -e {cmdline@} {name}";
    REQUIRE_NOTHROW(validate_custom_term(term));
    REQUIRE_NOTHROW(custom_term_assembler({"a", "b", "c"}, term, "Name"));

    const char *term2 =
        R"(/bin/sh -c alacritty\ msg\ create-window\ -T\ {name}\ {cmdline*}\ ||\ alacritty\ -T\ {name}\ {cmdline*})";

    REQUIRE_NOTHROW(validate_custom_term(term2));
    REQUIRE_NOTHROW(custom_term_assembler({"a", "b", "c"}, term2, "Name"));
}