1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
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
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
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
23 #include "html_parser.hpp"
24 #include <metaproxy/util.hpp>
26 #include <boost/lexical_cast.hpp>
30 #define BOOST_AUTO_TEST_MAIN
31 #define BOOST_TEST_DYN_LINK
33 #include <boost/test/auto_unit_test.hpp>
35 using namespace boost::unit_test;
36 namespace mp = metaproxy_1;
38 class MyEvent : public mp::HTMLParserEvent
42 void openTagStart(const char *tag, int tag_len) {
44 out.append(tag, tag_len);
47 void attribute(const char *tag, int tag_len,
48 const char *attr, int attr_len,
49 const char *value, int val_len, const char *sep) {
51 out.append(attr, attr_len);
54 out.append(value, val_len);
57 void anyTagEnd(const char *tag, int tag_len, int close_it) {
62 void closeTag(const char *tag, int tag_len) {
64 out.append(tag, tag_len);
66 void text(const char *value, int len) {
67 out.append(value, len);
71 BOOST_AUTO_TEST_CASE( test_html_parser_1 )
77 "<html><body><a t1=v1 t2='v2' t3=\"v3\">some text</a>"
78 "<hr><table ></table ><a href=\"x\"/></body></html>";
79 const char* expected =
80 "<html><body><a t1=v1 t2='v2' t3=\"v3\">some text</a>"
81 "<hr><table></table ><a href=\"x\"/></body></html>";
86 std::cout << "Expected" << std::endl;
87 std::cout << expected << std::endl;
88 std::cout << "Got" << std::endl;
89 std::cout << e.out << std::endl;
90 BOOST_CHECK_EQUAL(std::string(expected), e.out);
92 catch (std::exception & e)
94 std::cout << e.what();
95 std::cout << std::endl;
100 BOOST_AUTO_TEST_CASE( test_html_parser_2 )
106 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"
109 " <TITLE>YAZ 4.2.60</TITLE>\n"
112 " <P><A HREF=\"http://www.indexdata.com/yaz/\">YAZ</A> 4.2.60</P>\n"
113 " <P>Error: 404</P>\n"
114 " <P>Description: Not Found</P>\n"
118 const char* expected = html;
123 std::cout << "Expected" << std::endl;
124 std::cout << expected << std::endl;
125 std::cout << "Got" << std::endl;
126 std::cout << e.out << std::endl;
128 BOOST_CHECK_EQUAL(std::string(expected), e.out);
130 catch (std::exception & e)
132 std::cout << e.what();
133 std::cout << std::endl;
138 BOOST_AUTO_TEST_CASE( test_html_parser_3 )
144 "<?xml version=\"1.0\" strandalone=\"no\"?>\n"
145 "<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.4//EN\"\n"
146 " \"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd\"\n"
148 " <!ENTITY % local SYSTEM \"local.ent\">\n"
153 const char* expected = html;
158 std::cout << "Expected" << std::endl;
159 std::cout << expected << std::endl;
160 std::cout << "Got" << std::endl;
161 std::cout << e.out << std::endl;
163 BOOST_CHECK_EQUAL(std::string(expected), e.out);
165 catch (std::exception & e)
167 std::cout << e.what();
168 std::cout << std::endl;
174 // null ptr exception
175 BOOST_AUTO_TEST_CASE( test_html_parser_4 )
181 "<\"?xml version=\"1.0\" strandalone=\"no\"?>\n"
184 const char* expected = html;
189 std::cout << "Expected" << std::endl;
190 std::cout << expected << std::endl;
191 std::cout << "Got" << std::endl;
192 std::cout << e.out << std::endl;
194 BOOST_CHECK_EQUAL(std::string(expected), e.out);
196 catch (std::exception & e)
198 std::cout << e.what();
199 std::cout << std::endl;
208 * c-file-style: "Stroustrup"
209 * indent-tabs-mode: nil
211 * vim: shiftwidth=4 tabstop=8 expandtab