1 //===- IndentedOstreamTest.cpp - Indented raw ostream Tests ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "mlir/Support/IndentedOstream.h"
10 #include "gmock/gmock.h"
13 using ::testing::StrEq
;
15 TEST(FormatTest
, SingleLine
) {
17 llvm::raw_string_ostream
os(str
);
18 raw_indented_ostream
ros(os
);
20 EXPECT_THAT(str
, StrEq("10"));
23 TEST(FormatTest
, SimpleMultiLine
) {
25 llvm::raw_string_ostream
os(str
);
26 raw_indented_ostream
ros(os
);
32 EXPECT_THAT(str
, StrEq("ab\nc\n"));
35 TEST(FormatTest
, SimpleMultiLineIndent
) {
37 llvm::raw_string_ostream
os(str
);
38 raw_indented_ostream
ros(os
);
44 EXPECT_THAT(str
, StrEq(" a b\n c\n"));
47 TEST(FormatTest
, SingleRegion
) {
49 llvm::raw_string_ostream
os(str
);
50 raw_indented_ostream
ros(os
);
53 raw_indented_ostream::DelimitedScope
scope(ros
);
54 ros
<< "inside " << 10;
57 raw_indented_ostream::DelimitedScope
scope(ros
, "{\n", "\n}\n");
62 const auto *expected
=
70 EXPECT_THAT(str
, StrEq(expected
));
72 // Repeat the above with inline form.
75 ros
.scope().os
<< "inside " << 10 << "\n two\n";
76 ros
.scope().os
.scope("{\n", "\n}\n").os
<< "inner inner";
78 EXPECT_THAT(os
.str(), StrEq(expected
));
81 TEST(FormatTest
, Reindent
) {
83 llvm::raw_string_ostream
os(str
);
84 raw_indented_ostream
ros(os
);
86 // String to print with some additional empty lines at the start and lines
88 const auto *desc
= R
"(
96 ros
.printReindented(desc
);
97 const auto *expected
=
103 EXPECT_THAT(str
, StrEq(expected
));
106 TEST(FormatTest
, ReindentLineEndings
) {
108 llvm::raw_string_ostream
os(str
);
109 raw_indented_ostream
ros(os
);
111 // Similar string as the previous test, but with \r\n (Windows style) line
112 // breaks. Note that C++'s internal string representation uses \n, so just
113 // running the previous test as-is on Windows is not sufficient.
115 "\r\n\r\n\r\n First line\r\n second line";
116 ros
.printReindented(desc
);
117 const auto *expected
= "First line\r\n second line";
118 EXPECT_THAT(str
, StrEq(expected
));