BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
fileutils.test.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/tests/testmodel/fileutils.test.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "folderbasedtest.h"
16 #include "google_test.h"
17 #include "mvvm/utils/fileutils.h"
18 #include "test_utils.h"
19 #include <QDir>
20 #include <stdexcept>
21 #include <string>
22 
23 using namespace ModelView;
24 
26 public:
27  FileUtilsTest() : FolderBasedTest("test_FileUtils") {}
29 };
30 
32 
34 {
35  EXPECT_TRUE(Utils::exists(testPath()));
36  EXPECT_FALSE(Utils::exists(std::string()));
37  EXPECT_FALSE(Utils::exists(std::string("abc")));
38 }
39 
41 {
42  std::string dirname = testPath() + std::string("/") + "subdir";
43  Utils::remove(dirname);
44 
45  EXPECT_TRUE(Utils::create_directory(dirname));
46  EXPECT_TRUE(Utils::exists(dirname));
47 }
48 
50 {
51  std::string dirname = testPath() + std::string("/") + "subdir2";
52  Utils::create_directory(dirname);
53 
54  EXPECT_TRUE(Utils::exists(dirname));
55  Utils::remove_all((dirname));
56  EXPECT_FALSE(Utils::exists(dirname));
57 }
58 
60 {
61  std::string filename = testPath() + std::string("/testmodel/fileutils.test.cpp");
62  std::string base_name = Utils::base_name(filename);
63 
64  EXPECT_EQ("fileutils.test", base_name);
65 }
66 
68 {
69  TestUtils::CreateTestFile(testPath(), "a.txt");
70  TestUtils::CreateTestFile(testPath(), "name0.json");
71  TestUtils::CreateTestFile(testPath(), "name1.json");
72 
73  auto found_files = Utils::FindFiles(testPath(), ".json");
74 
75  ASSERT_EQ(found_files.size(), 2);
76  EXPECT_NE(found_files.end(), std::find(found_files.begin(), found_files.end(),
77  Utils::join(testPath(), "name0.json")));
78  EXPECT_NE(found_files.end(), std::find(found_files.begin(), found_files.end(),
79  Utils::join(testPath(), "name1.json")));
80 }
81 
83 {
84  // parent path of testPath() is the main test folder
85  // "<build>/test_output/test_FileUtils" -> "<build>/test_output/"
86  EXPECT_EQ(Utils::parent_path(testPath()), TestUtils::TestOutputDir());
87 
88  // "<build>/test_output/test_FileUtils/a.txt" -> "<build>/test_output/test_FileUtils/"
89  auto filename = TestUtils::CreateTestFile(testPath(), "a.txt");
90  EXPECT_EQ(Utils::parent_path(filename), testPath());
91 }
92 
94 {
95  // creating new empty directory
96  std::string dirname = testPath() + std::string("/") + "subdir_is_empty";
97  Utils::remove_all(dirname);
98  Utils::create_directory(dirname);
99 
100  // it should be empty
101  EXPECT_TRUE(Utils::is_empty(dirname));
102 
103  // creating file in it, directory should be not empty
104  auto filename = TestUtils::CreateTestFile(dirname, "a.txt");
105  EXPECT_FALSE(Utils::is_empty(dirname));
106  // file itself should be not empty
107  EXPECT_FALSE(Utils::is_empty(dirname));
108 
109  // creating empty file
110  auto empty_filename = TestUtils::CreateEmptyFile(dirname, "a2.txt");
111  EXPECT_TRUE(Utils::is_empty(empty_filename));
112 }
Convenience class which creates a directory on disk for test content.
Defines class CLASS?
TEST_F(FileUtilsTest, exists)
Defines class CLASS?
Defines class CLASS?
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")
MVVM_MODEL_EXPORT std::string base_name(const std::string &path)
Provide the filename of a file path.
Definition: fileutils.cpp:78
MVVM_MODEL_EXPORT std::vector< std::string > FindFiles(const std::string &dirname, const std::string &ext)
Returns list of files with given extention found in given directory.
Definition: fileutils.cpp:87
MVVM_MODEL_EXPORT bool is_empty(const std::string &path)
Returns true if the file indicated by 'path' refers to empty file or directory.
Definition: fileutils.cpp:121
MVVM_MODEL_EXPORT bool remove(const std::string &path)
Removes file or empty directory.
Definition: fileutils.cpp:57
MVVM_MODEL_EXPORT void remove_all(const std::string &path)
Removes directory with all its content.
Definition: fileutils.cpp:67
MVVM_MODEL_EXPORT std::string parent_path(const std::string &path)
Returns the path to the parent directory.
Definition: fileutils.cpp:111
MVVM_MODEL_EXPORT std::string join(const std::string &part1, const std::string &part2)
Joins two path elements into the path.
Definition: fileutils.cpp:37
MVVM_MODEL_EXPORT bool exists(const std::string &fileName)
Returns true if file exists.
Definition: fileutils.cpp:27
MVVM_MODEL_EXPORT bool create_directory(const std::string &path)
Create directory, parent directory must exist.
Definition: fileutils.cpp:47
materialitems.h Collection of materials to populate MaterialModel.
bool exists(const QString &fileName)
Returns true if file exists.
std::string CreateEmptyFile(const std::string &dirname, const std::string &fileName)
Helper function to create empty file in a given directory (directory should exist).
Definition: test_utils.cpp:107
std::string CreateTestFile(const std::string &dirname, const std::string &fileName)
Helper function to create test file in a given directory (directory should exist).
Definition: test_utils.cpp:91
std::string TestOutputDir()
Returns full path to the main test folder, as defined by CMake at compile time.
Definition: test_utils.cpp:35
Defines class CLASS?