BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
fileutils.test.cpp File Reference

Implements class CLASS? More...

Include dependency graph for fileutils.test.cpp:

Go to the source code of this file.

Classes

class  FileUtilsTest
 

Functions

 TEST_F (FileUtilsTest, base_name)
 
 TEST_F (FileUtilsTest, create_directory)
 
 TEST_F (FileUtilsTest, exists)
 
 TEST_F (FileUtilsTest, FindFiles)
 
 TEST_F (FileUtilsTest, is_empty)
 
 TEST_F (FileUtilsTest, parent_path)
 
 TEST_F (FileUtilsTest, remove_all)
 

Detailed Description

Implements class CLASS?

Homepage:\n http://www.bornagainproject.org
License:\n GNU General Public License v3 or higher (see COPYING)
Authors
Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)

Definition in file fileutils.test.cpp.

Function Documentation

◆ TEST_F() [1/7]

TEST_F ( FileUtilsTest  ,
base_name   
)

Definition at line 59 of file fileutils.test.cpp.

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 }
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

References ModelView::Utils::base_name(), and FileSystemUtils::filename().

Here is the call graph for this function:

◆ TEST_F() [2/7]

TEST_F ( FileUtilsTest  ,
create_directory   
)

Definition at line 40 of file fileutils.test.cpp.

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 }
MVVM_MODEL_EXPORT bool remove(const std::string &path)
Removes file or empty directory.
Definition: fileutils.cpp:57
MVVM_MODEL_EXPORT bool create_directory(const std::string &path)
Create directory, parent directory must exist.
Definition: fileutils.cpp:47
bool exists(const QString &fileName)
Returns true if file exists.

References ModelView::Utils::create_directory(), ModelView::Utils::exists(), and ModelView::Utils::remove().

Here is the call graph for this function:

◆ TEST_F() [3/7]

TEST_F ( FileUtilsTest  ,
exists   
)

Definition at line 33 of file fileutils.test.cpp.

34 {
35  EXPECT_TRUE(Utils::exists(testPath()));
36  EXPECT_FALSE(Utils::exists(std::string()));
37  EXPECT_FALSE(Utils::exists(std::string("abc")));
38 }

References ModelView::Utils::exists().

Here is the call graph for this function:

◆ TEST_F() [4/7]

TEST_F ( FileUtilsTest  ,
FindFiles   
)

Definition at line 67 of file fileutils.test.cpp.

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 }
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
std::string join(const std::vector< std::string > &joinable, const std::string &joint)
Returns string obtain by joining vector elements.
Definition: StringUtils.cpp:71
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

References TestUtils::CreateTestFile(), ModelView::Utils::FindFiles(), and ModelView::Utils::join().

Here is the call graph for this function:

◆ TEST_F() [5/7]

TEST_F ( FileUtilsTest  ,
is_empty   
)

Definition at line 93 of file fileutils.test.cpp.

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 }
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 void remove_all(const std::string &path)
Removes directory with all its content.
Definition: fileutils.cpp:67
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

References ModelView::Utils::create_directory(), TestUtils::CreateEmptyFile(), TestUtils::CreateTestFile(), FileSystemUtils::filename(), ModelView::Utils::is_empty(), and ModelView::Utils::remove_all().

Here is the call graph for this function:

◆ TEST_F() [6/7]

TEST_F ( FileUtilsTest  ,
parent_path   
)

Definition at line 82 of file fileutils.test.cpp.

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 }
MVVM_MODEL_EXPORT std::string parent_path(const std::string &path)
Returns the path to the parent directory.
Definition: fileutils.cpp:111
std::string TestOutputDir()
Returns full path to the main test folder, as defined by CMake at compile time.
Definition: test_utils.cpp:35

References TestUtils::CreateTestFile(), FileSystemUtils::filename(), ModelView::Utils::parent_path(), and TestUtils::TestOutputDir().

Here is the call graph for this function:

◆ TEST_F() [7/7]

TEST_F ( FileUtilsTest  ,
remove_all   
)

Definition at line 49 of file fileutils.test.cpp.

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 }

References ModelView::Utils::create_directory(), ModelView::Utils::exists(), and ModelView::Utils::remove_all().

Here is the call graph for this function: