GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: test/common/test_utils.h Lines: 21 31 67.7 %
Date: 2025-03-04 18:34:12 Branches: 15 30 50.0 %

Line Branch Exec Source
1
//
2
// Created by acey on 23.08.22.
3
//
4
5
#ifndef LOG_TEST_TEST_UTILS_H_
6
#define LOG_TEST_TEST_UTILS_H_
7
8
#include <string>
9
#include <functional>
10
#include <gtest/gtest.h>
11
12
extern char **test_argv;
13
14
520
inline static bool isSubstring(const std::string &string, const std::string &substring) {
15
520
  bool res = (string.find(substring) != std::string::npos);
16
17
520
  if(!res) {
18
    std::cout << "   String: " << string << std::endl;
19
    std::cout << "Substring: " << substring << std::endl;
20
  }
21
22
520
  return res;
23
}
24
25
817
inline static std::string removeNumbersFromString(std::string str) {
26
817
  int current = 0;
27
17203
  for (std::size_t i = 0; i < str.length(); i++) {
28
16386
    if (!isdigit(str[i])) {
29
8748
      str[current] = str[i];
30
8748
      current++;
31
    }
32
  }
33
817
  return str.substr(0, current);;
34
}
35
36
//! Macros to capture stdout and stderr and assign output directly to std::string or suppress output
37
#define LPP_CAPTURE_STDOUT(x) []() {testing::internal::CaptureStdout(); x; \
38
return testing::internal::GetCapturedStdout();}()
39
40
#define LPP_CAPTURE_STDERR(x) []() {testing::internal::CaptureStderr(); x; \
41
return testing::internal::GetCapturedStderr();}()
42
43
//! Get file name from __FILE__ macro
44
#define LPP_FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
45
46
17
inline int ForkAndReap(int *Ws, const std::function<void()> &fn) {
47
17
  pid_t pid = fork();
48
34
  if (fork() < 0) {
49
    return -1;
50
  }
51
52
17
  if (pid == 0) {
53
    fn();
54
    _exit(0);
55
  }
56
57
  while (true) {
58
17
    if (waitpid(pid, Ws, WUNTRACED) < 0) {
59
      if (EINTR == errno) {
60
        continue;
61
      }
62
63
      abort();
64

17
    } else if (!WIFEXITED(*Ws) && !WIFSIGNALED(*Ws)) { //shouldn't have stopped
65
      if (kill(pid, SIGTERM) < 0 || kill(pid, SIGCONT) < 0) {
66
        abort();
67
      }
68
    }
69
17
    break;
70
  }
71
17
  return 0;
72
}
73
74
/**
75
 * Checks if function calls abort;
76
 * @param fn
77
 * @return true if function called abort(), otherwise false
78
 */
79
80
17
inline bool checkAbort(const std::function<void()> &fn) {
81
  int ws;
82

17
  return ForkAndReap(&ws, fn) >= 0 && WIFSIGNALED(ws) && WTERMSIG(SIGABRT);
83
}
84
85
//! Ros testing utils
86
namespace lpp {
87
namespace testing {
88
89
inline static constexpr const char *ERROR_MESSAGE = "Base angle (%f) is less than the minimum angle (%f)";
90
inline static const std::string EXPECTED_ERROR_MESSAGE = "Base angle (3.300000) is less than the minimum angle (5.500000)\n";
91
}
92
}
93
94
95
namespace lpp {
96
namespace rostest{
97
inline static const std::string debug = "\x1B[m[DEBUG] [.]: Test\x1B[m\n";
98
inline static const std::string info = "\x1B[m[ INFO] [.]: Test\x1B[m\n";
99
inline static const std::string warning = "\x1B[m[ WARN] [.]: Test\x1B[m\n";
100
inline static const std::string error = "\x1B[m[ERROR] [.]: Test\x1B[m\n";
101
inline static const std::string fatal = "\x1B[m[FATAL] [.]: Test\x1B[m\n";
102
}
103
104
105
namespace rosprintf {
106
inline static const std::string debug = "\x1B[m[DEBUG] [.]: Base angle (.) is less than the minimum angle (.)\x1B[m\n";
107
inline static const std::string info = "\x1B[m[ INFO] [.]: Base angle (.) is less than the minimum angle (.)\x1B[m\n";
108
inline static const std::string warning = "\x1B[m[ WARN] [.]: Base angle (.) is less than the minimum angle (.)\x1B[m\n";
109
inline static const std::string error = "\x1B[m[ERROR] [.]: Base angle (.) is less than the minimum angle (.)\x1B[m\n";
110
inline static const std::string fatal = "\x1B[m[FATAL] [.]: Base angle (.) is less than the minimum angle (.)\x1B[m\n";
111
}
112
113
114
namespace logstr {
115
inline static const std::string info = "\x1B[m[ INFO] [.]: LOG_STRING: collected info\x1B[m\n";
116
inline static const std::string warning = "\x1B[m[ WARN] [.]: LOG_STRING: collected warn\x1B[m\n";
117
inline static const std::string error = "\x1B[m[ERROR] [.]: LOG_STRING: collected error\x1B[m\n";
118
inline static const std::string fatal = "\x1B[m[FATAL] [.]: LOG_STRING: collected fatal\x1B[m\n";
119
}
120
121
namespace custom {
122
inline static const std::string test123 = "test123\n";
123
inline static const std::string debug = "Log++ [debug] ";
124
inline static const std::string info = "Log++ [info] ";
125
inline static const std::string warning = "Log++ [warning] ";
126
inline static const std::string error = "Log++ [error] ";
127
inline static const std::string fatal = "Log++ [fatal] ";
128
}
129
}
130
131
132
#endif //LOG_TEST_TEST_UTILS_H_