1 // Copyright 2018 The Amber Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "amber/result.h"
16 
17 #include "gtest/gtest.h"
18 
19 namespace amber {
20 
21 using ResultTest = testing::Test;
22 
TEST_F(ResultTest,SuccessByDefault)23 TEST_F(ResultTest, SuccessByDefault) {
24   Result r;
25   EXPECT_TRUE(r.IsSuccess());
26 }
27 
TEST_F(ResultTest,ErrorWithString)28 TEST_F(ResultTest, ErrorWithString) {
29   Result r("Test Failed");
30   EXPECT_FALSE(r.IsSuccess());
31   EXPECT_EQ("Test Failed", r.Error());
32 }
33 
TEST_F(ResultTest,ErrorWithEmptyString)34 TEST_F(ResultTest, ErrorWithEmptyString) {
35   Result r("");
36   EXPECT_FALSE(r.IsSuccess());
37   EXPECT_EQ("<no error message given>", r.Error());
38 }
39 
TEST_F(ResultTest,Copy)40 TEST_F(ResultTest, Copy) {
41   Result r("Testing");
42   Result r2(r);
43 
44   EXPECT_FALSE(r2.IsSuccess());
45   EXPECT_EQ("Testing", r2.Error());
46 }
47 
TEST_F(ResultTest,Append1String)48 TEST_F(ResultTest, Append1String) {
49   Result r;
50   r += "Test Failed";
51   EXPECT_EQ("Test Failed", r.Error());
52 }
53 
TEST_F(ResultTest,Append3Strings)54 TEST_F(ResultTest, Append3Strings) {
55   Result r;
56   r += "Error one";
57   r += "Error two";
58   r += "Error three";
59   EXPECT_EQ(R"(3 errors:
60  (1) Error one
61  (2) Error two
62  (3) Error three)",
63             r.Error());
64 }
65 
TEST_F(ResultTest,Append1SingleErrorResult)66 TEST_F(ResultTest, Append1SingleErrorResult) {
67   Result r;
68   r += Result("Test Failed");
69   EXPECT_EQ("Test Failed", r.Error());
70 }
71 
TEST_F(ResultTest,Append3SingleErrorResults)72 TEST_F(ResultTest, Append3SingleErrorResults) {
73   Result r;
74   r += Result("Error one");
75   r += Result("Error two");
76   r += Result("Error three");
77   EXPECT_EQ(R"(3 errors:
78  (1) Error one
79  (2) Error two
80  (3) Error three)",
81             r.Error());
82 }
83 
TEST_F(ResultTest,Append3MixedResults)84 TEST_F(ResultTest, Append3MixedResults) {
85   Result r;
86   r += Result("Error one");
87   r += Result();  // success
88   r += Result("Error two");
89   EXPECT_EQ(R"(2 errors:
90  (1) Error one
91  (2) Error two)",
92             r.Error());
93 }
94 
TEST_F(ResultTest,AppendMultipleErrorResults)95 TEST_F(ResultTest, AppendMultipleErrorResults) {
96   Result r1;
97   r1 += Result("r1 error one");
98   r1 += Result("r1 error two");
99   r1 += Result("r1 error three");
100   r1 += Result("");
101   Result r2;
102   r2 += Result("r2 error one");
103   r2 += r1;
104   r2 += Result("r2 error two");
105   EXPECT_EQ(R"(6 errors:
106  (1) r2 error one
107  (2) r1 error one
108  (3) r1 error two
109  (4) r1 error three
110  (5) <no error message given>
111  (6) r2 error two)",
112             r2.Error());
113 }
114 
115 }  // namespace amber
116