aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-01-13 20:00:25 +0100
committerDavid Robillard <d@drobilla.net>2022-01-14 01:13:45 -0500
commit0825ceb561b2f52cfa253cb8bb0613896f903363 (patch)
tree461d338415debf2fa9b489b53db738fbf7f8ddd8 /test
parentaf81ace5f5a8f4bb0df93dd937395c65e92a5b6a (diff)
downloadserd-0825ceb561b2f52cfa253cb8bb0613896f903363.tar.gz
serd-0825ceb561b2f52cfa253cb8bb0613896f903363.tar.bz2
serd-0825ceb561b2f52cfa253cb8bb0613896f903363.zip
Add extensible logging API
Diffstat (limited to 'test')
-rw-r--r--test/meson.build19
-rw-r--r--test/test_log.c128
2 files changed, 147 insertions, 0 deletions
diff --git a/test/meson.build b/test/meson.build
index 222ec1bc..4d9d2b43 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -10,6 +10,7 @@ unit_tests = [
'caret',
'env',
'free_null',
+ 'log',
'node',
'nodes',
'overflow',
@@ -102,6 +103,24 @@ if get_option('utils')
suite: ['serdi', 'options'])
endforeach
+ test('ansi_clicolor_force',
+ serdi,
+ args: files('bad/bad-lang.ttl'),
+ env: test_env + ['CLICOLOR_FORCE=1'],
+ should_fail: true)
+
+ test('ansi_clicolor_off',
+ serdi,
+ args: files('bad/bad-lang.ttl'),
+ env: test_env + ['CLICOLOR=0'],
+ should_fail: true)
+
+ test('ansi_no_color',
+ serdi,
+ args: files('bad/bad-lang.ttl'),
+ env: test_env + ['NO_COLOR=1'],
+ should_fail: true)
+
test('none',
serdi,
env: test_env,
diff --git a/test/test_log.c b/test/test_log.c
new file mode 100644
index 00000000..88d14dc6
--- /dev/null
+++ b/test/test_log.c
@@ -0,0 +1,128 @@
+/*
+ Copyright 2021 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#undef NDEBUG
+
+#include "serd/serd.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.buf, "test message 42"));
+ assert(message.len == 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_ERR_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[2] = {{"TEST_KEY", "TEST VALUE"},
+ {"SERD_FILE", "somename"}};
+
+ assert(!serd_xlogf(world, SERD_LOG_LEVEL_INFO, 2, 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;
+}