summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/tests/FilePathTest.cpp
blob: c985e559452774f2beb6dfffd4b31355c5d720d1 (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
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
/*************************************************************************
** FilePathTest.cpp                                                     **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2021 Martin Gieseking <martin.gieseking@uos.de>   **
**                                                                      **
** This program 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.                  **
**                                                                      **
** This program 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 this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/

#include <gtest/gtest.h>
#include <string>
#include "FilePath.hpp"
#include "FileSystem.hpp"

#ifndef SRCDIR
#define SRCDIR "."
#endif

using namespace std;


TEST(FilePathTest, empty) {
	FilePath path;
	ASSERT_TRUE(path.empty());
	path.set("/a/b/c/d", false, "/");
	ASSERT_FALSE(path.empty());
	ASSERT_EQ(path.absolute(), "/a/b/c/d");
}


TEST(FilePathTest, dir1) {
	FilePath fp("a/b/c/d", false, "/");
	ASSERT_EQ(fp.absolute(), "/a/b/c/d");
	ASSERT_EQ(fp.relative("/"), "a/b/c/d");
	ASSERT_EQ(fp.relative("/a/b"), "c/d");
	ASSERT_EQ(fp.relative("/a/b/c"), "d");
	ASSERT_EQ(fp.relative("/a/b/c/d"), ".");
	ASSERT_EQ(fp.relative("/a/b/x"), "../c/d");
	ASSERT_EQ(fp.relative("/a/b/x/y"), "../../c/d");
}


TEST(FilePathTest, dir2) {
	FilePath fp("a/b/c/d", false, "/x/y");
	ASSERT_EQ(fp.absolute(), "/x/y/a/b/c/d");
	ASSERT_EQ(fp.relative("/"), "x/y/a/b/c/d");
	ASSERT_EQ(fp.relative("/x/y/a/b"), "c/d");
	ASSERT_EQ(fp.relative("/x/y/a/b/c"), "d");
	ASSERT_EQ(fp.relative("/x/y/a/b/c/d"), ".");
	ASSERT_EQ(fp.relative("/x/y/a/b/x"), "../c/d");
	ASSERT_EQ(fp.relative("/x/y/a/b/x/y"), "../../c/d");
}


TEST(FilePathTest, file1) {
	FilePath fp("a/b/c/d/f.ext", true, "/");
	ASSERT_EQ(fp.absolute(), "/a/b/c/d/f.ext");
	ASSERT_EQ(fp.relative("/"), "a/b/c/d/f.ext");
	ASSERT_EQ(fp.relative("/a/b"), "c/d/f.ext");
	ASSERT_EQ(fp.relative("/a/b/c"), "d/f.ext");
	ASSERT_EQ(fp.relative("/a/b/c/d"), "f.ext");
	ASSERT_EQ(fp.relative("/a/b/x"), "../c/d/f.ext");
	ASSERT_EQ(fp.relative("/a/b/x/y"), "../../c/d/f.ext");
	ASSERT_EQ(fp.basename(), "f");
	ASSERT_EQ(fp.suffix(), "ext");
	fp.suffix("new");
	ASSERT_EQ(fp.suffix(), "new");
	ASSERT_EQ(fp.relative("/a/b/x/y"), "../../c/d/f.new");
	fp.suffix("");
	ASSERT_EQ(fp.suffix(), "");
	ASSERT_EQ(fp.relative("/a/b/x/y"), "../../c/d/f");
}


TEST(FilePathTest, file2) {
	FilePath fp("/f.ext", true, "/");
	ASSERT_EQ(fp.absolute(), "/f.ext");
	ASSERT_EQ(fp.relative("/a/b"), "../../f.ext");
}


TEST(FilePathTest, file3) {
	FilePath fp("/f.ext", true, "/x/y");
	ASSERT_EQ(fp.absolute(), "/f.ext");
	ASSERT_EQ(fp.relative("/a/b"), "../../f.ext");
}


TEST(FilePathTest, autodetect) {
	FileSystem::chdir(SRCDIR);
	FilePath fp1("FilePathTest.cpp");
	ASSERT_TRUE(fp1.isFile());
	ASSERT_FALSE(fp1.empty());
	string cwd = FileSystem::getcwd();
#ifdef _WIN32
	if (cwd.length() >=2 && isalpha(cwd[0]) && cwd[1] == ':')
		cwd[0] = tolower(cwd[0]);
#endif
	ASSERT_EQ(fp1.absolute(), cwd + "/FilePathTest.cpp") << "fp1=" << fp1.absolute();

	FilePath fp2("");
	ASSERT_FALSE(fp2.isFile());
	ASSERT_FALSE(fp2.empty());
	ASSERT_EQ(fp2.absolute(), cwd);
}