1 /*
2  * Copyright 2017, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.common;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.ExpectedException;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 /** Tests for {@link Functions}. */
28 @RunWith(JUnit4.class)
29 public class FunctionsTest {
30   @Rule public ExpectedException thrown = ExpectedException.none();
31 
32   @Test
testReturnNull()33   public void testReturnNull() {
34     assertThat(Functions.returnNull().apply("ignored")).isNull();
35   }
36 
37   @Test
testReturnConstant()38   public void testReturnConstant() {
39     assertThat(Functions.returnConstant(123).apply("ignored")).isEqualTo(123);
40   }
41 
42   @Test
testReturnToString()43   public void testReturnToString() {
44     assertThat(Functions.returnToString().apply("input")).isEqualTo("input");
45     assertThat(Functions.returnToString().apply(Boolean.FALSE)).isEqualTo("false");
46     assertThat(Functions.returnToString().apply(Double.valueOf(123.45))).isEqualTo("123.45");
47     assertThat(Functions.returnToString().apply(null)).isEqualTo(null);
48   }
49 
50   @Test
testThrowIllegalArgumentException()51   public void testThrowIllegalArgumentException() {
52     Function<Object, Void> f = Functions.throwIllegalArgumentException();
53     thrown.expect(IllegalArgumentException.class);
54     f.apply("ignored");
55   }
56 
57   @Test
testThrowAssertionError()58   public void testThrowAssertionError() {
59     Function<Object, Void> f = Functions.throwAssertionError();
60     thrown.handleAssertionErrors();
61     thrown.expect(AssertionError.class);
62     f.apply("ignored");
63   }
64 }
65