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 "mojo/public/cpp/bindings/string.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7
8 namespace mojo {
9 namespace test {
10
11 namespace {
12 const char* kHelloWorld = "hello world";
13 } // namespace
14
TEST(StringTest,DefaultIsNotNull)15 TEST(StringTest, DefaultIsNotNull) {
16 String s;
17 EXPECT_FALSE(s.is_null());
18 }
19
TEST(StringTest,ConstructedWithNULL)20 TEST(StringTest, ConstructedWithNULL) {
21 String s(nullptr);
22 EXPECT_TRUE(s.is_null());
23 }
24
TEST(StringTest,ConstructedWithNullCharPointer)25 TEST(StringTest, ConstructedWithNullCharPointer) {
26 const char* null = nullptr;
27 String s(null);
28 EXPECT_TRUE(s.is_null());
29 }
30
TEST(StringTest,AssignedNULL)31 TEST(StringTest, AssignedNULL) {
32 String s("");
33 EXPECT_FALSE(s.is_null());
34 s = nullptr;
35 EXPECT_TRUE(s.is_null());
36 }
37
TEST(StringTest,Empty)38 TEST(StringTest, Empty) {
39 String s("");
40 EXPECT_FALSE(s.is_null());
41 EXPECT_TRUE(s.get().empty());
42 }
43
TEST(StringTest,Basic)44 TEST(StringTest, Basic) {
45 String s(kHelloWorld);
46 EXPECT_EQ(std::string(kHelloWorld), s.get());
47 }
48
TEST(StringTest,Assignment)49 TEST(StringTest, Assignment) {
50 String s(kHelloWorld);
51 String t = s; // Makes a copy.
52 EXPECT_FALSE(t.is_null());
53 EXPECT_EQ(std::string(kHelloWorld), t.get());
54 EXPECT_FALSE(s.is_null());
55 }
56
TEST(StringTest,Equality)57 TEST(StringTest, Equality) {
58 String s(kHelloWorld);
59 String t(kHelloWorld);
60 EXPECT_EQ(s, t);
61 EXPECT_TRUE(s == s);
62 EXPECT_FALSE(s != s);
63 EXPECT_TRUE(s == t);
64 EXPECT_FALSE(s != t);
65 EXPECT_TRUE(kHelloWorld == s);
66 EXPECT_TRUE(s == kHelloWorld);
67 EXPECT_TRUE("not" != s);
68 EXPECT_FALSE("not" == s);
69 EXPECT_TRUE(s != "not");
70 EXPECT_FALSE(s == "not");
71
72 // Test null strings.
73 String n1;
74 String n2;
75 EXPECT_TRUE(n1 == n1);
76 EXPECT_FALSE(n1 != n2);
77 EXPECT_TRUE(n1 == n2);
78 EXPECT_FALSE(n1 != n2);
79 EXPECT_TRUE(n1 != s);
80 EXPECT_FALSE(n1 == s);
81 EXPECT_TRUE(s != n1);
82 EXPECT_FALSE(s == n1);
83 }
84
TEST(StringTest,LessThanNullness)85 TEST(StringTest, LessThanNullness) {
86 String null;
87 String null2;
88 EXPECT_FALSE(null < null2);
89 EXPECT_FALSE(null2 < null);
90
91 String real("real");
92 EXPECT_TRUE(null < real);
93 EXPECT_FALSE(real < null);
94 }
95
TEST(StringTest,MoveConstructors)96 TEST(StringTest, MoveConstructors) {
97 std::string std_str(kHelloWorld);
98
99 String str1(std::move(std_str));
100 EXPECT_TRUE(kHelloWorld == str1);
101
102 String str2(std::move(str1));
103 EXPECT_TRUE(kHelloWorld == str2);
104 EXPECT_TRUE(str1.is_null());
105 }
106
TEST(StringTest,MoveAssignments)107 TEST(StringTest, MoveAssignments) {
108 std::string std_str(kHelloWorld);
109
110 String str1;
111 str1 = std::move(std_str);
112 EXPECT_TRUE(kHelloWorld == str1);
113
114 String str2;
115 str2 = std::move(str1);
116 EXPECT_TRUE(kHelloWorld == str2);
117 EXPECT_TRUE(str1.is_null());
118 }
119
TEST(StringTest,Storage)120 TEST(StringTest, Storage) {
121 String str(kHelloWorld);
122
123 EXPECT_TRUE(kHelloWorld == str.storage());
124
125 std::string storage = str.PassStorage();
126 EXPECT_TRUE(str.is_null());
127 EXPECT_TRUE(kHelloWorld == storage);
128 }
129
130 } // namespace test
131 } // namespace mojo
132