1 // Copyright 2015 The Weave 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 <weave/error.h>
6 
7 #include <gtest/gtest.h>
8 
9 namespace weave {
10 
11 namespace {
12 
GenerateNetworkError()13 ErrorPtr GenerateNetworkError() {
14   tracked_objects::Location loc("GenerateNetworkError", "error_unittest.cc", 15,
15                                 ::tracked_objects::GetProgramCounter());
16   return Error::Create(loc, "not_found", "Resource not found");
17 }
18 
GenerateHttpError()19 ErrorPtr GenerateHttpError() {
20   ErrorPtr inner = GenerateNetworkError();
21   return Error::Create(FROM_HERE, "404", "Not found", std::move(inner));
22 }
23 
24 }  // namespace
25 
TEST(Error,Single)26 TEST(Error, Single) {
27   ErrorPtr err = GenerateNetworkError();
28   EXPECT_EQ("not_found", err->GetCode());
29   EXPECT_EQ("Resource not found", err->GetMessage());
30   EXPECT_EQ("GenerateNetworkError", err->GetLocation().function_name);
31   EXPECT_EQ("error_unittest.cc", err->GetLocation().file_name);
32   EXPECT_EQ(15, err->GetLocation().line_number);
33   EXPECT_EQ(nullptr, err->GetInnerError());
34   EXPECT_TRUE(err->HasError("not_found"));
35   EXPECT_FALSE(err->HasError("404"));
36   EXPECT_FALSE(err->HasError("bar"));
37 }
38 
TEST(Error,Nested)39 TEST(Error, Nested) {
40   ErrorPtr err = GenerateHttpError();
41   EXPECT_EQ("404", err->GetCode());
42   EXPECT_EQ("Not found", err->GetMessage());
43   EXPECT_NE(nullptr, err->GetInnerError());
44   EXPECT_TRUE(err->HasError("not_found"));
45   EXPECT_TRUE(err->HasError("404"));
46   EXPECT_FALSE(err->HasError("bar"));
47 }
48 
TEST(Error,Clone)49 TEST(Error, Clone) {
50   ErrorPtr err = GenerateHttpError();
51   ErrorPtr clone = err->Clone();
52   const Error* error1 = err.get();
53   const Error* error2 = clone.get();
54   while (error1 && error2) {
55     EXPECT_NE(error1, error2);
56     EXPECT_EQ(error1->GetCode(), error2->GetCode());
57     EXPECT_EQ(error1->GetMessage(), error2->GetMessage());
58     EXPECT_EQ(error1->GetLocation().function_name,
59               error2->GetLocation().function_name);
60     EXPECT_EQ(error1->GetLocation().file_name, error2->GetLocation().file_name);
61     EXPECT_EQ(error1->GetLocation().line_number,
62               error2->GetLocation().line_number);
63     error1 = error1->GetInnerError();
64     error2 = error2->GetInnerError();
65   }
66   EXPECT_EQ(error1, error2);
67 }
68 
69 }  // namespace weave
70