diff options
author | David Robillard <d@drobilla.net> | 2021-02-20 19:18:28 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-03-08 23:23:05 -0500 |
commit | b983d2a94b0f84b4c06889be77fbdacc502f2d7b (patch) | |
tree | 8fa3a46d95fc6b08aa323797436aa5582522b1cd /test | |
parent | 846553f8894264d64cdd1dfb08df56fe08189dac (diff) | |
download | serd-b983d2a94b0f84b4c06889be77fbdacc502f2d7b.tar.gz serd-b983d2a94b0f84b4c06889be77fbdacc502f2d7b.tar.bz2 serd-b983d2a94b0f84b4c06889be77fbdacc502f2d7b.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')
-rw-r--r-- | test/test_env.c | 24 | ||||
-rw-r--r-- | test/test_read_chunk.c | 25 | ||||
-rw-r--r-- | test/test_reader_writer.c | 45 |
3 files changed, 53 insertions, 41 deletions
diff --git a/test/test_env.c b/test/test_env.c index 3e38f71d..da53df45 100644 --- a/test/test_env.c +++ b/test/test_env.c @@ -22,20 +22,21 @@ #include <string.h> 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; } static void test_env(void) { + static const SerdStringView eg = SERD_STATIC_STRING("http://example.org/"); + SerdNode* hello = serd_new_string(SERD_STATIC_STRING("hello\"")); - SerdNode* eg = serd_new_uri(SERD_STATIC_STRING("http://example.org/")); SerdNode* foo_u = serd_new_uri(SERD_STATIC_STRING("http://example.org/foo")); SerdNode* foo_c = serd_new_curie(SERD_STATIC_STRING("eg.2:foo")); SerdNode* b = serd_new_curie(SERD_STATIC_STRING("invalid")); @@ -43,7 +44,7 @@ test_env(void) const SerdStringView prefix = SERD_STATIC_STRING("eg.2"); SerdEnv* env = serd_env_new(SERD_EMPTY_STRING()); - serd_env_set_prefix(env, prefix, serd_node_string_view(eg)); + serd_env_set_prefix(env, prefix, eg); assert(!serd_env_base_uri(env)); assert(!serd_env_set_base_uri(env, SERD_EMPTY_STRING())); @@ -81,19 +82,22 @@ test_env(void) assert(!serd_env_expand(env, blank)); serd_node_free(blank); - int n_prefixes = 0; - serd_env_set_prefix(env, prefix, serd_node_string_view(eg)); - serd_env_foreach(env, count_prefixes, &n_prefixes); + size_t n_prefixes = 0; + SerdSink* const count_prefixes_sink = + serd_sink_new(&n_prefixes, count_prefixes, NULL); + + serd_env_set_prefix(env, prefix, eg); + serd_env_write_prefixes(env, count_prefixes_sink); assert(n_prefixes == 1); SerdNode* qualified = serd_env_qualify(env, foo_u); assert(serd_node_equals(qualified, foo_c)); serd_node_free(qualified); + serd_sink_free(count_prefixes_sink); serd_node_free(foo_c); serd_node_free(foo_u); serd_node_free(b); - serd_node_free(eg); serd_env_free(env); } diff --git a/test/test_read_chunk.c b/test/test_read_chunk.c index d77c3398..0f24089d 100644 --- a/test/test_read_chunk.c +++ b/test/test_read_chunk.c @@ -70,15 +70,29 @@ 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); + SerdSink* sink = serd_sink_new(NULL, on_event, NULL); SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); assert(reader); @@ -109,5 +123,6 @@ main(void) serd_reader_free(reader); serd_sink_free(sink); serd_world_free(world); + return 0; } diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 9ccfa967..b43337f5 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -26,14 +26,11 @@ #include <string.h> static SerdStatus -test_sink(void* handle, - SerdStatementFlags flags, - const SerdStatement* statement) +count_statements(void* handle, const SerdEvent* event) { - (void)flags; - (void)statement; - - ++*(size_t*)handle; + if (event->type == SERD_STATEMENT) { + ++*(size_t*)handle; + } return SERD_SUCCESS; } @@ -72,13 +69,12 @@ test_read_chunks(void) size_t n_statements = 0; FILE* const f = tmpfile(); static const char null = 0; - SerdSink* sink = serd_sink_new(&n_statements, NULL); - SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); - assert(reader); + SerdSink* const sink = serd_sink_new(&n_statements, count_statements, NULL); assert(sink); - assert(f); - serd_sink_set_statement_func(sink, test_sink); + + SerdReader* const reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); + assert(reader); SerdStatus st = serd_reader_start_stream( reader, (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, f, NULL, 1); @@ -148,17 +144,14 @@ test_get_blank(void) static void test_read_string(void) { - SerdWorld* world = serd_world_new(); - size_t n_statements = 0; - SerdSink* sink = serd_sink_new(&n_statements, NULL); - SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); + SerdWorld* world = serd_world_new(); + size_t n_statements = 0; - assert(reader); + SerdSink* sink = serd_sink_new(&n_statements, count_statements, NULL); assert(sink); - serd_sink_set_statement_func(sink, test_sink); - - serd_sink_set_statement_func(sink, test_sink); + SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); + assert(reader); // Test reading a string that ends exactly at the end of input (no newline) assert( @@ -281,14 +274,14 @@ test_writer(const char* const path) static void test_reader(const char* path) { - SerdWorld* world = serd_world_new(); - size_t n_statements = 0; - SerdSink* const sink = serd_sink_new(&n_statements, NULL); - SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); + SerdWorld* world = serd_world_new(); + size_t n_statements = 0; - assert(reader); + SerdSink* const sink = serd_sink_new(&n_statements, count_statements, NULL); assert(sink); - serd_sink_set_statement_func(sink, test_sink); + + SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); + assert(reader); serd_reader_add_blank_prefix(reader, "tmp"); |