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.dart.engine.context.AnalysisContext; |
| 17 import com.google.dart.engine.element.Element; |
| 18 import com.google.dart.engine.element.ElementLocation; |
| 19 import com.google.dart.engine.internal.element.ElementLocationImpl; |
| 20 |
| 21 import junit.framework.TestCase; |
| 22 |
| 23 import static org.fest.assertions.Assertions.assertThat; |
| 24 import static org.fest.assertions.MapAssert.entry; |
| 25 import static org.mockito.Mockito.mock; |
| 26 import static org.mockito.Mockito.when; |
| 27 |
| 28 public class ElementCodecTest extends TestCase { |
| 29 private AnalysisContext context = mock(AnalysisContext.class); |
| 30 private StringCodec stringCodec = new StringCodec(); |
| 31 private ElementCodec codec = new ElementCodec(stringCodec); |
| 32 |
| 33 public void test_localLocalVariable() throws Exception { |
| 34 { |
| 35 Element element = mock(Element.class); |
| 36 ElementLocation location = new ElementLocationImpl(new String[] {"main", "
foo@1", "bar@2"}); |
| 37 when(context.getElement(location)).thenReturn(element); |
| 38 when(element.getLocation()).thenReturn(location); |
| 39 int id = codec.encode(element); |
| 40 assertEquals(element, codec.decode(context, id)); |
| 41 } |
| 42 { |
| 43 Element element = mock(Element.class); |
| 44 ElementLocation location = new ElementLocationImpl(new String[] {"main", "
foo@10", "bar@20"}); |
| 45 when(context.getElement(location)).thenReturn(element); |
| 46 when(element.getLocation()).thenReturn(location); |
| 47 int id = codec.encode(element); |
| 48 assertEquals(element, codec.decode(context, id)); |
| 49 } |
| 50 // check strings, "foo" as a single string, no "foo@1" or "foo@10" |
| 51 assertThat(stringCodec.getNameToIndex()).hasSize(3).includes( |
| 52 entry("main", 0), |
| 53 entry("foo", 1), |
| 54 entry("bar", 2)); |
| 55 } |
| 56 |
| 57 public void test_localVariable() throws Exception { |
| 58 { |
| 59 Element element = mock(Element.class); |
| 60 ElementLocation location = new ElementLocationImpl(new String[] {"main", "
foo@42"}); |
| 61 when(context.getElement(location)).thenReturn(element); |
| 62 when(element.getLocation()).thenReturn(location); |
| 63 int id = codec.encode(element); |
| 64 assertEquals(element, codec.decode(context, id)); |
| 65 } |
| 66 { |
| 67 Element element = mock(Element.class); |
| 68 ElementLocation location = new ElementLocationImpl(new String[] {"main", "
foo@4200"}); |
| 69 when(context.getElement(location)).thenReturn(element); |
| 70 when(element.getLocation()).thenReturn(location); |
| 71 int id = codec.encode(element); |
| 72 assertEquals(element, codec.decode(context, id)); |
| 73 } |
| 74 // check strings, "foo" as a single string, no "foo@42" or "foo@4200" |
| 75 assertThat(stringCodec.getNameToIndex()).hasSize(2).includes(entry("main", 0
), entry("foo", 1)); |
| 76 } |
| 77 |
| 78 public void test_notLocal() throws Exception { |
| 79 Element element = mock(Element.class); |
| 80 ElementLocation location = new ElementLocationImpl(new String[] {"foo", "bar
"}); |
| 81 when(element.getLocation()).thenReturn(location); |
| 82 when(context.getElement(location)).thenReturn(element); |
| 83 int id = codec.encode(element); |
| 84 assertEquals(element, codec.decode(context, id)); |
| 85 // check strings |
| 86 assertThat(stringCodec.getNameToIndex()).hasSize(2).includes(entry("foo", 0)
, entry("bar", 1)); |
| 87 } |
| 88 } |
OLD | NEW |