Skip to content

Commit 9aeb149

Browse files
committed
tests: add some tests cases for "default" BB codes
1 parent e84d384 commit 9aeb149

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

tests/components/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(component_tests_sources
1818
../../code/bbcode/AdvancedTemplateBBCode.cpp
1919
../../code/bbcode/BBCodeParser.cpp
2020
../../code/bbcode/CustomizedSimpleBBCode.cpp
21+
../../code/bbcode/DefaultCodes.hpp
2122
../../code/bbcode/HorizontalRuleBBCode.cpp
2223
../../code/bbcode/ListBBCode.cpp
2324
../../code/bbcode/SimpleBBCode.cpp
@@ -51,6 +52,7 @@ set(component_tests_sources
5152
bbcode/AdvancedTplAmpTransformBBCode.cpp
5253
bbcode/BBCodeParser.cpp
5354
bbcode/CustomizedSimpleBBCode.cpp
55+
bbcode/DefaultCodes.cpp
5456
bbcode/HorizontalRuleBBCode.cpp
5557
bbcode/KillSpacesBeforeNewline.cpp
5658
bbcode/ListBBCode.cpp
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
-------------------------------------------------------------------------------
3+
This file is part of the Private Message Database test suite.
4+
Copyright (C) 2025 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/DefaultCodes.hpp"
23+
24+
TEST_CASE("default BB codes")
25+
{
26+
const std::string forum_url = "https://for.um/";
27+
28+
// Currently, the BBCodeParser has no way to directly get the added codes.
29+
// So we just check whether they are actually applied to texts instead.
30+
31+
SECTION("check that [b], [i], [s] and [u] codes get applied")
32+
{
33+
BBCodeParser parser;
34+
bbcode_default::addDefaultCodes(parser);
35+
36+
const std::string input = "[b]bold[/b] [i]italics[/i] [s]strike throug[/s] [u]underlined[/u]";
37+
const std::string expected = "<b>bold</b> <i>italics</i> <span style=\"text-decoration:line-through;\">strike throug</span> <u>underlined</u>";
38+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::HTML4_01, true);
39+
40+
REQUIRE( parsed == expected );
41+
}
42+
43+
SECTION("check that [sub] code gets applied")
44+
{
45+
BBCodeParser parser;
46+
bbcode_default::addDefaultCodes(parser);
47+
48+
const std::string input = "a[sub]1[/sub]+a[sub]2[/sub]=a[sub]3[/sub]";
49+
const std::string expected = "a<sub>1</sub>+a<sub>2</sub>=a<sub>3</sub>";
50+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::HTML4_01, true);
51+
52+
REQUIRE( parsed == expected );
53+
}
54+
55+
SECTION("check that [sup] code gets applied")
56+
{
57+
BBCodeParser parser;
58+
bbcode_default::addDefaultCodes(parser);
59+
60+
const std::string input = "a[sup]2[/sup]+b[sup]2[/sup]=c[sup]2[/sup]";
61+
const std::string expected = "a<sup>2</sup>+b<sup>2</sup>=c<sup>2</sup>";
62+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::HTML4_01, true);
63+
64+
REQUIRE( parsed == expected );
65+
}
66+
67+
SECTION("check that alignment codes get applied")
68+
{
69+
BBCodeParser parser;
70+
bbcode_default::addDefaultCodes(parser);
71+
72+
const std::string input = "[left]left text[/left]\n[right]right text[/right]\n[center]centered text[/center]";
73+
const std::string expected = "<div align=\"left\">left text</div><br>\n<div align=\"right\">right text</div><br>\n<center>centered text</center>";
74+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::HTML4_01, true);
75+
76+
REQUIRE( parsed == expected );
77+
}
78+
79+
SECTION("check that [tt] code gets applied")
80+
{
81+
BBCodeParser parser;
82+
bbcode_default::addDefaultCodes(parser);
83+
84+
const std::string input = "[tt]code[/tt]";
85+
const std::string expected = "<tt style=\"font-size: medium\">code</tt>";
86+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::HTML4_01, true);
87+
88+
REQUIRE( parsed == expected );
89+
}
90+
91+
SECTION("check that simple [url] code gets applied")
92+
{
93+
BBCodeParser parser;
94+
bbcode_default::addDefaultCodes(parser);
95+
96+
const std::string input = "[url]https://example.com/?foo=bar&baz=1[/url]";
97+
const std::string expected = "<a href=\"https://example.com/?foo=bar&amp;baz=1\" target=\"_blank\">https://example.com/?foo=bar&amp;baz=1</a>";
98+
const auto parsed = parser.parse(input, forum_url, HTMLStandard::HTML4_01, true);
99+
100+
REQUIRE( parsed == expected );
101+
}
102+
103+
// TODO: Add more checks for the other default codes.
104+
}

tests/components/component_tests.cbp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Unit filename="../../code/bbcode/BBCodeParser.hpp" />
6464
<Unit filename="../../code/bbcode/CustomizedSimpleBBCode.cpp" />
6565
<Unit filename="../../code/bbcode/CustomizedSimpleBBCode.hpp" />
66+
<Unit filename="../../code/bbcode/DefaultCodes.hpp" />
6667
<Unit filename="../../code/bbcode/HorizontalRuleBBCode.cpp" />
6768
<Unit filename="../../code/bbcode/HorizontalRuleBBCode.hpp" />
6869
<Unit filename="../../code/bbcode/ListBBCode.cpp" />
@@ -119,6 +120,7 @@
119120
<Unit filename="bbcode/AdvancedTplAmpTransformBBCode.cpp" />
120121
<Unit filename="bbcode/BBCodeParser.cpp" />
121122
<Unit filename="bbcode/CustomizedSimpleBBCode.cpp" />
123+
<Unit filename="bbcode/DefaultCodes.cpp" />
122124
<Unit filename="bbcode/HorizontalRuleBBCode.cpp" />
123125
<Unit filename="bbcode/KillSpacesBeforeNewline.cpp" />
124126
<Unit filename="bbcode/ListBBCode.cpp" />

0 commit comments

Comments
 (0)