blob: 1df71560e81dc2be66bb0b18e662cdf1b91d4241 [file] [log] [blame]
Austin Schuh24adb6b2015-09-06 17:37:40 -07001// Copyright (c) 2013, Matt Godbolt
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// Redistributions of source code must retain the above copyright notice, this
8// list of conditions and the following disclaimer.
9//
10// Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24// POSSIBILITY OF SUCH DAMAGE.
25
26#include "seasocks/util/Html.h"
27
28#include <gmock/gmock.h>
29
30using namespace seasocks;
31using namespace seasocks::html;
32
33TEST(HtmlTests, shouldRenderASimpleDocument) {
34 auto document = html::html(head(title("Hello")), body(h1("A header"), "This is a document"));
35 EXPECT_EQ("<html><head><title>Hello</title></head><body><h1>A header</h1>This is a document</body></html>", document.str());
36}
37
38TEST(HtmlTests, shouldAllowAppending) {
39 auto list = ul();
40 list << li("Element 1") << li("Element 2");
41 EXPECT_EQ("<ul><li>Element 1</li><li>Element 2</li></ul>", list.str());
42}
43
44TEST(HtmlTests, shouldAllowCommaSeparation) {
45 auto list = ul(li("Element 1"), li("Element 2"));
46 EXPECT_EQ("<ul><li>Element 1</li><li>Element 2</li></ul>", list.str());
47}
48
49TEST(HtmlTests, shouldHandleAttributes) {
50 auto elem = span("Some text").clazz("class");
51 EXPECT_EQ("<span class=\"class\">Some text</span>", elem.str());
52}
53
54TEST(HtmlTests, shouldAppendLikeAStreamFromEmpty) {
55 auto elem = empty() << "Text " << 1 << ' ' << 10.23;
56 EXPECT_EQ("Text 1 10.23", elem.str());
57}
58
59TEST(HtmlTests, shouldNotNeedExtraMarkupForTextNodes) {
60 auto elem = text("This ") << text("is") << text(" a test") << ".";
61 EXPECT_EQ("This is a test.", elem.str());
62}
63
64TEST(HtmlTests, shouldWorkWithAnchors) {
65 auto elem = a("http://google.com/?q=badgers", div(span("foo")));
66 EXPECT_EQ("<a href=\"http://google.com/?q=badgers\"><div><span>foo</span></div></a>", elem.str());
67}
68
69TEST(HtmlTests, shouldAddAll) {
70 std::vector<std::string> strings = { "Hi", "Moo", "Foo" };
71 auto list = ul().addAll(strings, [](const std::string& s) { return li(s); });
72 EXPECT_EQ("<ul><li>Hi</li><li>Moo</li><li>Foo</li></ul>", list.str());
73
74}