1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/common/common_type_converters.h"
#include "base/strings/utf_string_conversions.h"
#include "mojo/public/cpp/bindings/allocation_scope.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace common {
namespace test {
namespace {
void ExpectEqualsStringPiece(const std::string& expected,
const base::StringPiece& str) {
EXPECT_EQ(expected, str.as_string());
}
void ExpectEqualsMojoString(const std::string& expected,
const String& str) {
EXPECT_EQ(expected, str.To<std::string>());
}
void ExpectEqualsString16(const base::string16& expected,
const base::string16& actual) {
EXPECT_EQ(expected, actual);
}
void ExpectEqualsMojoString(const base::string16& expected,
const String& str) {
EXPECT_EQ(expected, str.To<base::string16>());
}
} // namespace
TEST(CommonTypeConvertersTest, StringPiece) {
AllocationScope scope;
std::string kText("hello world");
base::StringPiece string_piece(kText);
String mojo_string(string_piece);
ExpectEqualsMojoString(kText, mojo_string);
ExpectEqualsStringPiece(kText, mojo_string.To<base::StringPiece>());
// Test implicit construction and conversion:
ExpectEqualsMojoString(kText, string_piece);
ExpectEqualsStringPiece(kText, mojo_string);
// Test null String:
base::StringPiece empty_string_piece = String();
EXPECT_TRUE(empty_string_piece.empty());
}
TEST(CommonTypeConvertersTest, String16) {
AllocationScope scope;
const base::string16 string16(base::ASCIIToUTF16("hello world"));
const String mojo_string(string16);
ExpectEqualsMojoString(string16, mojo_string);
EXPECT_EQ(string16, mojo_string.To<base::string16>());
// Test implicit construction and conversion:
ExpectEqualsMojoString(string16, string16);
ExpectEqualsString16(string16, mojo_string);
// Test empty string conversion.
ExpectEqualsMojoString(base::string16(), base::string16());
}
} // namespace test
} // namespace common
} // namespace mojo
|