summaryrefslogtreecommitdiffstats
path: root/src/parser/threaded_sax_token_parser_test.cpp
blob: 1338b2acca7f99eaa6e8b267357766da31cea39b (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
/* -*- 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/threaded_sax_token_parser.hpp"
#include "orcus/tokens.hpp"
#include "orcus/xml_namespace.hpp"
#include "orcus/parser_base.hpp"
#include "orcus/stream.hpp"

#include <cstring>

using namespace std;
using namespace orcus;

void test_sax_token_parser_1()
{
    // Array of tokens to define for this test.
    const char* token_names[] = {
        "??",       // 0
        "andy",     // 1
        "bruce",    // 2
        "charlie",  // 3
        "david",    // 4
        "edward"    // 5
    };

    size_t token_count = std::size(token_names);

    // Token constants.
    const xml_token_t op_andy    = 1;
    const xml_token_t op_bruce   = 2;
    const xml_token_t op_charlie = 3;
    const xml_token_t op_david   = 4;
    const xml_token_t op_edward  = 5;

    struct check
    {
        const char* raw_name;
        xml_token_t token;
        bool start_element;
    };

    tokens token_map(token_names, token_count);
    xmlns_repository ns_repo;
    xmlns_context ns_cxt = ns_repo.create_context();

    {
        // Test XML content.
        const char* content = "<?xml version=\"1.0\"?><root><andy/><bruce/><charlie/><david/><edward/><frank/></root>";
        size_t content_size = strlen(content);

        // Expected outcome.
        const check checks[] = {
            { "root",    XML_UNKNOWN_TOKEN, true  }, // name not on the master token list.
            { "andy",    op_andy,           true  },
            { "andy",    op_andy,           false },
            { "bruce",   op_bruce,          true  },
            { "bruce",   op_bruce,          false },
            { "charlie", op_charlie,        true  },
            { "charlie", op_charlie,        false },
            { "david",   op_david,          true  },
            { "david",   op_david,          false },
            { "edward",  op_edward,         true  },
            { "edward",  op_edward,         false },
            { "frank",   XML_UNKNOWN_TOKEN, true  }, // name not on the master token list.
            { "frank",   XML_UNKNOWN_TOKEN, false }, // name not on the master token list.
            { "root",    XML_UNKNOWN_TOKEN, false }, // name not on the master token list.
        };

        class handler
        {
            const check* mp_head;
            const check* mp_check;
        public:
            handler(const check* p) : mp_head(p), mp_check(p) {}

            void start_element(const orcus::xml_token_element_t& elem)
            {
                assert(std::string_view(mp_check->raw_name) == elem.raw_name);
                assert(mp_check->token == elem.name);
                assert(mp_check->start_element);
                ++mp_check;
            }

            void end_element(const orcus::xml_token_element_t& elem)
            {
                assert(std::string_view(mp_check->raw_name) == elem.raw_name);
                assert(mp_check->token == elem.name);
                assert(!mp_check->start_element);
                ++mp_check;
            }

            void characters(std::string_view /*val*/, bool /*transient*/) {}

            size_t get_token_count() const
            {
                return std::distance(mp_head, mp_check);
            }
        };

        handler hdl(checks);
        threaded_sax_token_parser<handler> parser(content, content_size, token_map, ns_cxt, hdl, 1, 100);
        parser.parse();

        assert(hdl.get_token_count() == std::size(checks));
    }

    {
        // This content intentially contains invalid XML part at offset 28.
        const char* content = "<?xml version=\"1.0\"?><root><<andy/><bruce/><charlie/><david/><edward/><frank/></root>";
        size_t content_size = strlen(content);

        class handler
        {
        public:
            handler() {}

            void start_element(const orcus::xml_token_element_t& /*elem*/) {}

            void end_element(const orcus::xml_token_element_t& /*elem*/) {}

            void characters(std::string_view /*val*/, bool /*transient*/) {}
        };

        try
        {
            handler hdl;
            threaded_sax_token_parser<handler> parser(content, content_size, token_map, ns_cxt, hdl, 1, 100);
            parser.parse();
            assert(!"An exception was expected, but one was not thrown.");
        }
        catch (const malformed_xml_error& e)
        {
            assert(e.offset() == 28u);
        }
        catch (const std::exception&)
        {
            assert(!"Wrong exception was thrown!");
        }
    }

    {
        // Test XML content.
        const char* content = "<?xml version=\"1.0\"?><root><andy/><bruce/><charlie/><david/><edward/><frank/></root>";
        size_t content_size = strlen(content);

        class mock_exception : public std::exception {};

        class handler
        {
        public:
            handler() {}

            void start_element(const orcus::xml_token_element_t& /*elem*/) {}

            void end_element(const orcus::xml_token_element_t& /*elem*/)
            {
                throw mock_exception();
            }

            void characters(std::string_view /*val*/, bool /*transient*/) {}
        };

        handler hdl;
        threaded_sax_token_parser<handler> parser(content, content_size, token_map, ns_cxt, hdl, 1, 100);

        try
        {
            parser.parse();
            assert(!"A mock exception was expected but not thrown.");
        }
        catch (const mock_exception&)
        {
            // expected.
        }
    }
}

int main()
{
    test_sax_token_parser_1();
    return EXIT_SUCCESS;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */