Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added unit tests for lot to stderr and stderr-full #812

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/libbluechi/test/log/bc_log_to_stderr_full_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "libbluechi/log/log.h"

LogLevel lvl = LOG_LEVEL_INFO;
const char *file = "testfile.c";
const char *line = "line10";
const char *func = "testfunc";
const char *msg = "log_message";

bool check_buffer(char buf[]) {
bool result = true;
if (!strstr(buf, log_level_to_string(lvl))) {
result = false;
fprintf(stderr, "FAILED: LogLevel not found in log entry");
}
if (!strstr(buf, file)) {
result = false;
fprintf(stderr, "FAILED: File location not found in log entry");
}
if (!strstr(buf, line)) {
result = false;
fprintf(stderr, "FAILED: Line location not found in log entry");
}
if (!strstr(buf, func)) {
result = false;
fprintf(stderr, "FAILED: Function name not found in log entry");
}
if (!strstr(buf, msg)) {
result = false;
fprintf(stderr, "FAILED: Message not found in log entry");
}

return result;
}

int main() {
bool result = true;

char buf[BUFSIZ] = { '\0' };

/* test case: log without data */

// set buffer to stderr
setbuf(stderr, buf);
bc_log_to_stderr_full_with_location(lvl, file, line, func, msg, NULL);
// avoid adding test logs to buffer
setbuf(stderr, NULL);
result = check_buffer(buf);
if (strstr(buf, "data=")) {
result = false;
fprintf(stderr, "FAILED: Data found in log entry, but none was expected");
}

/* test case: log with data */

// set buffer to stderr
setbuf(stderr, buf);
const char *data = "{data: this is it}";
bc_log_to_stderr_full_with_location(lvl, file, line, func, msg, data);
// avoid adding test logs to buffer
setbuf(stderr, NULL);
result = check_buffer(buf);
if (!strstr(buf, data)) {
result = false;
fprintf(stderr, "FAILED: Data not found in log entry, but was expected");
}

if (result) {
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
57 changes: 57 additions & 0 deletions src/libbluechi/test/log/bc_log_to_stderr_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "libbluechi/log/log.h"

LogLevel lvl = LOG_LEVEL_INFO;
const char *file = "testfile.c";
const char *line = "line10";
const char *func = "testfunc";
const char *msg = "log_message";

bool check_buffer(char buf[]) {
bool result = true;
if (!strstr(buf, log_level_to_string(lvl))) {
result = false;
fprintf(stderr, "FAILED: LogLevel not found in log entry");
}
if (strstr(buf, file)) {
result = false;
fprintf(stderr, "FAILED: File location found in log entry, but none expected");
}
if (strstr(buf, line)) {
result = false;
fprintf(stderr, "FAILED: Line location found in log entry, but none expected");
}
if (strstr(buf, func)) {
result = false;
fprintf(stderr, "FAILED: Function name found in log entry, but none expected");
}
if (!strstr(buf, msg)) {
result = false;
fprintf(stderr, "FAILED: Message not found in log entry");
}

return result;
}

int main() {
bool result = true;

char buf[BUFSIZ] = { '\0' };

// set buffer to stderr
setbuf(stderr, buf);
bc_log_to_stderr_with_location(lvl, file, line, func, msg, NULL);
// avoid adding test logs to buffer
setbuf(stderr, NULL);
result = check_buffer(buf);

if (result) {
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
6 changes: 4 additions & 2 deletions src/libbluechi/test/log/meson.build
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

common_src = [
'string_to_log_level_test',
'log_target_to_str_test',
'bc_log_init_test',
'bc_log_to_stderr_full_test',
'bc_log_to_stderr_test',
'log_target_to_str_test',
'string_to_log_level_test',
]

foreach src : common_src
Expand Down
Loading