OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library test.services.src.index.store.codec; | 5 library test.services.src.index.store.codec; |
6 | 6 |
7 import 'package:analysis_services/index/index.dart'; | 7 import 'package:analysis_services/index/index.dart'; |
8 import 'package:analysis_services/src/index/store/codec.dart'; | 8 import 'package:analysis_services/src/index/store/codec.dart'; |
9 import 'package:analysis_testing/mocks.dart'; | 9 import 'package:analysis_testing/mocks.dart'; |
10 import 'package:analysis_testing/reflective_tests.dart'; | 10 import 'package:analysis_testing/reflective_tests.dart'; |
11 import 'package:analyzer/src/generated/element.dart'; | 11 import 'package:analyzer/src/generated/element.dart'; |
12 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
13 import 'package:typed_mock/typed_mock.dart'; | |
14 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
15 | 14 |
| 15 import '../abstract_single_unit.dart'; |
| 16 |
16 | 17 |
17 main() { | 18 main() { |
18 groupSep = ' | '; | 19 groupSep = ' | '; |
19 group('ContextCodec', () { | 20 group('ContextCodec', () { |
20 runReflectiveTests(_ContextCodecTest); | 21 runReflectiveTests(_ContextCodecTest); |
21 }); | 22 }); |
22 group('ElementCodec', () { | 23 group('ElementCodec', () { |
23 runReflectiveTests(_ElementCodecTest); | 24 runReflectiveTests(_ElementCodecTest); |
24 }); | 25 }); |
25 group('RelationshipCodec', () { | 26 group('RelationshipCodec', () { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // encode | 65 // encode |
65 int id = codec.encode(context); | 66 int id = codec.encode(context); |
66 expect(id, 1); | 67 expect(id, 1); |
67 expect(codec.decode(id), context); | 68 expect(codec.decode(id), context); |
68 } | 69 } |
69 } | 70 } |
70 } | 71 } |
71 | 72 |
72 | 73 |
73 @ReflectiveTestCase() | 74 @ReflectiveTestCase() |
74 class _ElementCodecTest { | 75 class _ElementCodecTest extends AbstractSingleUnitTest { |
75 ElementCodec codec; | 76 ElementCodec codec; |
76 AnalysisContext context = new MockAnalysisContext('context'); | 77 AnalysisContext context = new MockAnalysisContext('context'); |
77 StringCodec stringCodec = new StringCodec(); | 78 StringCodec stringCodec = new StringCodec(); |
78 | 79 |
79 void setUp() { | 80 void setUp() { |
| 81 super.setUp(); |
80 codec = new ElementCodec(stringCodec); | 82 codec = new ElementCodec(stringCodec); |
81 } | 83 } |
82 | 84 |
| 85 void test_field() { |
| 86 resolveTestUnit(''' |
| 87 class A { |
| 88 int field; |
| 89 } |
| 90 '''); |
| 91 FieldElement field = findElement('field', ElementKind.FIELD); |
| 92 PropertyAccessorElement getter = field.getter; |
| 93 PropertyAccessorElement setter = field.setter; |
| 94 { |
| 95 int id = codec.encode(getter); |
| 96 expect(codec.decode(context, id), getter); |
| 97 } |
| 98 { |
| 99 int id = codec.encode(setter); |
| 100 expect(codec.decode(context, id), setter); |
| 101 } |
| 102 { |
| 103 int id = codec.encode(field); |
| 104 expect(codec.decode(context, id), field); |
| 105 } |
| 106 } |
| 107 |
83 void test_localLocalVariable() { | 108 void test_localLocalVariable() { |
| 109 resolveTestUnit(''' |
| 110 main() { |
| 111 { |
| 112 foo() { |
| 113 int bar; // A |
| 114 } |
| 115 } |
| 116 { |
| 117 foo() { |
| 118 int bar; // B |
| 119 } |
| 120 } |
| 121 } |
| 122 '''); |
84 { | 123 { |
85 Element element = new MockElement(); | 124 LocalVariableElement element = findNodeElementAtString('bar; // A', null); |
86 ElementLocation location = new ElementLocationImpl.con3(['main', 'foo@1', | |
87 'bar@2']); | |
88 when(context.getElement(location)).thenReturn(element); | |
89 when(element.location).thenReturn(location); | |
90 int id = codec.encode(element); | 125 int id = codec.encode(element); |
91 expect(codec.decode(context, id), element); | 126 expect(codec.decode(context, id), element); |
92 } | 127 } |
93 { | 128 { |
94 Element element = new MockElement(); | 129 LocalVariableElement element = findNodeElementAtString('bar; // B', null); |
95 ElementLocation location = new ElementLocationImpl.con3(['main', 'foo@10', | |
96 'bar@20']); | |
97 when(context.getElement(location)).thenReturn(element); | |
98 when(element.location).thenReturn(location); | |
99 int id = codec.encode(element); | 130 int id = codec.encode(element); |
100 expect(codec.decode(context, id), element); | 131 expect(codec.decode(context, id), element); |
101 } | 132 } |
102 // check strings, "foo" as a single string, no "foo@1" or "foo@10" | 133 // check strings, "foo" as a single string, no "foo@17" or "bar@35" |
103 expect(stringCodec.nameToIndex, hasLength(3)); | 134 expect(stringCodec.nameToIndex, hasLength(4)); |
104 expect(stringCodec.nameToIndex, containsPair('main', 0)); | 135 expect(stringCodec.nameToIndex, containsPair('f/test.dart', 0)); |
105 expect(stringCodec.nameToIndex, containsPair('foo', 1)); | 136 expect(stringCodec.nameToIndex, containsPair('main', 1)); |
106 expect(stringCodec.nameToIndex, containsPair('bar', 2)); | 137 expect(stringCodec.nameToIndex, containsPair('foo', 2)); |
| 138 expect(stringCodec.nameToIndex, containsPair('bar', 3)); |
107 } | 139 } |
108 | 140 |
109 void test_localVariable() { | 141 void test_localVariable() { |
| 142 resolveTestUnit(''' |
| 143 main() { |
| 144 { |
| 145 int foo; // A |
| 146 } |
| 147 { |
| 148 int foo; // B |
| 149 } |
| 150 } |
| 151 '''); |
110 { | 152 { |
111 Element element = new MockElement(); | 153 LocalVariableElement element = findNodeElementAtString('foo; // A', null); |
112 ElementLocation location = new ElementLocationImpl.con3(['main', | |
113 'foo@42']); | |
114 when(context.getElement(location)).thenReturn(element); | |
115 when(element.location).thenReturn(location); | |
116 int id = codec.encode(element); | 154 int id = codec.encode(element); |
117 expect(codec.decode(context, id), element); | 155 expect(codec.decode(context, id), element); |
118 } | 156 } |
119 { | 157 { |
120 Element element = new MockElement(); | 158 LocalVariableElement element = findNodeElementAtString('foo; // B', null); |
121 ElementLocation location = new ElementLocationImpl.con3(['main', | |
122 'foo@4200']); | |
123 when(context.getElement(location)).thenReturn(element); | |
124 when(element.location).thenReturn(location); | |
125 int id = codec.encode(element); | 159 int id = codec.encode(element); |
126 expect(codec.decode(context, id), element); | 160 expect(codec.decode(context, id), element); |
127 } | 161 } |
128 // check strings, "foo" as a single string, no "foo@42" or "foo@4200" | 162 // check strings, "foo" as a single string, no "foo@21" or "foo@47" |
129 expect(stringCodec.nameToIndex, hasLength(2)); | 163 expect(stringCodec.nameToIndex, hasLength(3)); |
130 expect(stringCodec.nameToIndex, containsPair('main', 0)); | 164 expect(stringCodec.nameToIndex, containsPair('f/test.dart', 0)); |
131 expect(stringCodec.nameToIndex, containsPair('foo', 1)); | 165 expect(stringCodec.nameToIndex, containsPair('main', 1)); |
| 166 expect(stringCodec.nameToIndex, containsPair('foo', 2)); |
132 } | 167 } |
133 | 168 |
134 void test_notLocal() { | 169 void test_notLocal() { |
135 Element element = new MockElement(); | 170 resolveTestUnit(''' |
136 ElementLocation location = new ElementLocationImpl.con3(['foo', 'bar']); | 171 main() { |
137 when(element.location).thenReturn(location); | 172 int foo; |
138 when(context.getElement(location)).thenReturn(element); | 173 } |
| 174 '''); |
| 175 LocalVariableElement element = findElement('foo'); |
139 int id = codec.encode(element); | 176 int id = codec.encode(element); |
140 expect(codec.encode(element), id); | 177 expect(codec.encode(element), id); |
141 expect(codec.decode(context, id), element); | 178 expect(codec.decode(context, id), element); |
142 // check strings | 179 // check strings |
143 expect(stringCodec.nameToIndex, hasLength(2)); | 180 expect(stringCodec.nameToIndex, hasLength(3)); |
144 expect(stringCodec.nameToIndex, containsPair('foo', 0)); | 181 expect(stringCodec.nameToIndex, containsPair('f/test.dart', 0)); |
145 expect(stringCodec.nameToIndex, containsPair('bar', 1)); | 182 expect(stringCodec.nameToIndex, containsPair('main', 1)); |
| 183 expect(stringCodec.nameToIndex, containsPair('foo', 2)); |
146 } | 184 } |
147 } | 185 } |
148 | 186 |
149 | 187 |
150 @ReflectiveTestCase() | 188 @ReflectiveTestCase() |
151 class _RelationshipCodecTest { | 189 class _RelationshipCodecTest { |
152 StringCodec stringCodec = new StringCodec(); | 190 StringCodec stringCodec = new StringCodec(); |
153 RelationshipCodec codec; | 191 RelationshipCodec codec; |
154 | 192 |
155 void setUp() { | 193 void setUp() { |
(...skipping 12 matching lines...) Expand all Loading... |
168 class _StringCodecTest { | 206 class _StringCodecTest { |
169 StringCodec codec = new StringCodec(); | 207 StringCodec codec = new StringCodec(); |
170 | 208 |
171 void test_all() { | 209 void test_all() { |
172 int idA = codec.encode('aaa'); | 210 int idA = codec.encode('aaa'); |
173 int idB = codec.encode('bbb'); | 211 int idB = codec.encode('bbb'); |
174 expect(codec.decode(idA), 'aaa'); | 212 expect(codec.decode(idA), 'aaa'); |
175 expect(codec.decode(idB), 'bbb'); | 213 expect(codec.decode(idB), 'bbb'); |
176 } | 214 } |
177 } | 215 } |
OLD | NEW |