Elements  6.2
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 <algorithm> // for for_each, transform, copy_if
#include <string> // for std::string
#include <vector> // for std::vector
#include <boost/filesystem/fstream.hpp> // for ofstream
#include <boost/filesystem/operations.hpp> // for exists
#include <boost/test/unit_test.hpp> // for boost unit test macros
#include "ElementsKernel/Path.h" // for joinPath, Item
#include "ElementsKernel/System.h" // for DEFAULT_INSTALL_PREFIX
#include "ElementsKernel/Temporary.h" // for TempDir, TempEnv
#include <ElementsKernel/Exception.h> // for Exception
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
Elements::Kernel::Path::Item
boost::filesystem::path Item
Definition: Path.h:56
System.h
This file is intended to iron out all the differences between systems (currently Linux and MacOSX)
std::for_each
T for_each(T... args)
std::string
STL class.
Elements::Kernel::getConfigurationPath
ELEMENTS_API Path::Item getConfigurationPath(const T &file_name, bool raise_exception=true)
Path.h
provide functions to retrieve resources pointed by environment variables
std::vector
STL class.
std::distance
T distance(T... args)
Exception.h
defines the base Elements exception class
Elements::Kernel::getConfigurationVariableName
ELEMENTS_API std::string getConfigurationVariableName()
retrieve the variable name used for the configuration file lookup
Definition: Configuration.cpp:41
Elements::Kernel::getConfigurationLocations
ELEMENTS_API std::vector< Path::Item > getConfigurationLocations(bool exist_only=false)
Definition: Configuration.cpp:49
Elements::Kernel::Path::join
ELEMENTS_API auto join(Args &&... args) -> decltype(joinPath(std::forward< Args >(args)...))
alias for the joinPath function
Elements::TempEnv
Environment TempEnv
Definition: Temporary.h:67
std::copy_if
T copy_if(T... args)
Temporary.h
Handling of temporary files, directories and environments.
Elements::System::DEFAULT_INSTALL_PREFIX
const std::string DEFAULT_INSTALL_PREFIX
constant for the canonical installation prefix (on Linux and MacOSX at least)
Definition: System.h:90
Configuration.h
provide functions to retrieve configuration files
Elements
Definition: callBackExample.h:35