BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
TestUtils Namespace Reference

Various common utils for unit tests. More...

Functions

std::vector< double > binCenters (const QCPGraph *graph)
 Returns vector representing bin centers on QCPgraph. More...
 
std::vector< double > binErrors (const QCPGraph *graph)
 Returns vector representing bin errors of QCPGraph. More...
 
std::vector< double > binValues (const QCPGraph *graph)
 Returns vector representing y-values on QCPgraph. More...
 
template<typename T >
void clean_items (T &items)
 Deletes items in the container and cleans container afterwards. More...
 
template<typename T >
auto create_pointers (const std::vector< std::unique_ptr< T >> &vec)
 Creates vector of pointers from vector of unique_ptr. More...
 
template<typename B , typename D >
auto create_row (int ncolumns)
 Creates vector of unique_ptr of given type. More...
 
std::string CreateEmptyFile (const std::string &dirname, const std::string &fileName)
 Helper function to create empty file in a given directory (directory should exist). More...
 
std::string CreateTestDirectory (const std::string &test_sub_dir)
 Creates test directory in main test folder and returns full path. More...
 
std::string CreateTestFile (const std::string &dirname, const std::string &fileName)
 Helper function to create test file in a given directory (directory should exist). More...
 
template<typename G , typename T >
std::vector< double > get_values (const G *graph, T operand)
 Returns vector representing bin centers/values on QCPGraph. More...
 
template<typename T >
T * GetPlottable (QCustomPlot *custom_plot)
 Finds and returns specific plottable in QCustomPlot canvas. More...
 
QString JsonToString (const QJsonObject &object)
 
QJsonDocument LoadJson (const std::string &fileName)
 
QString ModelToJsonString (ModelView::SessionModel &model)
 Returns string representing serialized json content of the model. More...
 
void SaveJson (const QJsonArray &object, const std::string &fileName)
 
void SaveJson (const QJsonObject &object, const std::string &fileName)
 
std::string TestDirectoryPath (const std::string &test_sub_dir)
 Returns full path to the main test folder in CMAKE_BINARY_DIR. More...
 
std::string TestFileName (const std::string &test_sub_dir, const std::string &file_name)
 Returns full path to the file in test directory. More...
 
std::string TestOutputDir ()
 Returns full path to the main test folder, as defined by CMake at compile time. More...
 
template<typename T , typename... Args>
std::vector< T > toVector (Args &&... args)
 Creates vector of T from argument list. Used in EXPECT_EQ macros for convenience. More...
 

Detailed Description

Various common utils for unit tests.

Function Documentation

◆ binCenters()

std::vector< double > TestUtils::binCenters ( const QCPGraph *  graph)

Returns vector representing bin centers on QCPgraph.

Definition at line 18 of file customplot_test_utils.cpp.

19 {
20  return get_values(graph, [](auto x) { return x.key; });
21 }
std::vector< double > get_values(const G *graph, T operand)
Returns vector representing bin centers/values on QCPGraph.

References get_values().

Referenced by TEST_F().

Here is the call graph for this function:

◆ binErrors()

std::vector< double > TestUtils::binErrors ( const QCPGraph *  graph)

Returns vector representing bin errors of QCPGraph.

Definition at line 28 of file customplot_test_utils.cpp.

29 {
30  std::vector<double> result;
31  if (auto errorBars = GetPlottable<QCPErrorBars>(graph->parentPlot()); errorBars) {
32  auto container = errorBars->data();
33  std::transform(container->begin(), container->end(), std::back_inserter(result),
34  [](auto x) { return x.errorPlus; });
35  };
36  return result;
37 }

Referenced by TEST_F().

◆ binValues()

std::vector< double > TestUtils::binValues ( const QCPGraph *  graph)

Returns vector representing y-values on QCPgraph.

Definition at line 23 of file customplot_test_utils.cpp.

24 {
25  return get_values(graph, [](auto x) { return x.value; });
26 }

References get_values().

Referenced by TEST_F().

Here is the call graph for this function:

◆ clean_items()

template<typename T >
void TestUtils::clean_items ( T &  items)

Deletes items in the container and cleans container afterwards.

Definition at line 71 of file test_utils.h.

72 {
73  for (auto item : items)
74  delete item;
75  items.clear();
76 }

◆ create_pointers()

template<typename T >
auto TestUtils::create_pointers ( const std::vector< std::unique_ptr< T >> &  vec)

Creates vector of pointers from vector of unique_ptr.

Definition at line 90 of file test_utils.h.

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 }

Referenced by ViewItemTest::test_data(), and ViewModelBaseTest::test_data().

◆ create_row()

template<typename B , typename D >
auto TestUtils::create_row ( int  ncolumns)

Creates vector of unique_ptr of given type.

Definition at line 80 of file test_utils.h.

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 }

◆ CreateEmptyFile()

std::string TestUtils::CreateEmptyFile ( const std::string &  dirname,
const std::string &  fileName 
)

Helper function to create empty file in a given directory (directory should exist).

Returns full path of the file.

Definition at line 107 of file test_utils.cpp.

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 }
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")

References FileSystemUtils::filename().

Referenced by TEST_F().

Here is the call graph for this function:

◆ CreateTestDirectory()

std::string TestUtils::CreateTestDirectory ( const std::string &  test_sub_dir)

Creates test directory in main test folder and returns full path.

