Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Florian Weikert | a37e86c | 2015-08-27 12:57:25 +0000 | [diff] [blame] | 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 | // http://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.devtools.build.lib.syntax; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | |
| 18 | import com.google.devtools.build.lib.events.Location; |
| 19 | |
| 20 | import org.junit.Test; |
| 21 | import org.junit.runner.RunWith; |
| 22 | import org.junit.runners.JUnit4; |
| 23 | |
| 24 | /** |
| 25 | * A test class for exceptions. |
| 26 | */ |
| 27 | @RunWith(JUnit4.class) |
| 28 | public class ExceptionTest { |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 29 | private static final ASTNode DUMMY_NODE = new Identifier("DUMMY"); |
Florian Weikert | a37e86c | 2015-08-27 12:57:25 +0000 | [diff] [blame] | 30 | |
| 31 | @Test |
| 32 | public void testEmptyMessage() throws Exception { |
| 33 | EvalExceptionWithStackTrace ex = |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 34 | new EvalExceptionWithStackTrace(new NullPointerException(), DUMMY_NODE); |
Florian Weikert | 6a66339 | 2015-09-02 14:04:33 +0000 | [diff] [blame] | 35 | assertThat(ex.getMessage()) |
| 36 | .contains("Null Pointer: ExceptionTest.testEmptyMessage() in ExceptionTest.java:"); |
Florian Weikert | a37e86c | 2015-08-27 12:57:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | @Test |
| 40 | public void testExceptionCause() throws Exception { |
| 41 | IllegalArgumentException iae = new IllegalArgumentException("foo"); |
| 42 | EvalException ee = new EvalException(Location.BUILTIN, iae); |
| 43 | |
| 44 | runExceptionTest(iae, iae); |
| 45 | runExceptionTest(ee, iae); |
| 46 | } |
| 47 | |
| 48 | private void runExceptionTest(Exception toThrow, Exception expectedCause) { |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 49 | EvalExceptionWithStackTrace ex = new EvalExceptionWithStackTrace(toThrow, DUMMY_NODE); |
Florian Weikert | a37e86c | 2015-08-27 12:57:25 +0000 | [diff] [blame] | 50 | assertThat(ex.getCause()).isEqualTo(expectedCause); |
| 51 | } |
| 52 | } |