blob: 3814be544fbda3a39e51d3bd775a0234c012f58b [file] [log] [blame]
Masaya Suzuki5cecb862019-03-25 17:35:44 -07001// 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.
14package com.google.gitiles;
15
16/** Assertion methods for Gitiles. */
17public class MoreAssert {
Masaya Suzuki5cecb862019-03-25 17:35:44 -070018 /** 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 Suzuki5cecb862019-03-25 17:35:44 -070022 } catch (Throwable actual) {
23 if (expected.isAssignableFrom(actual.getClass())) {
David Pursehouse5ec7c622019-05-13 06:57:45 +020024 @SuppressWarnings("unchecked")
25 T toReturn = (T) actual;
26 return toReturn;
Masaya Suzuki5cecb862019-03-25 17:35:44 -070027 }
28 throw new AssertionError(
29 "Expected " + expected.getSimpleName() + ", but got " + actual.getClass().getSimpleName(),
30 actual);
31 }
Masaya Suzukid4efa392019-06-14 10:13:56 -070032 throw new AssertionError("Expected " + expected.getSimpleName() + " to be thrown");
Masaya Suzuki5cecb862019-03-25 17:35:44 -070033 }
34
35 public interface ThrowingRunnable {
36 void run() throws Throwable;
37 }
Masaya Suzukid4efa392019-06-14 10:13:56 -070038
39 private MoreAssert() {}
Masaya Suzuki5cecb862019-03-25 17:35:44 -070040}