summaryrefslogtreecommitdiffstats
path: root/src/parser/xml_namespace_test.cpp
blob: de3891e800a6595dde5eeb5eb9e08aa160109de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "test_global.hpp"
#include "orcus/xml_namespace.hpp"

#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace orcus;

namespace {

void test_basic()
{
    ORCUS_TEST_FUNC_SCOPE;

    std::string_view xmlns1("http://some.xmlns/");
    std::string_view xmlns2("http://other.xmlns/");

    xmlns_repository repo;
    xmlns_context cxt1 = repo.create_context();
    xmlns_context cxt2 = repo.create_context();

    std::string_view empty, myns("myns");
    {
        // context 1
        xmlns_id_t test1 = cxt1.push(empty, xmlns1); // register default namespace.
        assert(cxt1.get(empty) == test1);
        xmlns_id_t test2 = cxt1.push(myns, xmlns2);
        assert(cxt1.get(myns) == test2);
        assert(test1 != test2);
    }

    {
        // context 2
        xmlns_id_t test1 = cxt2.push(empty, xmlns2); // register default namespace.
        assert(cxt2.get(empty) == test1);
        xmlns_id_t test2 = cxt2.push(myns, xmlns1);
        assert(cxt2.get(myns) == test2);
        assert(test1 != test2);
    }

    // Now, compare the registered namespaces between the two namespaces.
    assert(cxt1.get(empty) == cxt2.get(myns));
    assert(cxt1.get(myns) == cxt2.get(empty));
}

void test_all_namespaces()
{
    ORCUS_TEST_FUNC_SCOPE;

    std::string_view key1("a"), key2("b"), key3("c");
    std::string_view ns1("foo"), ns2("baa"), ns3("hmm");

    xmlns_repository repo;
    xmlns_context cxt = repo.create_context();
    xmlns_id_t ns;

    ns = cxt.push(key1, ns1);
    assert(ns1 == ns);
    ns = cxt.push(key2, ns2);
    assert(ns2 == ns);
    ns = cxt.push(key3, ns3);
    assert(ns3 == ns);

    std::vector<xmlns_id_t> all_ns = cxt.get_all_namespaces();
    assert(all_ns.size() == 3);
    assert(ns1 == all_ns[0]);
    assert(ns2 == all_ns[1]);
    assert(ns3 == all_ns[2]);
}

const xmlns_id_t NS_test_name1 = "test:name:1";
const xmlns_id_t NS_test_name2 = "test:name:2";
const xmlns_id_t NS_test_name3 = "test:name:3";

xmlns_id_t NS_test_all[] = {
    NS_test_name1,
    NS_test_name2,
    NS_test_name3,
    nullptr
};

xmlns_id_t NS_test_all_reverse[] = {
    NS_test_name3,
    NS_test_name2,
    NS_test_name1,
    nullptr
};

void test_predefined_ns()
{
    xmlns_repository ns_repo;
    ns_repo.add_predefined_values(NS_test_all);
    xmlns_context cxt = ns_repo.create_context();
    xmlns_id_t ns_id = cxt.push("tn1", "test:name:1");
    assert(ns_id == NS_test_name1);
    ns_id = cxt.push("tn2", "test:name:2");
    assert(ns_id == NS_test_name2);
    ns_id = cxt.push("tn3", "test:name:3");
    assert(ns_id == NS_test_name3);
    assert(cxt.get("tn1") == NS_test_name1);
    assert(cxt.get("tn2") == NS_test_name2);
    assert(cxt.get("tn3") == NS_test_name3);
}

void test_xml_name_t()
{
    ORCUS_TEST_FUNC_SCOPE;

    xml_name_t name1;
    name1.ns = NS_test_name1;
    name1.name = "foo";

    xml_name_t name2 = name1;
    assert(name1 == name2);

    name2.name = "foo2";
    assert(name1 != name2);

    xml_name_t name3 = name1;
    name3.ns = NS_test_name2;
    assert(name1 != name3);
}

void test_ns_context()
{
    ORCUS_TEST_FUNC_SCOPE;

    xmlns_repository repo;
    repo.add_predefined_values(NS_test_all);

    xmlns_repository repo2;
    repo2.add_predefined_values(NS_test_all_reverse);

    xmlns_context cxt;
    cxt = repo.create_context(); // copy assignment
    size_t id1 = cxt.get_index(NS_test_name3);
    xmlns_context cxt2 = cxt; // copy ctor
    size_t id2 = cxt2.get_index(NS_test_name3);

    assert(id1 == id2);

    xmlns_context cxt3 = repo2.create_context();
    id2 = cxt3.get_index(NS_test_name3);

    assert(id1 != id2);

    cxt3 = std::move(cxt2); // move assignment
    id2 = cxt3.get_index(NS_test_name3);

    assert(id1 == id2);

    try
    {
        id1 = cxt2.get_index(NS_test_name2);
        assert(!"exception was supposed to be thrown due to no associated repos.");
    }
    catch (const std::exception&)
    {
        // expected
    }

    xmlns_context cxt4(std::move(cxt3)); // move ctor
    id1 = cxt4.get_index(NS_test_name3);

    xmlns_context cxt5 = repo.create_context();
    id2 = cxt5.get_index(NS_test_name3);

    assert(id1 == id2);

    try
    {
        id1 = cxt3.get_index(NS_test_name2);
        assert(!"exception was supposed to be thrown due to no associated repos.");
    }
    catch (const std::exception&)
    {
        // expected
    }

    cxt4 = repo.create_context();
    cxt5 = repo2.create_context();
    id1 = cxt4.get_index(NS_test_name1);
    id2 = cxt5.get_index(NS_test_name1);

    assert(id1 != id2);

    cxt3 = repo.create_context();
    cxt5.swap(cxt3);
    id2 = cxt5.get_index(NS_test_name1);

    assert(id1 == id2);
}

void test_repo_move()
{
    ORCUS_TEST_FUNC_SCOPE;

    static_assert(!std::is_copy_constructible_v<xmlns_repository>);
    static_assert(std::is_move_constructible_v<xmlns_repository>);

    xmlns_repository repo;
    repo.add_predefined_values(NS_test_all);

    xmlns_repository repo_moved = std::move(repo); // move construction
    xmlns_repository repo_moved2;
    repo_moved2 = std::move(repo_moved); // move assignment

    xmlns_id_t ns_id = repo_moved2.get_identifier(0);
    assert(ns_id != XMLNS_UNKNOWN_ID);
    ns_id = repo_moved2.get_identifier(1);
    assert(ns_id != XMLNS_UNKNOWN_ID);
    ns_id = repo_moved2.get_identifier(2);
    assert(ns_id != XMLNS_UNKNOWN_ID);
    ns_id = repo_moved2.get_identifier(3);
    assert(ns_id == XMLNS_UNKNOWN_ID);
}

} // anonymous namespace

int main()
{
    test_basic();
    test_all_namespaces();
    test_predefined_ns();
    test_xml_name_t();
    test_ns_context();
    test_repo_move();

    return EXIT_SUCCESS;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */