Commit 9858a2bf authored by GILLES Sebastien's avatar GILLES Sebastien

#1477 Adapt WriteLuaFile test to avoid issues due to removed directory. The...

#1477 Adapt WriteLuaFile test to avoid issues due to removed directory. The test has been mostly rewritten in the process.
parent 45f02c9c
......@@ -150,6 +150,10 @@ namespace MoReFEM::Internal::MoReFEMDataNS
break;
}
case overwrite_directory::yes:
{
std::cout << "Removing pre-existing directory " << directory << " before recreating it."
<< std::endl;
}
break;
}
......
......@@ -79,7 +79,11 @@ namespace MoReFEM
else if constexpr(ProgramTypeT == program_type::test)
{
if (FilesystemNS::Folder::DoExist(result_directory_))
{
std::cout << "Removing pre-existing directory " << result_directory_ << " before recreating it."
<< std::endl;
FilesystemNS::Folder::Remove(result_directory_, __FILE__, __LINE__);
}
}
}
......
......@@ -33,6 +33,16 @@
using namespace MoReFEM;
namespace // anonymous
{
const std::string& GetOutputDirectory();
} // namespace anonymous
PRAGMA_DIAGNOSTIC(push)
# ifdef __clang__
......@@ -40,63 +50,100 @@ PRAGMA_DIAGNOSTIC(ignored "-Wdisabled-macro-expansion")
# endif // __clang__
BOOST_FIXTURE_TEST_CASE(check_value, TestNS::FixtureNS::Environment)
BOOST_FIXTURE_TEST_CASE(create_default_lua_when_unexisting, TestNS::FixtureNS::Environment)
{
decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
decltype(auto) output_directory = GetOutputDirectory();
const std::string output_directory = environment.SubstituteValues("${MOREFEM_TEST_OUTPUT_DIR}/TestWriteLuaFile/");
auto generated_lua = output_directory + "implicitly_default_generated.lua";
if (FilesystemNS::Folder::DoExist(output_directory))
FilesystemNS::Folder::Remove(output_directory, __FILE__, __LINE__);
BOOST_REQUIRE(!FilesystemNS::File::DoExist(generated_lua)); //The directory in which it stands has just been recreated!
// When the Lua file doesn't exist, it is created and an exception is thrown to stop the program.
using input_data_type = TestNS::WriteLuaFileNS::InputData;
BOOST_CHECK_THROW(TestNS::InitializeTestMoReFEMData<input_data_type> init(std::move(generated_lua)),
std::exception);
}
FilesystemNS::Folder::Create(output_directory, __FILE__, __LINE__);
auto generated_lua = environment.SubstituteValues(output_directory + "generated_default_lua_file.lua");
BOOST_FIXTURE_TEST_CASE(create_explicitly_default_lua_file, TestNS::FixtureNS::Environment)
{
decltype(auto) output_directory = GetOutputDirectory();
if (FilesystemNS::File::DoExist(generated_lua))
FilesystemNS::File::Remove(generated_lua, __FILE__, __LINE__);
auto implicitly_generated_lua = output_directory + "implicitly_default_generated.lua";
auto explicitly_generated_lua = output_directory + "explicitly_default_generated.lua";
using input_data_type = TestNS::WriteLuaFileNS::InputData;
Utilities::InputDataNS::CreateDefaultInputFile<input_data_type::Tuple>(explicitly_generated_lua);
BOOST_CHECK_THROW(TestNS::InitializeTestMoReFEMData<input_data_type> init(std::move(generated_lua)),
std::exception);
BOOST_CHECK(FilesystemNS::File::AreEquals(implicitly_generated_lua, explicitly_generated_lua, __FILE__, __LINE__));
}
BOOST_FIXTURE_TEST_CASE(write_full_fledged_lua_file, TestNS::FixtureNS::Environment)
{
decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
auto full_fledged_lua_file =
environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Core/MoReFEMData/WriteLuaFile/demo.lua");
using input_data_type = TestNS::WriteLuaFileNS::InputData;
TestNS::InitializeTestMoReFEMData<input_data_type> init(std::move(full_fledged_lua_file));
decltype(auto) morefem_data = init.GetMoReFEMData();
auto generated_by_direct_call_lua_file = output_directory + "/generated_by_direct_call_default_lua_file.lua";
// Write in output directory the content of the Lua file. We can't however do a direct comparison: Lua file may
// not be paginated (indentation, spaces, new lines, ...) and so forth the same way.
decltype(auto) output_directory = GetOutputDirectory();
Utilities::InputDataNS::CreateDefaultInputFile<input_data_type::Tuple>(generated_by_direct_call_lua_file);
auto lua_file_with_content = output_directory + "explicitly_with_content.lua";
Utilities::InputDataNS::Write(morefem_data.GetInputData(), lua_file_with_content);
BOOST_CHECK(FilesystemNS::File::AreEquals(generated_lua, generated_by_direct_call_lua_file, __FILE__, __LINE__));
// However if now we load the newly generated file and rewrite it, we should obtain the exact same file.
TestNS::InitializeTestMoReFEMData<input_data_type> init_from_generated(lua_file_with_content);
decltype(auto) morefem_data = init.GetMoReFEMData();
auto regenerated_lua_file_with_content = output_directory + "regenerated_from_explicitly_with_content.lua";
Utilities::InputDataNS::Write(init_from_generated.GetMoReFEMData().GetInputData(),
regenerated_lua_file_with_content);
auto lua_file_with_content = output_directory + "/generated_with_content.lua";
Utilities::InputDataNS::Write(morefem_data.GetInputData(), lua_file_with_content);
BOOST_CHECK(FilesystemNS::File::AreEquals(lua_file_with_content,
regenerated_lua_file_with_content,
__FILE__, __LINE__));
}
BOOST_FIXTURE_TEST_CASE(reload_and_rewrite, TestNS::FixtureNS::Environment)
PRAGMA_DIAGNOSTIC(pop)
namespace // anonymous
{
decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
const std::string output_directory = environment.SubstituteValues("${MOREFEM_TEST_OUTPUT_DIR}/TestWriteLuaFile/");
auto lua_file_with_content = output_directory + "/generated_with_content.lua";
using input_data_type = TestNS::WriteLuaFileNS::InputData;
TestNS::InitializeTestMoReFEMData<input_data_type> init(std::move(lua_file_with_content));
const std::string& GetOutputDirectory()
{
static bool first_call = true;
static std::string ret;
decltype(auto) morefem_data = init.GetMoReFEMData();
auto regenerated_file = output_directory + "/regenerated_with_content.lua";
Utilities::InputDataNS::Write(morefem_data.GetInputData(), regenerated_file);
if (first_call)
{
decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
BOOST_CHECK(FilesystemNS::File::AreEquals(lua_file_with_content, regenerated_file, __FILE__, __LINE__));
// Deliberately not the same as in the Lua file (to avoid automatic deletion of the directory during
// MoReFEMData initialization).
ret = environment.SubstituteValues("${MOREFEM_TEST_OUTPUT_DIR}/TestWriteLuaFile_not_from_lua_file/");
}
if (FilesystemNS::Folder::DoExist(ret))
FilesystemNS::Folder::Remove(ret, __FILE__, __LINE__);
FilesystemNS::Folder::Create(ret, __FILE__, __LINE__);
first_call = false;
}
return ret;
}
} // namespace anonymous
PRAGMA_DIAGNOSTIC(pop)
......@@ -58,8 +58,11 @@ namespace MoReFEM
* \brief Constructor.
*
* \param[in] lua_file Path to the Lua input data file.
*
* \tparam T A type convertible to std::string; the point here is to use an universal reference.
*/
explicit InitializeTestMoReFEMData(std::string&& lua_file);
template<class T>
explicit InitializeTestMoReFEMData(T&& lua_file);
//! Destructor.
~InitializeTestMoReFEMData() = default;
......
......@@ -21,8 +21,9 @@ namespace MoReFEM
template<class InputDataT>
template<class T>
InitializeTestMoReFEMData<InputDataT>
::InitializeTestMoReFEMData(std::string&& lua_file)
::InitializeTestMoReFEMData(T&& lua_file)
{
decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment