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 EXPECT_THAT(str, StrEq("10"));
21}
22
23TEST(FormatTest, SimpleMultiLine) {
24 std::string str;
25 llvm::raw_string_ostream os(str);
26 raw_indented_ostream ros(os);
27 ros << "a";
28 ros << "b";
29 ros << "\n";
30 ros << "c";
31 ros << "\n";
32 EXPECT_THAT(str, StrEq("ab\nc\n"));
33}
34
35TEST(FormatTest, SimpleMultiLineIndent) {
36 std::string str;
37 llvm::raw_string_ostream os(str);
38 raw_indented_ostream ros(os);
39 ros.indent(with: 2) << "a";
40 ros.indent(with: 4) << "b";
41 ros << "\n";
42 ros << "c";
43 ros << "\n";
44 EXPECT_THAT(str, StrEq(" a b\n c\n"));
45}
46
47TEST(FormatTest, SingleRegion) {
48 std::string str;
49 llvm::raw_string_ostream os(str);
50 raw_indented_ostream ros(os);
51 ros << "before\n";
52 {
53 raw_indented_ostream::DelimitedScope scope(ros);
54 ros << "inside " << 10;
55 ros << "\n two\n";
56 {
57 raw_indented_ostream::DelimitedScope scope(ros, "{\n", "\n}\n");
58 ros << "inner inner";
59 }
60 }
61 ros << "after";
62 const auto *expected =
63 R"(before
64 inside 10
65 two
66 {
67 inner inner
68 }
69after)";
70 EXPECT_THAT(str, StrEq(expected));
71
72 // Repeat the above with inline form.
73 str.clear();
74 ros << "before\n";
75 ros.scope().os << "inside " << 10 << "\n two\n";
76 ros.scope().os.scope(open: "{\n", close: "\n}\n").os << "inner inner";
77 ros << "after";
78 EXPECT_THAT(os.str(), StrEq(expected));
79}
80
81TEST(FormatTest, Reindent) {
82 std::string str;
83 llvm::raw_string_ostream os(str);
84 raw_indented_ostream ros(os);
85
86 // String to print with some additional empty lines at the start and lines
87 // with just spaces.
88 const auto *desc = R"(
89
90
91 First line
92 second line
93
94
95 )";
96 ros.printReindented(str: desc);
97 const auto *expected =
98 R"(First line
99 second line
100
101
102)";
103 EXPECT_THAT(str, StrEq(expected));
104}
105
106TEST(FormatTest, ReindentLineEndings) {
107 std::string str;
108 llvm::raw_string_ostream os(str);
109 raw_indented_ostream ros(os);
110
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.
114 const auto *desc =
115 "\r\n\r\n\r\n First line\r\n second line";
116 ros.printReindented(str: desc);
117 const auto *expected = "First line\r\n second line";
118 EXPECT_THAT(str, StrEq(expected));
119}
120

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

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