TextFlow.tests.cpp 6.5 KB

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