• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.binder.lookup;
18 
19 import static com.google.common.collect.Iterables.getOnlyElement;
20 import static com.google.common.truth.Truth.assertThat;
21 import static org.junit.Assert.fail;
22 
23 import com.google.common.collect.ImmutableList;
24 import com.google.turbine.binder.sym.ClassSymbol;
25 import com.google.turbine.tree.Tree.Ident;
26 import java.util.NoSuchElementException;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 
31 @RunWith(JUnit4.class)
32 public class TopLevelIndexTest {
33 
34   private static final TopLevelIndex index = buildIndex();
35 
buildIndex()36   private static TopLevelIndex buildIndex() {
37     return SimpleTopLevelIndex.of(
38         ImmutableList.of(
39             new ClassSymbol("java/util/Map"),
40             new ClassSymbol("java/util/List"),
41             new ClassSymbol("java.util.Optional")));
42   }
43 
44   @Test
simple()45   public void simple() {
46     LookupResult result = index.scope().lookup(lookupKey(ImmutableList.of("java", "util", "Map")));
47     assertThat(result.sym()).isEqualTo(new ClassSymbol("java/util/Map"));
48     assertThat(result.remaining()).isEmpty();
49   }
50 
51   @Test
nested()52   public void nested() {
53     LookupResult result =
54         index.scope().lookup(lookupKey(ImmutableList.of("java", "util", "Map", "Entry")));
55     assertThat(result.sym()).isEqualTo(new ClassSymbol("java/util/Map"));
56     assertThat(getOnlyElement(result.remaining()).value()).isEqualTo("Entry");
57   }
58 
59   @Test
empty()60   public void empty() {
61     assertThat(index.scope().lookup(lookupKey(ImmutableList.of("java", "NoSuch", "Entry"))))
62         .isNull();
63     assertThat(index.lookupPackage(ImmutableList.of("java", "math"))).isNull();
64     assertThat(index.lookupPackage(ImmutableList.of("java", "util", "Map"))).isNull();
65   }
66 
67   @Test
packageScope()68   public void packageScope() {
69     Scope scope = index.lookupPackage(ImmutableList.of("java", "util"));
70 
71     assertThat(scope.lookup(lookupKey(ImmutableList.of("Map"))).sym())
72         .isEqualTo(new ClassSymbol("java/util/Map"));
73     assertThat(scope.lookup(lookupKey(ImmutableList.of("List"))).sym())
74         .isEqualTo(new ClassSymbol("java/util/List"));
75     assertThat(scope.lookup(lookupKey(ImmutableList.of("NoSuch")))).isNull();
76   }
77 
78   @Test
overrideClass()79   public void overrideClass() {
80     {
81       // the use of Foo as a class name in the package java is "sticky"
82       TopLevelIndex index =
83           SimpleTopLevelIndex.of(
84               ImmutableList.of(new ClassSymbol("java/Foo"), new ClassSymbol("java/Foo/Bar")));
85 
86       LookupResult result = index.scope().lookup(lookupKey(ImmutableList.of("java", "Foo")));
87       assertThat(result.sym()).isEqualTo(new ClassSymbol("java/Foo"));
88       assertThat(result.remaining()).isEmpty();
89     }
90     {
91       // the use of Foo as a package name under java is "sticky"
92       TopLevelIndex index =
93           SimpleTopLevelIndex.of(
94               ImmutableList.of(new ClassSymbol("java/Foo/Bar"), new ClassSymbol("java/Foo")));
95 
96       assertThat(index.scope().lookup(lookupKey(ImmutableList.of("java", "Foo")))).isNull();
97       LookupResult packageResult =
98           index
99               .lookupPackage(ImmutableList.of("java", "Foo"))
100               .lookup(lookupKey(ImmutableList.of("Bar")));
101       assertThat(packageResult.sym()).isEqualTo(new ClassSymbol("java/Foo/Bar"));
102       assertThat(packageResult.remaining()).isEmpty();
103     }
104   }
105 
106   @Test
emptyLookup()107   public void emptyLookup() {
108     LookupKey key = lookupKey(ImmutableList.of("java", "util", "List"));
109     key = key.rest();
110     key = key.rest();
111     try {
112       key.rest();
113       fail("expected exception");
114     } catch (NoSuchElementException e) {
115       // expected
116     }
117   }
118 
lookupKey(ImmutableList<String> names)119   private LookupKey lookupKey(ImmutableList<String> names) {
120     ImmutableList.Builder<Ident> result = ImmutableList.builder();
121     for (String name : names) {
122       result.add(new Ident(-1, name));
123     }
124     return new LookupKey(result.build());
125   }
126 }
127