| 1 |
|
| 2 |
// Copyright Catch2 Authors |
| 3 |
// Distributed under the Boost Software License, Version 1.0. |
| 4 |
// (See accompanying file LICENSE.txt or copy at |
| 5 |
// https://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
// SPDX-License-Identifier: BSL-1.0 |
| 8 |
|
| 9 |
#include <catch2/catch_test_macros.hpp> |
| 10 |
#include <catch2/internal/catch_textflow.hpp> |
| 11 |
|
| 12 |
#include <sstream> |
| 13 |
|
| 14 |
using Catch::TextFlow::Column; |
| 15 |
using Catch::TextFlow::AnsiSkippingString; |
| 16 |
|
| 17 |
namespace { |
| 18 |
static std::string as_written(Column const& c) { |
| 19 |
std::stringstream sstr; |
| 20 |
sstr << c; |
| 21 |
return sstr.str(); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
TEST_CASE( "TextFlow::Column one simple line", |
| 26 |
"[TextFlow][column][approvals]" ) { |
| 27 |
Column col( "simple short line" ); |
| 28 |
|
| 29 |
REQUIRE(as_written(col) == "simple short line"); |
| 30 |
} |
| 31 |
|
| 32 |
TEST_CASE( "TextFlow::Column respects already present newlines", |
| 33 |
"[TextFlow][column][approvals]" ) { |
| 34 |
Column col( "abc\ndef" ); |
| 35 |
REQUIRE( as_written( col ) == "abc\ndef" ); |
| 36 |
} |
| 37 |
|
| 38 |
TEST_CASE( "TextFlow::Column respects width setting", |
| 39 |
"[TextFlow][column][approvals]" ) { |
| 40 |
Column col( "The quick brown fox jumped over the lazy dog" ); |
| 41 |
|
| 42 |
SECTION( "width=20" ) { |
| 43 |
col.width( 20 ); |
| 44 |
REQUIRE( as_written( col ) == "The quick brown fox\n" |
| 45 |
"jumped over the lazy\n" |
| 46 |
"dog" ); |
| 47 |
} |
| 48 |
SECTION("width=10") { |
| 49 |
col.width( 10 ); |
| 50 |
REQUIRE( as_written( col ) == "The quick\n" |
| 51 |
"brown fox\n" |
| 52 |
"jumped\n" |
| 53 |
"over the\n" |
| 54 |
"lazy dog" ); |
| 55 |
} |
| 56 |
SECTION("width=5") { |
| 57 |
// This is so small some words will have to be split with hyphen |
| 58 |
col.width(5); |
| 59 |
REQUIRE( as_written( col ) == "The\n" |
| 60 |
"quick\n" |
| 61 |
"brown\n" |
| 62 |
"fox\n" |
| 63 |
"jump-\n" |
| 64 |
"ed\n" |
| 65 |
"over\n" |
| 66 |
"the\n" |
| 67 |
"lazy\n" |
| 68 |
"dog" ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
TEST_CASE( "TextFlow::Column respects indentation setting", |
| 73 |
"[TextFlow][column][approvals]" ) { |
| 74 |
Column col( "First line\nSecond line\nThird line" ); |
| 75 |
|
| 76 |
SECTION("Default: no indentation at all") { |
| 77 |
REQUIRE(as_written(col) == "First line\nSecond line\nThird line"); |
| 78 |
} |
| 79 |
SECTION("Indentation on first line only") { |
| 80 |
col.initialIndent(3); |
| 81 |
REQUIRE(as_written(col) == " First line\nSecond line\nThird line"); |
| 82 |
} |
| 83 |
SECTION("Indentation on all lines") { |
| 84 |
col.indent(3); |
| 85 |
REQUIRE(as_written(col) == " First line\n Second line\n Third line"); |
| 86 |
} |
| 87 |
SECTION("Indentation on later lines only") { |
| 88 |
col.indent(5).initialIndent(0); |
| 89 |
REQUIRE(as_written(col) == "First line\n Second line\n Third line"); |
| 90 |
} |
| 91 |
SECTION("Different indentation on first and later lines") { |
| 92 |
col.initialIndent(1).indent(2); |
| 93 |
REQUIRE(as_written(col) == " First line\n Second line\n Third line"); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
TEST_CASE("TextFlow::Column indentation respects whitespace", "[TextFlow][column][approvals]") { |
| 98 |
Column col(" text with whitespace\n after newlines"); |
| 99 |
|
| 100 |
SECTION("No extra indentation") { |
| 101 |
col.initialIndent(0).indent(0); |
| 102 |
REQUIRE(as_written(col) == " text with whitespace\n after newlines"); |
| 103 |
} |
| 104 |
SECTION("Different indentation on first and later lines") { |
| 105 |
col.initialIndent(1).indent(2); |
| 106 |
REQUIRE(as_written(col) == " text with whitespace\n after newlines"); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
TEST_CASE( "TextFlow::Column linebreaking prefers boundary characters", |
| 111 |
"[TextFlow][column][approvals]" ) { |
| 112 |
SECTION("parentheses") { |
| 113 |
Column col("(Hello)aaa(World)"); |
| 114 |
SECTION("width=20") { |
| 115 |
col.width(20); |
| 116 |
REQUIRE(as_written(col) == "(Hello)aaa(World)"); |
| 117 |
} |
| 118 |
SECTION("width=15") { |
| 119 |
col.width(15); |
| 120 |
REQUIRE(as_written(col) == "(Hello)aaa\n(World)"); |
| 121 |
} |
| 122 |
SECTION("width=8") { |
| 123 |
col.width(8); |
| 124 |
REQUIRE(as_written(col) == "(Hello)\naaa\n(World)"); |
| 125 |
} |
| 126 |
} |
| 127 |
SECTION("commas") { |
| 128 |
Column col("Hello, world"); |
| 129 |
col.width(8); |
| 130 |
|
| 131 |
REQUIRE(as_written(col) == "Hello,\nworld"); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
TEST_CASE( "TextFlow::Column respects indentation for empty lines", |
| 137 |
"[TextFlow][column][approvals][!shouldfail]" ) { |
| 138 |
// This is currently bugged and does not do what it should |
| 139 |
Column col("\n\nthird line"); |
| 140 |
col.indent(2); |
| 141 |
|
| 142 |
//auto b = col.begin(); |
| 143 |
//auto e = col.end(); |
| 144 |
|
| 145 |
//auto b1 = *b; |
| 146 |
//++b; |
| 147 |
//auto b2 = *b; |
| 148 |
//++b; |
| 149 |
//auto b3 = *b; |
| 150 |
//++b; |
| 151 |
|
| 152 |
//REQUIRE(b == e); |
| 153 |
|
| 154 |
std::string written = as_written(col); |
| 155 |
|
| 156 |
REQUIRE(written == " \n \n third line"); |
| 157 |
} |
| 158 |
|
| 159 |
TEST_CASE( "TextFlow::Column leading/trailing whitespace", |
| 160 |
"[TextFlow][column][approvals]" ) { |
| 161 |
SECTION("Trailing whitespace") { |
| 162 |
Column col("some trailing whitespace: \t"); |
| 163 |
REQUIRE(as_written(col) == "some trailing whitespace: \t"); |
| 164 |
} |
| 165 |
SECTION("Some leading whitespace") { |
| 166 |
Column col("\t \t whitespace wooo"); |
| 167 |
REQUIRE(as_written(col) == "\t \t whitespace wooo"); |
| 168 |
} |
| 169 |
SECTION("both") { |
| 170 |
Column col(" abc "); |
| 171 |
REQUIRE(as_written(col) == " abc "); |
| 172 |
} |
| 173 |
SECTION("whitespace only") { |
| 174 |
Column col("\t \t"); |
| 175 |
REQUIRE(as_written(col) == "\t \t"); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
TEST_CASE( "TextFlow::Column can handle empty string", |
| 180 |
"[TextFlow][column][approvals]" ) { |
| 181 |
Column col(""); |
| 182 |
REQUIRE(as_written(col) == ""); |
| 183 |
} |
| 184 |
|
| 185 |
TEST_CASE( "#1400 - TextFlow::Column wrapping would sometimes duplicate words", |
| 186 |
"[TextFlow][column][regression][approvals]" ) { |
| 187 |
const auto long_string = std::string( |
| 188 |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nisl \n" |
| 189 |
"massa, luctus ut ligula vitae, suscipit tempus velit. Vivamus sodales, quam in \n" |
| 190 |
"convallis posuere, libero nisi ultricies orci, nec lobortis.\n"); |
| 191 |
|
| 192 |
auto col = Column(long_string) |
| 193 |
.width(79) |
| 194 |
.indent(2); |
| 195 |
|
| 196 |
REQUIRE(as_written(col) == |
| 197 |
" Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nisl \n" |
| 198 |
" massa, luctus ut ligula vitae, suscipit tempus velit. Vivamus sodales, quam\n" |
| 199 |
" in \n" |
| 200 |
" convallis posuere, libero nisi ultricies orci, nec lobortis."); |
| 201 |
} |
| 202 |
|
| 203 |
TEST_CASE( "TextFlow::AnsiSkippingString skips ansi sequences", |
| 204 |
"[TextFlow][ansiskippingstring][approvals]" ) { |
| 205 |
|
| 206 |
SECTION("basic string") { |
| 207 |
std::string text = "a\033[38;2;98;174;239mb\033[38mc\033[0md\033[me"; |
| 208 |
AnsiSkippingString str(text); |
| 209 |
|
| 210 |
SECTION( "iterates forward" ) { |
| 211 |
auto it = str.begin(); |
| 212 |
CHECK(*it == 'a'); |
| 213 |
++it; |
| 214 |
CHECK(*it == 'b'); |
| 215 |
++it; |
| 216 |
CHECK(*it == 'c'); |
| 217 |
++it; |
| 218 |
CHECK(*it == 'd'); |
| 219 |
++it; |
| 220 |
CHECK(*it == 'e'); |
| 221 |
++it; |
| 222 |
CHECK(it == str.end()); |
| 223 |
} |
| 224 |
SECTION( "iterates backwards" ) { |
| 225 |
auto it = str.end(); |
| 226 |
--it; |
| 227 |
CHECK(*it == 'e'); |
| 228 |
--it; |
| 229 |
CHECK(*it == 'd'); |
| 230 |
--it; |
| 231 |
CHECK(*it == 'c'); |
| 232 |
--it; |
| 233 |
CHECK(*it == 'b'); |
| 234 |
--it; |
| 235 |
CHECK(*it == 'a'); |
| 236 |
CHECK(it == str.begin()); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
SECTION( "ansi escape sequences at the start" ) { |
| 241 |
std::string text = "\033[38;2;98;174;239ma\033[38;2;98;174;239mb\033[38mc\033[0md\033[me"; |
| 242 |
AnsiSkippingString str(text); |
| 243 |
auto it = str.begin(); |
| 244 |
CHECK(*it == 'a'); |
| 245 |
++it; |
| 246 |
CHECK(*it == 'b'); |
| 247 |
++it; |
| 248 |
CHECK(*it == 'c'); |
| 249 |
++it; |
| 250 |
CHECK(*it == 'd'); |
| 251 |
++it; |
| 252 |
CHECK(*it == 'e'); |
| 253 |
++it; |
| 254 |
CHECK(it == str.end()); |
| 255 |
--it; |
| 256 |
CHECK(*it == 'e'); |
| 257 |
--it; |
| 258 |
CHECK(*it == 'd'); |
| 259 |
--it; |
| 260 |
CHECK(*it == 'c'); |
| 261 |
--it; |
| 262 |
CHECK(*it == 'b'); |
| 263 |
--it; |
| 264 |
CHECK(*it == 'a'); |
| 265 |
CHECK(it == str.begin()); |
| 266 |
} |
| 267 |
|
| 268 |
SECTION( "ansi escape sequences at the end" ) { |
| 269 |
std::string text = "a\033[38;2;98;174;239mb\033[38mc\033[0md\033[me\033[38;2;98;174;239m"; |
| 270 |
AnsiSkippingString str(text); |
| 271 |
auto it = str.begin(); |
| 272 |
CHECK(*it == 'a'); |
| 273 |
++it; |
| 274 |
CHECK(*it == 'b'); |
| 275 |
++it; |
| 276 |
CHECK(*it == 'c'); |
| 277 |
++it; |
| 278 |
CHECK(*it == 'd'); |
| 279 |
++it; |
| 280 |
CHECK(*it == 'e'); |
| 281 |
++it; |
| 282 |
CHECK(it == str.end()); |
| 283 |
--it; |
| 284 |
CHECK(*it == 'e'); |
| 285 |
--it; |
| 286 |
CHECK(*it == 'd'); |
| 287 |
--it; |
| 288 |
CHECK(*it == 'c'); |
| 289 |
--it; |
| 290 |
CHECK(*it == 'b'); |
| 291 |
--it; |
| 292 |
CHECK(*it == 'a'); |
| 293 |
CHECK(it == str.begin()); |
| 294 |
} |
| 295 |
|
| 296 |
SECTION( "skips consecutive escapes" ) { |
| 297 |
std::string text = "\033[38;2;98;174;239m\033[38;2;98;174;239ma\033[38;2;98;174;239mb\033[38m\033[38m\033[38mc\033[0md\033[me"; |
| 298 |
AnsiSkippingString str(text); |
| 299 |
auto it = str.begin(); |
| 300 |
CHECK(*it == 'a'); |
| 301 |
++it; |
| 302 |
CHECK(*it == 'b'); |
| 303 |
++it; |
| 304 |
CHECK(*it == 'c'); |
| 305 |
++it; |
| 306 |
CHECK(*it == 'd'); |
| 307 |
++it; |
| 308 |
CHECK(*it == 'e'); |
| 309 |
++it; |
| 310 |
CHECK(it == str.end()); |
| 311 |
--it; |
| 312 |
CHECK(*it == 'e'); |
| 313 |
--it; |
| 314 |
CHECK(*it == 'd'); |
| 315 |
--it; |
| 316 |
CHECK(*it == 'c'); |
| 317 |
--it; |
| 318 |
CHECK(*it == 'b'); |
| 319 |
--it; |
| 320 |
CHECK(*it == 'a'); |
| 321 |
CHECK(it == str.begin()); |
| 322 |
} |
| 323 |
|
| 324 |
SECTION( "handles incomplete ansi sequences" ) { |
| 325 |
std::string text = "a\033[b\033[30c\033[30;d\033[30;2e"; |
| 326 |
AnsiSkippingString str(text); |
| 327 |
CHECK(std::string(str.begin(), str.end()) == text); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
TEST_CASE( "TextFlow::AnsiSkippingString computes the size properly", |
| 332 |
"[TextFlow][ansiskippingstring][approvals]" ) { |
| 333 |
std::string text = "\033[38;2;98;174;239m\033[38;2;98;174;239ma\033[38;2;98;174;239mb\033[38m\033[38m\033[38mc\033[0md\033[me"; |
| 334 |
AnsiSkippingString str(text); |
| 335 |
CHECK(str.size() == 5); |
| 336 |
} |
| 337 |
|
| 338 |
TEST_CASE( "TextFlow::AnsiSkippingString substrings properly", |
| 339 |
"[TextFlow][ansiskippingstring][approvals]" ) { |
| 340 |
SECTION("basic test") { |
| 341 |
std::string text = "a\033[38;2;98;174;239mb\033[38mc\033[0md\033[me"; |
| 342 |
AnsiSkippingString str(text); |
| 343 |
auto a = str.begin(); |
| 344 |
auto b = str.begin(); |
| 345 |
++b; |
| 346 |
++b; |
| 347 |
CHECK(str.substring(a, b) == "a\033[38;2;98;174;239mb\033[38m"); |
| 348 |
++a; |
| 349 |
++b; |
| 350 |
CHECK(str.substring(a, b) == "b\033[38mc\033[0m"); |
| 351 |
CHECK(str.substring(a, str.end()) == "b\033[38mc\033[0md\033[me"); |
| 352 |
CHECK(str.substring(str.begin(), str.end()) == text); |
| 353 |
} |
| 354 |
SECTION("escapes at the start") { |
| 355 |
std::string text = "\033[38;2;98;174;239m\033[38;2;98;174;239ma\033[38;2;98;174;239mb\033[38m\033[38m\033[38mc\033[0md\033[me"; |
| 356 |
AnsiSkippingString str(text); |
| 357 |
auto a = str.begin(); |
| 358 |
auto b = str.begin(); |
| 359 |
++b; |
| 360 |
++b; |
| 361 |
CHECK(str.substring(a, b) == "\033[38;2;98;174;239m\033[38;2;98;174;239ma\033[38;2;98;174;239mb\033[38m\033[38m\033[38m"); |
| 362 |
++a; |
| 363 |
++b; |
| 364 |
CHECK(str.substring(a, b) == "b\033[38m\033[38m\033[38mc\033[0m"); |
| 365 |
CHECK(str.substring(a, str.end()) == "b\033[38m\033[38m\033[38mc\033[0md\033[me"); |
| 366 |
CHECK(str.substring(str.begin(), str.end()) == text); |
| 367 |
} |
| 368 |
SECTION("escapes at the end") { |
| 369 |
std::string text = "a\033[38;2;98;174;239mb\033[38mc\033[0md\033[me\033[38m"; |
| 370 |
AnsiSkippingString str(text); |
| 371 |
auto a = str.begin(); |
| 372 |
auto b = str.begin(); |
| 373 |
++b; |
| 374 |
++b; |
| 375 |
CHECK(str.substring(a, b) == "a\033[38;2;98;174;239mb\033[38m"); |
| 376 |
++a; |
| 377 |
++b; |
| 378 |
CHECK(str.substring(a, b) == "b\033[38mc\033[0m"); |
| 379 |
CHECK(str.substring(a, str.end()) == "b\033[38mc\033[0md\033[me\033[38m"); |
| 380 |
CHECK(str.substring(str.begin(), str.end()) == text); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
TEST_CASE( "TextFlow::Column skips ansi escape sequences", |
| 385 |
"[TextFlow][column][approvals]" ) { |
| 386 |
std::string text = "\033[38;2;98;174;239m\033[38;2;198;120;221mThe quick brown \033[38;2;198;120;221mfox jumped over the lazy dog\033[0m"; |
| 387 |
Column col(text); |
| 388 |
|
| 389 |
SECTION( "width=20" ) { |
| 390 |
col.width( 20 ); |
| 391 |
REQUIRE( as_written( col ) == "\033[38;2;98;174;239m\033[38;2;198;120;221mThe quick brown \033[38;2;198;120;221mfox\n" |
| 392 |
"jumped over the lazy\n" |
| 393 |
"dog\033[0m" ); |
| 394 |
} |
| 395 |
|
| 396 |
SECTION( "width=80" ) { |
| 397 |
col.width( 80 ); |
| 398 |
REQUIRE( as_written( col ) == text ); |
| 399 |
} |
| 400 |
} |