error_support_test.cc 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2020 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. #include "error_support.h"
  5. #include <string>
  6. #include <vector>
  7. #include "test_platform.h"
  8. namespace crdtp {
  9. TEST(ErrorSupportTest, Empty) {
  10. ErrorSupport errors;
  11. EXPECT_TRUE(errors.Errors().empty());
  12. }
  13. TEST(ErrorSupportTest, Nesting) {
  14. ErrorSupport errors;
  15. // Enter field foo, inter element at index 42, enter field bar, and encounter
  16. // an error there ("something wrong").
  17. errors.Push();
  18. errors.SetName("foo");
  19. errors.Push();
  20. errors.SetIndex(42);
  21. errors.Push();
  22. errors.SetName("bar_sibling");
  23. errors.SetName("bar");
  24. errors.AddError("something wrong");
  25. errors.Pop(); // bar
  26. errors.Pop(); // 42
  27. // The common case is actually that we'll enter some field, set the name
  28. // or index, and leave without ever producing an error.
  29. errors.Push();
  30. errors.SetName("no_error_here");
  31. errors.Pop(); // no_error_here
  32. errors.Push();
  33. errors.SetName("bang");
  34. errors.AddError("one last error");
  35. errors.Pop(); // bang
  36. errors.Pop(); // foo
  37. std::string out(errors.Errors().begin(), errors.Errors().end());
  38. EXPECT_EQ("foo.42.bar: something wrong; foo.bang: one last error", out);
  39. }
  40. } // namespace crdtp