-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathdebug_unittest.cpp
More file actions
executable file
·126 lines (95 loc) · 3.46 KB
/
debug_unittest.cpp
File metadata and controls
executable file
·126 lines (95 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
///usr/bin/env ccache g++ -Wall -Wextra -Werror -Wno-error=cpp -O3 -std=gnu++17 -pthread "$0" stdout_capture_lib.cpp debug.cpp -lgtest -lgtest_main -o /tmp/a && /tmp/a "$@"; exit
// For the line just above, see my answer here: https://stackoverflow.com/a/75491834/4561887
/*
This file is part of eRCaGuy_hello_world: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
GS
July 2025
Unit test this library.
To build and run:
1. Follow my instructions:
Here: https://stackoverflow.com/a/75718815/4561887
Or here: https://stackoverflow.com/a/75719098/4561887
2. Build and run this file:
```bash
cd path/to/here
./stdout_capture_lib_unittest.cpp
```
OR:
```bash
# Do NOT cd into the `cpp` dir here:
cd path/to/eRCaGuy_hello_world
ccache g++ -Wall -Wextra -Werror -Wno-error=cpp -O3 -std=gnu++17 -pthread cpp/debug_unittest.cpp cpp/stdout_capture_lib.cpp cpp/debug.cpp -lgtest -lgtest_main -o /tmp/a && /tmp/a
```
*/
// Primary include
#include "debug.h"
// Local includes
#include "stdout_capture_lib.hpp"
// 3rd-party includes
#include <gtest/gtest.h>
// Linux/Unix includes
// NA
// C++ includes
#include <iostream> // For `std::cin`, `std::cout`, `std::endl`, etc.
// C includes
// NA
// anonymous namespace
namespace
{
TEST(Debug, TodoPrints)
{
StdoutCapture capture;
std::string captured;
std::string expected;
capture.start();
TODO_PRINTF("Statement 1\n");
captured = capture.stop();
printf("Captured output: %s", captured.c_str());
expected = "TODO: debug_unittest.cpp:TestBody():63: Statement 1\n";
EXPECT_EQ(captured, expected);
capture.start();
TODO_PRINTF("Statement 2: %s, %i, %u\n", "test", 42, 100u);
captured = capture.stop();
printf("Captured output: %s", captured.c_str());
expected = "TODO: debug_unittest.cpp:TestBody():70: Statement 2: test, 42, 100\n";
EXPECT_EQ(captured, expected);
}
TEST(Debug, DebugPrints)
{
StdoutCapture capture;
std::string captured;
std::string expected;
capture.start();
DEBUG_PRINTF("Statement 1\n");
captured = capture.stop();
printf("Captured output: %s", captured.c_str());
expected = "debug_unittest.cpp:TestBody():84: Statement 1\n";
EXPECT_EQ(captured, expected);
capture.start();
DEBUG_PRINTF("Statement 2: %s, %i, %u\n", "test", 42, 100u);
captured = capture.stop();
printf("Captured output: %s", captured.c_str());
expected = "debug_unittest.cpp:TestBody():91: Statement 2: test, 42, 100\n";
EXPECT_EQ(captured, expected);
}
} // namespace
/*
Example run and output:
eRCaGuy_hello_world/cpp$ ./debug_unittest.cpp
Running main() from /home/gabriel/Downloads/Install_Files/gtest/googletest-1.14.0/googletest/src/gtest_main.cc
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from Debug
[ RUN ] Debug.TodoPrints
Captured output: TODO: debug_unittest.cpp:TestBody():63: Statement 1
Captured output: TODO: debug_unittest.cpp:TestBody():70: Statement 2: test, 42, 100
[ OK ] Debug.TodoPrints (0 ms)
[ RUN ] Debug.DebugPrints
Captured output: debug_unittest.cpp:TestBody():84: Statement 1
Captured output: debug_unittest.cpp:TestBody():91: Statement 2: test, 42, 100
[ OK ] Debug.DebugPrints (0 ms)
[----------] 2 tests from Debug (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (0 ms total)
[ PASSED ] 2 tests.
*/