Elements  5.12.0
A C++ base framework for the Euclid Software.
ElementsKernel/tests/src/Configuration_test.cpp

This is an example of how to use the TempDir and TemEnv classes.

#include "ElementsKernel/Configuration.h" // header to test
#include <string> // for std::string
#include <vector> // for std::vector
#include <algorithm> // for for_each, transform, copy_if
#include <boost/test/unit_test.hpp> // for boost unit test macros
#include <boost/filesystem/operations.hpp> // for exists
#include <boost/filesystem/fstream.hpp> // for ofstream
#include "ElementsKernel/Temporary.h" // for TempDir, TempEnv
#include <ElementsKernel/Exception.h> // for Exception
#include "ElementsKernel/Path.h" // for joinPath, Item
#include "ElementsKernel/System.h" // for DEFAULT_INSTALL_PREFIX
using boost::filesystem::exists;
using boost::filesystem::is_regular;
namespace Elements {
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// Begin of the Boost tests
//
//-----------------------------------------------------------------------------
struct Configuration_Fixture {
TempDir m_top_dir;
vector<Path::Item> m_item_list;
vector<Path::Item> m_target_item_list;
vector<Path::Item> m_real_item_list;
vector<Path::Item> m_target_real_item_list;
Configuration_Fixture(): m_top_dir{ "Configuration_test-%%%%%%%" } {
using std::copy_if;
m_item_list.emplace_back(m_top_dir.path() / "test1");
m_item_list.emplace_back(m_top_dir.path() / "test1" / "foo");
m_item_list.emplace_back(m_top_dir.path() / "test2");
m_item_list.emplace_back(m_top_dir.path() / "test3");
for_each(m_item_list.cbegin(), m_item_list.cend(),
[](Path::Item p) {
boost::filesystem::create_directory(p);
});
m_item_list.emplace_back(m_top_dir.path() / "test4");
m_target_item_list = m_item_list;
m_target_item_list.emplace_back(Path::Item(System::DEFAULT_INSTALL_PREFIX) / "share" / "conf");
m_real_item_list.resize(m_item_list.size());
auto it = copy_if(m_item_list.begin(), m_item_list.end(),
m_real_item_list.begin(),
[](const Path::Item& p){
return exists(p);
});
m_real_item_list.erase(it, m_real_item_list.end());
m_target_real_item_list.resize(m_target_item_list.size());
auto it2 = copy_if(m_target_item_list.begin(), m_target_item_list.end(),
m_target_real_item_list.begin(),
[](const Path::Item& p){
return exists(p);
});
m_target_real_item_list.erase(it2, m_target_real_item_list.end());
}
~Configuration_Fixture() {
}
};
BOOST_AUTO_TEST_SUITE(Configuration_test)
//-----------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(ConfigurationException_test) {
BOOST_CHECK_THROW(getConfigurationPath("NonExistingFile.conf"), Exception);
}
BOOST_AUTO_TEST_CASE(ConfigurationVariableName_test) {
BOOST_CHECK_EQUAL(getConfigurationVariableName(), "ELEMENTS_CONF_PATH");
}
BOOST_FIXTURE_TEST_CASE(getFromLocations_test, Configuration_Fixture) {
auto env = TempEnv();
env["ELEMENTS_CONF_PATH"] = Path::join(m_item_list);
auto locations = getConfigurationLocations();
BOOST_CHECK_EQUAL_COLLECTIONS(locations.begin(), locations.end(),
m_target_item_list.begin(), m_target_item_list.end());
}
BOOST_FIXTURE_TEST_CASE(getFromLocationsExist_test, Configuration_Fixture) {
auto env = TempEnv();
env["ELEMENTS_CONF_PATH"] = Path::join(m_real_item_list);
auto locations = getConfigurationLocations(true);
BOOST_CHECK_EQUAL_COLLECTIONS(locations.begin(), locations.end(),
m_target_real_item_list.begin(), m_target_real_item_list.end());
}
BOOST_AUTO_TEST_SUITE_END()
//-----------------------------------------------------------------------------
//
// End of the Boost tests
//
//-----------------------------------------------------------------------------
} // namespace Elements
provide functions to retrieve configuration files
defines the base Elements exception class
provide functions to retrieve resources pointed by environment variables
This file is intended to iron out all the differences between systems (currently Linux and MacOSX)
Handling of temporary files, directories and environments.
T copy_if(T... args)
T distance(T... args)
T for_each(T... args)
ELEMENTS_API auto join(Args &&... args) -> decltype(joinPath(std::forward< Args >(args)...))
alias for the joinPath function
boost::filesystem::path Item
Definition: Path.h:61
const std::string DEFAULT_INSTALL_PREFIX
constant for the canonical installation prefix (on Linux and MacOSX at least)
Definition: System.h:90
ELEMENTS_API Path::Item getConfigurationPath(const T &file_name, bool raise_exception=true)
ELEMENTS_API std::string getConfigurationVariableName()
ELEMENTS_API std::vector< Path::Item > getConfigurationLocations(bool exist_only=false)
Environment TempEnv
Definition: Temporary.h:71