OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium 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 <sstream> | |
6 | |
7 #include "gtest/gtest.h" | |
8 #include "mojo/public/cpp/bindings/string.h" | |
9 | |
10 namespace mojo { | |
11 namespace test { | |
12 | |
13 TEST(StringTest, DefaultIsNull) { | |
14 String s; | |
15 EXPECT_TRUE(s.is_null()); | |
16 } | |
17 | |
18 TEST(StringTest, ConstructedWithNull) { | |
19 String s(nullptr); | |
20 EXPECT_TRUE(s.is_null()); | |
21 } | |
22 | |
23 TEST(StringTest, ConstructedWithNullCharPointer) { | |
24 const char* null = nullptr; | |
25 String s(null); | |
26 EXPECT_TRUE(s.is_null()); | |
27 } | |
28 | |
29 TEST(StringTest, AssignedNull) { | |
30 String s(""); | |
31 EXPECT_FALSE(s.is_null()); | |
32 s = nullptr; | |
33 EXPECT_TRUE(s.is_null()); | |
34 } | |
35 | |
36 TEST(StringTest, AssignedNullCharPointer) { | |
37 String s(""); | |
38 EXPECT_FALSE(s.is_null()); | |
39 const char* null = nullptr; | |
40 s = null; | |
41 EXPECT_TRUE(s.is_null()); | |
42 } | |
43 | |
44 TEST(StringTest, Empty) { | |
45 String s(""); | |
46 EXPECT_FALSE(s.is_null()); | |
47 EXPECT_TRUE(s.get().empty()); | |
48 } | |
49 | |
50 TEST(StringTest, Basic) { | |
51 String s("hello world"); | |
52 EXPECT_EQ(std::string("hello world"), s.get()); | |
53 } | |
54 | |
55 TEST(StringTest, Assignment) { | |
56 String s("hello world"); | |
57 String t = s; // Makes a copy. | |
58 EXPECT_FALSE(t.is_null()); | |
59 EXPECT_EQ(std::string("hello world"), t.get()); | |
60 EXPECT_FALSE(s.is_null()); | |
61 } | |
62 | |
63 TEST(StringTest, ConstAt) { | |
64 const String s("abc"); | |
65 EXPECT_EQ('a', s.at(0)); | |
66 EXPECT_EQ('b', s.at(1)); | |
67 EXPECT_EQ('c', s.at(2)); | |
68 } | |
69 | |
70 TEST(StringTest, NonConstAt) { | |
71 String s("abc"); | |
72 EXPECT_EQ('a', s.at(0)); | |
73 EXPECT_EQ('b', s.at(1)); | |
74 s.at(0) = 'x'; | |
75 s.at(1) = 'y'; | |
76 EXPECT_EQ('x', s.at(0)); | |
77 EXPECT_EQ('y', s.at(1)); | |
78 EXPECT_EQ('c', s.at(2)); | |
79 } | |
80 | |
81 TEST(StringTest, ConstArraySubscript) { | |
82 const String s("abc"); | |
83 EXPECT_EQ('a', s[0]); | |
84 EXPECT_EQ('b', s[1]); | |
85 EXPECT_EQ('c', s[2]); | |
86 } | |
87 | |
88 TEST(StringTest, NonConstArraySubscript) { | |
89 String s("abc"); | |
90 EXPECT_EQ('a', s[0]); | |
91 EXPECT_EQ('b', s[1]); | |
92 s[0] = 'x'; | |
93 s[1] = 'y'; | |
94 EXPECT_EQ('x', s[0]); | |
95 EXPECT_EQ('y', s[1]); | |
96 EXPECT_EQ('c', s[2]); | |
97 } | |
98 | |
99 TEST(StringTest, Equality) { | |
100 String s("hello world"); | |
101 String t("hello world"); | |
102 EXPECT_EQ(s, t); | |
103 EXPECT_TRUE(s == t); | |
104 EXPECT_TRUE("hello world" == s); | |
105 EXPECT_TRUE(s == "hello world"); | |
106 EXPECT_TRUE("not" != s); | |
107 EXPECT_TRUE(s != "not"); | |
108 } | |
109 | |
110 TEST(StringTest, LessThanNullness) { | |
111 String null; | |
112 String null2; | |
113 EXPECT_FALSE(null < null2); | |
114 EXPECT_FALSE(null2 < null); | |
115 | |
116 String real("real"); | |
117 EXPECT_TRUE(null < real); | |
118 EXPECT_FALSE(real < null); | |
119 } | |
120 | |
121 TEST(StringTest, OutputFormatting) { | |
122 String s("abc"); | |
123 String null; | |
124 | |
125 std::ostringstream so; | |
126 so << "s=" << s << ", null=" << null; | |
127 EXPECT_EQ("s=abc, null=", so.str()); | |
128 } | |
129 | |
130 } // namespace test | |
131 } // namespace mojo | |
OLD | NEW |