aboutsummaryrefslogtreecommitdiff
path: root/tests/xml/parser/driver.cxx
blob: 05c0f3511f13be2c01640c730b98d7f4028eabc8 (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
// file      : tests/xml/parser/driver.cxx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <string>
#include <cassert>
#include <iostream>
#include <sstream>

#include <cutl/xml/parser.hxx>

using namespace std;
namespace xml = cutl::xml;
using namespace xml;

int
main ()
{
  // Test error handling.
  //
  try
  {
    istringstream is ("<root><nested>X</nasted></root>");
    parser p (is, "test");

    assert (p.next () == parser::start_element);
    assert (p.next () == parser::start_element);
    assert (p.next () == parser::characters && p.value () == "X");
    p.next ();
    assert (false);
  }
  catch (const xml::exception& e)
  {
    // cerr << e.what () << endl;
  }

  try
  {
    istringstream is ("<root/>");
    is.exceptions (ios_base::badbit | ios_base::failbit);
    parser p (is, "test");

    is.setstate (ios_base::badbit);
    p.next ();
    assert (false);
  }
  catch (const ios_base::failure& e)
  {
  }

  // Test content processing.
  //

  // empty
  //
  {
    istringstream is ("<root x=' x '>  \n\t </root>");
    parser p (is, "empty");

    assert (p.next () == parser::start_element);
    p.content (parser::empty);
    assert (p.next () == parser::start_attribute);
    assert (p.next () == parser::characters && p.value () == " x ");
    assert (p.next () == parser::end_attribute);
    assert (p.next () == parser::end_element);
    assert (p.next () == parser::eof);
  }

  try
  {
    istringstream is ("<root>  \n &amp; X \t </root>");
    parser p (is, "empty");

    assert (p.next () == parser::start_element);
    p.content (parser::empty);
    p.next ();
    assert (false);
  }
  catch (const xml::exception& e)
  {
    // cerr << e.what () << endl;
  }

  // simple
  //
  {
    istringstream is ("<root> X </root>");
    parser p (is, "simple");

    assert (p.next () == parser::start_element);
    p.content (parser::simple);
    assert (p.next () == parser::characters && p.value () == " X ");
    assert (p.next () == parser::end_element);
    assert (p.next () == parser::eof);
  }

  try
  {
    istringstream is ("<root> ? <nested/></root>");
    parser p (is, "simple");

    assert (p.next () == parser::start_element);
    p.content (parser::simple);
    assert (p.next () == parser::characters && p.value () == " ? ");
    p.next ();
    assert (false);
  }
  catch (const xml::exception& e)
  {
    // cerr << e.what () << endl;
  }

  // complex
  //
  {
    istringstream is ("<root x=' x '>\n"
                      "  <nested>\n"
                      "    <inner/>\n"
                      "    <inner> X </inner>\n"
                      "  </nested>\n"
                      "</root>\n");
    parser p (is, "complex");

    assert (p.next () == parser::start_element); // root
    p.content (parser::complex);

    assert (p.next () == parser::start_attribute);
    assert (p.next () == parser::characters && p.value () == " x ");
    assert (p.next () == parser::end_attribute);

    assert (p.next () == parser::start_element); // nested
    p.content (parser::complex);

    assert (p.next () == parser::start_element); // inner
    p.content (parser::empty);
    assert (p.next () == parser::end_element);   // inner

    assert (p.next () == parser::start_element); // inner
    p.content (parser::simple);
    assert (p.next () == parser::characters && p.value () == " X ");
    assert (p.next () == parser::end_element);   // inner

    assert (p.next () == parser::end_element);   // nested
    assert (p.next () == parser::end_element);   // root
    assert (p.next () == parser::eof);
  }

  try
  {
    istringstream is ("<root> \n<n/> X <n> X </n>  </root>");
    parser p (is, "complex");

    assert (p.next () == parser::start_element);
    p.content (parser::complex);
    assert (p.next () == parser::start_element);
    assert (p.next () == parser::end_element);
    p.next ();
    assert (false);
  }
  catch (const xml::exception& e)
  {
    // cerr << e.what () << endl;
  }
}