testDocumentationFormatter.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include <sstream>
  4. #include <string>
  5. #include <utility>
  6. #include <cmDocumentationFormatter.h>
  7. #include "testCommon.h"
  8. namespace {
  9. using TestCases =
  10. const std::initializer_list<std::pair<std::string, std::string>>;
  11. bool testPrintFormattedNoIndent()
  12. {
  13. const TestCases testCases = {
  14. { "", "" },
  15. { "One line no EOL text", "One line no EOL text\n" },
  16. { "Short text. Two sentences.", "Short text. Two sentences.\n" },
  17. { "Short text\non\nmultiple\nlines\n",
  18. "Short text\n\non\n\nmultiple\n\nlines\n\n" },
  19. { "Just one a very long word: "
  20. "01234567890123456789012345678901234567890123456789012345"
  21. "678901234567890123456789",
  22. "Just one a very long "
  23. "word:\n01234567890123456789012345678901234567890123456789012345"
  24. "678901234567890123456789\n" },
  25. { " Pre-formatted paragraph with the very long word stays the same: "
  26. "0123456789012345678901234567890123456789012345678901234567890123456789",
  27. " Pre-formatted paragraph with the very long word stays the same: "
  28. "0123456789012345678901234567890123456789012345678901234567890123456789"
  29. "\n" },
  30. { "Extra spaces are removed. However, \n not in a "
  31. "pre-formatted\n "
  32. "paragraph",
  33. "Extra spaces are removed. However,\n\n not in a pre-formatted\n "
  34. "paragraph\n" },
  35. { "This is the text paragraph longer than a pre-defined wrapping position "
  36. "of the `cmDocumentationFormatter` class. And it's gonna be wrapped "
  37. "over multiple lines!",
  38. "This is the text paragraph longer than a pre-defined wrapping position "
  39. "of the\n`cmDocumentationFormatter` class. And it's gonna be wrapped "
  40. "over multiple\nlines!\n" }
  41. };
  42. cmDocumentationFormatter formatter;
  43. for (auto& test : testCases) {
  44. std::ostringstream out;
  45. formatter.PrintFormatted(out, test.first);
  46. ASSERT_EQUAL(out.str(), test.second);
  47. }
  48. return true;
  49. }
  50. bool testPrintFormattedIndent2()
  51. {
  52. const TestCases testCases = {
  53. { "", "" },
  54. { "One line no EOL text", " One line no EOL text\n" },
  55. { "Short text. Two sentences.", " Short text. Two sentences.\n" },
  56. { "Short text\non\nmultiple\nlines\n",
  57. " Short text\n\n on\n\n multiple\n\n lines\n\n" },
  58. { "Just one a very long word: "
  59. "01234567890123456789012345678901234567890123456789012345"
  60. "678901234567890123456789",
  61. " Just one a very long "
  62. "word:\n 01234567890123456789012345678901234567890123456789012345"
  63. "678901234567890123456789\n" },
  64. { " Pre-formatted paragraph with the very long word stays the same: "
  65. "0123456789012345678901234567890123456789012345678901234567890123456789",
  66. " Pre-formatted paragraph with the very long word stays the same: "
  67. "0123456789012345678901234567890123456789012345678901234567890123456789"
  68. "\n" },
  69. { "Extra spaces are removed. However, \n not in a "
  70. "pre-formatted\n "
  71. "paragraph",
  72. " Extra spaces are removed. However,\n\n not in a "
  73. "pre-formatted\n "
  74. "paragraph\n" },
  75. { "This is the text paragraph longer than a pre-defined wrapping position "
  76. "of the `cmDocumentationFormatter` class. And it's gonna be wrapped "
  77. "over multiple lines!",
  78. " This is the text paragraph longer than a pre-defined wrapping "
  79. "position of\n"
  80. " the `cmDocumentationFormatter` class. And it's gonna be wrapped "
  81. "over\n multiple lines!\n" }
  82. };
  83. cmDocumentationFormatter formatter;
  84. formatter.SetIndent(2);
  85. for (auto& test : testCases) {
  86. std::ostringstream out;
  87. formatter.PrintFormatted(out, test.first);
  88. ASSERT_EQUAL(out.str(), test.second);
  89. }
  90. return true;
  91. }
  92. bool testPrintFormattedIndent10()
  93. {
  94. const TestCases testCases = {
  95. { "", "" },
  96. { "One line no EOL text", " One line no EOL text\n" },
  97. { "This is the text paragraph longer than a pre-defined wrapping position "
  98. "of the `cmDocumentationFormatter` class. And it's gonna be wrapped "
  99. "over multiple lines!",
  100. " This is the text paragraph longer than a pre-defined "
  101. "wrapping\n"
  102. " position of the `cmDocumentationFormatter` class. "
  103. "And it's gonna\n"
  104. " be wrapped over multiple lines!\n" }
  105. };
  106. cmDocumentationFormatter formatter;
  107. formatter.SetIndent(10);
  108. for (auto& test : testCases) {
  109. std::ostringstream out;
  110. formatter.PrintFormatted(out, test.first);
  111. ASSERT_EQUAL(out.str(), test.second);
  112. }
  113. return true;
  114. }
  115. }
  116. int testDocumentationFormatter(int /*unused*/, char* /*unused*/[])
  117. {
  118. return runTests({ testPrintFormattedNoIndent, testPrintFormattedIndent2,
  119. testPrintFormattedIndent10 },
  120. false);
  121. }