Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
html_parse_test_base.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
20 
21 #ifndef PAGESPEED_KERNEL_HTML_HTML_PARSE_TEST_BASE_H_
22 #define PAGESPEED_KERNEL_HTML_HTML_PARSE_TEST_BASE_H_
23 
35 
36 namespace net_instaweb {
37 
39 class HtmlParseTestBaseNoAlloc : public testing::Test {
40  protected:
41  static const char kTestDomain[];
42  static const char kXhtmlDtd[];
43 
45  : message_handler_(new NullMutex),
46  write_to_string_(&output_buffer_),
47  added_filter_(false) {
48  }
49  virtual ~HtmlParseTestBaseNoAlloc();
50 
63  virtual bool AddBody() const = 0;
64 
70  virtual bool AddHtmlTags() const { return true; }
71 
75  void SetDoctype(StringPiece directive) {
76  directive.CopyToString(&doctype_string_);
77  }
78 
79  virtual GoogleString AddHtmlBody(StringPiece html) {
80  GoogleString ret;
81  if (AddHtmlTags()) {
82  ret = AddBody() ? "<html><body>\n" : "<html>\n";
83  StrAppend(&ret, html, (AddBody() ? "</body></html>\n" : "</html>"));
84  } else {
85  html.CopyToString(&ret);
86  }
87  return ret;
88  }
89 
92  void ValidateNoChanges(StringPiece case_id, StringPiece html_input) {
93  ValidateExpected(case_id, html_input, html_input);
94  }
95 
97  void ValidateNoChangesFail(StringPiece case_id, StringPiece html_input) {
98  ValidateExpectedFail(case_id, html_input, html_input);
99  }
100 
101  virtual void SetupWriter() {
102  SetupWriter(&html_writer_filter_);
103  }
104 
105  void SetupWriter(scoped_ptr<HtmlWriterFilter>* html_writer_filter) {
106  output_buffer_.clear();
107  if (html_writer_filter->get() == NULL) {
108  html_writer_filter->reset(new HtmlWriterFilter(html_parse()));
109  (*html_writer_filter)->set_writer(&write_to_string_);
110  html_parse()->AddFilter(html_writer_filter->get());
111  }
112  }
113 
115  void Parse(StringPiece case_id, StringPiece html_input) {
118  GoogleString dummy_url = StrCat(kTestDomain, case_id, ".html");
119  ParseUrl(dummy_url, html_input);
120  }
121 
123  virtual void ParseUrl(StringPiece url, StringPiece html_input);
124 
128  bool ValidateExpected(StringPiece case_id,
129  StringPiece html_input,
130  StringPiece expected);
131 
133  bool ValidateExpectedUrl(StringPiece url,
134  StringPiece html_input,
135  StringPiece expected);
136 
138  void ValidateExpectedFail(StringPiece case_id,
139  StringPiece html_input,
140  StringPiece expected);
141 
142  virtual HtmlParse* html_parse() = 0;
143 
144  const GoogleUrl& html_gurl() { return html_parse()->google_url(); }
145 
146  MockMessageHandler message_handler_;
147  StringWriter write_to_string_;
148  GoogleString output_buffer_;
149  bool added_filter_;
150  scoped_ptr<HtmlWriterFilter> html_writer_filter_;
151  GoogleString doctype_string_;
152 
153  private:
154 
155 };
156 
158  public:
159  HtmlParseTestBase() : html_parse_(&message_handler_) { }
160  protected:
161  virtual HtmlParse* html_parse() { return &html_parse_; }
162 
163  HtmlParse html_parse_;
164 
165  private:
166 
167 };
168 
169 }
170 
171 #endif
class GoogleUrl
Definition: google_url.h:58
Definition: html_parse_test_base.h:157
void AddFilter(HtmlFilter *filter)
Application methods for parsing functions and adding filters.
const GoogleUrl & google_url() const
Gets a parsed GoogleUrl& corresponding to url().
Definition: html_parse.h:385
Shared infrastructure for unit-testing the HTML parser.
Definition: html_parse_test_base.h:39
void SetDoctype(StringPiece directive)
Definition: html_parse_test_base.h:75
void ValidateExpectedFail(StringPiece case_id, StringPiece html_input, StringPiece expected)
Fail to ValidateExpected.
Definition: html_parse.h:88
bool ValidateExpectedUrl(StringPiece url, StringPiece html_input, StringPiece expected)
Same as ValidateExpected, but with an explicit URL rather than an id.
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
void ValidateNoChangesFail(StringPiece case_id, StringPiece html_input)
Fail to ValidateNoChanges.
Definition: html_parse_test_base.h:97
bool ValidateExpected(StringPiece case_id, StringPiece html_input, StringPiece expected)
static const char kXhtmlDtd[]
DOCTYPE string for claiming XHTML.
Definition: html_parse_test_base.h:42
Definition: null_mutex.h:29
void ValidateNoChanges(StringPiece case_id, StringPiece html_input)
Definition: html_parse_test_base.h:92
virtual bool AddHtmlTags() const
Definition: html_parse_test_base.h:70
void Parse(StringPiece case_id, StringPiece html_input)
Parse html_input, the result is stored in output_buffer_.
Definition: html_parse_test_base.h:115
virtual void ParseUrl(StringPiece url, StringPiece html_input)
Parse given an explicit URL rather than an id to build URL around.