Skip to content

Commit b5ece33

Browse files
committed
Moving more functions into catch_test_common
1 parent 721932a commit b5ece33

File tree

3 files changed

+65
-53
lines changed

3 files changed

+65
-53
lines changed

tests/catch_test_common.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <glib.h>
1919
#include <iostream>
20+
#include <setjmp.h>
2021
#include <stdio.h>
2122
#include <string>
2223
#include <tuple>
@@ -27,6 +28,9 @@
2728

2829
extern int foreground;
2930

31+
int exit_status = 0;
32+
bool fail_next = false;
33+
jmp_buf exit_jump;
3034
std::string err_log_lines, out_log_lines;
3135

3236
captured_stdio captured_stderr;
@@ -179,3 +183,53 @@ std::tuple<std::string, std::string> end_capture(bool print)
179183

180184
return std::tuple<std::string, std::string>(get_captured_stderr(print), get_captured_stdout(print));
181185
}
186+
187+
extern "C" {
188+
int mocked_asprintf(char **strp, const char *fmt, ...)
189+
{
190+
if (fail_next) {
191+
fail_next = false;
192+
*strp = nullptr;
193+
return -1;
194+
}
195+
196+
va_list args;
197+
va_start(args, fmt);
198+
int result = vasprintf(strp, fmt, args);
199+
va_end(args);
200+
return result;
201+
}
202+
203+
void mocked_exit(int status)
204+
{
205+
exit_status = status;
206+
longjmp(exit_jump, 1);
207+
}
208+
209+
void mocked_g_logger(int log_level, const char *format, ...)
210+
{
211+
char *log_message;
212+
va_list args;
213+
214+
va_start(args, format);
215+
216+
vasprintf(&log_message, format, args);
217+
218+
va_end(args);
219+
220+
err_log_lines.append(log_message);
221+
err_log_lines.append("\n");
222+
223+
free(log_message);
224+
}
225+
226+
char *mocked_strndup(const char *s, size_t n)
227+
{
228+
if (fail_next) {
229+
fail_next = false;
230+
return nullptr;
231+
}
232+
233+
return strndup(s, n);
234+
}
235+
}

tests/catch_test_common.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ std::tuple<std::string, std::string> end_capture(bool print = false);
4040

4141
int run_command(const std::string &file, std::vector<std::string> argv = {}, const std::string &input = "");
4242

43+
extern "C" {
44+
int mocked_asprintf(char **strp, const char *fmt, ...);
45+
void mocked_exit(int status);
46+
void mocked_g_logger(int log_level, const char *format, ...);
47+
char *mocked_strndup(const char *s, size_t n);
48+
}
49+
4350
#endif

tests/unit_test_renderd_config.cpp

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <cstdarg>
21
#include <cstdlib>
32
#include <cstring>
43
#include <fstream>
@@ -15,60 +14,12 @@
1514
#define RENDERD_CONF "./etc/renderd/renderd.conf.examples"
1615
#endif
1716

18-
int exit_status = 0;
19-
static bool fail_next = false;
20-
static jmp_buf exit_jump;
17+
extern int exit_status;
18+
extern bool fail_next;
19+
extern jmp_buf exit_jump;
2120
extern std::string err_log_lines;
2221

2322
extern "C" {
24-
int mocked_asprintf(char **strp, const char *fmt, ...)
25-
{
26-
if (fail_next) {
27-
fail_next = false;
28-
*strp = nullptr;
29-
return -1;
30-
}
31-
32-
va_list args;
33-
va_start(args, fmt);
34-
int result = vasprintf(strp, fmt, args);
35-
va_end(args);
36-
return result;
37-
}
38-
39-
void mocked_exit(int status)
40-
{
41-
exit_status = status;
42-
longjmp(exit_jump, 1);
43-
}
44-
45-
void mocked_g_logger(int log_level, const char *format, ...)
46-
{
47-
char *log_message;
48-
va_list args;
49-
50-
va_start(args, format);
51-
52-
vasprintf(&log_message, format, args);
53-
54-
va_end(args);
55-
56-
err_log_lines.append(log_message);
57-
err_log_lines.append("\n");
58-
59-
free(log_message);
60-
}
61-
62-
char *mocked_strndup(const char *s, size_t n)
63-
{
64-
if (fail_next) {
65-
fail_next = false;
66-
return nullptr;
67-
}
68-
69-
return strndup(s, n);
70-
}
71-
7223
#define asprintf mocked_asprintf
7324
#define exit mocked_exit
7425
#define g_logger mocked_g_logger
@@ -611,9 +562,9 @@ TEST_CASE("renderd.conf file processing exit handling", "[renderd_config] [proce
611562
}
612563

613564
REQUIRE(exit_status == 0);
614-
REQUIRE_THAT(err_log_lines, Catch::Matchers::Contains("renderd: num_slave_threads = '4'"));
615565
REQUIRE_THAT(err_log_lines, Catch::Matchers::Contains("renderd(1): num_threads = '2'"));
616566
REQUIRE_THAT(err_log_lines, Catch::Matchers::Contains("renderd(2): num_threads = '2'"));
567+
REQUIRE_THAT(err_log_lines, Catch::Matchers::Contains("renderd: num_slave_threads = '4'"));
617568
}
618569

619570
SECTION("renderd.conf renderd section not using unix socketname", "should return 0") {

0 commit comments

Comments
 (0)