|
| 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 | +} |
0 commit comments