From 7026fb72f85e349eea64a62bd924358c608520cc Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 30 May 2020 21:32:37 +0200 Subject: 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. --- tests/env_test.c | 10 +++++----- tests/read_chunk_test.c | 25 +++++++++++++++++++++---- tests/serd_test.c | 16 +++++++--------- tests/sink_test.c | 27 ++++++++++++++++++++++----- 4 files changed, 55 insertions(+), 23 deletions(-) (limited to 'tests') diff --git a/tests/env_test.c b/tests/env_test.c index ee172fff..cc04366d 100644 --- a/tests/env_test.c +++ b/tests/env_test.c @@ -22,12 +22,12 @@ #include static SerdStatus -count_prefixes(void* handle, const SerdNode* name, const SerdNode* uri) +count_prefixes(void* handle, const SerdEvent* event) { - (void)name; - (void)uri; + if (event->type == SERD_PREFIX) { + ++*(int*)handle; + } - ++*(int*)handle; return SERD_SUCCESS; } @@ -87,7 +87,7 @@ test_env(void) size_t n_prefixes = 0; SerdSink* count_prefixes_sink = serd_sink_new(&n_prefixes, NULL); - serd_sink_set_prefix_func(count_prefixes_sink, count_prefixes); + serd_sink_set_event_func(count_prefixes_sink, count_prefixes); serd_env_set_prefix(env, pre, eg); serd_env_write_prefixes(env, count_prefixes_sink); serd_sink_free(count_prefixes_sink); diff --git a/tests/read_chunk_test.c b/tests/read_chunk_test.c index 31b11603..dfa0c0ac 100644 --- a/tests/read_chunk_test.c +++ b/tests/read_chunk_test.c @@ -70,15 +70,31 @@ on_end(void* handle, const SerdNode* node) return SERD_SUCCESS; } +static SerdStatus +on_event(void* handle, const SerdEvent* 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_SUCCESS; +} + int main(void) { SerdWorld* world = serd_world_new(); SerdSink* sink = serd_sink_new(NULL, 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); + serd_sink_set_event_func(sink, on_event); SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0, sink, 4096); assert(reader); @@ -108,5 +124,6 @@ main(void) serd_reader_free(reader); serd_sink_free(sink); serd_world_free(world); + return 0; } diff --git a/tests/serd_test.c b/tests/serd_test.c index f7931364..16209c27 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -28,14 +28,12 @@ #define NS_XSD "http://www.w3.org/2001/XMLSchema#" static SerdStatus -count_statements(void* handle, - SerdStatementFlags flags, - const SerdStatement* statement) +count_statements(void* handle, const SerdEvent* event) { - (void)flags; - (void)statement; + if (event->type == SERD_STATEMENT) { + ++*(size_t*)handle; + } - ++*(size_t*)handle; return SERD_SUCCESS; } @@ -99,7 +97,7 @@ test_read_chunks(void) SerdSink* sink = serd_sink_new(&n_statements, NULL); assert(sink); - serd_sink_set_statement_func(sink, count_statements); + serd_sink_set_event_func(sink, count_statements); SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0, sink, 4096); assert(reader); @@ -219,7 +217,7 @@ test_read_string(void) SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0, sink, 4096); assert(reader); - serd_sink_set_statement_func(sink, count_statements); + serd_sink_set_event_func(sink, count_statements); // Test reading a string that ends exactly at the end of input (no newline) assert(!serd_reader_start_string( @@ -676,7 +674,7 @@ test_reader(const char* path) size_t n_statements = 0; SerdSink* sink = serd_sink_new(&n_statements, NULL); - serd_sink_set_statement_func(sink, count_statements); + serd_sink_set_event_func(sink, count_statements); SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0, sink, 4096); assert(reader); diff --git a/tests/sink_test.c b/tests/sink_test.c index 71981cdb..cde0050e 100644 --- a/tests/sink_test.c +++ b/tests/sink_test.c @@ -74,6 +74,25 @@ on_end(void* handle, const SerdNode* node) return state->return_status; } +static SerdStatus +on_event(void* handle, const SerdEvent* 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_SUCCESS; +} + int main(void) { @@ -99,22 +118,20 @@ main(void) assert(!serd_sink_write(sink, 0, base, uri, blank, NULL)); assert(!serd_sink_write_end(sink, blank)); - // Set functions and try again + // Set event handler and try again + + serd_sink_set_event_func(sink, on_event); - serd_sink_set_base_func(sink, on_base); assert(!serd_sink_write_base(sink, base)); assert(serd_node_equals(state.last_base, base)); - serd_sink_set_prefix_func(sink, on_prefix); assert(!serd_sink_write_prefix(sink, name, uri)); assert(serd_node_equals(state.last_name, name)); assert(serd_node_equals(state.last_namespace, uri)); - serd_sink_set_statement_func(sink, on_statement); assert(!serd_sink_write_statement(sink, 0, statement)); assert(serd_statement_equals(state.last_statement, statement)); - serd_sink_set_end_func(sink, on_end); assert(!serd_sink_write_end(sink, blank)); assert(serd_node_equals(state.last_end, blank)); -- cgit v1.2.1