Event.text for whitespace in <x/ > tags
[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 {
40 public:
41     std::string out;
42     void openTagStart(const char *tag, int tag_len) {
43         out += "<";
44         out.append(tag, tag_len);
45     } 
46     
47     void attribute(const char *tag, int tag_len,
48                    const char *attr, int attr_len,
49                    const char *value, int val_len) {
50         out += " ";
51         out.append(attr, attr_len);
52         out += "=\"";
53         out.append(value, val_len);
54         out += "\"";
55     }
56     void anyTagEnd(const char *tag, int tag_len, int close_it) {
57         if (close_it)
58             out += "/";
59         out += ">";
60     }
61     void closeTag(const char *tag, int tag_len) {
62         out += "</";
63         out.append(tag, tag_len);
64     }
65     void text(const char *value, int len) {
66         out.append(value, len);
67     }
68 };
69
70 BOOST_AUTO_TEST_CASE( test_html_parser_1 )
71 {
72     try
73     {
74         mp::HTMLParser hp;
75         const char* html =
76             "<html><body><a t1=v1 t2='v2' t3=\"v3\">some text</a>"
77             "<hr><table ></table  ><a href=\"x\"/></body></html>";
78         const char* expected =
79             "<html><body><a t1=\"v1\" t2=\"v2\" t3=\"v3\">some text</a>"
80             "<hr><table></table  ><a href=\"x\"/></body></html>";
81         MyEvent e;
82         hp.set_verbose(1);
83         hp.parse(e, html);
84
85         std::cout << "Expected" << std::endl;
86         std::cout << expected << std::endl;
87         std::cout << "Got" << std::endl;
88         std::cout << e.out << std::endl;
89         BOOST_CHECK_EQUAL(std::string(expected), e.out);
90     }
91     catch (std::exception & e) 
92     {
93         std::cout << e.what();
94         std::cout << std::endl;
95         BOOST_CHECK (false);
96     }
97 }
98
99 BOOST_AUTO_TEST_CASE( test_html_parser_2 )
100 {
101     try
102     {
103         mp::HTMLParser hp;
104         const char* html = 
105             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"
106             "<HTML>\n"
107             " <HEAD>\n"
108             "  <TITLE>YAZ 4.2.60</TITLE>\n"
109             " </HEAD>\n"
110             " <BODY>\n"
111             "  <P><A HREF=\"http://www.indexdata.com/yaz/\">YAZ</A> 4.2.60</P>\n"
112             "  <P>Error: 404</P>\n"
113             "  <P>Description: Not Found</P>\n"
114             " </BODY>\n"
115             "</HTML>";
116
117         const char* expected = html;
118         MyEvent e;
119         hp.set_verbose(1);
120         hp.parse(e, html);
121
122         std::cout << "Expected" << std::endl;
123         std::cout << expected << std::endl;
124         std::cout << "Got" << std::endl;
125         std::cout << e.out << std::endl;
126
127         BOOST_CHECK_EQUAL(std::string(expected), e.out);
128     }
129     catch (std::exception & e) 
130     {
131         std::cout << e.what();
132         std::cout << std::endl;
133         BOOST_CHECK (false);
134     }
135 }
136
137 BOOST_AUTO_TEST_CASE( test_html_parser_3 )
138 {
139     try
140     {
141         mp::HTMLParser hp;
142         const char* html =
143             "<?xml version=\"1.0\" strandalone=\"no\"?>\n"
144             "<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.4//EN\"\n"
145             "  \"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd\"\n"
146             "[\n"
147             " <!ENTITY % local SYSTEM \"local.ent\">\n"
148             " %local;\n"
149             "]>\n"
150             "<book></book>";
151
152         const char* expected = html;
153         MyEvent e;
154         hp.set_verbose(1);
155         hp.parse(e, html);
156
157         std::cout << "Expected" << std::endl;
158         std::cout << expected << std::endl;
159         std::cout << "Got" << std::endl;
160         std::cout << e.out << std::endl;
161
162         BOOST_CHECK_EQUAL(std::string(expected), e.out);
163     }
164     catch (std::exception & e) 
165     {
166         std::cout << e.what();
167         std::cout << std::endl;
168         BOOST_CHECK (false);
169     }
170 }
171
172 #if 0
173 // null ptr exception
174 BOOST_AUTO_TEST_CASE( test_html_parser_4 )
175 {
176     try
177     {
178         mp::HTMLParser hp;
179         const char* html =
180             "<\"?xml version=\"1.0\" strandalone=\"no\"?>\n"
181             "<book></book>";
182
183         const char* expected = html;
184         MyEvent e;
185         hp.set_verbose(1);
186         hp.parse(e, html);
187
188         std::cout << "Expected" << std::endl;
189         std::cout << expected << std::endl;
190         std::cout << "Got" << std::endl;
191         std::cout << e.out << std::endl;
192
193         BOOST_CHECK_EQUAL(std::string(expected), e.out);
194     }
195     catch (std::exception & e) 
196     {
197         std::cout << e.what();
198         std::cout << std::endl;
199         BOOST_CHECK (false);
200     }
201 }
202 #endif
203
204 /*
205  * Local variables:
206  * c-basic-offset: 4
207  * c-file-style: "Stroustrup"
208  * indent-tabs-mode: nil
209  * End:
210  * vim: shiftwidth=4 tabstop=8 expandtab
211  */
212