[go: nahoru, domu]

Skip to content

Commit

Permalink
Factor out URI prefix in some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drobilla committed Jun 26, 2024
1 parent 6f97a18 commit 7d6ae82
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
17 changes: 8 additions & 9 deletions test/test_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdint.h>
#include <string.h>

#define NS_EG "http://example.org/"
#define USTR(s) ((const uint8_t*)(s))

static SerdStatus
Expand All @@ -24,12 +25,11 @@ count_prefixes(void* handle, const SerdNode* name, const SerdNode* uri)
static void
test_env(void)
{
SerdNode u = serd_node_from_string(SERD_URI, USTR("http://example.org/foo"));
SerdNode b = serd_node_from_string(SERD_CURIE, USTR("invalid"));
SerdNode c = serd_node_from_string(SERD_CURIE, USTR("eg.2:b"));
SerdNode u = serd_node_from_string(SERD_URI, USTR(NS_EG "foo"));
SerdNode b = serd_node_from_string(SERD_CURIE, USTR("invalid"));
SerdNode c = serd_node_from_string(SERD_CURIE, USTR("eg.2:b"));
SerdEnv* env = serd_env_new(NULL);
serd_env_set_prefix_from_strings(
env, USTR("eg.2"), USTR("http://example.org/"));
serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR(NS_EG ""));

assert(!serd_env_set_base_uri(env, NULL));
assert(serd_env_set_base_uri(env, &SERD_NODE_NULL));
Expand All @@ -49,15 +49,15 @@ test_env(void)
assert(serd_node_equals(&xnode, &SERD_NODE_NULL));

SerdNode xu = serd_env_expand_node(env, &u);
assert(!strcmp((const char*)xu.buf, "http://example.org/foo"));
assert(!strcmp((const char*)xu.buf, NS_EG "foo"));
serd_node_free(&xu);

SerdNode badpre = serd_node_from_string(SERD_CURIE, USTR("hm:what"));
SerdNode xbadpre = serd_env_expand_node(env, &badpre);
assert(serd_node_equals(&xbadpre, &SERD_NODE_NULL));

SerdNode xc = serd_env_expand_node(env, &c);
assert(!strcmp((const char*)xc.buf, "http://example.org/b"));
assert(!strcmp((const char*)xc.buf, NS_EG "b"));
serd_node_free(&xc);

assert(serd_env_set_prefix(env, &SERD_NODE_NULL, &SERD_NODE_NULL));
Expand All @@ -72,8 +72,7 @@ test_env(void)
assert(serd_node_equals(&xblank, &SERD_NODE_NULL));

int n_prefixes = 0;
serd_env_set_prefix_from_strings(
env, USTR("eg.2"), USTR("http://example.org/"));
serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR(NS_EG));
serd_env_foreach(env, count_prefixes, &n_prefixes);
assert(n_prefixes == 1);

Expand Down
36 changes: 20 additions & 16 deletions test/test_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <stdio.h>
#include <string.h>

#define NS_EG "http://example.org/"

#define USTR(s) ((const uint8_t*)(s))

static void
Expand All @@ -22,8 +24,8 @@ test_write_long_literal(void)

assert(writer);

SerdNode s = serd_node_from_string(SERD_URI, USTR("http://example.org/s"));
SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/p"));
SerdNode s = serd_node_from_string(SERD_URI, USTR(NS_EG "s"));
SerdNode p = serd_node_from_string(SERD_URI, USTR(NS_EG "p"));
SerdNode o =
serd_node_from_string(SERD_LITERAL, USTR("hello \"\"\"world\"\"\"!"));

Expand Down Expand Up @@ -52,16 +54,16 @@ test_write_nested_anon(void)

assert(writer);

