Mercurial > code
view C++/tests/Xdg/main.cpp @ 334:0b576ee64d45
* Create brand new hierarchy
* Rename DynLib to Dynlib
* Remove some warnings
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 08 Mar 2015 14:26:33 +0100 |
parents | C++/Tests/Xdg/main.cpp@f6d9fdb5eeeb |
children | d5ec1174b707 |
line wrap: on
line source
/* * main.cpp -- main test file for XDG * * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <gtest/gtest.h> #include <Xdg.h> using namespace testing; namespace { std::string myhome; } TEST(HomeEmpty, config) { ASSERT_TRUE(unsetenv("XDG_CONFIG_HOME") == 0); try { Xdg xdg; ASSERT_EQ(myhome + "/.config", xdg.configHome()); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(HomeEmpty, data) { ASSERT_TRUE(unsetenv("XDG_DATA_HOME") == 0); try { Xdg xdg; ASSERT_EQ(myhome + "/.local/share", xdg.dataHome()); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(HomeEmpty, cache) { ASSERT_TRUE(unsetenv("XDG_CACHE_HOME") == 0); try { Xdg xdg; ASSERT_EQ(myhome + "/.cache", xdg.cacheHome()); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(HomeEmpty, runtime) { ASSERT_TRUE(unsetenv("XDG_RUNTIME_DIR") == 0); try { Xdg xdg; try { xdg.runtimeDir(); ASSERT_TRUE(false); } catch (const std::exception &) { } } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(HomeValid, config) { ASSERT_TRUE(setenv("XDG_CONFIG_HOME", "/test/config", true) == 0); try { Xdg xdg; ASSERT_EQ("/test/config", xdg.configHome()); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(HomeValid, data) { ASSERT_TRUE(setenv("XDG_DATA_HOME", "/test/data", true) == 0); try { Xdg xdg; ASSERT_EQ("/test/data", xdg.dataHome()); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(HomeValid, cache) { ASSERT_TRUE(setenv("XDG_CACHE_HOME", "/test/cache", true) == 0); try { Xdg xdg; ASSERT_EQ("/test/cache", xdg.cacheHome()); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(HomeValid, runtime) { ASSERT_TRUE(setenv("XDG_RUNTIME_DIR", "/test/runtime", true) == 0); try { Xdg xdg; ASSERT_EQ("/test/runtime", xdg.runtimeDir()); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(HomeInvalid, config) { ASSERT_TRUE(setenv("XDG_CONFIG_HOME", "invalid", true) == 0); try { Xdg xdg; ASSERT_EQ(myhome + "/.config", xdg.configHome()); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(HomeInvalid, data) { ASSERT_TRUE(setenv("XDG_DATA_HOME", "invalid", true) == 0); try { Xdg xdg; ASSERT_EQ(myhome + "/.local/share", xdg.dataHome()); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(HomeInvalid, cache) { ASSERT_TRUE(setenv("XDG_CACHE_HOME", "invalid", true) == 0); try { Xdg xdg; ASSERT_EQ(myhome + "/.cache", xdg.cacheHome()); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(HomeInvalid, runtime) { ASSERT_TRUE(setenv("XDG_RUNTIME_DIR", "invalid", true) == 0); try { Xdg xdg; try { xdg.runtimeDir(); ASSERT_TRUE(false); } catch (const std::exception &) { } } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(DirectoriesEmpty, config) { ASSERT_TRUE(unsetenv("XDG_CONFIG_DIRS") == 0); try { Xdg xdg; const auto &list = xdg.configDirs(); ASSERT_EQ((size_t)1, list.size()); ASSERT_EQ("/etc/xdg", list[0]); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(DirectoriesEmpty, data) { ASSERT_TRUE(unsetenv("XDG_DATA_DIRS") == 0); try { Xdg xdg; const auto &list = xdg.dataDirs(); ASSERT_EQ((size_t)2, list.size()); ASSERT_EQ("/usr/local/share", list[0]); ASSERT_EQ("/usr/share", list[1]); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(DirectoriesValid, config) { ASSERT_TRUE(setenv("XDG_CONFIG_DIRS", "/config1:/config2", true) == 0); try { Xdg xdg; const auto &list = xdg.configDirs(); ASSERT_EQ((size_t)2, list.size()); ASSERT_EQ("/config1", list[0]); ASSERT_EQ("/config2", list[1]); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(DirectoriesValid, data) { ASSERT_TRUE(setenv("XDG_DATA_DIRS", "/data1:/data2", true) == 0); try { Xdg xdg; const auto &list = xdg.dataDirs(); ASSERT_EQ((size_t)2, list.size()); ASSERT_EQ("/data1", list[0]); ASSERT_EQ("/data2", list[1]); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(DirectoriesInvalid, config) { ASSERT_TRUE(setenv("XDG_CONFIG_DIRS", "bad1:bad2", true) == 0); try { Xdg xdg; const auto &list = xdg.configDirs(); ASSERT_EQ((size_t)1, list.size()); ASSERT_EQ("/etc/xdg", list[0]); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(DirectoriesInvalid, data) { ASSERT_TRUE(setenv("XDG_DATA_DIRS", "bad1:bad2", true) == 0); try { Xdg xdg; const auto &list = xdg.dataDirs(); ASSERT_EQ((size_t)2, list.size()); ASSERT_EQ("/usr/local/share", list[0]); ASSERT_EQ("/usr/share", list[1]); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(DirectoriesMixed, config) { ASSERT_TRUE(setenv("XDG_CONFIG_DIRS", "/config1:bad:/config2", true) == 0); try { Xdg xdg; const auto &list = xdg.configDirs(); ASSERT_EQ((size_t)2, list.size()); ASSERT_EQ("/config1", list[0]); ASSERT_EQ("/config2", list[1]); } catch (const std::exception &) { ASSERT_TRUE(false); } } TEST(DirectoriesMixed, data) { ASSERT_TRUE(setenv("XDG_DATA_DIRS", "/data1:bad:/data2", true) == 0); try { Xdg xdg; const auto &list = xdg.dataDirs(); ASSERT_EQ((size_t)2, list.size()); ASSERT_EQ("/data1", list[0]); ASSERT_EQ("/data2", list[1]); } catch (const std::exception &) { ASSERT_TRUE(false); } } int main(int argc, char **argv) { auto home = getenv("HOME"); if (home == nullptr) return 0; myhome = home; InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }