• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chromeos-dbus-bindings/indented_text.h"
6 
7 #include <string>
8 #include <vector>
9 
10 #include <gmock/gmock.h>
11 #include <gtest/gtest.h>
12 
13 using std::string;
14 using std::vector;
15 using testing::ElementsAre;
16 using testing::Test;
17 
18 namespace chromeos_dbus_bindings {
19 
20 class IndentedTextTest : public Test {
21  protected:
GetOffset() const22   size_t GetOffset() const { return text_.offset_; }
GetHistory() const23   const vector<size_t>& GetHistory() const { return text_.offset_history_; }
24   IndentedText text_;
25 };
26 
TEST_F(IndentedTextTest,Constructor)27 TEST_F(IndentedTextTest, Constructor) {
28   EXPECT_EQ("", text_.GetContents());
29   EXPECT_EQ(0u, GetOffset());
30   EXPECT_TRUE(GetHistory().empty());
31 }
32 
TEST_F(IndentedTextTest,AddLine)33 TEST_F(IndentedTextTest, AddLine) {
34   const char kTestString0[] = "test";
35   text_.AddLine(kTestString0);
36   EXPECT_EQ(string(kTestString0) + "\n", text_.GetContents());
37   EXPECT_EQ(0u, GetOffset());
38   EXPECT_TRUE(GetHistory().empty());
39 
40   const char kTestString1[] = "me";
41   text_.AddLine(kTestString1);
42   EXPECT_EQ(string(kTestString0) + "\n" + kTestString1 + "\n",
43             text_.GetContents());
44   EXPECT_EQ(0u, GetOffset());
45   EXPECT_TRUE(GetHistory().empty());
46 }
47 
TEST_F(IndentedTextTest,AddLineWithOffset)48 TEST_F(IndentedTextTest, AddLineWithOffset) {
49   const char kTestString[] = "test";
50   const int kShift = 4;
51   text_.AddLineWithOffset(kTestString, kShift);
52   EXPECT_EQ(string(kShift, ' ') + kTestString + "\n", text_.GetContents());
53 }
54 
TEST_F(IndentedTextTest,AddLineAndPushOffsetTo)55 TEST_F(IndentedTextTest, AddLineAndPushOffsetTo) {
56   text_.AddLineAndPushOffsetTo("foo(bar(baz", 2, '(');
57   EXPECT_THAT(GetHistory(), ElementsAre(8));
58 }
59 
TEST_F(IndentedTextTest,AddBlock)60 TEST_F(IndentedTextTest, AddBlock) {
61   IndentedText block0;
62   const char kTestString[] = "test";
63   block0.AddLineWithOffset(kTestString, 10);
64   block0.AddLineWithOffset(kTestString, 20);
65   IndentedText block1;
66   block1.AddLineWithOffset(kTestString, 5);
67   block1.AddLineWithOffset(kTestString, 15);
68   text_.AddBlock(block0);
69   text_.AddBlock(block1);
70   EXPECT_EQ(block0.GetContents() + block1.GetContents(), text_.GetContents());
71 }
72 
TEST_F(IndentedTextTest,AddBlockWithOffset)73 TEST_F(IndentedTextTest, AddBlockWithOffset) {
74   const char kTestString[] = "test";
75   IndentedText block;
76   const size_t kOffset0 = 0;
77   block.AddLineWithOffset(kTestString, kOffset0);
78   const size_t kOffset1 = 4;
79   block.AddLineWithOffset(kTestString, kOffset1);
80   const size_t kOffset2 = 20;
81   text_.AddBlockWithOffset(block, kOffset2);
82   EXPECT_EQ(string(kOffset2 + kOffset0, ' ') + kTestString + "\n" +
83             string(kOffset2 + kOffset1, ' ') + kTestString + "\n",
84             text_.GetContents());
85 }
86 
TEST_F(IndentedTextTest,PushPop)87 TEST_F(IndentedTextTest, PushPop) {
88   const char kTestString[] = "test";
89   text_.AddLine(kTestString);
90 
91   const size_t kShift0 = 2;
92   text_.PushOffset(kShift0);
93   EXPECT_EQ(2u, GetOffset());
94   EXPECT_THAT(GetHistory(), ElementsAre(kShift0));
95   text_.AddLine(kTestString);
96 
97   const size_t kShift1 = 4;
98   text_.PushOffset(kShift1);
99   EXPECT_EQ(kShift0 + kShift1, GetOffset());
100   EXPECT_THAT(GetHistory(), ElementsAre(kShift0, kShift1));
101   text_.AddLine(kTestString);
102 
103   text_.PopOffset();
104   text_.AddLine(kTestString);
105   EXPECT_EQ(2u, GetOffset());
106   EXPECT_THAT(GetHistory(), ElementsAre(kShift0));
107 
108   text_.PopOffset();
109   text_.AddLine(kTestString);
110   EXPECT_EQ(0u, GetOffset());
111   EXPECT_TRUE(GetHistory().empty());
112 
113   EXPECT_EQ(string(kTestString) + "\n" +
114             string(kShift0, ' ') + kTestString + "\n" +
115             string(kShift0 + kShift1, ' ') + kTestString + "\n" +
116             string(kShift0, ' ') + kTestString + "\n" +
117             string(kTestString) + "\n",
118             text_.GetContents());
119 }
120 
TEST_F(IndentedTextTest,Reset)121 TEST_F(IndentedTextTest, Reset) {
122   text_.PushOffset(10);
123   text_.AddLine("test");
124   EXPECT_NE("", text_.GetContents());
125   EXPECT_NE(0u, GetOffset());
126   EXPECT_FALSE(GetHistory().empty());
127   text_.Reset();
128   EXPECT_EQ("", text_.GetContents());
129   EXPECT_EQ(0u, GetOffset());
130   EXPECT_TRUE(GetHistory().empty());
131 }
132 
TEST_F(IndentedTextTest,AddComments_Empty)133 TEST_F(IndentedTextTest, AddComments_Empty) {
134   text_.AddComments("");
135   EXPECT_EQ("", text_.GetContents());
136 }
137 
TEST_F(IndentedTextTest,AddComments_WhitespaceOnly)138 TEST_F(IndentedTextTest, AddComments_WhitespaceOnly) {
139   text_.AddComments("  \n \t  \n");
140   EXPECT_EQ("", text_.GetContents());
141 }
142 
TEST_F(IndentedTextTest,AddComments_EmptyLines)143 TEST_F(IndentedTextTest, AddComments_EmptyLines) {
144   string comment_block = R"(
145 
146     line1
147 
148     line2
149 
150 
151   )";
152   text_.AddComments(comment_block);
153   EXPECT_EQ("// line1\n"
154             "//\n"
155             "// line2\n", text_.GetContents());
156 }
157 
TEST_F(IndentedTextTest,AddComments_Indentation)158 TEST_F(IndentedTextTest, AddComments_Indentation) {
159   string comment_block = R"(
160     line1
161       - bullet1
162         line2
163       - bullet2
164   line3
165   )";
166   text_.AddComments(comment_block);
167   EXPECT_EQ("// line1\n"
168             "//   - bullet1\n"
169             "//     line2\n"
170             "//   - bullet2\n"
171             "// line3\n", text_.GetContents());
172 }
173 
TEST_F(IndentedTextTest,GetLines)174 TEST_F(IndentedTextTest, GetLines) {
175   text_.AddLine("no indent");
176   text_.PushOffset(2);
177   text_.AddLine("2 spaces");
178   text_.AddLine("");
179 
180   EXPECT_EQ((vector<string>{"no indent", "  2 spaces", ""}), text_.GetLines());
181 }
182 
183 }  // namespace chromeos_dbus_bindings
184