diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/meson.build | 29 | ||||
-rw-r--r-- | test/test_log.c | 121 |
2 files changed, 148 insertions, 2 deletions
diff --git a/test/meson.build b/test/meson.build index a2436b22..01f75b5a 100644 --- a/test/meson.build +++ b/test/meson.build @@ -127,6 +127,7 @@ unit_tests = [ 'caret', 'env', 'free_null', + 'log', 'node', 'overflow', 'reader', @@ -210,7 +211,7 @@ simple_command_tests = { if is_variable('serd_pipe') pipe_script_args = common_script_args + ['--tool', serd_pipe] serd_ttl = files('../serd.ttl')[0] - bad_input_file = files('extra/bad/bad-base.ttl') + bad_input_file = files('extra/bad/bad-lang.ttl') test('serd_ttl', serd_pipe, args: [serd_ttl], env: test_env, suite: 'data') @@ -250,7 +251,31 @@ if is_variable('serd_pipe') files('test_quiet.py'), args: pipe_script_args + [bad_input_file], env: test_env, - suite: cmd_suite, + suite: 'log', + ) + test( + 'CLICOLOR_FORCE', + serd_pipe, + args: bad_input_file, + env: test_env + ['CLICOLOR_FORCE=1'], + should_fail: true, + suite: 'log', + ) + test( + 'CLICOLOR', + serd_pipe, + args: bad_input_file, + env: test_env + ['CLICOLOR=0'], + should_fail: true, + suite: 'log', + ) + test( + 'NO_COLOR', + serd_pipe, + args: bad_input_file, + env: test_env + ['NO_COLOR=1'], + should_fail: true, + suite: 'log', ) # Inputs diff --git a/test/test_log.c b/test/test_log.c new file mode 100644 index 00000000..3ab2008f --- /dev/null +++ b/test/test_log.c @@ -0,0 +1,121 @@ +// Copyright 2021 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#undef NDEBUG + +#include "serd/caret.h" +#include "serd/log.h" +#include "serd/node.h" +#include "serd/status.h" +#include "serd/string_view.h" +#include "serd/world.h" + +#include <assert.h> +#include <stdbool.h> +#include <string.h> + +static SerdStatus +custom_log_func(void* const handle, + const SerdLogLevel level, + const size_t n_fields, + const SerdLogField* const fields, + const SerdStringView message) +{ + (void)message; + + bool* const called = (bool*)handle; + + assert(level == SERD_LOG_LEVEL_NOTICE); + assert(n_fields == 1); + assert(!strcmp(fields[0].key, "TEST_KEY")); + assert(!strcmp(fields[0].value, "TEST VALUE")); + assert(!strcmp(message.data, "test message 42")); + assert(message.length == strlen("test message 42")); + + *called = true; + return SERD_SUCCESS; +} + +static void +test_bad_arg(void) +{ + SerdWorld* const world = serd_world_new(); + bool called = false; + + serd_set_log_func(world, custom_log_func, &called); + + assert(serd_logf(world, SERD_LOG_LEVEL_ERROR, "%s", "") == SERD_BAD_ARG); + + serd_world_free(world); +} + +static void +test_default_log(void) +{ + SerdWorld* const world = serd_world_new(); + + for (unsigned i = 0; i <= SERD_LOG_LEVEL_DEBUG; ++i) { + const SerdLogLevel level = (SerdLogLevel)i; + + assert(!serd_logf(world, level, "test")); + } + + serd_world_free(world); +} + +static void +test_custom_log(void) +{ + SerdWorld* const world = serd_world_new(); + bool called = false; + + serd_set_log_func(world, custom_log_func, &called); + + const SerdLogField fields[1] = {{"TEST_KEY", "TEST VALUE"}}; + assert(!serd_xlogf( + world, SERD_LOG_LEVEL_NOTICE, 1, fields, "test message %d", 42)); + + assert(called); + serd_world_free(world); +} + +static void +test_caret(void) +{ + SerdWorld* const world = serd_world_new(); + SerdNode* const name = serd_new_string(serd_string("filename")); + SerdCaret* const caret = serd_caret_new(name, 46, 2); + + serd_logf_at(world, SERD_LOG_LEVEL_NOTICE, caret, "is just ahead of me"); + serd_logf_at(world, SERD_LOG_LEVEL_NOTICE, NULL, "isn't"); + + serd_caret_free(caret); + serd_node_free(name); + serd_world_free(world); +} + +static void +test_filename_only(void) +{ + SerdWorld* const world = serd_world_new(); + + const SerdLogField fields[3] = {{"TEST_KEY", "TEST VALUE"}, + {"SERD_FILE", "somename"}, + {"SERD_CHECK", "somecheck"}}; + + assert(!serd_xlogf(world, SERD_LOG_LEVEL_INFO, 3, fields, "no numbers here")); + + serd_world_free(world); +} + +int +main(void) +{ + test_bad_arg(); + test_default_log(); + test_custom_log(); + test_caret(); + test_filename_only(); + + return 0; +} |