diff options
author | David Robillard <d@drobilla.net> | 2018-05-11 18:11:44 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-05-27 18:23:15 +0200 |
commit | 4bc530b356ed4b390816669725e684d038e48aa0 (patch) | |
tree | 4a79ccbd7e3fa8dc53f85d93fab19d09f99c5c0d /tests | |
parent | 1f4990c7d2e297dc0252ff193e66339d8db38837 (diff) | |
download | serd-4bc530b356ed4b390816669725e684d038e48aa0.tar.gz serd-4bc530b356ed4b390816669725e684d038e48aa0.tar.bz2 serd-4bc530b356ed4b390816669725e684d038e48aa0.zip |
Clean up node construction API
Diffstat (limited to 'tests')
-rw-r--r-- | tests/serd_test.c | 162 |
1 files changed, 92 insertions, 70 deletions
diff --git a/tests/serd_test.c b/tests/serd_test.c index 9d203a15..01f1e60b 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -91,7 +91,7 @@ check_file_uri(const char* hostname, expected_path = path; } - SerdNode* node = serd_node_new_file_uri(path, hostname, 0, escape); + SerdNode* node = serd_node_new_file_uri(path, hostname, escape); const char* node_str = serd_node_get_string(node); char* out_hostname = NULL; char* out_path = serd_file_uri_parse(node_str, &out_hostname); @@ -113,6 +113,23 @@ check_file_uri(const char* hostname, return ret; } +static int +check_rel_uri(const char* uri, + const SerdNode* base, + const SerdNode* root, + const char* expected) +{ + SerdNode* rel = serd_node_new_relative_uri(uri, base, root); + const int ret = strcmp(serd_node_get_string(rel), expected); + serd_node_free(rel); + if (ret) { + FAILF("Bad relative URI `%s' (expected `%s')\n", + serd_node_get_string(rel), + expected); + } + return ret; +} + int main(void) { @@ -273,14 +290,14 @@ main(void) // Test serd_node_equals const uint8_t replacement_char_str[] = { 0xEF, 0xBF, 0xBD, 0 }; - SerdNode* lhs = serd_node_new_string(SERD_LITERAL, (const char*)replacement_char_str); - SerdNode* rhs = serd_node_new_string(SERD_LITERAL, "123"); + SerdNode* lhs = serd_node_new_string((const char*)replacement_char_str); + SerdNode* rhs = serd_node_new_string("123"); if (serd_node_equals(lhs, rhs)) { FAILF("%s == %s\n", serd_node_get_string(lhs), serd_node_get_string(rhs)); } - SerdNode* qnode = serd_node_new_string(SERD_CURIE, "foo:bar"); + SerdNode* qnode = serd_node_new_curie("foo:bar"); if (serd_node_equals(lhs, qnode)) { FAILF("%s == %s\n", serd_node_get_string(lhs), serd_node_get_string(qnode)); @@ -301,39 +318,17 @@ main(void) // Test serd_node_new_string - SerdNode* hello = serd_node_new_string(SERD_LITERAL, "hello\""); + SerdNode* hello = serd_node_new_string("hello\""); if (serd_node_get_length(hello) != 6 || serd_node_get_flags(hello) != SERD_HAS_QUOTE || strcmp(serd_node_get_string(hello), "hello\"")) { FAILF("Bad node %s\n", serd_node_get_string(hello)); } - if (serd_node_new_string(SERD_URI, NULL)) { + if (serd_node_new_string(NULL)) { FAIL("Successfully created node from NULL string\n"); } - // Test serd_node_new_substring - - if (serd_node_new_substring(SERD_LITERAL, NULL, 32)) { - FAIL("Successfully created node from NULL substring\n"); - } - - SerdNode* a_b = serd_node_new_substring(SERD_LITERAL, "a\"bc", 3); - if (serd_node_get_length(a_b) != 3 || - serd_node_get_flags(a_b) != SERD_HAS_QUOTE || - strncmp(serd_node_get_string(a_b), "a\"b", 3)) { - FAILF("Bad node %s\n", serd_node_get_string(a_b)); - } - - serd_node_free(a_b); - a_b = serd_node_new_substring(SERD_LITERAL, "a\"bc", 10); - if (serd_node_get_length(a_b) != 4 || - serd_node_get_flags(a_b) != SERD_HAS_QUOTE || - strncmp(serd_node_get_string(a_b), "a\"bc", 4)) { - FAILF("Bad node %s\n", serd_node_get_string(a_b)); - } - serd_node_free(a_b); - // Test serd_node_new_literal if (serd_node_new_literal(NULL, NULL, NULL)) { @@ -357,8 +352,7 @@ main(void) } serd_node_free(hello_l); - SerdNode* eg_Thing = - serd_node_new_uri_from_string("http://example.org/Thing", NULL, NULL); + SerdNode* eg_Thing = serd_node_new_uri("http://example.org/Thing"); SerdNode* hello_dt = serd_node_new_literal("hello_dt\"", eg_Thing, NULL); @@ -372,43 +366,71 @@ main(void) serd_node_free(hello_dt); serd_node_free(eg_Thing); - // Test serd_node_new_uri_from_string + // Test absolute URI creation - if (serd_node_new_uri_from_string(NULL, NULL, NULL)) { + if (serd_node_new_uri(NULL)) { FAIL("Successfully created NULL URI\n"); } - SerdURI base_uri; - SerdNode* base = serd_node_new_uri_from_string("http://example.org/", - NULL, &base_uri); - SerdNode* nil = serd_node_new_uri_from_string(NULL, &base_uri, NULL); - SerdNode* nil2 = serd_node_new_uri_from_string("", &base_uri, NULL); - if (serd_node_get_type(nil) != SERD_URI || - strcmp(serd_node_get_string(nil), serd_node_get_string(base)) || - serd_node_get_type(nil2) != SERD_URI || - strcmp(serd_node_get_string(nil2), serd_node_get_string(base))) { + SerdNode* not_a_uri = serd_node_new_string("hello"); + SerdNode* root = serd_node_new_uri("http://example.org/a/b/"); + SerdNode* base = serd_node_new_uri("http://example.org/a/b/c/"); + SerdNode* nil = serd_node_new_resolved_uri(NULL, base); + SerdNode* nil2 = serd_node_new_resolved_uri("", base); + if (serd_node_new_resolved_uri("", NULL)) { + FAIL("Successfully created URI resolved against NULL\n"); + } else if (serd_node_new_resolved_uri("", not_a_uri)) { + FAIL("Successfully created URI resolved against non-URI\n"); + } else if (serd_node_get_type(nil) != SERD_URI || + strcmp(serd_node_get_string(nil), serd_node_get_string(base)) || + serd_node_get_type(nil2) != SERD_URI || + strcmp(serd_node_get_string(nil2), serd_node_get_string(base))) { FAILF("URI %s != base %s\n", serd_node_get_string(nil), serd_node_get_string(base)); } - // Test serd_node_new_relative_uri - SerdNode* abs = serd_node_new_string(SERD_URI, "http://example.org/foo/bar"); - SerdURI abs_uri; - serd_uri_parse(serd_node_get_string(abs), &abs_uri); + // Test relative URI creation + + if (check_rel_uri("http://example.org/a/b/c/foo", base, NULL, "foo") || + check_rel_uri("http://example.org/a/", base, NULL, "../../") || + check_rel_uri("http://example.org/a/", base, root, + "http://example.org/a/") || + check_rel_uri("http://example.org/", base, NULL, + "http://example.org/") || + check_rel_uri("http://drobilla.net/a", base, NULL, + "http://drobilla.net/a")) { + return 1; + } + + // Test URI resolution + + if (serd_node_resolve(NULL, base)) { + FAIL("Successfully resolved null URI\n"); + } else if (serd_node_resolve(nil, NULL)) { + FAIL("Successfully resolved against null URI\n"); + } else if (serd_node_resolve(not_a_uri, base)) { + FAIL("Successfully resolved a non-URI\n"); + } else if (serd_node_resolve(nil, not_a_uri)) { + FAIL("Successfully resolved against a non-URI\n"); + } - SerdURI rel_uri; - SerdNode* rel = serd_node_new_relative_uri(&abs_uri, &base_uri, NULL, &rel_uri); - if (strcmp(serd_node_get_string(rel), "/foo/bar")) { - FAILF("Bad relative URI %s (expected '/foo/bar')\n", - serd_node_get_string(rel)); + SerdNode* rel = serd_node_new_relative_uri( + "http://example.org/a/b/c/foo", base, NULL); + SerdNode* resolved = serd_node_resolve(rel, base); + if (strcmp(serd_node_get_string(resolved), + "http://example.org/a/b/c/foo")) { + FAILF("Bad resolved URI %s (expected 'http://example.org/a/b/c/foo'\n", + serd_node_get_string(resolved)); } - serd_node_free(abs); serd_node_free(nil); serd_node_free(nil2); + serd_node_free(not_a_uri); + serd_node_free(resolved); serd_node_free(rel); serd_node_free(base); + serd_node_free(root); // Test serd_node_new_blank @@ -428,13 +450,13 @@ main(void) SerdWorld* world = serd_world_new(); - SerdNode* u = serd_node_new_string(SERD_URI, "http://example.org/foo"); - SerdNode* b = serd_node_new_string(SERD_CURIE, "invalid"); - SerdNode* c = serd_node_new_string(SERD_CURIE, "eg.2:b"); + SerdNode* u = serd_node_new_uri("http://example.org/foo"); + SerdNode* b = serd_node_new_curie("invalid"); + SerdNode* c = serd_node_new_curie("eg.2:b"); SerdEnv* env = serd_env_new(NULL); serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/"); - if (serd_env_get_base_uri(env, NULL)) { + if (serd_env_get_base_uri(env)) { FAIL("Unexpected initial base URI\n"); } @@ -446,7 +468,7 @@ main(void) FAILF("Set base URI to %s\n", serd_node_get_string(hello)); } - if (serd_env_get_base_uri(env, NULL)) { + if (serd_env_get_base_uri(env)) { FAIL("Unexpected base URI\n"); } @@ -468,7 +490,7 @@ main(void) } serd_node_free(xu); - SerdNode* badpre = serd_node_new_string(SERD_CURIE, "hm:what"); + SerdNode* badpre = serd_node_new_curie("hm:what"); SerdNode* xbadpre = serd_env_expand_node(env, badpre); if (xbadpre) { FAILF("Expanded invalid curie %s\n", serd_node_get_string(badpre)); @@ -486,7 +508,7 @@ main(void) FAIL("Set NULL prefix\n"); } - SerdNode* lit = serd_node_new_string(SERD_LITERAL, "hello"); + SerdNode* lit = serd_node_new_string("hello"); if (!serd_env_set_prefix(env, b, lit)) { FAIL("Set prefix to literal\n"); } @@ -498,7 +520,7 @@ main(void) FAILF("Bad prefix count %d\n", n_prefixes); } - SerdNode* shorter_uri = serd_node_new_string(SERD_URI, "urn:foo"); + SerdNode* shorter_uri = serd_node_new_uri("urn:foo"); const SerdNode* prefix_name; if (serd_env_qualify(env, shorter_uri, &prefix_name, &suffix)) { FAILF("Qualified %s\n", serd_node_get_string(shorter_uri)); @@ -517,7 +539,7 @@ main(void) } SerdWriter* writer = serd_writer_new( - world, SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd); + world, SERD_TURTLE, (SerdStyle)0, env, serd_file_sink, fd); if (!writer) { FAIL("Failed to create writer\n"); } @@ -538,9 +560,9 @@ main(void) } uint8_t buf[] = { 0xEF, 0xBF, 0xBD, 0 }; - SerdNode* s = serd_node_new_string(SERD_URI, ""); - SerdNode* p = serd_node_new_string(SERD_URI, "http://example.org/pred"); - SerdNode* o = serd_node_new_string(SERD_LITERAL, (char*)buf); + SerdNode* s = serd_node_new_uri(""); + SerdNode* p = serd_node_new_uri("http://example.org/pred"); + SerdNode* o = serd_node_new_string((char*)buf); // Write 3 invalid statements (should write nothing) const SerdNode* junk[][5] = { { s, p, NULL }, @@ -561,7 +583,7 @@ main(void) } } - SerdNode* urn_Type = serd_node_new_uri_from_string("urn:Type", NULL, NULL); + SerdNode* urn_Type = serd_node_new_uri("urn:Type"); SerdNode* t = serd_node_new_literal((char*)buf, urn_Type, NULL); SerdNode* l = serd_node_new_literal((char*)buf, NULL, "en"); @@ -584,8 +606,8 @@ main(void) // Write statements with bad UTF-8 (should be replaced) const char bad_str[] = { (char)0xFF, (char)0x90, 'h', 'i', 0 }; - SerdNode* bad_lit = serd_node_new_string(SERD_LITERAL, bad_str); - SerdNode* bad_uri = serd_node_new_string(SERD_URI, bad_str); + SerdNode* bad_lit = serd_node_new_string(bad_str); + SerdNode* bad_uri = serd_node_new_uri(bad_str); if (iface->statement(iface->handle, 0, NULL, s, p, bad_lit)) { FAIL("Failed to write junk UTF-8 literal\n"); } else if (iface->statement(iface->handle, 0, NULL, s, p, bad_uri)) { @@ -596,7 +618,7 @@ main(void) // Write 1 valid statement serd_node_free(o); - o = serd_node_new_string(SERD_LITERAL, "hello"); + o = serd_node_new_string("hello"); if (iface->statement(iface->handle, 0, NULL, s, p, o)) { FAIL("Failed to write valid statement\n"); } @@ -613,9 +635,9 @@ main(void) // Test buffer sink SerdBuffer buffer = { NULL, 0 }; writer = serd_writer_new( - world, SERD_TURTLE, (SerdStyle)0, env, NULL, serd_buffer_sink, &buffer); + world, SERD_TURTLE, (SerdStyle)0, env, serd_buffer_sink, &buffer); - o = serd_node_new_string(SERD_URI, "http://example.org/base"); + o = serd_node_new_uri("http://example.org/base"); if (serd_writer_set_base_uri(writer, o)) { FAIL("Failed to write to chunk sink\n"); } @@ -640,7 +662,7 @@ main(void) FAIL("Failed to create reader\n"); } - SerdNode* g = serd_node_new_string(SERD_URI, "http://example.org/"); + SerdNode* g = serd_node_new_uri("http://example.org/"); serd_reader_set_default_graph(reader, g); serd_reader_add_blank_prefix(reader, "tmp"); serd_reader_add_blank_prefix(reader, NULL); |