File: TestParsePath.cxx

package info (click to toggle)
mpd 0.24.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,736 kB
  • sloc: cpp: 75,014; python: 1,408; xml: 628; perl: 469; java: 289; sh: 286; ansic: 235; makefile: 105
file content (142 lines) | stat: -rw-r--r-- 3,588 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
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
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project

#include "config/Path.hxx"
#include "config/Data.hxx"
#include "fs/Path.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/glue/StandardDirectory.hxx"
#include "fs/XDG.hxx" // for USE_XDG

#include <gtest/gtest.h>

#include <stdexcept>

#ifndef _WIN32

AllocatedPath
GetHomeDir(const char *user_name) noexcept
{
	return AllocatedPath::FromFS(PATH_LITERAL("/home")) / AllocatedPath::FromUTF8(user_name);
}

AllocatedPath
GetHomeDir() noexcept
{
	return GetHomeDir("foo");
}

#endif

AllocatedPath
GetUserConfigDir() noexcept
{
#ifdef _WIN32
	return AllocatedPath::FromFS(PATH_LITERAL("c:\\users\\foo\\config"));
#else
	return GetHomeDir() / AllocatedPath::FromFS(PATH_LITERAL(".config"));
#endif
}

AllocatedPath
GetUserMusicDir() noexcept
{
#ifdef _WIN32
	return AllocatedPath::FromFS(PATH_LITERAL("c:\\users\\foo\\Music"));
#else
	return GetHomeDir() / AllocatedPath::FromFS(PATH_LITERAL("Music"));
#endif
}

AllocatedPath
GetUserCacheDir() noexcept
{
#ifdef _WIN32
	return nullptr;
#else
	return GetHomeDir() / AllocatedPath::FromFS(PATH_LITERAL(".cache"));
#endif
}

AllocatedPath
GetAppCacheDir() noexcept
{
#ifdef _WIN32
	return nullptr;
#else
	return GetUserCacheDir() / AllocatedPath::FromFS(PATH_LITERAL("mpd"));
#endif
}

AllocatedPath
GetUserRuntimeDir() noexcept
{
#ifdef _WIN32
	return nullptr;
#else
	return AllocatedPath::FromFS(PATH_LITERAL("/run/user/foo"));
#endif
}

AllocatedPath
GetAppRuntimeDir() noexcept
{
#ifdef _WIN32
	return nullptr;
#else
	return GetUserRuntimeDir() / AllocatedPath::FromFS(PATH_LITERAL("mpd"));
#endif
}

const char *
ConfigData::GetString([[maybe_unused]] ConfigOption option,
		      const char *default_value) const noexcept
{
	return default_value;
}

TEST(ParsePath, Basic)
{
	EXPECT_THROW(ParsePath(""), std::runtime_error);
	EXPECT_EQ(ParsePath("/"), AllocatedPath::FromFS(PATH_LITERAL("/")));
	EXPECT_EQ(ParsePath("/abc"), AllocatedPath::FromFS(PATH_LITERAL("/abc")));

#ifdef _WIN32
	EXPECT_EQ(ParsePath("c:/abc"), AllocatedPath::FromFS(PATH_LITERAL("c:/abc")));
	EXPECT_EQ(ParsePath("c:\\abc"), AllocatedPath::FromFS(PATH_LITERAL("c:\\abc")));
#endif
}

#ifndef _WIN32

TEST(ParsePath, Tilde)
{
	EXPECT_EQ(ParsePath("~"), AllocatedPath::FromFS(PATH_LITERAL("/home/foo")));
	EXPECT_EQ(ParsePath("~/"), AllocatedPath::FromFS(PATH_LITERAL("/home/foo")));
	EXPECT_EQ(ParsePath("~/abc"), AllocatedPath::FromFS(PATH_LITERAL("/home/foo/abc")));
	EXPECT_EQ(ParsePath("~bar"), AllocatedPath::FromFS(PATH_LITERAL("/home/bar")));
	EXPECT_EQ(ParsePath("~bar/"), AllocatedPath::FromFS(PATH_LITERAL("/home/bar")));
	EXPECT_EQ(ParsePath("~bar/abc"), AllocatedPath::FromFS(PATH_LITERAL("/home/bar/abc")));
}

TEST(ParsePath, Home)
{
	EXPECT_EQ(ParsePath("$HOME"), AllocatedPath::FromFS(PATH_LITERAL("/home/foo")));
	EXPECT_EQ(ParsePath("$HOME/"), AllocatedPath::FromFS(PATH_LITERAL("/home/foo")));
	EXPECT_EQ(ParsePath("$HOME/abc"), AllocatedPath::FromFS(PATH_LITERAL("/home/foo/abc")));
}

#endif

#ifdef USE_XDG

TEST(ParsePath, XDG)
{
	EXPECT_EQ(ParsePath("$XDG_CONFIG_HOME"), AllocatedPath::FromFS(PATH_LITERAL("/home/foo/.config")));
	EXPECT_EQ(ParsePath("$XDG_CONFIG_HOME/abc"), AllocatedPath::FromFS(PATH_LITERAL("/home/foo/.config/abc")));
	EXPECT_EQ(ParsePath("$XDG_MUSIC_DIR"), AllocatedPath::FromFS(PATH_LITERAL("/home/foo/Music")));
	EXPECT_EQ(ParsePath("$XDG_CACHE_HOME"), AllocatedPath::FromFS(PATH_LITERAL("/home/foo/.cache")));
	EXPECT_EQ(ParsePath("$XDG_RUNTIME_DIR/mpd"), AllocatedPath::FromFS(PATH_LITERAL("/run/user/foo/mpd")));
}

#endif