xaizek / dit (License: GPLv3) (since 2018-12-07)
Command-line task keeper that remembers all old values and is meant to combine several orthogonal features to be rather flexible in managing items.
<root> / tests / Invocation.cpp (c7e4a1383cf22ce209f418f899a7d7aacef1a41e) (13KiB) (mode 100644) [raw]
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
// Copyright (C) 2015 xaizek <xaizek@posteo.net>
//
// This file is part of dit.
//
// dit is free software: you can redistribute it and/or modify
// it under the terms of version 3 of the GNU Affero General Public
// License as published by the Free Software Foundation.
//
// dit 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 dit.  If not, see <http://www.gnu.org/licenses/>.

#include "Catch/catch.hpp"

#include <boost/program_options/errors.hpp>

#include <string>
#include <vector>

#include "Invocation.hpp"

TEST_CASE("Help request is detected.", "[invocation]")
{
    auto aliasResolver = [](const std::string &) { return std::string(); };

    Invocation invocation;
    invocation.setCmdLine({ "--help" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    REQUIRE(invocation.shouldPrintHelp());
    REQUIRE(!invocation.getHelp().empty());
}

TEST_CASE("Version request is detected.", "[invocation]")
{
    auto aliasResolver = [](const std::string &) { return std::string(); };

    Invocation invocation;
    invocation.setCmdLine({ "--version" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    REQUIRE(invocation.shouldPrintVersion());
}

TEST_CASE("Project name can be empty.", "[invocation][project]")
{
    auto aliasResolver = [](const std::string &) { return std::string(); };

    Invocation invocation;
    invocation.setDefPrjName("defprj");
    invocation.setCmdLine({ "." });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    REQUIRE(invocation.getPrjName() == "defprj");
}

TEST_CASE("Project name is extracted correctly.", "[invocation][project]")
{
    auto aliasResolver = [](const std::string &) { return std::string(); };

    Invocation invocation;
    invocation.setCmdLine({ ".proj" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    REQUIRE(invocation.getPrjName() == "proj");
}

TEST_CASE("Default project name is used.", "[invocation][project][defaults]")
{
    auto aliasResolver = [](const std::string &) { return std::string(); };

    Invocation invocation;
    invocation.setDefPrjName("def");
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    REQUIRE(invocation.getPrjName() == "def");
}

TEST_CASE("Default command-line is used.", "[invocation][defaults]")
{
    auto aliasResolver = [](const std::string &) { return std::string(); };

    Invocation invocation;
    invocation.setDefCmdLine("defcmd 'with' \"args\" goes\\ here");
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();

    REQUIRE(cmd == "defcmd");
    REQUIRE(args.size() == 3);
    REQUIRE(args[0] == "with");
    REQUIRE(args[1] == "args");
    REQUIRE(args[2] == "goes here");
}

TEST_CASE("No argument alias is expanded.", "[invocation][aliases]")
{
    auto aliasResolver = [](const std::string &name) -> std::string {
        if (name == "alias") {
            return "cmd pre-arg";
        }
        return {};
    };

    Invocation invocation;
    invocation.setCmdLine({ "alias", "arg" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();

    REQUIRE(cmd == "cmd");
    REQUIRE(args.size() == 2);
    REQUIRE(args[0] == "pre-arg");
    REQUIRE(args[1] == "arg");
}

TEST_CASE("Single argument alias is expanded.", "[invocation][aliases]")
{
    auto aliasResolver = [](const std::string &name) -> std::string {
        if (name == "alias") {
            return "cmd ${1}";
        }
        return {};
    };

    Invocation invocation;
    invocation.setCmdLine({ "alias", "arg" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();

    REQUIRE(cmd == "cmd");
    REQUIRE(args.size() == 1);
    REQUIRE(args[0] == "arg");
}

TEST_CASE("Multiple argument alias is expanded.", "[invocation][aliases]")
{
    auto aliasResolver = [](const std::string &name) -> std::string {
        if (name == "alias") {
            return "cmd ${1} ${2}";
        }
        return {};
    };

    Invocation invocation;
    invocation.setCmdLine({ "alias", "arg1", "arg2" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();

    REQUIRE(cmd == "cmd");
    REQUIRE(args.size() == 2);
    REQUIRE(args[0] == "arg1");
    REQUIRE(args[1] == "arg2");
}

TEST_CASE("Unused arguments are appended.", "[invocation][aliases]")
{
    auto aliasResolver = [](const std::string &name) -> std::string {
        if (name == "alias") {
            return "cmd ${2}";
        }
        return {};
    };

    Invocation invocation;
    invocation.setCmdLine({ "alias", "arg1", "arg2", "arg3" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();

    REQUIRE(cmd == "cmd");
    REQUIRE(args.size() == 3);
    REQUIRE(args[0] == "arg2");
    REQUIRE(args[1] == "arg1");
    REQUIRE(args[2] == "arg3");
}

TEST_CASE("Arguments can repeat.", "[invocation][aliases]")
{
    auto aliasResolver = [](const std::string &name) -> std::string {
        if (name == "alias") {
            return "cmd ${1} ${1}";
        }
        return {};
    };

    Invocation invocation;
    invocation.setCmdLine({ "alias", "arg" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();

    REQUIRE(cmd == "cmd");
    REQUIRE(args.size() == 2);
    REQUIRE(args[0] == "arg");
    REQUIRE(args[1] == "arg");
}

TEST_CASE("Invalid argument syntax is unchanged.", "[invocation][aliases]")
{
    auto aliasResolver = [](const std::string &name) -> std::string {
        if (name == "alias") {
            return "cmd ${} ${-1} ${0} a${3}";
        }
        return {};
    };

    Invocation invocation;
    invocation.setCmdLine({ "alias" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();

    REQUIRE(cmd == "cmd");
    REQUIRE(args.size() == 4);
    REQUIRE(args[0] == "${}");
    REQUIRE(args[1] == "${-1}");
    REQUIRE(args[2] == "${0}");
    REQUIRE(args[3] == "a${3}");
}

TEST_CASE("Arguments can non-sequential.", "[invocation][aliases]")
{
    auto aliasResolver = [](const std::string &name) -> std::string {
        if (name == "alias") {
            return "cmd ${2} ${1}";
        }
        return {};
    };

    Invocation invocation;
    invocation.setCmdLine({ "alias", "arg1", "arg2" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();

    REQUIRE(cmd == "cmd");
    REQUIRE(args.size() == 2);
    REQUIRE(args[0] == "arg2");
    REQUIRE(args[1] == "arg1");
}

TEST_CASE("Absent arguments are expanded to empty strings.",
          "[invocation][aliases]")
{
    auto aliasResolver = [](const std::string &name) -> std::string {
        if (name == "alias") {
            return "cmd ${10} ${11}";
        }
        return {};
    };

    Invocation invocation;
    invocation.setCmdLine({ "alias" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse();

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();

    REQUIRE(cmd == "cmd");
    REQUIRE(args.size() == 2);
    REQUIRE(args[0] == "");
    REQUIRE(args[1] == "");
}

TEST_CASE("Completion stops after last argument is inserted.",
          "[invocation][aliases][completion]")
{
    auto aliasResolver = [](const std::string &name) {
        return (name == "alias") ? "cmd ${2} ${3} ${1}" : std::string();
    };

    Invocation invocation;
    invocation.setCmdLine({ "alias", "arg1", "arg2", "arg3" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse(true);

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();

    REQUIRE(cmd == "cmd");
    REQUIRE(args.size() == 2);
    REQUIRE(args[0] == "arg2");
    REQUIRE(args[1] == "arg3");
}

TEST_CASE("Wrong options.", "[invocation][completion]")
{
    auto aliasResolver = [](const std::string &) { return std::string(); };

    Invocation invocation;
    invocation.setCmdLine({ "--bla" });
    invocation.setAliasResolver(aliasResolver);

    SECTION("Ignored on completion")
    {
        invocation.parse(true);

        std::vector<std::string> args = invocation.getCmdArgs();
        REQUIRE(invocation.getCmdName() == std::string());
        REQUIRE(args.empty());
    }

    SECTION("Error on running")
    {
        REQUIRE_THROWS_AS(invocation.parse(),
                          const boost::program_options::error &);
    }
}

TEST_CASE("Invocation throws if alias resolved isn't set.", "[invocation]")
{
    Invocation invocation;
    invocation.setCmdLine({ "alias" });

    REQUIRE_THROWS_AS(invocation.parse(), const std::bad_function_call &);
}

TEST_CASE("Aliases are decomposed.", "[invocation][alias][composition]")
{
    auto aliasResolver = [](const std::string &name) -> std::string {
        if (name == "alias1") {
            return "prefix1 ${1}";
        }
        if (name == "alias2") {
            return "prefix2";
        }
        return {};
    };

    Invocation invocation;
    invocation.setCmdLine({ "alias2.alias1", "arg1", "arg2", "arg3" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse(true);

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();

    REQUIRE(cmd == "prefix2");
    REQUIRE(args.size() == 4);
    REQUIRE(args[0] == "prefix1");
    REQUIRE(args[1] == "arg1");
    REQUIRE(args[2] == "arg2");
    REQUIRE(args[3] == "arg3");
}

TEST_CASE("Alias composition configuration is collected.",
          "[invocation][alias][composition]")
{
    auto aliasResolver = [](const std::string &name) -> std::string {
        if (name == "alias1") {
            return "a=1 ls ${1}";
        }
        if (name == "alias2") {
            return "a+=2";
        }
        return {};
    };

    Invocation invocation;
    invocation.setCmdLine({ "alias2.alias1.cmd", "arg1", "arg2", "arg3" });
    invocation.setAliasResolver(aliasResolver);
    invocation.parse(true);

    std::string cmd = invocation.getCmdName();
    std::vector<std::string> args = invocation.getCmdArgs();
    using conf = std::pair<std::string, std::string>;
    std::vector<conf> confs = invocation.getConfs();

    REQUIRE(cmd == "ls");
    REQUIRE(args.size() == 4);
    REQUIRE(args[0] == "cmd");
    REQUIRE(args[1] == "arg1");
    REQUIRE(args[2] == "arg2");
    REQUIRE(args[3] == "arg3");
    REQUIRE(confs.size() == 2);
    REQUIRE(confs[0] == conf("a", "1"));
    REQUIRE(confs[1] == conf("a+", "2"));
}

TEST_CASE("At most one unnamed command is allowed.",
          "[invocation][composition]")
{
    auto aliasResolver = [](const std::string &) { return std::string(); };

    Invocation invocation;
    invocation.setAliasResolver(aliasResolver);
    invocation.setDefCmdLine("defcmd");

    SECTION("Single dot is treated specially")
    {
        invocation.setCmdLine({ ".proj", "." });
        invocation.parse();
        REQUIRE(invocation.getCmdName() == "defcmd");
    }

    SECTION("Two leading unnamed commands")
    {
        invocation.setCmdLine({ ".proj", "..cmd" });
        invocation.parse();
        REQUIRE(invocation.getCmdName() == "..cmd");
    }

    SECTION("Two trailing unnamed commands")
    {
        invocation.setCmdLine({ "cmd.." });
        invocation.parse();
        REQUIRE(invocation.getCmdName() == "cmd..");
    }
}

TEST_CASE("Unnamed command is expanded as default one.",
          "[invocation][composition]")
{
    auto aliasResolver = [](const std::string &name) {
        return (name == "alias") ? "alias-expanded" : std::string();
    };

    Invocation invocation;
    invocation.setAliasResolver(aliasResolver);
    invocation.setDefCmdLine("defcmd");

    SECTION("Just unnamed command")
    {
        invocation.setCmdLine({ "" });
        invocation.parse();
        REQUIRE(invocation.getCmdName() == "defcmd");
    }

    SECTION("Leading unnamed command")
    {
        invocation.setCmdLine({ ".alias" });
        invocation.parse();
        REQUIRE(invocation.getCmdName() == "defcmd");
    }

    SECTION("Trailing unnamed command")
    {
        invocation.setCmdLine({ "alias." });
        invocation.parse();
        REQUIRE(invocation.getCmdName() == "alias-expanded");
    }

    SECTION("Unnamed command and no default command-line")
    {
        invocation.setDefCmdLine("");
        invocation.setCmdLine({ "alias." });
        invocation.parse();
        REQUIRE(invocation.getCmdName() == "alias-expanded");
    }
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/dit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/dit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master