unified_diff_matcher_test.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "testing/base/unified_diff_matcher.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <string>
  8. #include "llvm/ADT/SmallVector.h"
  9. namespace Carbon::Testing {
  10. namespace {
  11. using ::testing::Matcher;
  12. using ::testing::StrEq;
  13. // Asserts that when expected does not match actual, the string
  14. // representation of the produced diff equals expected_diff.
  15. auto ExpectUnifiedDiff(const llvm::SmallVector<std::string>& actual,
  16. const llvm::SmallVector<Matcher<std::string>>& expected,
  17. const std::string& expected_diff) -> void {
  18. testing::StringMatchResultListener listener;
  19. EXPECT_FALSE(testing::ExplainMatchResult(
  20. ElementsAreArrayWithUnifiedDiff(expected), actual, &listener));
  21. EXPECT_THAT(listener.str(), testing::Eq(expected_diff));
  22. }
  23. TEST(UnifiedDiffMatcherTest, Matches) {
  24. llvm::SmallVector<std::string> actual = {"A", "B", "C"};
  25. llvm::SmallVector<Matcher<std::string>> expected = {StrEq("A"), StrEq("B"),
  26. StrEq("C")};
  27. EXPECT_THAT(actual, ElementsAreArrayWithUnifiedDiff(expected));
  28. }
  29. TEST(UnifiedDiffMatcherTest, MismatchMissing) {
  30. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  31. === diff in expected elements 1 to 3 (1-based index):
  32. A
  33. - is equal to "B"
  34. C
  35. === diff end
  36. )";
  37. ExpectUnifiedDiff({"A", "C"}, {StrEq("A"), StrEq("B"), StrEq("C")},
  38. ExpectedDiff);
  39. }
  40. TEST(UnifiedDiffMatcherTest, MismatchExtra) {
  41. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  42. === diff in expected elements 1 to 2 (1-based index):
  43. A
  44. + B
  45. C
  46. === diff end
  47. )";
  48. ExpectUnifiedDiff({"A", "B", "C"}, {StrEq("A"), StrEq("C")}, ExpectedDiff);
  49. }
  50. TEST(UnifiedDiffMatcherTest, MismatchBoth) {
  51. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  52. === diff in expected elements 1 to 2 (1-based index):
  53. A
  54. - is equal to "C"
  55. + B
  56. === diff end
  57. )";
  58. ExpectUnifiedDiff({"A", "B"}, {StrEq("A"), StrEq("C")}, ExpectedDiff);
  59. }
  60. TEST(UnifiedDiffMatcherTest, MismatchMultiple) {
  61. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  62. === diff in expected elements 1 to 5 (1-based index):
  63. A
  64. - is equal to "B"
  65. + X
  66. C
  67. - is equal to "D"
  68. + Y
  69. E
  70. === diff end
  71. )";
  72. ExpectUnifiedDiff(
  73. {"A", "X", "C", "Y", "E"},
  74. {StrEq("A"), StrEq("B"), StrEq("C"), StrEq("D"), StrEq("E")},
  75. ExpectedDiff);
  76. }
  77. TEST(UnifiedDiffMatcherTest, MismatchLongContext) {
  78. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  79. === diff in expected elements 2 to 8 (1-based index):
  80. 1
  81. 2
  82. 3
  83. - is equal to "X"
  84. + 4
  85. 5
  86. 6
  87. 7
  88. === diff end
  89. )";
  90. ExpectUnifiedDiff({"0", "1", "2", "3", "4", "5", "6", "7", "8"},
  91. {StrEq("0"), StrEq("1"), StrEq("2"), StrEq("3"), StrEq("X"),
  92. StrEq("5"), StrEq("6"), StrEq("7"), StrEq("8")},
  93. ExpectedDiff);
  94. }
  95. TEST(UnifiedDiffMatcherTest, Mismatch5LineContext) {
  96. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  97. === diff in expected elements 1 to 7 (1-based index):
  98. - is equal to "X"
  99. + 0
  100. 1
  101. 2
  102. 3
  103. 4
  104. 5
  105. - is equal to "Y"
  106. + 6
  107. === diff end
  108. )";
  109. ExpectUnifiedDiff({"0", "1", "2", "3", "4", "5", "6"},
  110. {StrEq("X"), StrEq("1"), StrEq("2"), StrEq("3"), StrEq("4"),
  111. StrEq("5"), StrEq("Y")},
  112. ExpectedDiff);
  113. }
  114. TEST(UnifiedDiffMatcherTest, Mismatch6LineContext) {
  115. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  116. === diff in expected elements 1 to 8 (1-based index):
  117. - is equal to "X"
  118. + 0
  119. 1
  120. 2
  121. 3
  122. 4
  123. 5
  124. 6
  125. - is equal to "Y"
  126. + 7
  127. === diff end
  128. )";
  129. ExpectUnifiedDiff({"0", "1", "2", "3", "4", "5", "6", "7"},
  130. {StrEq("X"), StrEq("1"), StrEq("2"), StrEq("3"), StrEq("4"),
  131. StrEq("5"), StrEq("6"), StrEq("Y")},
  132. ExpectedDiff);
  133. }
  134. TEST(UnifiedDiffMatcherTest, Mismatch7LineContext) {
  135. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  136. === diff in expected elements 1 to 4 (1-based index):
  137. - is equal to "X"
  138. + 0
  139. 1
  140. 2
  141. 3
  142. === diff in expected elements 6 to 9 (1-based index):
  143. 5
  144. 6
  145. 7
  146. - is equal to "Y"
  147. + 8
  148. === diff end
  149. )";
  150. ExpectUnifiedDiff({"0", "1", "2", "3", "4", "5", "6", "7", "8"},
  151. {StrEq("X"), StrEq("1"), StrEq("2"), StrEq("3"), StrEq("4"),
  152. StrEq("5"), StrEq("6"), StrEq("7"), StrEq("Y")},
  153. ExpectedDiff);
  154. }
  155. TEST(UnifiedDiffMatcherTest, MismatchEmptyExpected) {
  156. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  157. === diff in expected elements 1 to 1 (1-based index):
  158. + A
  159. === diff end
  160. )";
  161. ExpectUnifiedDiff({"A"}, {}, ExpectedDiff);
  162. }
  163. TEST(UnifiedDiffMatcherTest, MismatchEmptyActual) {
  164. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  165. === diff in expected elements 1 to 1 (1-based index):
  166. - is equal to "A"
  167. === diff end
  168. )";
  169. ExpectUnifiedDiff({}, {StrEq("A")}, ExpectedDiff);
  170. }
  171. TEST(UnifiedDiffMatcherTest, MismatchLongDifference) {
  172. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  173. === diff in expected elements 1 to 4 (1-based index):
  174. 1
  175. - is equal to "2"
  176. - is equal to "3"
  177. + X
  178. + Y
  179. + Z
  180. 4
  181. === diff end
  182. )";
  183. ExpectUnifiedDiff({"1", "X", "Y", "Z", "4"},
  184. {StrEq("1"), StrEq("2"), StrEq("3"), StrEq("4")},
  185. ExpectedDiff);
  186. }
  187. TEST(UnifiedDiffMatcherTest, MismatchGreedyResyncActualMissing) {
  188. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  189. === diff in expected elements 1 to 6 (1-based index):
  190. 1
  191. 2
  192. - is equal to "3"
  193. + X
  194. + 7
  195. 4
  196. 5
  197. 6
  198. === diff end
  199. )";
  200. ExpectUnifiedDiff({"1", "2", "X", "7", "4", "5", "6", "7", "8", "9"},
  201. {StrEq("1"), StrEq("2"), StrEq("3"), StrEq("4"), StrEq("5"),
  202. StrEq("6"), StrEq("7"), StrEq("8"), StrEq("9")},
  203. ExpectedDiff);
  204. }
  205. TEST(UnifiedDiffMatcherTest, MismatchGreedyResyncExpectedMissing) {
  206. constexpr char ExpectedDiff[] = R"(unified diff (- expected, + actual):
  207. === diff in expected elements 1 to 7 (1-based index):
  208. 1
  209. 2
  210. - is equal to "X"
  211. - is equal to "7"
  212. + 3
  213. 4
  214. 5
  215. 6
  216. === diff end
  217. )";
  218. ExpectUnifiedDiff(
  219. {"1", "2", "3", "4", "5", "6", "7", "8", "9"},
  220. {StrEq("1"), StrEq("2"), StrEq("X"), StrEq("7"), StrEq("4"), StrEq("5"),
  221. StrEq("6"), StrEq("7"), StrEq("8"), StrEq("9")},
  222. ExpectedDiff);
  223. }
  224. } // namespace
  225. } // namespace Carbon::Testing