aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/libutil/shlex.cc
blob: 2a13635f0e636eaa8fa6613281ac292d2a95efdc (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
#include "shlex.hh"

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <sstream>

using testing::Eq;

namespace nix {

TEST(Shlex, shell_split) {
    ASSERT_THAT(shell_split(""), Eq<std::vector<std::string>>({}));
    ASSERT_THAT(shell_split("  "), Eq<std::vector<std::string>>({}));

    ASSERT_THAT(
        shell_split("puppy doggy"),
        Eq<std::vector<std::string>>({
            "puppy",
            "doggy",
        })
    );

    ASSERT_THAT(
        shell_split("goldie \"puppy 'doggy'\" sweety"),
        Eq<std::vector<std::string>>({
            "goldie",
            "puppy 'doggy'",
            "sweety",
        })
    );

    ASSERT_THAT(
        shell_split("\"pupp\\\"y\""),
        Eq<std::vector<std::string>>({ "pupp\"y" })
    );

    ASSERT_THAT(
        shell_split("goldie 'puppy' doggy"),
        Eq<std::vector<std::string>>({
            "goldie",
            "puppy",
            "doggy",
        })
    );

    ASSERT_THAT(
        shell_split("'pupp\\\"y'"),
        Eq<std::vector<std::string>>({
            "pupp\\\"y",
        })
    );

    ASSERT_THROW(shell_split("\"puppy"), ShlexError);
    ASSERT_THROW(shell_split("'puppy"), ShlexError);
}

} // namespace nix