File: pathtools.cpp

package info (click to toggle)
soundscaperenderer 0.6.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,148 kB
  • sloc: cpp: 36,430; sh: 4,522; makefile: 847; ansic: 762; javascript: 593; python: 57
file content (100 lines) | stat: -rw-r--r-- 2,492 bytes parent folder | download | duplicates (3)
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
#include "catch/catch.hpp"

#include "pathtools.h"

using Catch::Matchers::StartsWith;
using Catch::Matchers::EndsWith;

TEST_CASE("make_path_relative_to_file") {

    constexpr auto f = pathtools::make_path_relative_to_file;

    SECTION("basic usage") {
        CHECK(f("a/b", "my/conf.txt") == "../a/b");
    }

    SECTION("empty path is unchanged") {
        CHECK(f("", "my/conf.txt") == "");
        CHECK(f("", "") == "");
    }

    SECTION("absolute path is unchanged") {
        CHECK(f("/a/b", "my/conf.txt") == "/a/b");
        CHECK(f("/a/b", "") == "/a/b");
    }

    SECTION("absolute file") {
        CHECK_THAT(f("a", "/my/conf.txt"), StartsWith("..") && EndsWith("a"));
    }

    SECTION("trailing slash is preserved") {
        CHECK(f("a/b/", "my/conf.txt") == "../a/b/");
    }

    SECTION("directory must have trailing slash") {
        CHECK(f("a/b", "c/d") == "../a/b");
        CHECK(f("a/b", "c/d/") == "../../a/b");
    }

    SECTION("path is simplified") {
        CHECK(f("a//./../b", "my/conf.txt") == "../b");
    }
}

TEST_CASE("make_path_relative_to_current_dir") {

    constexpr auto f = pathtools::make_path_relative_to_current_dir;

    SECTION("basic usage") {
        CHECK(f("a/b", "my/conf.txt") == "my/a/b");
    }

    SECTION("empty path is unchanged") {
        CHECK(f("", "my/conf.txt") == "");
        CHECK(f("", "") == "");
    }

    SECTION("absolute path is unchanged") {
        CHECK(f("/a/b", "my/conf.txt") == "/a/b");
        CHECK(f("/a/b", "") == "/a/b");
    }

    SECTION("absolute file") {
        CHECK_THAT(f("a", "/my/conf.txt"), StartsWith("..") && EndsWith("a"));
    }

    SECTION("trailing slash is preserved") {
        CHECK(f("a/b/", "my/conf.txt") == "my/a/b/");
    }

    SECTION("directory must have trailing slash") {
        CHECK(f("a/b", "c/d") == "c/a/b");
        CHECK(f("a/b", "c/d/") == "c/d/a/b");
    }

    SECTION("path is simplified") {
        CHECK(f("a//./../b", "my/conf.txt") == "my/b");
    }
}

TEST_CASE("get_escaped_filename") {

    constexpr auto f = pathtools::get_escaped_filename;

    SECTION("basic usage") {
        CHECK(f("a b") == "a\\ b");
    }

    SECTION("at beginning and end") {
        CHECK(f(" a ") == "\\ a\\ ");
    }

    SECTION("tab and newline are also escaped") {
        CHECK(f("a\tb") == "a\\\tb");
        CHECK(f("a\nb") == "a\\\nb");
    }

    SECTION("punctuation characters are not escaped") {
        CHECK(f(".!:;,?$\\") == ".!:;,?$\\");
    }
}