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
);
21 EXPECT_THAT(os
.str(), StrEq("10"));
24 TEST(FormatTest
, SimpleMultiLine
) {
26 llvm::raw_string_ostream
os(str
);
27 raw_indented_ostream
ros(os
);
34 EXPECT_THAT(os
.str(), StrEq("ab\nc\n"));
37 TEST(FormatTest
, SimpleMultiLineIndent
) {
39 llvm::raw_string_ostream
os(str
);
40 raw_indented_ostream
ros(os
);
47 EXPECT_THAT(os
.str(), StrEq(" a b\n c\n"));
50 TEST(FormatTest
, SingleRegion
) {
52 llvm::raw_string_ostream
os(str
);
53 raw_indented_ostream
ros(os
);
56 raw_indented_ostream::DelimitedScope
scope(ros
);
57 ros
<< "inside " << 10;
60 raw_indented_ostream::DelimitedScope
scope(ros
, "{\n", "\n}\n");
66 const auto *expected
=
74 EXPECT_THAT(os
.str(), StrEq(expected
));
76 // Repeat the above with inline form.
79 ros
.scope().os
<< "inside " << 10 << "\n two\n";
80 ros
.scope().os
.scope("{\n", "\n}\n").os
<< "inner inner";
83 EXPECT_THAT(os
.str(), StrEq(expected
));
86 TEST(FormatTest
, Reindent
) {
88 llvm::raw_string_ostream
os(str
);
89 raw_indented_ostream
ros(os
);
91 // String to print with some additional empty lines at the start and lines
93 const auto *desc
= R
"(
101 ros
.printReindented(desc
);
103 const auto *expected
=
109 EXPECT_THAT(os
.str(), StrEq(expected
));
112 TEST(FormatTest
, ReindentLineEndings
) {
114 llvm::raw_string_ostream
os(str
);
115 raw_indented_ostream
ros(os
);
117 // Similar string as the previous test, but with \r\n (Windows style) line
118 // breaks. Note that C++'s internal string representation uses \n, so just
119 // running the previous test as-is on Windows is not sufficient.
121 "\r\n\r\n\r\n First line\r\n second line";
122 ros
.printReindented(desc
);
124 const auto *expected
= "First line\r\n second line";
125 EXPECT_THAT(os
.str(), StrEq(expected
));