diff options
author | David Robillard <d@drobilla.net> | 2021-02-20 19:18:28 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:07 -0500 |
commit | d62c7dc45af3256c6cd70d12a11e91b5b872c7db (patch) | |
tree | 63f9f1cc8f8900ad59b5bd8f07b70e6008522d21 /test/test_sink.c | |
parent | ab1aa43256fac3e017212abe6f9d845bf74c024c (diff) | |
download | serd-d62c7dc45af3256c6cd70d12a11e91b5b872c7db.tar.gz serd-d62c7dc45af3256c6cd70d12a11e91b5b872c7db.tar.bz2 serd-d62c7dc45af3256c6cd70d12a11e91b5b872c7db.zip |
Replace multiple stream callbacks with SerdEvent
This makes plumbing easier since everything goes through the same "stream" and
only one callback is required to handling everything. It's also more easily
extensible in case more event types need to be added in the future.
Diffstat (limited to 'test/test_sink.c')
-rw-r--r-- | test/test_sink.c | 50 |
1 files changed, 43 insertions, 7 deletions
diff --git a/test/test_sink.c b/test/test_sink.c index 4b9a2eb2..b0756fcc 100644 --- a/test/test_sink.c +++ b/test/test_sink.c @@ -4,6 +4,7 @@ #undef NDEBUG #include "serd/env.h" +#include "serd/event.h" #include "serd/node.h" #include "serd/sink.h" #include "serd/statement.h" @@ -68,6 +69,24 @@ on_end(void* handle, const SerdNode* node) return state->return_status; } +static SerdStatus +on_event(void* const handle, const SerdEvent* const event) +{ + switch (event->type) { + case SERD_BASE: + return on_base(handle, event->base.uri); + case SERD_PREFIX: + return on_prefix(handle, event->prefix.name, event->prefix.uri); + case SERD_STATEMENT: + return on_statement( + handle, event->statement.flags, event->statement.statement); + case SERD_END: + return on_end(handle, event->end.node); + } + + return SERD_BAD_ARG; +} + static void test_callbacks(void) { @@ -81,23 +100,37 @@ test_callbacks(void) SerdStatement* const statement = serd_statement_new(base, uri, blank, NULL, NULL); + const SerdBaseEvent base_event = {SERD_BASE, uri}; + const SerdPrefixEvent prefix_event = {SERD_PREFIX, name, uri}; + const SerdStatementEvent statement_event = {SERD_STATEMENT, 0U, statement}; + const SerdEndEvent end_event = {SERD_END, blank}; + // Call functions on a sink with no functions set - SerdSink* null_sink = serd_sink_new(&state, NULL); + SerdSink* null_sink = serd_sink_new(&state, NULL, NULL); + assert(!serd_sink_write_base(null_sink, base)); assert(!serd_sink_write_prefix(null_sink, name, uri)); assert(!serd_sink_write_statement(null_sink, 0, statement)); assert(!serd_sink_write(null_sink, 0, base, uri, blank, NULL)); assert(!serd_sink_write_end(null_sink, blank)); + + SerdEvent event = {SERD_BASE}; + + event.base = base_event; + assert(!serd_sink_write_event(null_sink, &event)); + event.prefix = prefix_event; + assert(!serd_sink_write_event(null_sink, &event)); + event.statement = statement_event; + assert(!serd_sink_write_event(null_sink, &event)); + event.end = end_event; + assert(!serd_sink_write_event(null_sink, &event)); + serd_sink_free(null_sink); // Try again with a sink that has the event handler set - SerdSink* sink = serd_sink_new(&state, NULL); - serd_sink_set_base_func(sink, on_base); - serd_sink_set_prefix_func(sink, on_prefix); - serd_sink_set_statement_func(sink, on_statement); - serd_sink_set_end_func(sink, on_end); + SerdSink* sink = serd_sink_new(&state, on_event, NULL); assert(!serd_sink_write_base(sink, base)); assert(serd_node_equals(state.last_base, base)); @@ -112,6 +145,9 @@ test_callbacks(void) assert(!serd_sink_write_end(sink, blank)); assert(serd_node_equals(state.last_end, blank)); + const SerdEvent junk = {(SerdEventType)42}; + assert(serd_sink_write_event(sink, &junk) == SERD_BAD_ARG); + serd_sink_free(sink); serd_statement_free(statement); @@ -130,7 +166,7 @@ test_free(void) // Set up a sink with dynamically allocated data and a free function uintptr_t* data = (uintptr_t*)calloc(1, sizeof(uintptr_t)); - SerdSink* sink = serd_sink_new(data, free); + SerdSink* sink = serd_sink_new(data, NULL, free); // Free the sink, which should free the data (rely on valgrind or sanitizers) serd_sink_free(sink); |