ChimeraTK-DeviceAccess  03.18.00
testParserUtilities.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 
5 #include "parserUtilities.h"
6 
7 #include <iostream>
8 #include <utility>
9 
10 using namespace boost::unit_test_framework;
12 
13 // The test cases require the current working directory. This is provided
14 // to the test suite through the shell script:
15 // ./tests/scripts/testParserUtilities.sh
16 
17 /**********************************************************************************************************************/
18 /* Code for setting up test suite */
19 /**********************************************************************************************************************/
21  public:
22  explicit ParserUtilsTestClass(std::string currentWorkingDirectory);
23  void testGetCurrentWorkingDir();
24  void testConvertToAbsPath();
25  void testExtractDirectory();
26  void testExtractFileName();
27  void testConcatenatePaths();
28 
29  private:
30  std::string _currentWorkingDir;
31 };
32 
33 class ParserUtilitiesTestSuite : public test_suite {
34  public:
35  explicit ParserUtilitiesTestSuite(std::string const& currentWorkingDirectory)
36  : test_suite("ParserUtilitiesTestSuite") {
37  boost::shared_ptr<ParserUtilsTestClass> parserUtilTest(new ParserUtilsTestClass(currentWorkingDirectory));
38  add(BOOST_CLASS_TEST_CASE(&ParserUtilsTestClass::testGetCurrentWorkingDir, parserUtilTest));
39  add(BOOST_CLASS_TEST_CASE(&ParserUtilsTestClass::testConvertToAbsPath, parserUtilTest));
40  add(BOOST_CLASS_TEST_CASE(&ParserUtilsTestClass::testExtractDirectory, parserUtilTest));
41  add(BOOST_CLASS_TEST_CASE(&ParserUtilsTestClass::testExtractFileName, parserUtilTest));
42  add(BOOST_CLASS_TEST_CASE(&ParserUtilsTestClass::testConcatenatePaths, parserUtilTest));
43  }
44 };
45 
47  if(framework::master_test_suite().argc < 2) {
48  std::cout << "Usage: " << framework::master_test_suite().argv[0] << " currentWorkingDir" << std::endl;
49  return false;
50  }
51  auto* currentWorkingDir = framework::master_test_suite().argv[1];
52 
53  framework::master_test_suite().p_name.value = "Rebot backend test suite";
54  framework::master_test_suite().add(new ParserUtilitiesTestSuite(currentWorkingDir));
55 
56  return true;
57 }
58 
59 ParserUtilsTestClass::ParserUtilsTestClass(std::string currentWorkingDirectory)
60 : _currentWorkingDir(std::move(currentWorkingDirectory)) {}
61 
62 /**********************************************************************************************************************/
63 
64 /* Test cases... */
66  std::string expectedReturnValue = _currentWorkingDir + "/";
67  BOOST_CHECK(parsutils::getCurrentWorkingDirectory() == expectedReturnValue);
68 }
69 
71  BOOST_CHECK(parsutils::convertToAbsolutePath("./test") == _currentWorkingDir + "/" + "./test");
72  BOOST_CHECK(parsutils::convertToAbsolutePath("./test/") == _currentWorkingDir + "/" + "./test/");
73  BOOST_CHECK(parsutils::convertToAbsolutePath("/test") == "/test");
74  BOOST_CHECK(parsutils::convertToAbsolutePath("/test/") == "/test/");
75  BOOST_CHECK(parsutils::convertToAbsolutePath("/") == "/");
76  BOOST_CHECK(parsutils::convertToAbsolutePath("test") == _currentWorkingDir + "/" + "test");
77  BOOST_CHECK(parsutils::convertToAbsolutePath("test/") == _currentWorkingDir + "/" + "test/");
78  BOOST_CHECK(parsutils::convertToAbsolutePath("") == _currentWorkingDir + "/");
79 }
80 
82  BOOST_CHECK(parsutils::extractDirectory("./test") == "./");
83  BOOST_CHECK(parsutils::extractDirectory("./test/") == "./test/");
84  BOOST_CHECK(parsutils::extractDirectory("/test") == "/");
85  BOOST_CHECK(parsutils::extractDirectory("/") == "/");
86  BOOST_CHECK(parsutils::extractDirectory("/test/") == "/test/");
87  BOOST_CHECK(parsutils::extractDirectory("test") == "./");
88  BOOST_CHECK(parsutils::extractDirectory("test/") == "test/");
89  BOOST_CHECK(parsutils::extractDirectory("") == "./");
90 }
91 
93  BOOST_CHECK(parsutils::extractFileName("./test") == "test");
94  BOOST_CHECK(parsutils::extractFileName("./test/").empty());
95  BOOST_CHECK(parsutils::extractFileName("/test") == "test");
96  BOOST_CHECK(parsutils::extractFileName("/test/").empty());
97  BOOST_CHECK(parsutils::extractFileName("").empty());
98 }
99 
101  BOOST_CHECK(parsutils::concatenatePaths("./a", "b") == "./a/b");
102  BOOST_CHECK(parsutils::concatenatePaths("./a/", "b") == "./a/b");
103  BOOST_CHECK(parsutils::concatenatePaths("./a/", "/b") == "/b");
104  BOOST_CHECK(parsutils::concatenatePaths("a", "b") == "a/b");
105  BOOST_CHECK(parsutils::concatenatePaths("a/", "b") == "a/b");
106  BOOST_CHECK(parsutils::concatenatePaths("a/", "/b") == "/b");
107  BOOST_CHECK(parsutils::concatenatePaths("a/", "") == "a/");
108  BOOST_CHECK(parsutils::concatenatePaths("", "") == "/");
109 }
ParserUtilsTestClass::ParserUtilsTestClass
ParserUtilsTestClass(std::string currentWorkingDirectory)
Definition: testParserUtilities.cpp:59
ChimeraTK::parserUtilities::getCurrentWorkingDirectory
std::string getCurrentWorkingDirectory()
Returns absolute path to current working directory. The returned path ends with a forward slash.
Definition: parserUtilities.cc:19
ParserUtilitiesTestSuite
Definition: testParserUtilities.cpp:33
ParserUtilsTestClass::testGetCurrentWorkingDir
void testGetCurrentWorkingDir()
Definition: testParserUtilities.cpp:65
boost_dynamic_init_test.h
ParserUtilsTestClass::testConvertToAbsPath
void testConvertToAbsPath()
Definition: testParserUtilities.cpp:70
parserUtilities.h
ChimeraTK::parserUtilities::extractFileName
std::string extractFileName(std::string const &path)
Extract the string after the last '/' in a path. Returned substring does not include the '/' characte...
Definition: parserUtilities.cc:48
ChimeraTK::parserUtilities::convertToAbsolutePath
std::string convertToAbsolutePath(std::string const &relativePath)
Converts a relative path to its absolute path.
Definition: parserUtilities.cc:23
ParserUtilsTestClass::testExtractFileName
void testExtractFileName()
Definition: testParserUtilities.cpp:92
ChimeraTK::parserUtilities::extractDirectory
std::string extractDirectory(std::string const &path)
Returns the path to the directory containing the file provided as the input parameter.
Definition: parserUtilities.cc:35
ParserUtilsTestClass
Definition: testParserUtilities.cpp:20
ChimeraTK::parserUtilities::concatenatePaths
std::string concatenatePaths(const std::string &path1, const std::string &path2)
Concatenates two given paths using custom rules.
Definition: parserUtilities.cc:27
ParserUtilsTestClass::testConcatenatePaths
void testConcatenatePaths()
Definition: testParserUtilities.cpp:100
ParserUtilitiesTestSuite::ParserUtilitiesTestSuite
ParserUtilitiesTestSuite(std::string const &currentWorkingDirectory)
Definition: testParserUtilities.cpp:35
ParserUtilsTestClass::testExtractDirectory
void testExtractDirectory()
Definition: testParserUtilities.cpp:81
init_unit_test
bool init_unit_test()
Definition: testParserUtilities.cpp:46
ChimeraTK::parserUtilities
Definition: parserUtilities.h:7