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
12using namespace mlir;
13using ::testing::StrEq;
14
15TEST(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
24TEST(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
37TEST(FormatTest, SimpleMultiLineIndent) {
38 std::string str;
39 llvm::raw_string_ostream os(str);
40 raw_indented_ostream ros(os);
41 ros.indent(with: 2) << "a";
42 ros.indent(with: 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
50TEST(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 }
73after)";
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(open: "{\n", close: "\n}\n").os << "inner inner";
81 ros << "after";
82 ros.flush();
83 EXPECT_THAT(os.str(), StrEq(expected));
84}
85
86TEST(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.printReindented(str: 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
112TEST(FormatTest, ReindentLineEndings) {
113 std::string str;
114 llvm::raw_string_ostream os(str);
115 raw_indented_ostream ros(os);
116
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.
120 const auto *desc =
121 "\r\n\r\n\r\n First line\r\n second line";
122 ros.printReindented(str: desc);
123 ros.flush();
124 const auto *expected = "First line\r\n second line";
125 EXPECT_THAT(os.str(), StrEq(expected));
126}
127

source code of mlir/unittests/Support/IndentedOstreamTest.cpp