| 1 | /* |
|---|
| 2 | * Copyright 2008 Digital Bazaar, Inc. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of librdfa. |
|---|
| 5 | * |
|---|
| 6 | * librdfa is Free Software, and can be licensed under any of the |
|---|
| 7 | * following three licenses: |
|---|
| 8 | * |
|---|
| 9 | * 1. GNU Lesser General Public License (LGPL) V2.1 or any |
|---|
| 10 | * newer version |
|---|
| 11 | * 2. GNU General Public License (GPL) V2 or any newer version |
|---|
| 12 | * 3. Apache License, V2.0 or any newer version |
|---|
| 13 | * |
|---|
| 14 | * You may not use this file except in compliance with at least one of |
|---|
| 15 | * the above three licenses. |
|---|
| 16 | * |
|---|
| 17 | * See LICENSE-* at the top of this software distribution for more |
|---|
| 18 | * information regarding the details of each license. |
|---|
| 19 | * |
|---|
| 20 | * This unit test exercises the CURIE processing functions in librdfa. |
|---|
| 21 | * |
|---|
| 22 | * @author Manu Sporny |
|---|
| 23 | */ |
|---|
| 24 | #include <stdio.h> |
|---|
| 25 | #include <string.h> |
|---|
| 26 | #include <rdfa.h> |
|---|
| 27 | #include <rdfa_utils.h> |
|---|
| 28 | |
|---|
| 29 | #define XMLNS_DEFAULT_MAPPING "XMLNS_DEFAULT" |
|---|
| 30 | |
|---|
| 31 | // These are all of the @rel/@rev reserved words in XHTML 1.1 that |
|---|
| 32 | // should generate triples. |
|---|
| 33 | #define XHTML_RELREV_RESERVED_WORDS_SIZE 22 |
|---|
| 34 | static const char* |
|---|
| 35 | my_g_relrev_reserved_words[XHTML_RELREV_RESERVED_WORDS_SIZE] = |
|---|
| 36 | { |
|---|
| 37 | "alternate", "appendix", "bookmark", "chapter", "cite", "contents", |
|---|
| 38 | "copyright", "glossary", "help", "icon", "index", "meta", "next", "p3pv1", |
|---|
| 39 | "prev", "role", "section", "subsection", "start", "license", "up", "last" |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | // The base XHTML vocab URL is used to resolve URIs that are reserved |
|---|
| 43 | // words. Any reserved listed above is appended to the URL below to |
|---|
| 44 | // form a complete IRI. |
|---|
| 45 | #define XHTML_VOCAB_URI "http://www.w3.org/1999/xhtml/vocab#" |
|---|
| 46 | #define XHTML_VOCAB_URI_SIZE 35 |
|---|
| 47 | |
|---|
| 48 | // we need this declaration to compile cleanly, we shouldn't be |
|---|
| 49 | // calling it directly, but since this is a unit test, that's okay. |
|---|
| 50 | void rdfa_init_context(rdfacontext* context); |
|---|
| 51 | char* rdfa_resolve_relrev_curie(rdfacontext* context, const char* uri); |
|---|
| 52 | char* rdfa_resolve_property_curie(rdfacontext* context, const char* uri); |
|---|
| 53 | |
|---|
| 54 | // typedef for 3 argument CURIE processing function pointer |
|---|
| 55 | typedef char* (*curie_func_three_arg)(rdfacontext*, const char*, curieparse_t); |
|---|
| 56 | |
|---|
| 57 | // typedef for 2 argument CURIE processing function pointer |
|---|
| 58 | typedef char* (*curie_func_two_arg)(rdfacontext*, const char*); |
|---|
| 59 | |
|---|
| 60 | // typedef for CURIE list processing function pointer |
|---|
| 61 | typedef rdfalist* (*curie_list_func)(rdfacontext*, const char*, curieparse_t); |
|---|
| 62 | |
|---|
| 63 | // the number of tests run |
|---|
| 64 | int g_test_num = 0; |
|---|
| 65 | |
|---|
| 66 | // the number of tests that passed |
|---|
| 67 | int g_test_passes = 0; |
|---|
| 68 | |
|---|
| 69 | // the number of tests that failed |
|---|
| 70 | int g_test_fails = 0; |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * Runs a single unit test given the RDFa context, name of the test, |
|---|
| 74 | * test CURIE, processing function and what the final IRI should be. |
|---|
| 75 | * |
|---|
| 76 | * @param context the RDFa context. |
|---|
| 77 | * @param name the name of the test. |
|---|
| 78 | * @param curie the CURIE to resolve. |
|---|
| 79 | * @param cb the function callback to the CURIE resolution function. |
|---|
| 80 | * @param iri the value of what the resulting IRI should be. |
|---|
| 81 | */ |
|---|
| 82 | void run_test(rdfacontext* context, const char* name, const char* curie, |
|---|
| 83 | curie_func_three_arg cb, const char* iri, curieparse_t mode) |
|---|
| 84 | { |
|---|
| 85 | char* result = cb(context, curie, mode); |
|---|
| 86 | int compare = -1; |
|---|
| 87 | |
|---|
| 88 | // check to see if we should check for NULL or if the strings |
|---|
| 89 | // should match. |
|---|
| 90 | if(iri != NULL) |
|---|
| 91 | { |
|---|
| 92 | compare = strcmp(result, iri); |
|---|
| 93 | } |
|---|
| 94 | else if(iri == result) |
|---|
| 95 | { |
|---|
| 96 | compare = 0; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | printf("UT#%02i/%s \"%s\" ...", ++g_test_num, name, curie); |
|---|
| 100 | |
|---|
| 101 | // if the string compare shows identical values, pass the test, |
|---|
| 102 | // otherwise, fail the test. |
|---|
| 103 | if(compare == 0) |
|---|
| 104 | { |
|---|
| 105 | printf("PASS.\n"); |
|---|
| 106 | g_test_passes++; |
|---|
| 107 | } |
|---|
| 108 | else |
|---|
| 109 | { |
|---|
| 110 | printf("FAIL. Got \"%s\", but should have been \"%s\".\n", result, iri); |
|---|
| 111 | g_test_fails++; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | if(result != NULL) |
|---|
| 115 | { |
|---|
| 116 | free(result); |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * Runs a single unit test given the RDFa context, name of the test, |
|---|
| 122 | * a list of test CURIEs, processing function, and a list of output IRIs. |
|---|
| 123 | * |
|---|
| 124 | * @param context the RDFa context. |
|---|
| 125 | * @param name the name of the test. |
|---|
| 126 | * @param curie the CURIE to resolve. |
|---|
| 127 | * @param cb the function callback to the CURIE resolution function. |
|---|
| 128 | * @param iris a list of output IRIs. |
|---|
| 129 | */ |
|---|
| 130 | void run_list_test(rdfacontext* context, const char* name, const char* curies, |
|---|
| 131 | curie_list_func cb, rdfalist* iris, curieparse_t mode) |
|---|
| 132 | { |
|---|
| 133 | rdfalist* result = cb(context, curies, mode); |
|---|
| 134 | int compare = -1; |
|---|
| 135 | |
|---|
| 136 | if(result != NULL) |
|---|
| 137 | { |
|---|
| 138 | compare = -1; |
|---|
| 139 | int i; |
|---|
| 140 | rdfalistitem** iptr = iris->items; |
|---|
| 141 | rdfalistitem** rptr = result->items; |
|---|
| 142 | for(i = 0; i < result->num_items; i++) |
|---|
| 143 | { |
|---|
| 144 | compare = 0; |
|---|
| 145 | char* icurie = (*iptr)->data; |
|---|
| 146 | char* rcurie = (*rptr)->data; |
|---|
| 147 | |
|---|
| 148 | //printf("curie list: %s == %s\n", icurie, rcurie); |
|---|
| 149 | if(strcmp(icurie, rcurie) != 0) |
|---|
| 150 | { |
|---|
| 151 | compare = -1; |
|---|
| 152 | } |
|---|
| 153 | iptr++; |
|---|
| 154 | rptr++; |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | printf("UT#%02i/%s \"%s\" ...", ++g_test_num, name, curies); |
|---|
| 159 | |
|---|
| 160 | // if the string compare shows identical values, pass the test, |
|---|
| 161 | // otherwise, fail the test. |
|---|
| 162 | if(compare == 0) |
|---|
| 163 | { |
|---|
| 164 | printf("PASS.\n"); |
|---|
| 165 | g_test_passes++; |
|---|
| 166 | } |
|---|
| 167 | else |
|---|
| 168 | { |
|---|
| 169 | printf("FAIL."); |
|---|
| 170 | |
|---|
| 171 | g_test_fails++; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | if(result != NULL) |
|---|
| 175 | { |
|---|
| 176 | rdfa_free_list(result); |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | * Runs a set of unit tests given the RDFa context, base name of the test, |
|---|
| 182 | * a set of CURIEs, a processing function, and a base IRI. |
|---|
| 183 | * |
|---|
| 184 | * @param context the RDFa context. |
|---|
| 185 | * @param name the base name of the test. |
|---|
| 186 | * @param curie the set of CURIEs to resolve. |
|---|
| 187 | * @param cb the function callback to the CURIE resolution function. |
|---|
| 188 | * @param iri the base value of what the resulting IRI should be, the |
|---|
| 189 | * value of each set member will be appended to the IRI. |
|---|
| 190 | */ |
|---|
| 191 | void run_test_set(rdfacontext* context, const char* name, const char** curies, |
|---|
| 192 | size_t curies_size, curie_func_two_arg cb, const char* iri, curieparse_t mode) |
|---|
| 193 | { |
|---|
| 194 | int i; |
|---|
| 195 | for(i = 0; i < curies_size; i++) |
|---|
| 196 | { |
|---|
| 197 | char* full_iri = rdfa_join_string(iri, curies[i]); |
|---|
| 198 | char* result = cb(context, curies[i]); |
|---|
| 199 | int compare = -1; |
|---|
| 200 | |
|---|
| 201 | // check to see if we should check for NULL or if the strings |
|---|
| 202 | // should match. |
|---|
| 203 | if(iri != NULL) |
|---|
| 204 | { |
|---|
| 205 | compare = strcmp(result, full_iri); |
|---|
| 206 | } |
|---|
| 207 | else if(iri == result) |
|---|
| 208 | { |
|---|
| 209 | compare = 0; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | printf("UT#%02i/%s \"%s\" ...", ++g_test_num, name, full_iri); |
|---|
| 213 | |
|---|
| 214 | // if the string compare shows identical values, pass the test, |
|---|
| 215 | // otherwise, fail the test. |
|---|
| 216 | if(compare == 0) |
|---|
| 217 | { |
|---|
| 218 | printf("PASS.\n"); |
|---|
| 219 | g_test_passes++; |
|---|
| 220 | } |
|---|
| 221 | else |
|---|
| 222 | { |
|---|
| 223 | printf("FAIL. Got \"%s\", but should have been \"%s\".\n", result, iri); |
|---|
| 224 | g_test_fails++; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | free(result); |
|---|
| 228 | free(full_iri); |
|---|
| 229 | // char* full_iri = rdfa_join_string(iri, curies[i]); |
|---|
| 230 | |
|---|
| 231 | // run_test(context, name, curies[i], cb, full_iri, mode); |
|---|
| 232 | |
|---|
| 233 | // free(full_iri); |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | void run_curie_tests() |
|---|
| 238 | { |
|---|
| 239 | rdfacontext* context = |
|---|
| 240 | rdfa_create_context("http://example.org/"); |
|---|
| 241 | |
|---|
| 242 | rdfa_init_context(context); |
|---|
| 243 | |
|---|
| 244 | rdfa_update_mapping( |
|---|
| 245 | context->uri_mappings, "dc", "http://purl.org/dc/elements/1.1/"); |
|---|
| 246 | rdfa_update_mapping( |
|---|
| 247 | context->uri_mappings, "dctv", "http://purl.org/dc/dcmitype/"); |
|---|
| 248 | |
|---|
| 249 | printf("------------------------ CURIE tests ---------------------\n"); |
|---|
| 250 | |
|---|
| 251 | run_test(context, "IRI", "http://www.example.org/iri", |
|---|
| 252 | rdfa_resolve_curie, "http://www.example.org/iri", |
|---|
| 253 | CURIE_PARSE_HREF_SRC); |
|---|
| 254 | run_test(context, "Safe CURIE", "[dc:title]", |
|---|
| 255 | rdfa_resolve_curie, "http://purl.org/dc/elements/1.1/title", |
|---|
| 256 | CURIE_PARSE_PROPERTY); |
|---|
| 257 | run_test(context, "Unsafe CURIE", "dc:title", |
|---|
| 258 | rdfa_resolve_curie, "http://purl.org/dc/elements/1.1/title", |
|---|
| 259 | CURIE_PARSE_PROPERTY); |
|---|
| 260 | run_test(context, "Non-prefixed CURIE", ":nonprefixed", |
|---|
| 261 | rdfa_resolve_curie, "http://example.org/nonprefixed", |
|---|
| 262 | CURIE_PARSE_PROPERTY); |
|---|
| 263 | run_test(context, "Reference-only CURIE", "foobar", |
|---|
| 264 | rdfa_resolve_curie, NULL, CURIE_PARSE_PROPERTY); |
|---|
| 265 | run_test(context, "Reference-only safe CURIE", "[foobar]", |
|---|
| 266 | rdfa_resolve_curie, NULL, CURIE_PARSE_PROPERTY); |
|---|
| 267 | run_test(context, "Empty safe CURIE", "[]", |
|---|
| 268 | rdfa_resolve_curie, NULL, CURIE_PARSE_PROPERTY); |
|---|
| 269 | run_test(context, "Blank named safe CURIE", "[_:frank]", |
|---|
| 270 | rdfa_resolve_curie, "_:frank", CURIE_PARSE_PROPERTY); |
|---|
| 271 | |
|---|
| 272 | rdfalist* dctvlist = rdfa_create_list(2); |
|---|
| 273 | rdfa_add_item( |
|---|
| 274 | dctvlist, "http://purl.org/dc/dcmitype/Image", RDFALIST_FLAG_NONE); |
|---|
| 275 | rdfa_add_item( |
|---|
| 276 | dctvlist, "http://purl.org/dc/dcmitype/Sound", RDFALIST_FLAG_NONE); |
|---|
| 277 | run_list_test( |
|---|
| 278 | context, "XHTML multiple @type_of", "[dctv:Image] [dctv:Sound]", |
|---|
| 279 | rdfa_resolve_curie_list, dctvlist, CURIE_PARSE_INSTANCEOF_DATATYPE); |
|---|
| 280 | rdfa_free_list(dctvlist); |
|---|
| 281 | |
|---|
| 282 | rdfalist* nllist = rdfa_create_list(2); |
|---|
| 283 | rdfa_add_item( |
|---|
| 284 | nllist, XHTML_VOCAB_URI "next", RDFALIST_FLAG_NONE); |
|---|
| 285 | rdfa_add_item( |
|---|
| 286 | nllist, XHTML_VOCAB_URI "license", RDFALIST_FLAG_NONE); |
|---|
| 287 | run_list_test( |
|---|
| 288 | context, "XHTML multiple @rel/@rev", "next license", |
|---|
| 289 | rdfa_resolve_curie_list, nllist, CURIE_PARSE_RELREV); |
|---|
| 290 | rdfa_free_list(nllist); |
|---|
| 291 | |
|---|
| 292 | rdfalist* dtlist = rdfa_create_list(2); |
|---|
| 293 | rdfa_add_item( |
|---|
| 294 | dtlist, XHTML_VOCAB_URI "description", RDFALIST_FLAG_NONE); |
|---|
| 295 | rdfa_add_item( |
|---|
| 296 | dtlist, XHTML_VOCAB_URI "title", RDFALIST_FLAG_NONE); |
|---|
| 297 | run_list_test( |
|---|
| 298 | context, "XHTML multiple @property", "description title", |
|---|
| 299 | rdfa_resolve_curie_list, dtlist, CURIE_PARSE_PROPERTY); |
|---|
| 300 | rdfa_free_list(dtlist); |
|---|
| 301 | |
|---|
| 302 | run_test_set(context, "XHTML @rel/@rev reserved", |
|---|
| 303 | my_g_relrev_reserved_words, XHTML_RELREV_RESERVED_WORDS_SIZE, |
|---|
| 304 | rdfa_resolve_relrev_curie, XHTML_VOCAB_URI, CURIE_PARSE_RELREV); |
|---|
| 305 | |
|---|
| 306 | printf("---------------------- CURIE test results ---------------------\n" |
|---|
| 307 | "%i passed, %i failed\n", |
|---|
| 308 | g_test_passes, g_test_fails); |
|---|
| 309 | |
|---|
| 310 | rdfa_free_context(context); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | int main(int argc, char** argv) |
|---|
| 314 | { |
|---|
| 315 | printf("Running CURIE tests\n"); |
|---|
| 316 | run_curie_tests(); |
|---|
| 317 | |
|---|
| 318 | return 0; |
|---|
| 319 | } |
|---|