aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-08-11 17:31:21 +0200
committerDavid Robillard <d@drobilla.net>2021-03-08 23:23:06 -0500
commitd71d5bf14845b552547905584954eab6b52ed18c (patch)
tree8cc96cce5df9afa4beaa894c18a9c585d00b9675
parent40a83074705976ea59f0e1a5d06a4e5c7705472c (diff)
downloadserd-d71d5bf14845b552547905584954eab6b52ed18c.tar.gz
serd-d71d5bf14845b552547905584954eab6b52ed18c.tar.bz2
serd-d71d5bf14845b552547905584954eab6b52ed18c.zip
Clean up Env tests and behaviour
-rw-r--r--src/env.c14
-rw-r--r--test/test_env.c240
2 files changed, 185 insertions, 69 deletions
diff --git a/src/env.c b/src/env.c
index 49d2efa2..2a11f287 100644
--- a/src/env.c
+++ b/src/env.c
@@ -184,15 +184,15 @@ serd_env_set_prefix(SerdEnv* env,
serd_env_add(env, name, uri);
} else if (!env->base_uri_node) {
return SERD_ERR_BAD_ARG;
- } else {
- // Resolve relative URI and create a new node and URI for it
- SerdNode* const abs_uri = serd_new_resolved_uri(uri, env->base_uri);
-
- // Set prefix to resolved (absolute) URI
- serd_env_add(env, name, serd_node_string_view(abs_uri));
- serd_node_free(abs_uri);
}
+ // Resolve relative URI and create a new node and URI for it
+ SerdNode* abs_uri = serd_new_resolved_uri(uri, env->base_uri);
+
+ // Set prefix to resolved (absolute) URI
+ serd_env_add(env, name, serd_node_string_view(abs_uri));
+ serd_node_free(abs_uri);
+
return SERD_SUCCESS;
}
diff --git a/test/test_env.c b/test/test_env.c
index 651bf8c9..445e9f28 100644
--- a/test/test_env.c
+++ b/test/test_env.c
@@ -21,117 +21,233 @@
#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)
{
- static const SerdStringView prefix = SERD_STATIC_STRING("eg.2");
- static const SerdStringView eg = SERD_STATIC_STRING("http://example.org/");
-
- SerdNode* hello = serd_new_string(SERD_STATIC_STRING("hello\""));
- SerdNode* foo_u = serd_new_uri(SERD_STATIC_STRING("http://example.org/foo"));
- SerdNode* empty = serd_new_uri(SERD_STATIC_STRING(""));
- SerdNode* foo_c = serd_new_curie(SERD_STATIC_STRING("eg.2:foo"));
- SerdNode* b = serd_new_curie(SERD_STATIC_STRING("invalid"));
- SerdEnv* env = serd_env_new(SERD_EMPTY_STRING());
-
- serd_env_set_prefix(env, prefix, eg);
+ SerdNode* const eg = serd_new_uri(SERD_STATIC_STRING(NS_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, foo_u));
+ assert(!serd_env_qualify(NULL, eg));
+
+ // Only null is equal to null
+ assert(serd_env_equals(NULL, NULL));
+
+ serd_node_free(eg);
+}
+static void
+test_base_uri(void)
+{
+ SerdEnv* const env = serd_env_new(SERD_EMPTY_STRING());
+ SerdNode* const empty = serd_new_uri(SERD_STATIC_STRING(""));
+ SerdNode* const hello = serd_new_string(SERD_STATIC_STRING("hello"));
+ SerdNode* const eg = serd_new_uri(SERD_STATIC_STRING(NS_EG));
+
+ // Test that invalid calls work as expected
assert(!serd_env_base_uri(env));
assert(!serd_env_set_base_uri(env, SERD_EMPTY_STRING()));
assert(!serd_env_base_uri(env));
+ // Try setting a relative prefix with no base URI
assert(serd_env_set_prefix(env,
SERD_STATIC_STRING("eg.3"),
SERD_STATIC_STRING("rel")) == SERD_ERR_BAD_ARG);
- 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, serd_node_string_view(eg)));
+ assert(serd_node_equals(serd_env_base_uri(env), eg));
+ // Reset the base URI
assert(!serd_env_set_base_uri(env, SERD_EMPTY_STRING()));
+ assert(!serd_env_base_uri(env));
+ serd_node_free(eg);
serd_node_free(hello);
+ serd_node_free(empty);
+ 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_set_prefix(void)
+{
+ static const SerdStringView eg = SERD_STATIC_STRING(NS_EG);
+ static const SerdStringView name1 = SERD_STATIC_STRING("eg.1");
+ static const SerdStringView name2 = SERD_STATIC_STRING("eg.2");
+ static const SerdStringView rel = SERD_STATIC_STRING("rel");
+ static const SerdStringView base = SERD_STATIC_STRING("http://example.org/");
- SerdNode* badpre = serd_new_curie(SERD_STATIC_STRING("hm:what"));
- SerdNode* xbadpre = serd_env_expand(env, badpre);
- assert(!xbadpre);
- serd_node_free(badpre);
+ SerdEnv* const env = serd_env_new(SERD_EMPTY_STRING());
- SerdNode* xc = serd_env_expand(env, foo_c);
- assert(serd_node_equals(xc, foo_u));
- serd_node_free(xc);
+ // Set a valid prefix
+ assert(!serd_env_set_prefix(env, name1, eg));
- SerdNode* blank = serd_new_blank(SERD_STATIC_STRING("b1"));
- assert(!serd_env_expand(env, blank));
- serd_node_free(blank);
+ // 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(env,
+ SERD_STATIC_STRING("eg.3"),
+ SERD_STATIC_STRING("http://example.org/three")));
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);
+ serd_sink_free(count_prefixes_sink);
+ assert(n_prefixes == 3);
- SerdNode* qualified = serd_env_qualify(env, foo_u);
- assert(serd_node_equals(qualified, foo_c));
+ serd_env_free(env);
+}
- 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));
- assert(serd_env_equals(NULL, NULL));
+static void
+test_expand(void)
+{
+ static const SerdStringView eg = SERD_STATIC_STRING(NS_EG);
+ static const SerdStringView type = SERD_STATIC_STRING("Type");
+ static const SerdStringView base =
+ SERD_STATIC_STRING("http://example.org/b/");
+
+ SerdNode* const name = serd_new_string(SERD_STATIC_STRING("eg.1"));
+ SerdNode* const blank = serd_new_blank(SERD_STATIC_STRING("b1"));
+ SerdNode* const rel = serd_new_uri(SERD_STATIC_STRING("rel"));
+ SerdNode* const c1 = serd_new_curie(SERD_STATIC_STRING("eg.1:foo"));
+ SerdNode* const c1_full =
+ serd_new_uri(SERD_STATIC_STRING("http://example.org/foo"));
+ SerdNode* const c2 = serd_new_curie(SERD_STATIC_STRING("hm:what"));
+ SerdNode* const typed =
+ serd_new_typed_literal(SERD_STATIC_STRING("data"), type);
+ SerdEnv* const env = serd_env_new(base);
+
+ assert(!serd_env_set_prefix(env, serd_node_string_view(name), eg));
+
+ assert(!serd_env_expand(env, name));
+ assert(!serd_env_expand(env, blank));
- SerdNode* qualified2 = serd_env_expand(env_copy, foo_u);
- assert(serd_node_equals(qualified, foo_c));
- serd_node_free(qualified2);
+ // Expand CURIE
+ SerdNode* const c1_out = serd_env_expand(env, c1);
+ assert(serd_node_equals(c1_out, c1_full));
+ serd_node_free(c1_out);
- serd_env_set_prefix(env_copy,
- SERD_STATIC_STRING("test"),
- SERD_STATIC_STRING("http://example.org/test"));
+ // 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_equals(env, env_copy));
+ // 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"));
- serd_env_set_prefix(env,
- SERD_STATIC_STRING("test2"),
- SERD_STATIC_STRING("http://example.org/test"));
+ const SerdNode* const datatype = serd_node_datatype(typed_out);
+ assert(datatype);
+ assert(!strcmp(serd_node_string(datatype), "http://example.org/b/Type"));
+ serd_node_free(typed_out);
- assert(!serd_env_equals(env, env_copy));
+ assert(!serd_env_expand(env, c2));
- serd_node_free(qualified);
- serd_sink_free(count_prefixes_sink);
- serd_node_free(foo_c);
- serd_node_free(empty);
- serd_node_free(foo_u);
- serd_node_free(b);
- serd_env_free(env_copy);
+ serd_env_free(env);
+ serd_node_free(typed);
+ serd_node_free(c2);
+ serd_node_free(c1_full);
+ serd_node_free(c1);
+ serd_node_free(rel);
+ serd_node_free(blank);
+ serd_node_free(name);
+}
+
+static void
+test_qualify(void)
+{
+ static const SerdStringView eg = SERD_STATIC_STRING(NS_EG);
+
+ SerdNode* const name = serd_new_string(SERD_STATIC_STRING("eg"));
+ SerdNode* const c1 = serd_new_curie(SERD_STATIC_STRING("eg:foo"));
+
+ SerdNode* const u1 =
+ serd_new_uri(SERD_STATIC_STRING("http://example.org/foo"));
+
+ SerdNode* const u2 =
+ serd_new_uri(SERD_STATIC_STRING("http://drobilla.net/bar"));
+
+ SerdEnv* const env = serd_env_new(SERD_EMPTY_STRING());
+
+ assert(!serd_env_set_prefix(env, serd_node_string_view(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);
+
+ assert(!serd_env_qualify(env, u2));
serd_env_free(env);
+ serd_node_free(u2);
+ serd_node_free(c1);
+ serd_node_free(u1);
+ serd_node_free(name);
+}
+
+static void
+test_equals(void)
+{
+ static const SerdStringView name1 = SERD_STATIC_STRING("n1");
+ static const SerdStringView base1 = SERD_STATIC_STRING(NS_EG "b1/");
+ static const SerdStringView base2 = SERD_STATIC_STRING(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));
+
+ serd_env_set_base_uri(env2, base1);
+ assert(serd_env_equals(env1, env2));
+
+ assert(!serd_env_set_prefix(env1, name1, SERD_STATIC_STRING(NS_EG "n1")));
+ assert(!serd_env_equals(env1, env2));
+ assert(
+ !serd_env_set_prefix(env2, name1, SERD_STATIC_STRING(NS_EG "othern1")));
+ assert(!serd_env_equals(env1, env2));
+ assert(!serd_env_set_prefix(env2, name1, SERD_STATIC_STRING(NS_EG "n1")));
+ assert(serd_env_equals(env1, env2));
+
+ serd_env_set_base_uri(env2, base2);
+ assert(!serd_env_equals(env1, env2));
+
+ SerdEnv* const env3 = serd_env_copy(env2);
+ assert(serd_env_equals(env3, env2));
+ serd_env_free(env3);
+
+ 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;
}