1 // Copyright (c) 2012 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.
5 #include "sync/api/sync_error.h"
9 #include "base/location.h"
10 #include "testing/gtest/include/gtest/gtest.h"
18 typedef testing::Test SyncErrorTest
;
20 TEST_F(SyncErrorTest
, Unset
) {
22 EXPECT_FALSE(error
.IsSet());
25 TEST_F(SyncErrorTest
, Default
) {
26 tracked_objects::Location location
= FROM_HERE
;
27 std::string msg
= "test";
28 ModelType type
= PREFERENCES
;
29 SyncError
error(location
, SyncError::DATATYPE_ERROR
, msg
, type
);
30 ASSERT_TRUE(error
.IsSet());
31 EXPECT_EQ(location
.line_number(), error
.location().line_number());
32 EXPECT_EQ("datatype error was encountered: " + msg
, error
.message());
33 EXPECT_EQ(type
, error
.model_type());
36 TEST_F(SyncErrorTest
, Reset
) {
37 tracked_objects::Location location
= FROM_HERE
;
38 std::string msg
= "test";
39 ModelType type
= PREFERENCES
;
42 EXPECT_FALSE(error
.IsSet());
44 error
.Reset(location
, msg
, type
);
45 ASSERT_TRUE(error
.IsSet());
46 EXPECT_EQ(location
.line_number(), error
.location().line_number());
47 EXPECT_EQ(msg
, error
.message());
48 EXPECT_EQ(type
, error
.model_type());
50 tracked_objects::Location location2
= FROM_HERE
;
51 std::string msg2
= "test";
52 ModelType type2
= PREFERENCES
;
53 error
.Reset(location2
, msg2
, type2
);
54 ASSERT_TRUE(error
.IsSet());
55 EXPECT_EQ(location2
.line_number(), error
.location().line_number());
56 EXPECT_EQ(msg2
, error
.message());
57 EXPECT_EQ(type2
, error
.model_type());
60 TEST_F(SyncErrorTest
, Copy
) {
61 tracked_objects::Location location
= FROM_HERE
;
62 std::string msg
= "test";
63 ModelType type
= PREFERENCES
;
66 EXPECT_FALSE(error1
.IsSet());
67 SyncError
error2(error1
);
68 EXPECT_FALSE(error2
.IsSet());
70 error1
.Reset(location
, msg
, type
);
71 ASSERT_TRUE(error1
.IsSet());
72 EXPECT_EQ(location
.line_number(), error1
.location().line_number());
73 EXPECT_EQ(msg
, error1
.message());
74 EXPECT_EQ(type
, error1
.model_type());
76 SyncError
error3(error1
);
77 ASSERT_TRUE(error3
.IsSet());
78 EXPECT_EQ(error1
.location().line_number(), error3
.location().line_number());
79 EXPECT_EQ(error1
.message(), error3
.message());
80 EXPECT_EQ(error1
.model_type(), error3
.model_type());
83 EXPECT_FALSE(error4
.IsSet());
84 SyncError
error5(error4
);
85 EXPECT_FALSE(error5
.IsSet());
88 TEST_F(SyncErrorTest
, Assign
) {
89 tracked_objects::Location location
= FROM_HERE
;
90 std::string msg
= "test";
91 ModelType type
= PREFERENCES
;
94 EXPECT_FALSE(error1
.IsSet());
97 EXPECT_FALSE(error2
.IsSet());
99 error1
.Reset(location
, msg
, type
);
100 ASSERT_TRUE(error1
.IsSet());
101 EXPECT_EQ(location
.line_number(), error1
.location().line_number());
102 EXPECT_EQ(msg
, error1
.message());
103 EXPECT_EQ(type
, error1
.model_type());
106 ASSERT_TRUE(error2
.IsSet());
107 EXPECT_EQ(error1
.location().line_number(), error2
.location().line_number());
108 EXPECT_EQ(error1
.message(), error2
.message());
109 EXPECT_EQ(error1
.model_type(), error2
.model_type());
111 error2
= SyncError();
112 EXPECT_FALSE(error2
.IsSet());
115 TEST_F(SyncErrorTest
, ToString
) {
116 tracked_objects::Location location
= FROM_HERE
;
117 std::string msg
= "test";
118 ModelType type
= PREFERENCES
;
119 std::string expected
= std::string(ModelTypeToString(type
)) +
120 " datatype error was encountered: " + msg
;
121 LOG(INFO
) << "Expect " << expected
;
122 SyncError
error(location
, SyncError::DATATYPE_ERROR
, msg
, type
);
123 EXPECT_TRUE(error
.IsSet());
124 EXPECT_NE(string::npos
, error
.ToString().find(expected
));
127 EXPECT_FALSE(error2
.IsSet());
128 EXPECT_EQ(std::string(), error2
.ToString());
131 EXPECT_TRUE(error2
.IsSet());
132 EXPECT_NE(string::npos
, error
.ToString().find(expected
));
137 } // namespace syncer