1 //===- IndentedOstreamTest.cpp - Indented raw ostream Tests ---------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "mlir/Support/IndentedOstream.h"
10 #include "gmock/gmock.h"
11
12 using namespace mlir;
13 using ::testing::StrEq;
14
TEST(FormatTest,SingleLine)15 TEST(FormatTest, SingleLine) {
16 std::string str;
17 llvm::raw_string_ostream os(str);
18 raw_indented_ostream ros(os);
19 ros << 10;
20 ros.flush();
21 EXPECT_THAT(os.str(), StrEq("10"));
22 }
23
TEST(FormatTest,SimpleMultiLine)24 TEST(FormatTest, SimpleMultiLine) {
25 std::string str;
26 llvm::raw_string_ostream os(str);
27 raw_indented_ostream ros(os);
28 ros << "a";
29 ros << "b";
30 ros << "\n";
31 ros << "c";
32 ros << "\n";
33 ros.flush();
34 EXPECT_THAT(os.str(), StrEq("ab\nc\n"));
35 }
36
TEST(FormatTest,SimpleMultiLineIndent)37 TEST(FormatTest, SimpleMultiLineIndent) {
38 std::string str;
39 llvm::raw_string_ostream os(str);
40 raw_indented_ostream ros(os);
41 ros.indent(2) << "a";
42 ros.indent(4) << "b";
43 ros << "\n";
44 ros << "c";
45 ros << "\n";
46 ros.flush();
47 EXPECT_THAT(os.str(), StrEq(" a b\n c\n"));
48 }
49
TEST(FormatTest,SingleRegion)50 TEST(FormatTest, SingleRegion) {
51 std::string str;
52 llvm::raw_string_ostream os(str);
53 raw_indented_ostream ros(os);
54 ros << "before\n";
55 {
56 raw_indented_ostream::DelimitedScope scope(ros);
57 ros << "inside " << 10;
58 ros << "\n two\n";
59 {
60 raw_indented_ostream::DelimitedScope scope(ros, "{\n", "\n}\n");
61 ros << "inner inner";
62 }
63 }
64 ros << "after";
65 ros.flush();
66 const auto *expected =
67 R"(before
68 inside 10
69 two
70 {
71 inner inner
72 }
73 after)";
74 EXPECT_THAT(os.str(), StrEq(expected));
75
76 // Repeat the above with inline form.
77 str.clear();
78 ros << "before\n";
79 ros.scope().os << "inside " << 10 << "\n two\n";
80 ros.scope().os.scope("{\n", "\n}\n").os << "inner inner";
81 ros << "after";
82 ros.flush();
83 EXPECT_THAT(os.str(), StrEq(expected));
84 }
85
TEST(FormatTest,Reindent)86 TEST(FormatTest, Reindent) {
87 std::string str;
88 llvm::raw_string_ostream os(str);
89 raw_indented_ostream ros(os);
90
91 // String to print with some additional empty lines at the start and lines
92 // with just spaces.
93 const auto *desc = R"(
94
95
96 First line
97 second line
98
99
100 )";
101 ros.reindent(desc);
102 ros.flush();
103 const auto *expected =
104 R"(First line
105 second line
106
107
108 )";
109 EXPECT_THAT(os.str(), StrEq(expected));
110 }
111