Skip to content

Commit 18c075b

Browse files
committed
tests: add basic tests for BBCodeParser class
1 parent b5fdb63 commit 18c075b

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

tests/components/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(component_tests_sources
1616
../../code/XMLDocument.cpp
1717
../../code/XMLNode.cpp
1818
../../code/bbcode/AdvancedTemplateBBCode.cpp
19+
../../code/bbcode/BBCodeParser.cpp
1920
../../code/bbcode/CustomizedSimpleBBCode.cpp
2021
../../code/bbcode/HorizontalRuleBBCode.cpp
2122
../../code/bbcode/ListBBCode.cpp
@@ -47,6 +48,7 @@ set(component_tests_sources
4748
SortType.cpp
4849
bbcode/AdvancedTemplateBBCode.cpp
4950
bbcode/AdvancedTplAmpTransformBBCode.cpp
51+
bbcode/BBCodeParser.cpp
5052
bbcode/CustomizedSimpleBBCode.cpp
5153
bbcode/HorizontalRuleBBCode.cpp
5254
bbcode/KillSpacesBeforeNewline.cpp
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
-------------------------------------------------------------------------------
3+
This file is part of the Private Message Database test suite.
4+
Copyright (C) 2015, 2022 Dirk Stolle
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
-------------------------------------------------------------------------------
19+
*/
20+
21+
#include "../../locate_catch.hpp"
22+
#include "../../../code/bbcode/BBCodeParser.hpp"
23+
#include "../../../code/bbcode/SimpleBBCode.hpp"
24+
25+
struct B_To_C: public TextProcessor
26+
{
27+
public:
28+
virtual void applyToText(std::string& text) const
29+
{
30+
std::string::size_type pos = text.find("b]");
31+
while (pos != std::string::npos)
32+
{
33+
text.replace(pos, 1, "c");
34+
pos = text.find("b]", (pos == 0) ? 0 : pos - 1);
35+
}
36+
}
37+
}; // struct
38+
39+
struct Exchange: public TextProcessor
40+
{
41+
public:
42+
virtual void applyToText(std::string& text) const
43+
{
44+
std::string::size_type pos = text.find(">exchanging");
45+
while (pos != std::string::npos)
46+
{
47+
text.erase(pos + 1, 2);
48+
pos = text.find(">exchanging", pos + 5);
49+
}
50+
}
51+
}; // struct
52+
53+
TEST_CASE("BBCodeParser")
54+
{
55+
const std::string forum_url = "https://for.um/";
56+
57+
SECTION("bb code with n2lbr in HTML 4.01")
58+
{
59+
const SimpleBBCode code("b");
60+
BBCodeParser parser;
61+
parser.addCode(&code);
62+
63+
const std::string input = "[b]bold[/b]\ntext";
64+
const std::string expected = "<b>bold</b><br>\ntext";
65+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::HTML4_01, true);
66+
67+
REQUIRE( parsed == expected );
68+
}
69+
70+
SECTION("bb code with n2lbr in XHTML")
71+
{
72+
const SimpleBBCode code("b");
73+
BBCodeParser parser;
74+
parser.addCode(&code);
75+
76+
const std::string input = "[b]bold[/b]\ntext";
77+
const std::string expected = "<b>bold</b><br />\ntext";
78+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::XHTML, true);
79+
80+
REQUIRE( parsed == expected );
81+
}
82+
83+
SECTION("bb code without n2lbr")
84+
{
85+
const SimpleBBCode code("b");
86+
BBCodeParser parser;
87+
parser.addCode(&code);
88+
89+
const std::string input = "[b]bold[/b]\ntext";
90+
const std::string expected = "<b>bold</b>\ntext";
91+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::HTML4_01, false);
92+
93+
REQUIRE( parsed == expected );
94+
}
95+
96+
SECTION("bb code plus pre-processor")
97+
{
98+
const SimpleBBCode code("c");
99+
B_To_C pre_proc;
100+
101+
BBCodeParser parser;
102+
parser.addCode(&code);
103+
parser.addPreProcessor(&pre_proc);
104+
105+
const std::string input = "[b]exchanging[/b] text";
106+
const std::string expected = "<c>exchanging</c> text";
107+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::HTML4_01, true);
108+
109+
REQUIRE( parsed == expected );
110+
}
111+
112+
SECTION("bb code plus smilie in HTML 4.01")
113+
{
114+
const SimpleBBCode code("b");
115+
Smilie smilie(":)", "img/smile.png", UrlType::Relative);
116+
BBCodeParser parser;
117+
parser.addCode(&code);
118+
parser.addSmilie(smilie);
119+
120+
const std::string input = "[b]smiling[/b] face :)";
121+
const std::string expected = "<b>smiling</b> face <img src=\"https://for.um/img/smile.png\" alt=\":)\" border=\"0\">";
122+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::HTML4_01, true);
123+
124+
REQUIRE( parsed == expected );
125+
}
126+
127+
SECTION("bb code plus smilie in XHTML")
128+
{
129+
const SimpleBBCode code("b");
130+
Smilie smilie(":)", "img/smile.png", UrlType::Relative);
131+
BBCodeParser parser;
132+
parser.addCode(&code);
133+
parser.addSmilie(smilie);
134+
135+
const std::string input = "[b]smiling[/b] face :)";
136+
const std::string expected = "<b>smiling</b> face <img src=\"https://for.um/img/smile.png\" alt=\":)\" border=\"0\" />";
137+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::XHTML, true);
138+
139+
REQUIRE( parsed == expected );
140+
}
141+
142+
SECTION("bb code plus post-processor")
143+
{
144+
const SimpleBBCode code("b");
145+
Exchange post_proc;
146+
147+
BBCodeParser parser;
148+
parser.addCode(&code);
149+
parser.addPostProcessor(&post_proc);
150+
151+
const std::string input = "[b]exchanging[/b] text";
152+
const std::string expected = "<b>changing</b> text";
153+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::HTML4_01, true);
154+
155+
REQUIRE( parsed == expected );
156+
}
157+
}

tests/components/component_tests.cbp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
<Unit filename="../../code/bbcode/AdvancedTemplateBBCode.hpp" />
6060
<Unit filename="../../code/bbcode/AdvancedTplAmpTransformBBCode.hpp" />
6161
<Unit filename="../../code/bbcode/BBCode.hpp" />
62+
<Unit filename="../../code/bbcode/BBCodeParser.cpp" />
63+
<Unit filename="../../code/bbcode/BBCodeParser.hpp" />
6264
<Unit filename="../../code/bbcode/CustomizedSimpleBBCode.cpp" />
6365
<Unit filename="../../code/bbcode/CustomizedSimpleBBCode.hpp" />
6466
<Unit filename="../../code/bbcode/HorizontalRuleBBCode.cpp" />
@@ -114,6 +116,7 @@
114116
<Unit filename="SortType.cpp" />
115117
<Unit filename="bbcode/AdvancedTemplateBBCode.cpp" />
116118
<Unit filename="bbcode/AdvancedTplAmpTransformBBCode.cpp" />
119+
<Unit filename="bbcode/BBCodeParser.cpp" />
117120
<Unit filename="bbcode/CustomizedSimpleBBCode.cpp" />
118121
<Unit filename="bbcode/HorizontalRuleBBCode.cpp" />
119122
<Unit filename="bbcode/KillSpacesBeforeNewline.cpp" />

0 commit comments

Comments
 (0)