File
Blob: src/workerd/util/test.h
| 1 | // Copyright (c) 2024 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | #pragma once |
| 6 | #include <kj/test.h> |
| 7 | |
| 8 | namespace workerd { |
| 9 | |
| 10 | // Checks that code throws a kj::Exception matching the type and message of the given exception. |
| 11 | // |
| 12 | // Note: Performs no special handling for JsExceptionThrown, so tests that need to explicitly |
| 13 | // detect thrown JS exceptions will probably want to use a separate macro. |
| 14 | #define WD_EXPECT_THROW(expException, code, ...) \ |
| 15 | do { \ |
| 16 | auto expExcObj = (expException).clone(); \ |
| 17 | KJ_IF_SOME(e, ::kj::runCatchingExceptions([&]() { (void)({ code; }); })) { \ |
| 18 | KJ_EXPECT(e.getType() == expExcObj.getType(), "code threw wrong exception type: " #code, e, \ |
| 19 | ##__VA_ARGS__); \ |
| 20 | KJ_EXPECT(e.getDescription() == expExcObj.getDescription(), \ |
| 21 | "exception description didn't match", e, ##__VA_ARGS__); \ |
| 22 | } else { \ |
| 23 | KJ_FAIL_EXPECT("code did not throw: " #code, ##__VA_ARGS__); \ |
| 24 | } \ |
| 25 | } while (false) |
| 26 | |
| 27 | } // namespace workerd |