BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
test_utils.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/libtestmachinery/test_utils.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 "test_utils.h"
19 #include "mvvm/utils/fileutils.h"
20 #include "testconfig.h" // this file is auto generated by the build system in build directory
21 #include <QFile>
22 #include <QJsonDocument>
23 #include <QJsonObject>
24 #include <QString>
25 #include <QTextStream>
26 #include <stdexcept>
27 #include <string>
28 
29 using namespace ModelView;
30 
31 namespace {
32 void SaveDocument(const QJsonDocument& document, const std::string& fileName);
33 }
34 
36 {
37  return TestConfig::TestOutputDir(); // defined in auto-generated testconfig.h
38 }
39 
40 std::string TestUtils::CreateTestDirectory(const std::string& test_sub_dir)
41 {
42  std::string result = TestDirectoryPath(test_sub_dir);
44  return result;
45 }
46 
47 std::string TestUtils::TestDirectoryPath(const std::string& test_sub_dir)
48 {
49  return TestOutputDir() + std::string("/") + test_sub_dir;
50 }
51 
52 std::string TestUtils::TestFileName(const std::string& test_sub_dir, const std::string& file_name)
53 {
54 
55  return TestDirectoryPath(test_sub_dir) + std::string("/") + file_name;
56 }
57 
58 void TestUtils::SaveJson(const QJsonObject& object, const std::string& fileName)
59 {
60  QJsonDocument document(object);
61  SaveDocument(document, fileName);
62 }
63 
64 void TestUtils::SaveJson(const QJsonArray& object, const std::string& fileName)
65 {
66  QJsonDocument document(object);
67  SaveDocument(document, fileName);
68 }
69 
70 QString TestUtils::JsonToString(const QJsonObject& object)
71 {
72  QJsonDocument document(object);
73  return QString(document.toJson(QJsonDocument::Compact));
74 }
75 
77 {
78  return QString::fromStdString(JsonUtils::ModelToJsonString(model));
79 }
80 
81 QJsonDocument TestUtils::LoadJson(const std::string& fileName)
82 {
83  QFile jsonFile(QString::fromStdString(fileName));
84 
85  if (!jsonFile.open(QIODevice::ReadOnly))
86  throw std::runtime_error("TestUtils::LoadJson() -> Can't read file");
87 
88  return QJsonDocument().fromJson(jsonFile.readAll());
89 }
90 
91 std::string TestUtils::CreateTestFile(const std::string& dirname, const std::string& fileName)
92 {
93  std::string filename = dirname.empty() ? fileName : dirname + "/" + fileName;
94 
95  QFile file(QString::fromStdString(filename));
96  if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
97  throw std::runtime_error("TestFileUtils::createTestFile() -> Error. "
98  "Can't create file");
99 
100  QTextStream out(&file);
101  out << "Test file " << 42 << "\n";
102  file.close();
103 
104  return filename;
105 }
106 
107 std::string TestUtils::CreateEmptyFile(const std::string& dirname, const std::string& fileName)
108 {
109  std::string filename = dirname.empty() ? fileName : dirname + "/" + fileName;
110 
111  QFile file(QString::fromStdString(filename));
112  if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
113  throw std::runtime_error("TestFileUtils::createTestFile() -> Error. "
114  "Can't create file");
115  return filename;
116 }
117 
118 namespace {
119 
120 void SaveDocument(const QJsonDocument& document, const std::string& fileName)
121 {
122  QFile saveFile(QString::fromStdString(fileName));
123 
124  if (!saveFile.open(QIODevice::WriteOnly))
125  throw std::runtime_error("TestUtils::SaveDocument() -> Can't save file");
126 
127  saveFile.write(document.toJson());
128 }
129 
130 } // namespace
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
Defines class CLASS?
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 ModelToJsonString(const SessionModel &model)
Returns multiline string representing model content as json.
Definition: jsonutils.cpp:34
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.
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 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
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
Defines class CLASS?
Defines class CLASS?