Debian Codegraph @

src/test/java/com/google/escapevelocity/ReferenceNodeTest.java

raw
1 /*
2 * Copyright (C) 2018 Google, Inc.
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 package com.google.escapevelocity;
17
18 import static com.google.common.truth.Truth.assertThat;
19
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.primitives.Primitives;
23 import com.google.common.truth.Expect;
24 import com.google.escapevelocity.ReferenceNode.MethodReferenceNode;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29
30 /**
31 * Tests for {@link ReferenceNode}.
32 *
33 * @author [email protected] (Éamonn McManus)
34 */
35 @RunWith(JUnit4.class)
36 public class ReferenceNodeTest {
37 @Rule public Expect expect = Expect.create();
38
39 // This is the exhaustive list from
40 // https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.2.
41 // We put the "from" type first for consistency with that list, even though that is inconsistent
42 // with our method order (which is itself consistent with assignment, "to" on the left).
43 private static final ImmutableSet<ImmutableList<Class<?>>> ASSIGNMENT_COMPATIBLE =
44 makeAssignmentCompatibleSet();
45 private static ImmutableSet<ImmutableList<Class<?>>> makeAssignmentCompatibleSet() {
46 Class<?>[][] pairs = {
47 {byte.class, short.class},
48 {byte.class, int.class},
49 {byte.class, long.class},
50 {byte.class, float.class},
51 {byte.class, double.class},
52 {short.class, int.class},
53 {short.class, long.class},
54 {short.class, float.class},
55 {short.class, double.class},
56 {char.class, int.class},
57 {char.class, long.class},
58 {char.class, float.class},
59 {char.class, double.class},
60 {int.class, long.class},
61 {int.class, float.class},
62 {int.class, double.class},
63 {long.class, float.class},
64 {long.class, double.class},
65 {float.class, double.class},
66 };
67 ImmutableSet.Builder<ImmutableList<Class<?>>> builder = ImmutableSet.builder();
68 for (Class<?>[] pair : pairs) {
69 builder.add(ImmutableList.copyOf(pair));
70 }
71 return builder.build();
72 }
73
74 @Test
75 public void testPrimitiveTypeIsAssignmentCompatible() {
76 for (Class<?> from : Primitives.allPrimitiveTypes()) {
77 for (Class<?> to : Primitives.allPrimitiveTypes()) {
78 boolean expected =
79 (from == to || ASSIGNMENT_COMPATIBLE.contains(ImmutableList.of(from, to)));
80 boolean actual =
81 MethodReferenceNode.primitiveTypeIsAssignmentCompatible(to, from);
82 expect
83 .withMessage(from + " assignable to " + to)
84 .that(actual).isEqualTo(expected);
85 }
86 }
87 }
88
89 @Test
90 public void testCompatibleArgs() {
91 assertThat(MethodReferenceNode.compatibleArgs(
92 new Class<?>[]{int.class}, ImmutableList.of((Object) 5))).isTrue();
93 }
94 }