SerdNode s0 = serd_node_from_string(SERD_URI, USTR("http://example.org/s0"));
SerdNode p0 = serd_node_from_string(SERD_URI, USTR("http://example.org/p0"));
SerdNode s0 = serd_node_from_string(SERD_URI, USTR(NS_EG "s0"));
SerdNode p0 = serd_node_from_string(SERD_URI, USTR(NS_EG "p0"));
SerdNode b0 = serd_node_from_string(SERD_BLANK, USTR("b0"));
SerdNode p1 = serd_node_from_string(SERD_URI, USTR("http://example.org/p1"));
SerdNode p1 = serd_node_from_string(SERD_URI, USTR(NS_EG "p1"));
SerdNode b1 = serd_node_from_string(SERD_BLANK, USTR("b1"));
SerdNode p2 = serd_node_from_string(SERD_URI, USTR("http://example.org/p2"));
SerdNode o2 = serd_node_from_string(SERD_URI, USTR("http://example.org/o2"));
SerdNode p3 = serd_node_from_string(SERD_URI, USTR("http://example.org/p3"));
SerdNode p4 = serd_node_from_string(SERD_URI, USTR("http://example.org/p4"));
SerdNode o4 = serd_node_from_string(SERD_URI, USTR("http://example.org/o4"));
SerdNode p2 = serd_node_from_string(SERD_URI, USTR(NS_EG "p2"));
SerdNode o2 = serd_node_from_string(SERD_URI, USTR(NS_EG "o2"));
SerdNode p3 = serd_node_from_string(SERD_URI, USTR(NS_EG "p3"));
SerdNode p4 = serd_node_from_string(SERD_URI, USTR(NS_EG "p4"));
SerdNode o4 = serd_node_from_string(SERD_URI, USTR(NS_EG "o4"));
SerdNode nil = serd_node_from_string(
SERD_URI, USTR("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"));

Expand Down Expand Up @@ -132,8 +134,8 @@ test_writer_cleanup(void)
SerdWriter* writer =
serd_writer_new(SERD_TURTLE, (SerdStyle)0U, env, NULL, null_sink, NULL);

SerdNode s = serd_node_from_string(SERD_URI, USTR("http://example.org/s"));
SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/p"));
SerdNode s = serd_node_from_string(SERD_URI, USTR(NS_EG "s"));
SerdNode p = serd_node_from_string(SERD_URI, USTR(NS_EG "p"));

char o_buf[12] = {'b', '0', '\0'};
SerdNode o = serd_node_from_string(SERD_BLANK, USTR(o_buf));
Expand Down Expand Up @@ -183,8 +185,8 @@ test_write_bad_anon_stack(void)
SerdWriter* writer =
serd_writer_new(SERD_TURTLE, (SerdStyle)0U, env, NULL, null_sink, NULL);

SerdNode s = serd_node_from_string(SERD_URI, USTR("http://example.org/s"));
SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/p"));
SerdNode s = serd_node_from_string(SERD_URI, USTR(NS_EG "s"));
SerdNode p = serd_node_from_string(SERD_URI, USTR(NS_EG "p"));
SerdNode b0 = serd_node_from_string(SERD_BLANK, USTR("b0"));
SerdNode b1 = serd_node_from_string(SERD_BLANK, USTR("b1"));
SerdNode b2 = serd_node_from_string(SERD_BLANK, USTR("b2"));
Expand Down Expand Up @@ -219,8 +221,8 @@ test_strict_write(void)

const uint8_t bad_str[] = {0xFF, 0x90, 'h', 'i', 0};

SerdNode s = serd_node_from_string(SERD_URI, USTR("http://example.org/s"));
SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/p"));
SerdNode s = serd_node_from_string(SERD_URI, USTR(NS_EG "s"));
SerdNode p = serd_node_from_string(SERD_URI, USTR(NS_EG "p"));

SerdNode bad_lit = serd_node_from_string(SERD_LITERAL, bad_str);
SerdNode bad_uri = serd_node_from_string(SERD_URI, bad_str);
Expand Down Expand Up @@ -278,3 +280,5 @@ main(void)

return 0;
}

#undef NS_EG

0 comments on commit 7d6ae82

Please sign in to comment.