blob: b91e9c264f468dc5247ab7bae6a8e5dc7f510257 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Florian Weikerta37e86c2015-08-27 12:57:25 +00002//
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.
14package com.google.devtools.build.lib.syntax;
15
16import static com.google.common.truth.Truth.assertThat;
17
18import com.google.devtools.build.lib.events.Location;
19
20import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.junit.runners.JUnit4;
23
24/**
25 * A test class for exceptions.
26 */
27@RunWith(JUnit4.class)
28public class ExceptionTest {
Florian Weikert90a15962015-09-11 13:43:10 +000029 private static final ASTNode DUMMY_NODE = new Identifier("DUMMY");
Florian Weikerta37e86c2015-08-27 12:57:25 +000030
31 @Test
32 public void testEmptyMessage() throws Exception {
33 EvalExceptionWithStackTrace ex =
Florian Weikert90a15962015-09-11 13:43:10 +000034 new EvalExceptionWithStackTrace(new NullPointerException(), DUMMY_NODE);
Florian Weikert6a663392015-09-02 14:04:33 +000035 assertThat(ex.getMessage())
36 .contains("Null Pointer: ExceptionTest.testEmptyMessage() in ExceptionTest.java:");
Florian Weikerta37e86c2015-08-27 12:57:25 +000037 }
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 Weikert90a15962015-09-11 13:43:10 +000049 EvalExceptionWithStackTrace ex = new EvalExceptionWithStackTrace(toThrow, DUMMY_NODE);
Florian Weikerta37e86c2015-08-27 12:57:25 +000050 assertThat(ex.getCause()).isEqualTo(expectedCause);
51 }
52}