Avoid re allocations
[metaproxy-moved-to-github.git] / src / test_html_parser.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20 #include <iostream>
21 #include <stdexcept>
22
23 #include "html_parser.hpp"
24 #include <metaproxy/util.hpp>
25
26 #include <boost/lexical_cast.hpp>
27
28 #include <yaz/log.h>
29
30 #define BOOST_AUTO_TEST_MAIN
31 #define BOOST_TEST_DYN_LINK
32
33 #include <boost/test/auto_unit_test.hpp>
34
35 using namespace boost::unit_test;
36 namespace mp = metaproxy_1;
37
38 class MyEvent : public mp::HTMLParserEvent {
39     public:
40         std::string out;
41         void openTagStart(const char *name)
42         {
43             out += "<";
44             out += name;
45         } 
46         
47         void attribute(const char *tagName, 
48                 const char *name, const char *value, int val_len)
49         {
50             out += " ";
51             out += name;
52             out += "=\"";
53             out.append(value, val_len);
54             out += "\"";
55         }
56
57         void anyTagEnd(const char *name)
58         {
59             out += ">";
60         }
61         
62         void closeTag(const char *name)
63         {
64             out += "</";
65             out += name;
66         }
67         
68         void text(const char *value, int len)
69         {
70             out.append(value, len);
71         }
72 };
73
74
75 BOOST_AUTO_TEST_CASE( test_html_parser_1 )
76 {
77     try
78     {
79         mp::HTMLParser hp;
80         const char* html = 
81             "<html><body><a t1=v1 t2='v2' t3=\"v3\">some text</a>"
82             "<hr><table ></table  ></body></html";
83         const char* expected = 
84             "<html><body><a t1=\"v1\" t2=\"v2\" t3=\"v3\">some text</a>"
85             "<hr><table></table></body></html";
86         MyEvent e;
87         hp.parse(e, html);
88         BOOST_CHECK_EQUAL(std::string(expected), e.out);
89     }
90     catch (std::exception & e) 
91     {
92         std::cout << e.what();
93         std::cout << std::endl;
94         BOOST_CHECK (false);
95     }
96 }
97
98
99 /*
100  * Local variables:
101  * c-basic-offset: 4
102  * c-file-style: "Stroustrup"
103  * indent-tabs-mode: nil
104  * End:
105  * vim: shiftwidth=4 tabstop=8 expandtab
106  */
107