1 /*
2  * Copyright 2016 Google Inc. All Rights Reserved.
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 com.google.turbine.lower;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.collect.ImmutableList;
22 import com.google.turbine.binder.sym.ClassSymbol;
23 import com.google.turbine.binder.sym.TyVarSymbol;
24 import com.google.turbine.bytecode.sig.SigWriter;
25 import com.google.turbine.model.TurbineConstantTypeKind;
26 import com.google.turbine.type.Type;
27 import com.google.turbine.type.Type.ArrayTy;
28 import com.google.turbine.type.Type.ClassTy;
29 import com.google.turbine.type.Type.ClassTy.SimpleClassTy;
30 import com.google.turbine.type.Type.PrimTy;
31 import com.google.turbine.type.Type.TyVar;
32 import com.google.turbine.type.Type.WildLowerBoundedTy;
33 import com.google.turbine.type.Type.WildUnboundedTy;
34 import com.google.turbine.type.Type.WildUpperBoundedTy;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.JUnit4;
38 
39 @RunWith(JUnit4.class)
40 public class LowerSignatureTest {
41   @Test
simple()42   public void simple() {
43     Type.ClassTy type =
44         ClassTy.create(
45             ImmutableList.of(
46                 SimpleClassTy.create(
47                     new ClassSymbol("java/util/List"), ImmutableList.of(), ImmutableList.of())));
48     assertThat(SigWriter.type(new LowerSignature().signature(type))).isEqualTo("Ljava/util/List;");
49   }
50 
51   @Test
inner()52   public void inner() {
53     assertThat(
54             SigWriter.type(
55                 new LowerSignature()
56                     .signature(
57                         ClassTy.create(
58                             ImmutableList.of(
59                                 SimpleClassTy.create(
60                                     new ClassSymbol("test/Outer"),
61                                     ImmutableList.of(),
62                                     ImmutableList.of()),
63                                 SimpleClassTy.create(
64                                     new ClassSymbol("test/Outer$Inner"),
65                                     ImmutableList.of(),
66                                     ImmutableList.of()))))))
67         .isEqualTo("Ltest/Outer$Inner;");
68   }
69 
70   @Test
genericEnclosing()71   public void genericEnclosing() {
72     Type.ClassTy type =
73         ClassTy.create(
74             ImmutableList.of(
75                 SimpleClassTy.create(
76                     new ClassSymbol("test/Outer"),
77                     ImmutableList.of(ClassTy.OBJECT),
78                     ImmutableList.of()),
79                 SimpleClassTy.create(
80                     new ClassSymbol("test/Outer$Inner"),
81                     ImmutableList.of(ClassTy.OBJECT),
82                     ImmutableList.of())));
83     assertThat(SigWriter.type(new LowerSignature().signature(type)))
84         .isEqualTo("Ltest/Outer<Ljava/lang/Object;>.Inner<Ljava/lang/Object;>;");
85     // Type#toString is only for debugging
86     assertThat(type.toString()).isEqualTo("test/Outer<java/lang/Object>.Inner<java/lang/Object>");
87   }
88 
89   @Test
innerDefaultPackage()90   public void innerDefaultPackage() {
91     assertThat(
92             SigWriter.type(
93                 new LowerSignature()
94                     .signature(
95                         ClassTy.create(
96                             ImmutableList.of(
97                                 SimpleClassTy.create(
98                                     new ClassSymbol("Outer"),
99                                     ImmutableList.of(),
100                                     ImmutableList.of()),
101                                 SimpleClassTy.create(
102                                     new ClassSymbol("Outer$Inner"),
103                                     ImmutableList.of(),
104                                     ImmutableList.of()))))))
105         .isEqualTo("LOuter$Inner;");
106   }
107 
108   @Test
wildcard()109   public void wildcard() {
110     assertThat(
111             SigWriter.type(
112                 new LowerSignature()
113                     .signature(
114                         ClassTy.create(
115                             ImmutableList.of(
116                                 SimpleClassTy.create(
117                                     new ClassSymbol("test/Test"),
118                                     ImmutableList.of(
119                                         WildUnboundedTy.create(ImmutableList.of()),
120                                         WildLowerBoundedTy.create(
121                                             ClassTy.OBJECT, ImmutableList.of()),
122                                         WildUpperBoundedTy.create(
123                                             ClassTy.OBJECT, ImmutableList.of())),
124                                     ImmutableList.of()))))))
125         .isEqualTo("Ltest/Test<*-Ljava/lang/Object;+Ljava/lang/Object;>;");
126   }
127 
128   @Test
tyVar()129   public void tyVar() {
130     assertThat(
131             SigWriter.type(
132                 new LowerSignature()
133                     .signature(
134                         TyVar.create(
135                             new TyVarSymbol(ClassSymbol.OBJECT, "X"), ImmutableList.of()))))
136         .isEqualTo("TX;");
137   }
138 
139   @Test
primitive()140   public void primitive() {
141     assertThat(
142             SigWriter.type(
143                 new LowerSignature()
144                     .signature(PrimTy.create(TurbineConstantTypeKind.BOOLEAN, ImmutableList.of()))))
145         .isEqualTo("Z");
146   }
147 
148   @Test
voidType()149   public void voidType() {
150     assertThat(SigWriter.type(new LowerSignature().signature(Type.VOID))).isEqualTo("V");
151   }
152 
153   @Test
array()154   public void array() {
155     assertThat(
156             SigWriter.type(
157                 new LowerSignature()
158                     .signature(
159                         ArrayTy.create(
160                             ArrayTy.create(
161                                 ArrayTy.create(
162                                     PrimTy.create(
163                                         TurbineConstantTypeKind.BOOLEAN, ImmutableList.of()),
164                                     ImmutableList.of()),
165                                 ImmutableList.of()),
166                             ImmutableList.of()))))
167         .isEqualTo("[[[Z");
168   }
169 }
170