| 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 | |
| 16 | namespace { |
| 17 | static std::string as_written(Column const& c) { |
| 18 | std::stringstream sstr; |
| 19 | sstr << c; |
| 20 | return sstr.str(); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | TEST_CASE( "TextFlow::Column one simple line", |
| 25 | "[TextFlow][column][approvals]" ) { |
| 26 | Column col( "simple short line" ); |
| 27 | |
| 28 | REQUIRE(as_written(col) == "simple short line"); |
| 29 | } |
| 30 | |
| 31 | TEST_CASE( "TextFlow::Column respects already present newlines", |
| 32 | "[TextFlow][column][approvals]" ) { |
| 33 | Column col( "abc\ndef" ); |
| 34 | REQUIRE( as_written( col ) == "abc\ndef" ); |
| 35 | } |
| 36 | |
| 37 | TEST_CASE( "TextFlow::Column respects width setting", |
| 38 | "[TextFlow][column][approvals]" ) { |
| 39 | Column col( "The quick brown fox jumped over the lazy dog" ); |
| 40 | |
| 41 | SECTION( "width=20" ) { |
| 42 | col.width( 20 ); |
| 43 | REQUIRE( as_written( col ) == "The quick brown fox\n" |
| 44 | "jumped over the lazy\n" |
| 45 | "dog" ); |
| 46 | } |
| 47 | SECTION("width=10") { |
| 48 | col.width( 10 ); |
| 49 | REQUIRE( as_written( col ) == "The quick\n" |
| 50 | "brown fox\n" |
| 51 | "jumped\n" |
| 52 | "over the\n" |
| 53 | "lazy dog" ); |
| 54 | } |
| 55 | SECTION("width=5") { |
| 56 | // This is so small some words will have to be split with hyphen |
| 57 | col.width(5); |
| 58 | REQUIRE( as_written( col ) == "The\n" |
| 59 | "quick\n" |
| 60 | "brown\n" |
| 61 | "fox\n" |
| 62 | "jump-\n" |
| 63 | "ed\n" |
| 64 | "over\n" |
| 65 | "the\n" |
| 66 | "lazy\n" |
| 67 | "dog" ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | TEST_CASE( "TextFlow::Column respects indentation setting", |
| 72 | "[TextFlow][column][approvals]" ) { |
| 73 | Column col( "First line\nSecond line\nThird line" ); |
| 74 | |
| 75 | SECTION("Default: no indentation at all") { |
| 76 | REQUIRE(as_written(col) == "First line\nSecond line\nThird line"); |
| 77 | } |
| 78 | SECTION("Indentation on first line only") { |
| 79 | col.initialIndent(3); |
| 80 | REQUIRE(as_written(col) == " First line\nSecond line\nThird line"); |
| 81 | } |
| 82 | SECTION("Indentation on all lines") { |
| 83 | col.indent(3); |
| 84 | REQUIRE(as_written(col) == " First line\n Second line\n Third line"); |
| 85 | } |
| 86 | SECTION("Indentation on later lines only") { |
| 87 | col.indent(5).initialIndent(0); |
| 88 | REQUIRE(as_written(col) == "First line\n Second line\n Third line"); |
| 89 | } |
| 90 | SECTION("Different indentation on first and later lines") { |
| 91 | col.initialIndent(1).indent(2); |
| 92 | REQUIRE(as_written(col) == " First line\n Second line\n Third line"); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | TEST_CASE("TextFlow::Column indentation respects whitespace", "[TextFlow][column][approvals]") { |
| 97 | Column col(" text with whitespace\n after newlines"); |
| 98 | |
| 99 | SECTION("No extra indentation") { |
| 100 | col.initialIndent(0).indent(0); |
| 101 | REQUIRE(as_written(col) == " text with whitespace\n after newlines"); |
| 102 | } |
| 103 | SECTION("Different indentation on first and later lines") { |
| 104 | col.initialIndent(1).indent(2); |
| 105 | REQUIRE(as_written(col) == " text with whitespace\n after newlines"); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | TEST_CASE( "TextFlow::Column linebreaking prefers boundary characters", |
| 110 | "[TextFlow][column][approvals]" ) { |
| 111 | SECTION("parentheses") { |
| 112 | Column col("(Hello)aaa(World)"); |
| 113 | SECTION("width=20") { |
| 114 | col.width(20); |
| 115 | REQUIRE(as_written(col) == "(Hello)aaa(World)"); |
| 116 | } |
| 117 | SECTION("width=15") { |
| 118 | col.width(15); |
| 119 | REQUIRE(as_written(col) == "(Hello)aaa\n(World)"); |
| 120 | } |
| 121 | SECTION("width=8") { |
| 122 | col.width(8); |
| 123 | REQUIRE(as_written(col) == "(Hello)\naaa\n(World)"); |
| 124 | } |
| 125 | } |
| 126 | SECTION("commas") { |
| 127 | Column col("Hello, world"); |
| 128 | col.width(8); |
| 129 | |
| 130 | REQUIRE(as_written(col) == "Hello,\nworld"); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | TEST_CASE( "TextFlow::Column respects indentation for empty lines", |
| 136 | "[TextFlow][column][approvals][!shouldfail]" ) { |
| 137 | // This is currently bugged and does not do what it should |
| 138 | Column col("\n\nthird line"); |
| 139 | col.indent(2); |
| 140 | |
| 141 | //auto b = col.begin(); |
| 142 | //auto e = col.end(); |
| 143 | |
| 144 | //auto b1 = *b; |
| 145 | //++b; |
| 146 | //auto b2 = *b; |
| 147 | //++b; |
| 148 | //auto b3 = *b; |
| 149 | //++b; |
| 150 | |
| 151 | //REQUIRE(b == e); |
| 152 | |
| 153 | std::string written = as_written(col); |
| 154 | |
| 155 | REQUIRE(as_written(col) == " \n \n third line"); |
| 156 | } |
| 157 | |
| 158 | TEST_CASE( "TextFlow::Column leading/trailing whitespace", |
| 159 | "[TextFlow][column][approvals]" ) { |
| 160 | SECTION("Trailing whitespace") { |
| 161 | Column col("some trailing whitespace: \t"); |
| 162 | REQUIRE(as_written(col) == "some trailing whitespace: \t"); |
| 163 | } |
| 164 | SECTION("Some leading whitespace") { |
| 165 | Column col("\t \t whitespace wooo"); |
| 166 | REQUIRE(as_written(col) == "\t \t whitespace wooo"); |
| 167 | } |
| 168 | SECTION("both") { |
| 169 | Column col(" abc "); |
| 170 | REQUIRE(as_written(col) == " abc "); |
| 171 | } |
| 172 | SECTION("whitespace only") { |
| 173 | Column col("\t \t"); |
| 174 | REQUIRE(as_written(col) == "\t \t"); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | TEST_CASE( "TextFlow::Column can handle empty string", |
| 179 | "[TextFlow][column][approvals]" ) { |
| 180 | Column col(""); |
| 181 | REQUIRE(as_written(col) == ""); |
| 182 | } |
| 183 | |
| 184 | TEST_CASE( "#1400 - TextFlow::Column wrapping would sometimes duplicate words", |
| 185 | "[TextFlow][column][regression][approvals]" ) { |
| 186 | const auto long_string = std::string( |
| 187 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nisl \n" |
| 188 | "massa, luctus ut ligula vitae, suscipit tempus velit. Vivamus sodales, quam in \n" |
| 189 | "convallis posuere, libero nisi ultricies orci, nec lobortis.\n"); |
| 190 | |
| 191 | auto col = Column(long_string) |
| 192 | .width(79) |
| 193 | .indent(2); |
| 194 | |
| 195 | REQUIRE(as_written(col) == |
| 196 | " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nisl \n" |
| 197 | " massa, luctus ut ligula vitae, suscipit tempus velit. Vivamus sodales, quam\n" |
| 198 | " in \n" |
| 199 | " convallis posuere, libero nisi ultricies orci, nec lobortis."); |
| 200 | } |