BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
test_utils.h
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/libtestmachinery/test_utils.h
6 //! @brief Defines 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 #ifndef BORNAGAIN_MVVM_TESTS_LIBTESTMACHINERY_TEST_UTILS_H
16 #define BORNAGAIN_MVVM_TESTS_LIBTESTMACHINERY_TEST_UTILS_H
17 
19 #include <QString>
20 #include <memory>
21 
22 //! @file mvvm/tests/libtestmachinery/test_utils.h
23 //! @brief Collection of utility functions for various unit tests.
24 
25 class QJsonObject;
26 class QJsonArray;
27 
28 namespace ModelView {
29 class SessionModel;
30 }
31 
32 //! Various common utils for unit tests.
33 
34 namespace TestUtils {
35 
36 //! Returns full path to the main test folder, as defined by CMake at compile time.
37 //! Shoud point to CMAKE_BINARY_DIR/test_output
38 std::string TestOutputDir();
39 
40 //! Creates test directory in main test folder and returns full path.
41 //! If directory exists, will do nothing.
42 std::string CreateTestDirectory(const std::string& test_sub_dir);
43 
44 //! Returns full path to the main test folder in CMAKE_BINARY_DIR.
45 std::string TestDirectoryPath(const std::string& test_sub_dir);
46 
47 //! Returns full path to the file in test directory.
48 std::string TestFileName(const std::string& test_sub_dir, const std::string& file_name);
49 
50 void SaveJson(const QJsonObject& object, const std::string& fileName);
51 
52 void SaveJson(const QJsonArray& object, const std::string& fileName);
53 
54 QString JsonToString(const QJsonObject& object);
55 
56 //! Returns string representing serialized json content of the model.
58 
59 QJsonDocument LoadJson(const std::string& fileName);
60 
61 //! Helper function to create test file in a given directory (directory should exist).
62 //! Returns full path of the file.
63 std::string CreateTestFile(const std::string& dirname, const std::string& fileName);
64 
65 //! Helper function to create empty file in a given directory (directory should exist).
66 //! Returns full path of the file.
67 std::string CreateEmptyFile(const std::string& dirname, const std::string& fileName);
68 
69 //! Deletes items in the container and cleans container afterwards.
70 
71 template <typename T> void clean_items(T& items)
72 {
73  for (auto item : items)
74  delete item;
75  items.clear();
76 }
77 
78 //! Creates vector of unique_ptr of given type.
79 
80 template <typename B, typename D> auto create_row(int ncolumns)
81 {
82  std::vector<std::unique_ptr<B>> result;
83  for (int i = 0; i < ncolumns; ++i)
84  result.emplace_back(std::make_unique<D>());
85  return result;
86 }
87 
88 //! Creates vector of pointers from vector of unique_ptr.
89 
90 template <typename T> auto create_pointers(const std::vector<std::unique_ptr<T>>& vec)
91 {
92  std::vector<T*> result;
93  std::transform(vec.begin(), vec.end(), std::back_inserter(result),
94  [](auto& x) { return x.get(); });
95  return result;
96 }
97 
98 //! Creates vector of T from argument list. Used in EXPECT_EQ macros for convenience.
99 
100 template <typename T, typename... Args> std::vector<T> toVector(Args&&... args)
101 {
102  std::vector<T> v;
103  (v.push_back(T(args)), ...);
104  return v;
105 }
106 
107 } // namespace TestUtils
108 
109 #endif // BORNAGAIN_MVVM_TESTS_LIBTESTMACHINERY_TEST_UTILS_H
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Various common utils for unit tests.
Definition: test_utils.h:34
auto create_pointers(const std::vector< std::unique_ptr< T >> &vec)
Creates vector of pointers from vector of unique_ptr.
Definition: test_utils.h:90
auto create_row(int ncolumns)
Creates vector of unique_ptr of given type.
Definition: test_utils.h:80
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::vector< T > toVector(Args &&... args)
Creates vector of T from argument list. Used in EXPECT_EQ macros for convenience.
Definition: test_utils.h:100
std::string TestFileName(const std::string &test_sub_dir, const std::string &file_name)
Returns full path to the file in test directory.
Definition: test_utils.cpp:52
void clean_items(T &items)
Deletes items in the container and cleans container afterwards.
Definition: test_utils.h:71
std::string TestDirectoryPath(const std::string &test_sub_dir)
Returns full path to the main test folder in CMAKE_BINARY_DIR.
Definition: test_utils.cpp:47
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
void SaveJson(const QJsonObject &object, const std::string &fileName)
Definition: test_utils.cpp:58
std::string CreateTestDirectory(const std::string &test_sub_dir)
Creates test directory in main test folder and returns full path.
Definition: test_utils.cpp:40
QJsonDocument LoadJson(const std::string &fileName)
Definition: test_utils.cpp:81
QString JsonToString(const QJsonObject &object)
Definition: test_utils.cpp:70
std::string TestOutputDir()
Returns full path to the main test folder, as defined by CMake at compile time.
Definition: test_utils.cpp:35
QString ModelToJsonString(ModelView::SessionModel &model)
Returns string representing serialized json content of the model.
Definition: test_utils.cpp:76