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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
#include "fmtstrformatter.h"
#include "3rd-party/catch.hpp"
using namespace newsboat;
TEST_CASE("do_format replaces variables with values", "[FmtStrFormatter]")
{
FmtStrFormatter fmt;
SECTION("One format variable") {
fmt.register_fmt('a', "AAA");
SECTION("Conditional format strings") {
REQUIRE(fmt.do_format("%?a?%a&no?") == "AAA");
REQUIRE(fmt.do_format("%?b?%b&no?") == "no");
REQUIRE(fmt.do_format("%?a?[%-4a]&no?") == "[AAA ]");
}
SECTION("Two format variables") {
fmt.register_fmt('b', "BBB");
REQUIRE(fmt.do_format(
"asdf | %a | %?c?%a%b&%b%a? | qwert") ==
"asdf | AAA | BBBAAA | qwert");
REQUIRE(fmt.do_format("%?c?asdf?") == "");
SECTION("Three format variables") {
fmt.register_fmt('c', "CCC");
SECTION("Simple cases") {
REQUIRE(fmt.do_format("") == "");
// illegal single %
REQUIRE(fmt.do_format("%") == "");
REQUIRE(fmt.do_format("%%") == "%");
REQUIRE(fmt.do_format("%a%b%c") ==
"AAABBBCCC");
REQUIRE(fmt.do_format(
"%%%a%%%b%%%c%%") ==
"%AAA%BBB%CCC%");
}
SECTION("Alignment") {
REQUIRE(fmt.do_format("%4a") == " AAA");
REQUIRE(fmt.do_format("%-4a") ==
"AAA ");
SECTION("Alignment limits") {
REQUIRE(fmt.do_format("%2a") ==
"AA");
REQUIRE(fmt.do_format("%-2a") ==
"AA");
}
}
SECTION("Complex format string") {
REQUIRE(fmt.do_format("<%a> <%5b> | "
"%-5c%%") ==
"<AAA> < BBB> | CCC %");
REQUIRE(fmt.do_format("asdf | %a | "
"%?c?%a%b&%b%a? "
"| qwert") ==
"asdf | AAA | AAABBB | qwert");
}
SECTION("Format string fillers") {
REQUIRE(fmt.do_format("%>X", 3) ==
"XXX");
REQUIRE(fmt.do_format("%a%> %b", 10) ==
"AAA BBB");
REQUIRE(fmt.do_format("%a%> %b", 0) ==
"AAA BBB");
}
SECTION("Conditional format string") {
REQUIRE(fmt.do_format("%?c?asdf?") ==
"asdf");
}
}
}
}
}
TEST_CASE("do_format supports multibyte characters", "[FmtStrFormatter]")
{
FmtStrFormatter fmt;
SECTION("One format variable") {
fmt.register_fmt('a', "АБВ");
SECTION("Conditional format strings") {
REQUIRE(fmt.do_format("%?a?%a&no?") == "АБВ");
REQUIRE(fmt.do_format("%?b?%b&no?") == "no");
REQUIRE(fmt.do_format("%?a?[%-4a]&no?") == "[АБВ ]");
}
SECTION("Two format variables") {
fmt.register_fmt('b', "буква");
REQUIRE(fmt.do_format(
"asdf | %a | %?c?%a%b&%b%a? | qwert") ==
"asdf | АБВ | букваАБВ | qwert");
REQUIRE(fmt.do_format("%?c?asdf?") == "");
SECTION("Three format variables") {
fmt.register_fmt('c', "ещё одна переменная");
SECTION("Simple cases") {
REQUIRE(fmt.do_format("") == "");
// illegal single %
REQUIRE(fmt.do_format("%") == "");
REQUIRE(fmt.do_format("%%") == "%");
REQUIRE(fmt.do_format("%a%b%c") ==
"АБВбукваещё одна переменная");
REQUIRE(fmt.do_format(
"%%%a%%%b%%%c%%") ==
"%АБВ%буква%ещё одна переменная%");
}
SECTION("Alignment") {
REQUIRE(fmt.do_format("%4a") == " АБВ");
REQUIRE(fmt.do_format("%-4a") ==
"АБВ ");
SECTION("Alignment limits") {
REQUIRE(fmt.do_format("%2a") ==
"АБ");
REQUIRE(fmt.do_format("%-2a") ==
"АБ");
}
}
SECTION("Complex format string") {
REQUIRE(fmt.do_format("<%a> <%5b> | "
"%-5c%%") ==
"<АБВ> <буква> | ещё о%");
REQUIRE(fmt.do_format("asdf | %a | "
"%?c?%a%b&%b%a? "
"| qwert") ==
"asdf | АБВ | АБВбуква | qwert");
}
SECTION("Format string fillers") {
REQUIRE(fmt.do_format("%>X", 3) ==
"XXX");
REQUIRE(fmt.do_format("%a%> %b", 10) ==
"АБВ буква");
REQUIRE(fmt.do_format("%a%> %b", 0) ==
"АБВ буква");
}
SECTION("Conditional format string") {
REQUIRE(fmt.do_format("%?c?asdf?") ==
"asdf");
}
}
}
}
}
TEST_CASE("do_format() keeps wide characters within specified width",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
fmt.register_fmt('a', "ABC");
fmt.register_fmt('b', "def");
REQUIRE(fmt.do_format("%a %b", 0) == "ABC def");
REQUIRE(fmt.do_format("%a %b", 10) == "ABC def");
REQUIRE(fmt.do_format("%a %b", 9) == "ABC de");
REQUIRE(fmt.do_format("%a %b", 7) == "ABC ");
REQUIRE(fmt.do_format("%a %b", 6) == "ABC");
REQUIRE(fmt.do_format("%a %b", 4) == "AB");
}
TEST_CASE("do_format() does not include wide character if only 1 colum is left",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
fmt.register_fmt('a', "ABC");
REQUIRE(fmt.do_format("%a", 6) == "ABC");
REQUIRE(fmt.do_format("%a", 5) == "AB");
REQUIRE(fmt.do_format("%a", 4) == "AB");
REQUIRE(fmt.do_format("%-6a", 0) == "ABC");
REQUIRE(fmt.do_format("%-5a", 0) == "AB ");
REQUIRE(fmt.do_format("%-4a", 0) == "AB");
REQUIRE(fmt.do_format("%6a", 0) == "ABC");
REQUIRE(fmt.do_format("%5a", 0) == " AB");
REQUIRE(fmt.do_format("%4a", 0) == "AB");
}
TEST_CASE("do_format() does not escape less-than signs in regular text",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
fmt.register_fmt('a', "AAA");
fmt.register_fmt('b', "BBB");
REQUIRE(fmt.do_format("%a <%b>", 0) == "AAA <BBB>");
}
TEST_CASE("do_format() does not escape less-than signs in filling format",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
fmt.register_fmt('a', "AAA");
fmt.register_fmt('b', "BBB");
REQUIRE(fmt.do_format("%a%>.%b", 10) == "AAA....BBB");
REQUIRE(fmt.do_format("%a%><%b", 10) == "AAA<<<<BBB");
REQUIRE(fmt.do_format("%a%>>%b", 10) == "AAA>>>>BBB");
}
TEST_CASE("do_format ignores \"%?\" at the end of the format string (which "
"looks like a conditional but really isn't",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
REQUIRE(fmt.do_format("%?") == "");
}
TEST_CASE("do_format replaces %Nx with the value of \"x\", padded "
"with spaces on the left to fit N columns",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
SECTION("Undefined format char") {
REQUIRE(fmt.do_format("%4a") == " ");
REQUIRE(fmt.do_format("%8a") == " ");
}
SECTION("Defined format char") {
fmt.register_fmt('a', "hello");
fmt.register_fmt('r', "привет");
REQUIRE(fmt.do_format("%8a") == " hello");
REQUIRE(fmt.do_format("%5a") == "hello");
REQUIRE(fmt.do_format("%8r") == " привет");
REQUIRE(fmt.do_format("%6r") == "привет");
{
INFO("If the value is bigger than the padding, only first N "
"characters are used");
REQUIRE(fmt.do_format("%4a") == "hell");
REQUIRE(fmt.do_format("%2a") == "he");
REQUIRE(fmt.do_format("%4r") == "прив");
REQUIRE(fmt.do_format("%3r") == "при");
}
}
}
TEST_CASE("do_format replaces %-Nx with the value of \"x\", padded "
"with spaces on the right to fit N columns",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
SECTION("Undefined format char") {
REQUIRE(fmt.do_format("%-4a") == " ");
REQUIRE(fmt.do_format("%-8a") == " ");
}
SECTION("Defined format char") {
fmt.register_fmt('a', "hello");
fmt.register_fmt('r', "привет");
REQUIRE(fmt.do_format("%-8a") == "hello ");
REQUIRE(fmt.do_format("%-5a") == "hello");
REQUIRE(fmt.do_format("%-8r") == "привет ");
REQUIRE(fmt.do_format("%-6r") == "привет");
{
INFO("If the value is bigger than the padding, only first N "
"characters are used");
REQUIRE(fmt.do_format("%-4a") == "hell");
REQUIRE(fmt.do_format("%-2a") == "he");
REQUIRE(fmt.do_format("%-4r") == "прив");
REQUIRE(fmt.do_format("%-3r") == "при");
}
}
}
TEST_CASE("%>[char] pads consecutive text to the right, using [char] for "
"the padding",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
fmt.register_fmt('x', "example string");
fmt.register_fmt('y', "пример строки");
SECTION("The total length of the string is specified in the argument to do_format") {
{
INFO("Default (zero) is \"as long as needed to fit all the values\"");
REQUIRE(fmt.do_format("%x%> %y") == "example string пример строки");
REQUIRE(fmt.do_format("%x%> %y",
0) == "example string пример строки");
}
REQUIRE(fmt.do_format("%x%> %y",
30) == "example string пример строки");
REQUIRE(fmt.do_format("%x%> %y", 45)
== "example string пример строки");
}
SECTION("[char] is used for padding") {
REQUIRE(fmt.do_format("%x%>m%y") == "example stringmпример строки");
REQUIRE(fmt.do_format("%x%>f%y",
0) == "example stringfпример строки");
REQUIRE(fmt.do_format("%x%>k%y",
30) == "example stringkkkпример строки");
REQUIRE(fmt.do_format("%x%>i%y", 45)
== "example stringiiiiiiiiiiiiiiiiiiпример строки");
}
SECTION("If multiple %>[char] specifiers are used, the first one has a "
"maximal width and the others have the width of one column") {
fmt.register_fmt('s', "_short_");
const auto format = std::string("%s%>a%s%>b%s");
REQUIRE(fmt.do_format(format) == "_short_a_short_b_short_");
REQUIRE(fmt.do_format(format, 30) == "_short_aaaaaaaa_short_b_short_");
}
}
TEST_CASE("%?[char]?[then-format]&[else-format]? is replaced by "
"\"then-format\" if \"[char]\" has non-empty value, otherwise it's "
"replaced by \"else-format\"",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
fmt.register_fmt('t', "this is a non-empty string");
fmt.register_fmt('m', "");
SECTION("Standard case") {
REQUIRE(fmt.do_format("%?t?non-empty&empty?") == "non-empty");
REQUIRE(fmt.do_format("%?m?non-empty&empty?") == "empty");
REQUIRE(fmt.do_format("%?t?непустое&пустое?") ==
"непустое");
REQUIRE(fmt.do_format("%?m?непустое&пустое?") == "пустое");
}
SECTION("Else-format is optional") {
REQUIRE(fmt.do_format("%?t?непустое?") == "непустое");
REQUIRE(fmt.do_format("%?m?непустое?") == "");
}
SECTION("Whitespace on its own is handled same as empty") {
fmt.register_fmt('a', " \t ");
fmt.register_fmt('b', " some whitespace ");
REQUIRE(fmt.do_format("%?a?non-empty&empty?") == "empty");
REQUIRE(fmt.do_format("%?b?non-empty&empty?") == "non-empty");
}
}
TEST_CASE("do_format replaces \"%%\" with a percent sign",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
REQUIRE(fmt.do_format("%%") == "%");
}
TEST_CASE("Ampersand is treated literally outside of conditionals",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
fmt.register_fmt('a', "A");
fmt.register_fmt('b', "B");
REQUIRE(fmt.do_format("%a & %b were sitting on a pipe")
== "A & B were sitting on a pipe");
}
TEST_CASE("Question mark is treated literally outside of conditionals",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
fmt.register_fmt('x', "What's the ultimate answer");
fmt.register_fmt('y', "42");
REQUIRE(fmt.do_format("%x? %y") == "What's the ultimate answer? 42");
}
TEST_CASE("Center Text formatting",
"[FmtStrFormatter]")
{
FmtStrFormatter fmt;
fmt.register_fmt('T', "whatever");
REQUIRE(fmt.do_format("%=20T", 0) == " whatever ");
REQUIRE(fmt.do_format("%=19T", 0) == " whatever ");
REQUIRE(fmt.do_format("%=3T", 0) == "wha");
REQUIRE(fmt.do_format("%=0T", 20) == " whatever ");
}
|