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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
#include "textformatter.h"
#include "3rd-party/catch.hpp"
#include "regexmanager.h"
using namespace newsboat;
TEST_CASE("lines marked as `wrappable` are wrapped to fit width",
"[TextFormatter]")
{
TextFormatter fmt;
fmt.add_lines({std::make_pair(LineType::wrappable,
"this one is going to be wrapped"),
std::make_pair(LineType::softwrappable,
"this one is going to be wrapped at the window "
"border"),
std::make_pair(LineType::nonwrappable,
"this one is going to be preserved even though "
"it's much longer")});
SECTION("formatting to plain text") {
const std::string expected =
"this one \n"
"is going \n"
"to be \n"
"wrapped\n"
"this one is going to be wrapped at the \n"
"window border\n"
"this one is going to be preserved even though it's "
"much longer\n";
REQUIRE(fmt.format_text_plain(10, 40) == expected);
}
SECTION("formatting to list") {
const std::string expected_text =
"{list"
"{listitem text:\"this one \"}"
"{listitem text:\"is going \"}"
"{listitem text:\"to be \"}"
"{listitem text:\"wrapped\"}"
"{listitem text:\"this one is going to be wrapped at "
"the \"}"
"{listitem text:\"window border\"}"
"{listitem text:\"this one is going to be preserved "
"even though it's much longer\"}"
"}";
const std::size_t expected_count = 7;
const auto result =
fmt.format_text_to_list(nullptr, "", 10, 40);
REQUIRE(result.first == expected_text);
REQUIRE(result.second == expected_count);
}
}
TEST_CASE("line wrapping works for non-space-separated text", "[TextFormatter]")
{
TextFormatter fmt;
fmt.add_lines({std::make_pair(LineType::wrappable,
" つれづれなるままに、ひぐらしすずりにむかいて、")});
SECTION("preserve indent and doesn't return broken UTF-8") {
const std::string expected =
(" つれづれなるまま\n"
" に、ひぐらしすず\n"
" りにむかいて、\n");
REQUIRE(fmt.format_text_plain(20) == expected);
// +1 is not enough to store single wide-width char
REQUIRE(fmt.format_text_plain(20 + 1) == expected);
}
SECTION("truncate indent if given window width is too narrow") {
REQUIRE(fmt.format_text_plain(1) == (" \n"));
REQUIRE(fmt.format_text_plain(2) == (" \n"));
REQUIRE(fmt.format_text_plain(3) == (" \n"));
}
SECTION("discard current word if there's not enough space to put "
"single char") {
REQUIRE(fmt.format_text_plain(4) == (" \n"));
REQUIRE(fmt.format_text_plain(5) == (" \n"));
}
}
TEST_CASE("regex manager is used by format_text_to_list if one is passed",
"[TextFormatter]")
{
TextFormatter fmt;
fmt.add_line(LineType::wrappable, "Highlight me please!");
RegexManager rxmgr;
// the choice of green text on red background does not reflect my
// personal taste (or lack thereof) :)
rxmgr.handle_action(
"highlight", {"article", "please", "green", "default"});
const std::string expected_text =
"{list"
"{listitem text:\"Highlight me <0>please</>!\"}"
"}";
const std::size_t expected_count = 1;
const auto result = fmt.format_text_to_list(&rxmgr, "article", 100);
REQUIRE(result.first == expected_text);
REQUIRE(result.second == expected_count);
}
TEST_CASE("<hr> is rendered as a string of dashes framed with newlines",
"[TextFormatter]")
{
TextFormatter fmt;
fmt.add_line(LineType::hr, "");
SECTION("width = 3") {
const std::string expected =
"\n"
" - "
"\n"
"\n";
REQUIRE(fmt.format_text_plain(3) == expected);
}
SECTION("width = 10") {
const std::string expected =
"\n"
" -------- "
"\n"
"\n";
REQUIRE(fmt.format_text_plain(10) == expected);
}
SECTION("width = 72") {
const std::string expected =
"\n"
" -----------------------------------------------------"
"----------------- "
"\n"
"\n";
REQUIRE(fmt.format_text_plain(72) == expected);
}
}
TEST_CASE("wrappable sequences longer then format width are forced-wrapped",
"[TextFormatter]")
{
TextFormatter fmt;
fmt.add_line(LineType::wrappable, "0123456789101112");
fmt.add_line(LineType::softwrappable, "0123456789101112");
fmt.add_line(LineType::nonwrappable, "0123456789101112");
const std::string expected =
"01234\n"
"56789\n"
"10111\n"
"2\n"
"0123456789\n"
"101112\n"
"0123456789101112\n";
REQUIRE(fmt.format_text_plain(5, 10) == expected);
}
TEST_CASE("Lines marked as non-wrappable are always returned verbatim",
"[TextFormatter]")
{
TextFormatter fmt;
fmt.add_line(LineType::wrappable, " 0123456789101112");
fmt.add_line(LineType::softwrappable, " 0123456789101112");
fmt.add_line(LineType::nonwrappable, " 0123456789101112");
const std::string expected =
" \n"
" \n"
" 0123456789101112\n";
REQUIRE(fmt.format_text_plain(1, 1) == expected);
}
/*
* A simple wrapping function would simply split the line at the wrapping width.
* This, while it technically works, misaligns the text if the line is split
* before the whitespace separating the words.
* For example:
* "just a test"
* would be wrapped as
* "just"
* " a "
* "test"
* on a screen 4 columns wide. In this example, the 'a' is misaligned on the
* second line and it would make the text look jagged with a bigger input. Thus
* spaces at the beginning of lines after wrapping should be dropped.
*/
TEST_CASE("Ignore whitespace that's going to be wrapped onto the next line",
"[TextFormatter]")
{
TextFormatter fmt;
fmt.add_line(LineType::wrappable, "just a test");
const std::string expected =
"just\n"
"a \n"
"test\n";
REQUIRE(fmt.format_text_plain(4) == expected);
}
TEST_CASE(
"softwrappable lines are wrapped by format_text_to_list if "
"total_width != 0",
"[TextFormatter]")
{
TextFormatter fmt;
fmt.add_line(LineType::softwrappable, "just a test");
const size_t wrap_width = 100;
RegexManager* rxman = nullptr;
const std::string location = "";
SECTION("total_width == 4") {
const std::string expected_text =
"{list"
"{listitem text:\"just\"}"
"{listitem text:\"a \"}"
"{listitem text:\"test\"}"
"}";
const std::size_t expected_count = 3;
const auto result =
fmt.format_text_to_list(rxman, location, wrap_width, 4);
REQUIRE(result.first == expected_text);
REQUIRE(result.second == expected_count);
}
SECTION("total_width == 0") {
const std::string expected_text =
"{list"
"{listitem text:\"just a test\"}"
"}";
const std::size_t expected_count = 1;
const auto result =
fmt.format_text_to_list(rxman, location, wrap_width, 0);
REQUIRE(result.first == expected_text);
REQUIRE(result.second == expected_count);
}
}
TEST_CASE("Lines consisting entirely of spaces are replaced "
"by a single space",
"[TextFormatter]")
{
// Limit for wrappable lines
const size_t wrap_width = 3;
// Limit for softwrapable lines
const size_t total_width = 4;
TextFormatter fmt;
// All of these lines are longer than the wrap limits. If these lines were
// wrapped, the result would have more than 4 lines.
fmt.add_line(LineType::wrappable, " ");
fmt.add_line(LineType::wrappable, " ");
fmt.add_line(LineType::softwrappable, " ");
fmt.add_line(LineType::softwrappable, " ");
const std::string expected_text =
"{list"
"{listitem text:\" \"}"
"{listitem text:\" \"}"
"{listitem text:\" \"}"
"{listitem text:\" \"}"
"}";
const std::size_t expected_count = 4;
RegexManager* rxman = nullptr;
const std::string location = "";
const auto result = fmt.format_text_to_list(rxman, location, wrap_width,
total_width);
REQUIRE(result.first == expected_text);
REQUIRE(result.second == expected_count);
}
|