diff options
author | David Robillard <d@drobilla.net> | 2020-08-11 17:31:21 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-27 13:13:59 +0100 |
commit | 9b8f18073e634da1a10cd03967eaef5597c415cc (patch) | |
tree | 7dbb9971867fc48f76aa2d90bf45df46a37ba8c4 /tests | |
parent | 44afc616ada1315bfcc1f2fb6bd82656ced8b2f5 (diff) | |
download | serd-9b8f18073e634da1a10cd03967eaef5597c415cc.tar.gz serd-9b8f18073e634da1a10cd03967eaef5597c415cc.tar.bz2 serd-9b8f18073e634da1a10cd03967eaef5597c415cc.zip |
Clean up Env tests and behaviour
Diffstat (limited to 'tests')
-rw-r--r-- | tests/env_test.c | 261 |
1 files changed, 184 insertions, 77 deletions
diff --git a/tests/env_test.c b/tests/env_test.c index cff8e3cf..1d8b612f 100644 --- a/tests/env_test.c +++ b/tests/env_test.c @@ -21,123 +21,230 @@ #include <assert.h> #include <string.h> +#define NS_EG "http://example.org/" + static SerdStatus count_prefixes(void* handle, const SerdEvent* event) { - if (event->type == SERD_PREFIX) { - ++*(int*)handle; - } + *(int*)handle += event->type == SERD_PREFIX; return SERD_SUCCESS; } static void -test_env(void) +test_null(void) { - SerdWorld* world = serd_world_new(); - - SerdNode* hello = serd_new_string("hello\""); - SerdNode* eg = serd_new_uri("http://example.org/"); - SerdNode* foo_u = serd_new_uri("http://example.org/foo"); - SerdNode* empty = serd_new_uri(""); - SerdNode* foo_c = serd_new_curie("eg.2:foo"); - SerdNode* b = serd_new_curie("invalid"); - SerdNode* pre = serd_new_curie("eg.2"); - SerdEnv* env = serd_env_new(NULL); - serd_env_set_prefix(env, pre, eg); - + // Accessors are tolerant to a NULL env for convenience assert(!serd_env_base_uri(NULL)); assert(!serd_env_expand(NULL, NULL)); assert(!serd_env_qualify(NULL, NULL)); + // Only null is equal to null + assert(serd_env_equals(NULL, NULL)); +} + +static void +test_base_uri(void) +{ + SerdEnv* const env = serd_env_new(NULL); + SerdNode* const empty = serd_new_uri(""); + SerdNode* const hello = serd_new_string("hello"); + SerdNode* const eg = serd_new_uri(NS_EG); + + // Test that invalid calls work as expected assert(!serd_env_base_uri(env)); - assert(serd_env_set_base_uri(env, NULL)); - assert(serd_env_set_base_uri(env, empty)); - assert(serd_env_set_base_uri(env, hello)); + assert(serd_env_set_base_uri(env, empty) == SERD_ERR_BAD_ARG); + assert(serd_env_set_base_uri(env, hello) == SERD_ERR_BAD_ARG); + assert(serd_env_set_base_uri(NULL, eg) == SERD_ERR_BAD_ARG); assert(!serd_env_base_uri(env)); - SerdNode* xnode = serd_env_expand(env, hello); - assert(!xnode); - - assert(!serd_env_expand(env, b)); - assert(!serd_env_expand(env, hello)); + // Set a valid base URI + assert(!serd_env_set_base_uri(env, eg)); + assert(serd_node_equals(serd_env_base_uri(env), eg)); - assert(serd_env_set_base_uri(NULL, eg)); - assert(serd_env_set_base_uri(env, NULL)); + // Reset the base URI + assert(!serd_env_set_base_uri(env, NULL)); + assert(!serd_env_base_uri(env)); + serd_node_free(eg); serd_node_free(hello); + serd_node_free(empty); + serd_env_free(env); +} + +static void +test_set_prefix(void) +{ + SerdEnv* const env = serd_env_new(NULL); + SerdNode* const name1 = serd_new_string("eg.1"); + SerdNode* const name2 = serd_new_string("eg.2"); + SerdNode* const eg = serd_new_uri(NS_EG); + SerdNode* const curie = serd_new_curie("invalid"); + SerdNode* const rel = serd_new_uri("rel"); + SerdNode* const base = serd_new_uri("http://example.org/"); + + // Test that invalid calls work as expected + assert(serd_env_set_prefix(env, NULL, NULL) == SERD_ERR_BAD_ARG); + assert(serd_env_set_prefix(env, NULL, eg) == SERD_ERR_BAD_ARG); + assert(serd_env_set_prefix(env, curie, eg) == SERD_ERR_BAD_ARG); + assert(serd_env_set_prefix(env, name1, curie) == SERD_ERR_BAD_ARG); + + // Set a valid prefix + assert(!serd_env_set_prefix(env, name1, eg)); + + // Test setting a prefix from a relative URI + assert(serd_env_set_prefix(env, name2, rel) == SERD_ERR_BAD_ARG); + assert(!serd_env_set_base_uri(env, base)); + assert(!serd_env_set_prefix(env, name2, rel)); + + // Test setting a prefix from strings + assert(!serd_env_set_prefix_from_strings(env, + "eg.3", + "http://example.org/three")); + + // Test that we ended up with the expected number of prefixes + size_t n_prefixes = 0; + SerdSink* count_prefixes_sink = serd_sink_new(&n_prefixes, NULL); + serd_sink_set_event_func(count_prefixes_sink, count_prefixes); + serd_env_write_prefixes(env, count_prefixes_sink); + serd_sink_free(count_prefixes_sink); + assert(n_prefixes == 3); + + serd_node_free(base); + serd_node_free(rel); + serd_node_free(curie); + serd_node_free(eg); + serd_node_free(name2); + serd_node_free(name1); + serd_env_free(env); +} - SerdNode* xu = serd_env_expand(env, foo_c); - assert(!strcmp(serd_node_string(xu), "http://example.org/foo")); - serd_node_free(xu); +static void +test_expand(void) +{ + SerdNode* const name = serd_new_string("eg.1"); + SerdNode* const eg = serd_new_uri(NS_EG); + SerdNode* const blank = serd_new_blank("b1"); + SerdNode* const rel = serd_new_uri("rel"); + SerdNode* const base = serd_new_uri("http://example.org/b/"); + SerdNode* const c1 = serd_new_curie("eg.1:foo"); + SerdNode* const c1_full = serd_new_uri("http://example.org/foo"); + SerdNode* const c2 = serd_new_curie("hm:what"); + SerdNode* const type = serd_new_uri("Type"); + SerdNode* const typed = serd_new_typed_literal("data", type); + SerdEnv* const env = serd_env_new(base); + + assert(!serd_env_set_prefix(env, name, eg)); + + assert(!serd_env_expand(env, name)); + assert(!serd_env_expand(env, blank)); - SerdNode* badpre = serd_new_curie("hm:what"); - SerdNode* xbadpre = serd_env_expand(env, badpre); - assert(!xbadpre); - serd_node_free(badpre); + // Expand CURIE + SerdNode* const c1_out = serd_env_expand(env, c1); + assert(serd_node_equals(c1_out, c1_full)); + serd_node_free(c1_out); - SerdNode* xc = serd_env_expand(env, foo_c); - assert(serd_node_equals(xc, foo_u)); - serd_node_free(xc); + // Expand relative URI + SerdNode* const rel_out = serd_env_expand(env, rel); + assert(!strcmp(serd_node_string(rel_out), "http://example.org/b/rel")); + serd_node_free(rel_out); - assert(serd_env_set_prefix(env, NULL, NULL)); + // Expand literal with URI datatype + SerdNode* const typed_out = serd_env_expand(env, typed); + assert(typed_out); + assert(!strcmp(serd_node_string(typed_out), "data")); + assert(serd_node_datatype(typed_out)); + assert(!strcmp(serd_node_string(serd_node_datatype(typed_out)), + "http://example.org/b/Type")); + serd_node_free(typed_out); - SerdNode* lit = serd_new_string("hello"); - assert(serd_env_set_prefix(env, b, lit)); + assert(!serd_env_expand(env, c2)); - SerdNode* blank = serd_new_blank("b1"); - assert(!serd_env_expand(env, blank)); + serd_env_free(env); + serd_node_free(typed); + serd_node_free(type); + serd_node_free(c2); + serd_node_free(c1_full); + serd_node_free(c1); + serd_node_free(base); + serd_node_free(rel); serd_node_free(blank); + serd_node_free(eg); + serd_node_free(name); +} - size_t n_prefixes = 0; - SerdSink* count_prefixes_sink = serd_sink_new(&n_prefixes, NULL); - 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); - assert(n_prefixes == 1); +static void +test_qualify(void) +{ + SerdNode* const name = serd_new_string("eg"); + SerdNode* const eg = serd_new_uri(NS_EG); + SerdNode* const u1 = serd_new_uri("http://example.org/foo"); + SerdNode* const c1 = serd_new_curie("eg:foo"); + SerdNode* const u2 = serd_new_uri("http://drobilla.net/bar"); + SerdEnv* const env = serd_env_new(NULL); + + assert(!serd_env_set_prefix(env, name, eg)); + + assert(!serd_env_expand(env, name)); + + SerdNode* const u1_out = serd_env_qualify(env, u1); + assert(serd_node_equals(u1_out, c1)); + serd_node_free(u1_out); - SerdNode* shorter_uri = serd_new_uri("urn:foo"); - assert(!serd_env_qualify(env, shorter_uri)); - serd_node_free(shorter_uri); + assert(!serd_env_qualify(env, u2)); - SerdNode* qualified = serd_env_qualify(env, foo_u); - assert(serd_node_equals(qualified, foo_c)); + serd_env_free(env); + serd_node_free(u2); + serd_node_free(c1); + serd_node_free(u1); + serd_node_free(eg); + serd_node_free(name); +} - SerdEnv* env_copy = serd_env_copy(env); - assert(serd_env_equals(env, env_copy)); - assert(!serd_env_equals(env, NULL)); - assert(!serd_env_equals(NULL, env)); +static void +test_equals(void) +{ + SerdNode* const base1 = serd_new_uri(NS_EG "b1/"); + SerdNode* const base2 = serd_new_uri(NS_EG "b2/"); + SerdEnv* const env1 = serd_env_new(base1); + SerdEnv* const env2 = serd_env_new(base2); + + assert(!serd_env_equals(env1, NULL)); + assert(!serd_env_equals(NULL, env1)); assert(serd_env_equals(NULL, NULL)); + assert(!serd_env_equals(env1, env2)); - SerdNode* qualified2 = serd_env_expand(env_copy, foo_u); - assert(serd_node_equals(qualified, foo_c)); - serd_node_free(qualified2); + serd_env_set_base_uri(env2, base1); + assert(serd_env_equals(env1, env2)); - serd_env_set_prefix_from_strings( - env_copy, "test", "http://example.org/test"); - assert(!serd_env_equals(env, env_copy)); + assert(!serd_env_set_prefix_from_strings(env1, "n1", NS_EG "n1")); + assert(!serd_env_equals(env1, env2)); + assert(!serd_env_set_prefix_from_strings(env2, "n1", NS_EG "othern1")); + assert(!serd_env_equals(env1, env2)); + assert(!serd_env_set_prefix_from_strings(env2, "n1", NS_EG "n1")); + assert(serd_env_equals(env1, env2)); - serd_env_set_prefix_from_strings(env, "test2", "http://example.org/test"); - assert(!serd_env_equals(env, env_copy)); + serd_env_set_base_uri(env2, base2); + assert(!serd_env_equals(env1, env2)); - serd_node_free(qualified); - serd_node_free(foo_c); - serd_node_free(empty); - serd_node_free(foo_u); - serd_node_free(lit); - serd_node_free(b); - serd_node_free(pre); - serd_node_free(eg); - serd_env_free(env_copy); + SerdEnv* const env3 = serd_env_copy(env2); + assert(serd_env_equals(env3, env2)); + serd_env_free(env3); - serd_env_free(env); - serd_world_free(world); + serd_node_free(base1); + serd_node_free(base2); + serd_env_free(env1); + serd_env_free(env2); } int main(void) { - test_env(); + test_null(); + test_base_uri(); + test_set_prefix(); + test_expand(); + test_qualify(); + test_equals(); return 0; } |