OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2014, the Dart project authors. |
| 3 * |
| 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except |
| 5 * in compliance with the License. You may obtain a copy of the License at |
| 6 * |
| 7 * http://www.eclipse.org/legal/epl-v10.html |
| 8 * |
| 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
| 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
| 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
| 12 * the License. |
| 13 */ |
| 14 package com.google.dart.engine.internal.index.file; |
| 15 |
| 16 import com.google.common.base.Objects; |
| 17 import com.google.common.collect.ImmutableMap; |
| 18 import com.google.common.collect.Lists; |
| 19 import com.google.dart.engine.context.AnalysisContext; |
| 20 import com.google.dart.engine.element.Element; |
| 21 import com.google.dart.engine.index.Location; |
| 22 import com.google.dart.engine.index.Relationship; |
| 23 |
| 24 import junit.framework.TestCase; |
| 25 |
| 26 import static org.fest.assertions.Assertions.assertThat; |
| 27 import static org.mockito.Mockito.mock; |
| 28 import static org.mockito.Mockito.when; |
| 29 |
| 30 import java.util.List; |
| 31 import java.util.Map; |
| 32 |
| 33 public class IndexNodeTest extends TestCase { |
| 34 private AnalysisContext context = mock(AnalysisContext.class); |
| 35 private StringCodec stringCodec = new StringCodec(); |
| 36 private ElementCodec elementCodec = mock(ElementCodec.class); |
| 37 private int nextElementId = 0; |
| 38 private RelationshipCodec relationshipCodec = new RelationshipCodec(stringCode
c); |
| 39 private IndexNode node = new IndexNode(context, elementCodec, relationshipCode
c); |
| 40 |
| 41 public void test_getContext() throws Exception { |
| 42 assertSame(context, node.getContext()); |
| 43 } |
| 44 |
| 45 public void test_recordRelationship() throws Exception { |
| 46 Element elementA = mockElement(); |
| 47 Element elementB = mockElement(); |
| 48 Element elementC = mockElement(); |
| 49 Relationship relationship = Relationship.getRelationship("my-relationship"); |
| 50 Location locationA = new Location(elementB, 1, 2); |
| 51 Location locationB = new Location(elementC, 10, 20); |
| 52 // empty initially |
| 53 assertEquals(0, node.getLocationCount()); |
| 54 // record |
| 55 node.recordRelationship(elementA, relationship, locationA); |
| 56 assertEquals(1, node.getLocationCount()); |
| 57 node.recordRelationship(elementA, relationship, locationB); |
| 58 assertEquals(2, node.getLocationCount()); |
| 59 // get relations |
| 60 assertThat(node.getRelationships(elementB, relationship)).isEmpty(); |
| 61 { |
| 62 Location[] locations = node.getRelationships(elementA, relationship); |
| 63 assertThat(locations).hasSize(2); |
| 64 boolean hasLocationA = false; |
| 65 boolean hasLocationB = false; |
| 66 for (Location location : locations) { |
| 67 hasLocationA |= location.getOffset() == 1 && location.getLength() == 2; |
| 68 hasLocationB |= location.getOffset() == 10 && location.getLength() == 20
; |
| 69 } |
| 70 assertTrue(hasLocationA); |
| 71 assertTrue(hasLocationB); |
| 72 } |
| 73 // verify relations map |
| 74 { |
| 75 Map<RelationKeyData, List<LocationData>> relations = node.getRelations(); |
| 76 assertEquals(1, relations.size()); |
| 77 List<LocationData> locations = relations.values().iterator().next(); |
| 78 assertEquals(2, locations.size()); |
| 79 } |
| 80 } |
| 81 |
| 82 public void test_setRelations() throws Exception { |
| 83 Element elementA = mockElement(); |
| 84 Element elementB = mockElement(); |
| 85 Element elementC = mockElement(); |
| 86 Relationship relationship = Relationship.getRelationship("my-relationship"); |
| 87 // record |
| 88 { |
| 89 int elementIdA = 0; |
| 90 int elementIdB = 1; |
| 91 int elementIdC = 2; |
| 92 int relationshipId = relationshipCodec.encode(relationship); |
| 93 RelationKeyData key = new RelationKeyData(elementIdA, relationshipId); |
| 94 List<LocationData> locations = Lists.newArrayList( |
| 95 new LocationData(elementIdB, 1, 10), |
| 96 new LocationData(elementIdC, 2, 20)); |
| 97 Map<RelationKeyData, List<LocationData>> relations = ImmutableMap.of(key,
locations); |
| 98 node.setRelations(relations); |
| 99 } |
| 100 // request |
| 101 Location[] locations = node.getRelationships(elementA, relationship); |
| 102 assertThat(locations).hasSize(2); |
| 103 assertHasLocation(locations, elementB, 1, 10); |
| 104 assertHasLocation(locations, elementC, 2, 20); |
| 105 } |
| 106 |
| 107 private void assertHasLocation(Location[] locations, Element element, int offs
et, int length) { |
| 108 for (Location location : locations) { |
| 109 if (Objects.equal(location.getElement(), element) && location.getOffset()
== offset |
| 110 && location.getLength() == length) { |
| 111 return; |
| 112 } |
| 113 } |
| 114 fail("Expected to find Location(element=" + element + ", offset=" + offset +
", length=" |
| 115 + length + ")"); |
| 116 } |
| 117 |
| 118 private Element mockElement() { |
| 119 int elementId = nextElementId++; |
| 120 Element element = mock(Element.class); |
| 121 when(elementCodec.encode(element)).thenReturn(elementId); |
| 122 when(elementCodec.decode(context, elementId)).thenReturn(element); |
| 123 return element; |
| 124 } |
| 125 } |
OLD | NEW |