| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.gitiles; |
| 15 | |
| 16 | /** Assertion methods for Gitiles. */ |
| 17 | public class MoreAssert { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 18 | /** Simple version of assertThrows that will be introduced in JUnit 4.13. */ |
| 19 | public static <T extends Throwable> T assertThrows(Class<T> expected, ThrowingRunnable r) { |
| 20 | try { |
| 21 | r.run(); |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 22 | } catch (Throwable actual) { |
| 23 | if (expected.isAssignableFrom(actual.getClass())) { |
| David Pursehouse | 5ec7c62 | 2019-05-13 06:57:45 +0200 | [diff] [blame] | 24 | @SuppressWarnings("unchecked") |
| 25 | T toReturn = (T) actual; |
| 26 | return toReturn; |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 27 | } |
| 28 | throw new AssertionError( |
| 29 | "Expected " + expected.getSimpleName() + ", but got " + actual.getClass().getSimpleName(), |
| 30 | actual); |
| 31 | } |
| Masaya Suzuki | d4efa39 | 2019-06-14 10:13:56 -0700 | [diff] [blame] | 32 | throw new AssertionError("Expected " + expected.getSimpleName() + " to be thrown"); |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | public interface ThrowingRunnable { |
| 36 | void run() throws Throwable; |
| 37 | } |
| Masaya Suzuki | d4efa39 | 2019-06-14 10:13:56 -0700 | [diff] [blame] | 38 | |
| 39 | private MoreAssert() {} |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 40 | } |