1 // Copyright 2016 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 "headless/public/util/error_reporter.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 
8 namespace headless {
9 
TEST(ErrorReporterTest,NoErrors)10 TEST(ErrorReporterTest, NoErrors) {
11   ErrorReporter reporter;
12 #if DCHECK_IS_ON()
13   EXPECT_FALSE(reporter.HasErrors());
14   EXPECT_TRUE(reporter.errors().empty());
15 #endif  // DCHECK_IS_ON()
16 }
17 
TEST(ErrorReporterTest,TopLevelErrors)18 TEST(ErrorReporterTest, TopLevelErrors) {
19   ErrorReporter reporter;
20   reporter.AddError("instructions unclear");
21   reporter.AddError("head stuck in std::unordered_map");
22 #if DCHECK_IS_ON()
23   EXPECT_TRUE(reporter.HasErrors());
24   EXPECT_EQ(2u, reporter.errors().size());
25   EXPECT_EQ("instructions unclear", reporter.errors()[0]);
26   EXPECT_EQ("head stuck in std::unordered_map", reporter.errors()[1]);
27 #endif  // DCHECK_IS_ON()
28 }
29 
TEST(ErrorReporterTest,UnnamedContext)30 TEST(ErrorReporterTest, UnnamedContext) {
31   ErrorReporter reporter;
32   reporter.Push();
33   reporter.AddError("lp0 is on fire");
34   reporter.Pop();
35 #if DCHECK_IS_ON()
36   EXPECT_TRUE(reporter.HasErrors());
37   EXPECT_EQ(1u, reporter.errors().size());
38   EXPECT_EQ("lp0 is on fire", reporter.errors()[0]);
39 #endif  // DCHECK_IS_ON()
40 }
41 
TEST(ErrorReporterTest,NestedContexts)42 TEST(ErrorReporterTest, NestedContexts) {
43   ErrorReporter reporter;
44   reporter.Push();
45   reporter.SetName("ship");
46   reporter.Push();
47   reporter.SetName("front");
48   reporter.AddError("fell off");
49   reporter.Pop();
50   reporter.AddError("uh oh");
51   reporter.Pop();
52 #if DCHECK_IS_ON()
53   EXPECT_TRUE(reporter.HasErrors());
54   EXPECT_EQ(2u, reporter.errors().size());
55   EXPECT_EQ("ship.front: fell off", reporter.errors()[0]);
56   EXPECT_EQ("ship: uh oh", reporter.errors()[1]);
57 #endif  // DCHECK_IS_ON()
58 }
59 
60 }  // namespace headless
61