Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #ifndef SQL_TEST_SCOPED_ERROR_EXPECTER_H_ | |
| 6 #define SQL_TEST_SCOPED_ERROR_EXPECTER_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "sql/connection.h" | |
| 12 | |
| 13 // This is not strictly necessary for the operation of ScopedErrorExpecter, but | |
| 14 // the class is not useful without the SQLite error codes. | |
| 15 #include "third_party/sqlite/sqlite3.h" | |
| 16 | |
| 17 namespace sql { | |
| 18 namespace test { | |
| 19 | |
| 20 // sql::Connection and sql::Statement treat most SQLite errors as fatal in debug | |
| 21 // mode. The goal is to catch SQL errors before code is shipped to production. | |
| 22 // That fatal check makes it hard to write tests for error-handling code. This | |
| 23 // scoper lists errors to expect and treat as non-fatal. Errors are expected | |
| 24 // globally (on all connections). | |
| 25 // | |
| 26 // Since errors can be very context-dependent, the class is pedantic - specific | |
| 27 // errors must be expected, and every expected error must be seen. | |
| 28 // | |
| 29 // NOTE(shess): There are still fatal error cases this does not address. If | |
| 30 // your test is handling database errors and you're hitting a case not handled, | |
| 31 // contact me. | |
| 32 class ScopedErrorExpecter { | |
| 33 public: | |
| 34 ScopedErrorExpecter(); | |
| 35 ~ScopedErrorExpecter(); | |
| 36 | |
| 37 // Add an error to expect. Extended error codes can be specified | |
| 38 // individually, or the base code can be specified to expect errors for the | |
| 39 // entire group (SQLITE_IOERR_* versus SQLITE_IOERR). | |
| 40 void ExpectError(int err); | |
| 41 | |
| 42 // Return |true| if the all of the expected errors were encountered. Failure | |
| 43 // to call this results in an EXPECT failure when the instance is destructed. | |
| 44 bool SawExpectedErrors() WARN_UNUSED_RESULT; | |
| 45 | |
| 46 // Expose sqlite3_libversion_number() so that clients don't have to add a | |
| 47 // dependency on third_party/sqlite. | |
| 48 static int SQLiteLibVersionNumber() WARN_UNUSED_RESULT; | |
| 49 | |
| 50 private: | |
| 51 // Record that an error was observed. | |
| 52 bool ErrorSeen(int err); | |
|
Mark P
2016/05/23 19:53:27
Please explain what the return value means.
Scott Hess - ex-Googler
2016/06/03 00:02:12
Done.
| |
| 53 | |
| 54 // Callback passed to Connection::SetErrorExpecter(). | |
| 55 Connection::ErrorExpecterCallback callback_; | |
| 56 | |
| 57 // Record whether SawExpectedErrors() has been called. | |
| 58 bool checked_; | |
| 59 | |
| 60 // Errors to expect. | |
| 61 std::set<int> errors_expected_; | |
| 62 | |
| 63 // Expected errors which have been encountered. | |
| 64 std::set<int> errors_seen_; | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(ScopedErrorExpecter); | |
| 67 }; | |
| 68 | |
| 69 } // namespace test | |
| 70 } // namespace sql | |
| 71 | |
| 72 #endif // SQL_TEST_SCOPED_ERROR_EXPECTER_H_ | |
| OLD | NEW |