-
Notifications
You must be signed in to change notification settings - Fork 373
/
Copy pathHtmlEncode.C
42 lines (33 loc) · 1.17 KB
/
HtmlEncode.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Copyright (C) 2022 Emweb bv, Herent, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <boost/test/unit_test.hpp>
#include <Wt/Utils.h>
#include <Wt/WWebWidget.h>
using Wt::Utils::htmlAttributeValue;
// Test if the example in the documentation compiles and works properly
BOOST_AUTO_TEST_CASE(htmlAttributeValue_test_doc)
{
const std::string value = "Here's a value!";
const std::string attribute = "name=\"" + htmlAttributeValue(value) + "\"";
BOOST_REQUIRE_EQUAL("name=\"Here's a value!\"", attribute);
}
BOOST_AUTO_TEST_CASE(htmlAttributeValue_test_escape)
{
const std::vector<std::pair<std::string, std::string>> tests = {
{"", ""},
{R"=(escape" <this! &>)=", R"=(escape" <this! &>)="},
{R"=(""&"<<&)=", R"=(""&"<<&)="},
{"Nothing to escape", "Nothing to escape"},
};
for (const auto& test : tests) {
const auto& value = test.first;
const auto& result = test.second;
BOOST_REQUIRE_EQUAL(result, htmlAttributeValue(value));
// roundtrip test
std::string toUnescape = htmlAttributeValue(value);
BOOST_REQUIRE_EQUAL(value, Wt::WWebWidget::unescapeText(toUnescape));
}
}