scoped_error_expecter.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2013 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 "sql/test/scoped_error_expecter.h"
  5. #include "base/bind.h"
  6. #include "base/types/pass_key.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace sql {
  9. namespace test {
  10. ScopedErrorExpecter::ScopedErrorExpecter()
  11. : checked_(false) {
  12. callback_ = base::BindRepeating(&ScopedErrorExpecter::ErrorSeen,
  13. base::Unretained(this));
  14. Database::SetScopedErrorExpecter(&callback_,
  15. base::PassKey<ScopedErrorExpecter>());
  16. }
  17. ScopedErrorExpecter::~ScopedErrorExpecter() {
  18. EXPECT_TRUE(checked_) << " Test must call SawExpectedErrors()";
  19. Database::ResetScopedErrorExpecter(base::PassKey<ScopedErrorExpecter>());
  20. }
  21. void ScopedErrorExpecter::ExpectError(int err) {
  22. EXPECT_EQ(0u, errors_expected_.count(err))
  23. << " Error " << err << " is already expected";
  24. errors_expected_.insert(err);
  25. }
  26. bool ScopedErrorExpecter::SawExpectedErrors() {
  27. checked_ = true;
  28. return errors_expected_ == errors_seen_;
  29. }
  30. bool ScopedErrorExpecter::ErrorSeen(int err) {
  31. // Look for extended code.
  32. if (errors_expected_.count(err) > 0) {
  33. // Record that the error was seen.
  34. errors_seen_.insert(err);
  35. return true;
  36. }
  37. // Trim extended codes and check again.
  38. int base_err = err & 0xff;
  39. if (errors_expected_.count(base_err) > 0) {
  40. // Record that the error was seen.
  41. errors_seen_.insert(base_err);
  42. return true;
  43. }
  44. // Unexpected error.
  45. ADD_FAILURE() << " Unexpected SQLite error " << err;
  46. return false;
  47. }
  48. } // namespace test
  49. } // namespace sql