OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library test.services.src.index.store.codec; | |
6 | |
7 import 'package:analysis_services/index/index.dart'; | |
8 import 'package:analysis_services/src/index/store/codec.dart'; | |
9 import 'package:analysis_testing/abstract_single_unit.dart'; | |
10 import 'package:analysis_testing/mocks.dart'; | |
11 import 'package:analysis_testing/reflective_tests.dart'; | |
12 import 'package:analyzer/src/generated/element.dart'; | |
13 import 'package:analyzer/src/generated/engine.dart'; | |
14 import 'package:unittest/unittest.dart'; | |
15 | |
16 | |
17 main() { | |
18 groupSep = ' | '; | |
19 runReflectiveTests(_ContextCodecTest); | |
20 runReflectiveTests(_ElementCodecTest); | |
21 runReflectiveTests(_RelationshipCodecTest); | |
22 runReflectiveTests(_StringCodecTest); | |
23 } | |
24 | |
25 | |
26 @ReflectiveTestCase() | |
27 class _ContextCodecTest { | |
28 ContextCodec codec = new ContextCodec(); | |
29 | |
30 void test_encode_decode() { | |
31 AnalysisContext contextA = new MockAnalysisContext('contextA'); | |
32 AnalysisContext contextB = new MockAnalysisContext('contextB'); | |
33 int idA = codec.encode(contextA); | |
34 int idB = codec.encode(contextB); | |
35 expect(idA, codec.encode(contextA)); | |
36 expect(idB, codec.encode(contextB)); | |
37 expect(codec.decode(idA), contextA); | |
38 expect(codec.decode(idB), contextB); | |
39 } | |
40 | |
41 void test_remove() { | |
42 // encode | |
43 { | |
44 AnalysisContext context = new MockAnalysisContext('context'); | |
45 // encode | |
46 int id = codec.encode(context); | |
47 expect(id, 0); | |
48 expect(codec.decode(id), context); | |
49 // remove | |
50 codec.remove(context); | |
51 expect(codec.decode(id), isNull); | |
52 } | |
53 // encode again | |
54 { | |
55 AnalysisContext context = new MockAnalysisContext('context'); | |
56 // encode | |
57 int id = codec.encode(context); | |
58 expect(id, 1); | |
59 expect(codec.decode(id), context); | |
60 } | |
61 } | |
62 } | |
63 | |
64 | |
65 @ReflectiveTestCase() | |
66 class _ElementCodecTest extends AbstractSingleUnitTest { | |
67 ElementCodec codec; | |
68 AnalysisContext context = new MockAnalysisContext('context'); | |
69 StringCodec stringCodec = new StringCodec(); | |
70 | |
71 void setUp() { | |
72 super.setUp(); | |
73 codec = new ElementCodec(stringCodec); | |
74 } | |
75 | |
76 void test_encodeHash_notLocal() { | |
77 resolveTestUnit(''' | |
78 class A { | |
79 void mainA() { | |
80 int foo; // A | |
81 } | |
82 void mainB() { | |
83 int foo; // B | |
84 int bar; | |
85 } | |
86 } | |
87 '''); | |
88 MethodElement mainA = findElement('mainA'); | |
89 MethodElement mainB = findElement('mainB'); | |
90 Element fooA = mainA.localVariables[0]; | |
91 Element fooB = mainB.localVariables[0]; | |
92 Element bar = mainB.localVariables[1]; | |
93 int id_fooA = codec.encodeHash(fooA); | |
94 int id_fooB = codec.encodeHash(fooB); | |
95 int id_bar = codec.encodeHash(bar); | |
96 expect(id_fooA == id_fooB, isTrue); | |
97 expect(id_fooA == id_bar, isFalse); | |
98 } | |
99 | |
100 void test_field() { | |
101 resolveTestUnit(''' | |
102 class A { | |
103 int field; | |
104 } | |
105 '''); | |
106 FieldElement field = findElement('field', ElementKind.FIELD); | |
107 PropertyAccessorElement getter = field.getter; | |
108 PropertyAccessorElement setter = field.setter; | |
109 { | |
110 int id = codec.encode(getter); | |
111 expect(codec.decode(context, id), getter); | |
112 } | |
113 { | |
114 int id = codec.encode(setter); | |
115 expect(codec.decode(context, id), setter); | |
116 } | |
117 { | |
118 int id = codec.encode(field); | |
119 expect(codec.decode(context, id), field); | |
120 } | |
121 } | |
122 | |
123 void test_localLocalVariable() { | |
124 resolveTestUnit(''' | |
125 main() { | |
126 { | |
127 foo() { | |
128 int bar; // A | |
129 } | |
130 } | |
131 { | |
132 foo() { | |
133 int bar; // B | |
134 } | |
135 } | |
136 } | |
137 '''); | |
138 { | |
139 LocalVariableElement element = findNodeElementAtString('bar; // A', null); | |
140 int id = codec.encode(element); | |
141 expect(codec.decode(context, id), element); | |
142 } | |
143 { | |
144 LocalVariableElement element = findNodeElementAtString('bar; // B', null); | |
145 int id = codec.encode(element); | |
146 expect(codec.decode(context, id), element); | |
147 } | |
148 // check strings, "foo" as a single string, no "foo@17" or "bar@35" | |
149 expect(stringCodec.nameToIndex, hasLength(4)); | |
150 expect(stringCodec.nameToIndex, containsPair('file:///test.dart', 0)); | |
151 expect(stringCodec.nameToIndex, containsPair('main', 1)); | |
152 expect(stringCodec.nameToIndex, containsPair('foo', 2)); | |
153 expect(stringCodec.nameToIndex, containsPair('bar', 3)); | |
154 } | |
155 | |
156 void test_localVariable() { | |
157 resolveTestUnit(''' | |
158 main() { | |
159 { | |
160 int foo; // A | |
161 } | |
162 { | |
163 int foo; // B | |
164 } | |
165 } | |
166 '''); | |
167 { | |
168 LocalVariableElement element = findNodeElementAtString('foo; // A', null); | |
169 int id = codec.encode(element); | |
170 expect(codec.decode(context, id), element); | |
171 } | |
172 { | |
173 LocalVariableElement element = findNodeElementAtString('foo; // B', null); | |
174 int id = codec.encode(element); | |
175 expect(codec.decode(context, id), element); | |
176 } | |
177 // check strings, "foo" as a single string, no "foo@21" or "foo@47" | |
178 expect(stringCodec.nameToIndex, hasLength(3)); | |
179 expect(stringCodec.nameToIndex, containsPair('file:///test.dart', 0)); | |
180 expect(stringCodec.nameToIndex, containsPair('main', 1)); | |
181 expect(stringCodec.nameToIndex, containsPair('foo', 2)); | |
182 } | |
183 | |
184 void test_notLocal() { | |
185 resolveTestUnit(''' | |
186 main() { | |
187 int foo; | |
188 } | |
189 '''); | |
190 LocalVariableElement element = findElement('foo'); | |
191 int id = codec.encode(element); | |
192 expect(codec.encode(element), id); | |
193 expect(codec.decode(context, id), element); | |
194 // check strings | |
195 expect(stringCodec.nameToIndex, hasLength(3)); | |
196 expect(stringCodec.nameToIndex, containsPair('file:///test.dart', 0)); | |
197 expect(stringCodec.nameToIndex, containsPair('main', 1)); | |
198 expect(stringCodec.nameToIndex, containsPair('foo', 2)); | |
199 } | |
200 } | |
201 | |
202 | |
203 @ReflectiveTestCase() | |
204 class _RelationshipCodecTest { | |
205 StringCodec stringCodec = new StringCodec(); | |
206 RelationshipCodec codec; | |
207 | |
208 void setUp() { | |
209 codec = new RelationshipCodec(stringCodec); | |
210 } | |
211 | |
212 void test_all() { | |
213 Relationship relationship = Relationship.getRelationship('my-relationship'); | |
214 int id = codec.encode(relationship); | |
215 expect(codec.decode(id), relationship); | |
216 } | |
217 } | |
218 | |
219 | |
220 @ReflectiveTestCase() | |
221 class _StringCodecTest { | |
222 StringCodec codec = new StringCodec(); | |
223 | |
224 void test_all() { | |
225 int idA = codec.encode('aaa'); | |
226 int idB = codec.encode('bbb'); | |
227 expect(codec.decode(idA), 'aaa'); | |
228 expect(codec.decode(idB), 'bbb'); | |
229 } | |
230 } | |
OLD | NEW |