blob: 77a5a8068a6880838db59c710652690e09c65d8d [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;
Florian Weikerta37e86c2015-08-27 12:57:25 +000019import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.junit.runners.JUnit4;
22
23/**
24 * A test class for exceptions.
25 */
26@RunWith(JUnit4.class)
27public class ExceptionTest {
Florian Weikert90a15962015-09-11 13:43:10 +000028 private static final ASTNode DUMMY_NODE = new Identifier("DUMMY");
Florian Weikerta37e86c2015-08-27 12:57:25 +000029
30 @Test
31 public void testEmptyMessage() throws Exception {
32 EvalExceptionWithStackTrace ex =
Florian Weikert90a15962015-09-11 13:43:10 +000033 new EvalExceptionWithStackTrace(new NullPointerException(), DUMMY_NODE);
lberkiaea56b32017-05-30 12:35:33 +020034 assertThat(ex)
35 .hasMessageThat()
Florian Weikert6a663392015-09-02 14:04:33 +000036 .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);
lberkiaea56b32017-05-30 12:35:33 +020050 assertThat(ex).hasCauseThat().isEqualTo(expectedCause);
Florian Weikerta37e86c2015-08-27 12:57:25 +000051 }
52}