File: root_element_test.cpp

package info (click to toggle)
mapnik 2.0.0%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 35,496 kB
  • sloc: cpp: 91,793; python: 6,051; xml: 3,528; sh: 848; makefile: 70; lisp: 10
file content (158 lines) | stat: -rw-r--r-- 5,142 bytes parent folder | download
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
#define BOOST_TEST_MODULE root_element_test

/*
 * This test module contains several generators for 
 * the SVG root element.
 */

// boost.test
#include <boost/test/included/unit_test.hpp>

// boost.spirit
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/repository/include/karma_confix.hpp>

// boost
#include <boost/fusion/tuple.hpp>

// stl
#include <string>
#include <sstream>

namespace karma = boost::spirit::karma;
namespace repository = boost::spirit::repository;
namespace fusion = boost::fusion;
using namespace karma;

struct F 
{
    F() :
    width(100),
    height(100),
    version(1.1),
    xmlns("http://www.w3.org/2000/svg"),
    expected_output("<svg width=\"100px\" height=\"100px\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">") {}

    ~F() {}

    const int width;
    const int height;
    const float version;
    const std::string xmlns;
    const std::string expected_output;
    std::ostringstream actual_output;
};

/*
 * This test case uses one compound generator to generate the SVG  element.
 *
 * It makes only one call to the format function and uses an ostringstream
 * as its output destination.
 *
 * The attribute values are provided as arguments to the format function.
 * The disadvantage of this is that the number of arguments is limited
 * by the preprocessor constant SPIRIT_ARGUMENTS_LIMIT, whose default
 * value is 10. The advantage is that they can be of any type (as long
 * as they are compatible with the corresponding generator type).
 */
BOOST_FIXTURE_TEST_CASE(bgcolor_stream_test_case, F)
{
    actual_output
    << format(
        "<svg width=\"" << int_ << string << "\" "
        << "height=\"" << int_ << string << "\" "
        << "version=\"" << float_ << "\" "
        << "xmlns=\"" << string << "\""
        << ">",
        width, "px", height, "px", version, xmlns);

    BOOST_CHECK_EQUAL(actual_output.str(), expected_output);
}

/*
 * This test case uses confix to wrap each xml attribute's value
 * inside double quotes.
 *
 * Notice that the generators' attribute list contains also tuples.
 * Tuples are needed to specify each confix's attributes that contain
 * more than one generator, like confix()[int_ << string] (this 
 * generator needs a tuple: tuple<int, string>).
 *
 * The difference between this generator and the one in test case
 * 'bgcolor_stream_test_case' is the use of less << operators and
 * a more clean handling of double quotes.
 */
BOOST_FIXTURE_TEST_CASE(bgcolor_stream_confix_test_case, F)
{
    using repository::confix;
    using fusion::tuple;

    actual_output
    << format(
        "<svg width=" << confix('"', '"')[int_ << string]
        << " height=" << confix('"', '"')[int_ << string]
        << " version=" << confix('"', '"')[float_]
        << " xmlns=" << confix('"', '"')[string]
        << ">",
        tuple<int, std::string>(width, "px"), tuple<int, std::string>(height, "px"), version, xmlns); 
    
    BOOST_CHECK_EQUAL(actual_output.str(), expected_output);
}

/*
 * This test case generates the angle brackets of the svg/xml tag
 * using confix. notice that a confix generator can be part of another
 * confix generator's expression.
 *
 * Notice also that the attribute list is the same as in 
 * 'bgcolor_stream_confix_test_case'. From this one can see that each 
 * generator is meant to have a list of attributes if it has more than one. 
 *
 * If the generator is nested inside another generator, the former's attribute 
 * list will be another list (a tuple, for example) inside the latter's.
 */
BOOST_FIXTURE_TEST_CASE(bgcolor_stream_confix_complete_test_case, F)
{
    using repository::confix;
    using fusion::tuple;

    actual_output
    << format(
        confix('<', ">")[
        "svg width=" << confix('"', '"')[int_ << string]
        << " height=" << confix('"', '"')[int_ << string]
        << " version=" << confix('"', '"')[float_]
        << " xmlns=" << confix('"', '"')[string]],
        tuple<int, std::string>(width, "px"), tuple<int, std::string>(height, "px"), version, xmlns); 
    
    BOOST_CHECK_EQUAL(actual_output.str(), expected_output);
}

/*
 * This test case uses an iterator to receive the generated
 * output. The iterator comes from the std::ostringstream that's
 * been used in the previous test cases, so the output is
 * actually generated into the stream.
 *
 * Using iterators instead of streams has the advantage that
 * more advanced concepts are implemented in karma for them,
 * like rules and grammars.
 */
BOOST_FIXTURE_TEST_CASE(bgcolor_stream_iterator_test_case, F)
{
    using repository::confix;
    using fusion::tuple;

    std::ostream_iterator<char> actual_output_iterator(actual_output);

    generate(
    actual_output_iterator,
    confix("<", ">")[
        "svg width=" << confix('"', '"')[int_ << string]
        << " height=" << confix('"', '"')[int_ << string]
        << " version=" << confix('"', '"')[float_]
        << " xmlns=" << confix('"', '"')[string]],
    tuple<int, std::string>(width, "px"), tuple<int, std::string>(height, "px"), version, xmlns);

    BOOST_CHECK_EQUAL(actual_output.str(), expected_output);
}