If directory exists, will do nothing.

Definition at line 40 of file test_utils.cpp.

41 {
42  std::string result = TestDirectoryPath(test_sub_dir);
44  return result;
45 }
MVVM_MODEL_EXPORT bool create_directory(const std::string &path)
Create directory, parent directory must exist.
Definition: fileutils.cpp:47
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

References ModelView::Utils::create_directory(), and TestDirectoryPath().

Referenced by FolderBasedTest::FolderBasedTest().

Here is the call graph for this function:

◆ CreateTestFile()

std::string TestUtils::CreateTestFile ( const std::string &  dirname,
const std::string &  fileName 
)

Helper function to create test file in a given directory (directory should exist).

Returns full path of the file.

Definition at line 91 of file test_utils.cpp.

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 }

References FileSystemUtils::filename().

Referenced by TEST_F().

Here is the call graph for this function:

◆ get_values()

template<typename G , typename T >
std::vector<double> TestUtils::get_values ( const G *  graph,
operand 
)

Returns vector representing bin centers/values on QCPGraph.

Definition at line 28 of file customplot_test_utils.h.

29 {
30  std::vector<double> result;
31  auto graph_data = *graph->data();
32  std::transform(std::begin(graph_data), std::end(graph_data), std::back_inserter(result),
33  [operand](const auto& point) { return operand(point); });
34  return result;
35 }

Referenced by binCenters(), and binValues().

◆ GetPlottable()

template<typename T >
T* TestUtils::GetPlottable ( QCustomPlot *  custom_plot)

Finds and returns specific plottable in QCustomPlot canvas.

Definition at line 47 of file customplot_test_utils.h.

48 {
49  for (int i = 0; i < custom_plot->plottableCount(); ++i) {
50  if (auto plottable = dynamic_cast<T*>(custom_plot->plottable()); plottable)
51  return plottable;
52  }
53  return nullptr;
54 }

◆ JsonToString()

QString TestUtils::JsonToString ( const QJsonObject &  object)

Definition at line 70 of file test_utils.cpp.

71 {
72  QJsonDocument document(object);
73  return QString(document.toJson(QJsonDocument::Compact));
74 }

Referenced by TEST_F().

◆ LoadJson()

QJsonDocument TestUtils::LoadJson ( const std::string &  fileName)

Definition at line 81 of file test_utils.cpp.

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 }

Referenced by TEST_F().

◆ ModelToJsonString()

QString TestUtils::ModelToJsonString ( ModelView::SessionModel model)

Returns string representing serialized json content of the model.

Definition at line 76 of file test_utils.cpp.

77 {
78  return QString::fromStdString(JsonUtils::ModelToJsonString(model));
79 }
MVVM_MODEL_EXPORT std::string ModelToJsonString(const SessionModel &model)
Returns multiline string representing model content as json.
Definition: jsonutils.cpp:34

References ModelView::JsonUtils::ModelToJsonString().

Here is the call graph for this function:

◆ SaveJson() [1/2]

void TestUtils::SaveJson ( const QJsonArray &  object,
const std::string &  fileName 
)

Definition at line 64 of file test_utils.cpp.

65 {
66  QJsonDocument document(object);
67  SaveDocument(document, fileName);
68 }

◆ SaveJson() [2/2]

void TestUtils::SaveJson ( const QJsonObject &  object,
const std::string &  fileName 
)

Definition at line 58 of file test_utils.cpp.

59 {
60  QJsonDocument document(object);
61  SaveDocument(document, fileName);
62 }

Referenced by TEST_F().

◆ TestDirectoryPath()

std::string TestUtils::TestDirectoryPath ( const std::string &  test_sub_dir)

Returns full path to the main test folder in CMAKE_BINARY_DIR.

Definition at line 47 of file test_utils.cpp.

48 {
49  return TestOutputDir() + std::string("/") + test_sub_dir;
50 }
std::string TestOutputDir()
Returns full path to the main test folder, as defined by CMake at compile time.
Definition: test_utils.cpp:35

References TestOutputDir().

Referenced by CreateTestDirectory(), TestFileName(), and FolderBasedTest::testPath().

Here is the call graph for this function:

◆ TestFileName()

std::string TestUtils::TestFileName ( const std::string &  test_sub_dir,
const std::string &  file_name 
)

Returns full path to the file in test directory.

Definition at line 52 of file test_utils.cpp.

53 {
54 
55  return TestDirectoryPath(test_sub_dir) + std::string("/") + file_name;
56 }

References TestDirectoryPath().

Referenced by TEST_F().

Here is the call graph for this function:

◆ TestOutputDir()

std::string TestUtils::TestOutputDir ( )

Returns full path to the main test folder, as defined by CMake at compile time.

Shoud point to CMAKE_BINARY_DIR/test_output

Definition at line 35 of file test_utils.cpp.

36 {
37  return TestConfig::TestOutputDir(); // defined in auto-generated testconfig.h
38 }

Referenced by TEST_F(), and TestDirectoryPath().

◆ toVector()

template<typename T , typename... Args>
std::vector<T> TestUtils::toVector ( Args &&...  args)

Creates vector of T from argument list. Used in EXPECT_EQ macros for convenience.

Definition at line 100 of file test_utils.h.

101 {
102  std::vector<T> v;
103  (v.push_back(T(args)), ...);
104  return v;
105 }