#include "test.h"
#include "namespace.h"

static int
test_namespace()
{
    LSUP_NSMap *nsm = LSUP_nsmap_new();
    ASSERT (nsm != NULL, "Error creating namespace map!");

    EXPECT_PASS (
            LSUP_nsmap_add (nsm, "dc", "http://purl.org/dc/elements/1.1/"));
    EXPECT_PASS (LSUP_nsmap_add (nsm, "dcterms", "http://purl.org/dc/terms/"));

    EXPECT_STR_EQ (
            LSUP_nsmap_get (nsm, "dc"), "http://purl.org/dc/elements/1.1/");
    EXPECT_STR_EQ (
            LSUP_nsmap_get (nsm, "dcterms"), "http://purl.org/dc/terms/");
    // Prefixes longer than 7 chars are truncated.
    ASSERT (
            LSUP_nsmap_get (nsm, "dctermsxxx") == NULL,
            "Non-existent NS found!");
    ASSERT (LSUP_nsmap_get (nsm, "none") == NULL, "Non-existent NS found!");

    char *fq_uri, *pfx_uri;
    fq_uri = "http://purl.org/dc/elements/1.1/title";

    EXPECT_PASS (LSUP_nsmap_denormalize_uri (nsm, fq_uri, &pfx_uri));
    EXPECT_STR_EQ (pfx_uri, "dc:title");

    fq_uri = NULL;
    EXPECT_PASS (LSUP_nsmap_normalize_uri (nsm, pfx_uri, &fq_uri));
    EXPECT_STR_EQ (fq_uri, "http://purl.org/dc/elements/1.1/title");

    EXPECT_PASS (LSUP_nsmap_remove (nsm, "dc"));
    ASSERT (
            LSUP_nsmap_remove (nsm, "none") == LSUP_NOACTION,
            "Wrong result for removal of non-existent prefix!");
    ASSERT (LSUP_nsmap_get (nsm, "dc") == NULL, "Deleted NS found!");

    LSUP_nsmap_free (nsm);
    free (fq_uri);
    free (pfx_uri);

    return 0;
}

int namespace_tests()
{
    RUN (test_namespace);
    return 0;
}