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.
4
5#include "sync/api/sync_error.h"
6
7#include <string>
8
9#include "base/location.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace syncer {
13
14namespace {
15
16using std::string;
17
18typedef testing::Test SyncErrorTest;
19
20TEST_F(SyncErrorTest, Unset) {
21  SyncError error;
22  EXPECT_FALSE(error.IsSet());
23}
24
25TEST_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());
34}
35
36TEST_F(SyncErrorTest, Reset) {
37  tracked_objects::Location location = FROM_HERE;
38  std::string msg = "test";
39  ModelType type = PREFERENCES;
40
41  SyncError error;
42  EXPECT_FALSE(error.IsSet());
43
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());
49
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());
58}
59
60TEST_F(SyncErrorTest, Copy) {
61  tracked_objects::Location location = FROM_HERE;
62  std::string msg = "test";
63  ModelType type = PREFERENCES;
64
65  SyncError error1;
66  EXPECT_FALSE(error1.IsSet());
67  SyncError error2(error1);
68  EXPECT_FALSE(error2.IsSet());
69
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());
75
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());
81
82  SyncError error4;
83  EXPECT_FALSE(error4.IsSet());
84  SyncError error5(error4);
85  EXPECT_FALSE(error5.IsSet());
86}
87
88TEST_F(SyncErrorTest, Assign) {
89  tracked_objects::Location location = FROM_HERE;
90  std::string msg = "test";
91  ModelType type = PREFERENCES;
92
93  SyncError error1;
94  EXPECT_FALSE(error1.IsSet());
95  SyncError error2;
96  error2 = error1;
97  EXPECT_FALSE(error2.IsSet());
98
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());
104
105  error2 = error1;
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());
110
111  error2 = SyncError();
112  EXPECT_FALSE(error2.IsSet());
113}
114
115TEST_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));
125
126  SyncError error2;
127  EXPECT_FALSE(error2.IsSet());
128  EXPECT_EQ(std::string(), error2.ToString());
129
130  error2 = error;
131  EXPECT_TRUE(error2.IsSet());
132  EXPECT_NE(string::npos, error.ToString().find(expected));
133}
134
135}  // namespace
136
137}  // namespace syncer
138