summaryrefslogtreecommitdiffstats
path: root/src/parser/parser_test_xml_validation.cpp
blob: 3be1804a90198ff8877918b27e45379cdf08a408 (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
/* -*- 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/sax_parser.hpp>
#include <orcus/stream.hpp>

#include <iostream>
#include <boost/range/iterator_range.hpp>

#include "filesystem_env.hpp"

void test_valid()
{
    ORCUS_TEST_FUNC_SCOPE;

    struct _handler : public orcus::sax_handler {};

    fs::path root_dir = fs::path{SRCDIR} / "test" / "xml" / "valids";

    if (!fs::is_directory(root_dir))
        return;

    for (const fs::path& entry : boost::make_iterator_range(fs::directory_iterator{root_dir}, {}))
    {
        std::cout << "input file: " << entry << std::endl;

        orcus::file_content content(entry.string());

        _handler hdl;
        orcus::sax_parser<_handler> parser(content.str(), hdl);

        try
        {
            parser.parse();
        }
        catch (const orcus::malformed_xml_error& e)
        {
            std::cerr << orcus::create_parse_error_output(content.str(), e.offset()) << std::endl;
            std::cerr << e.what() << std::endl;
            assert(!"This was supposed to be a valid XML!");

        }
    }
}

void test_invalid()
{
    ORCUS_TEST_FUNC_SCOPE;

    struct _handler : public orcus::sax_handler {};

    fs::path root_dir = fs::path{SRCDIR} / "test" / "xml" / "invalids";

    if (!fs::is_directory(root_dir))
        return;

    for (const fs::path& entry : boost::make_iterator_range(fs::directory_iterator{root_dir}, {}))
    {
        std::cout << "input file: " << entry << std::endl;

        orcus::file_content content(entry.string());

        _handler hdl;
        orcus::sax_parser<_handler> parser(content.str(), hdl);

        try
        {
            parser.parse();
            assert(!"exception was expected, but one was not thrown.");
        }
        catch (const orcus::malformed_xml_error& e)
        {
            std::cerr << orcus::create_parse_error_output(content.str(), e.offset()) << std::endl;
            std::cerr << e.what() << std::endl;
        }
        catch (...)
        {
            assert(!"wrong exception was thrown.");
        }
    }
}

int main()
{
    test_valid();
    test_invalid();

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