| 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.index.store.codec; |
| 6 |
| 7 import 'package:analysis_server/src/index/store/codec.dart'; |
| 8 import 'package:analyzer/src/generated/element.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/index.dart'; |
| 11 import 'package:typed_mock/typed_mock.dart'; |
| 12 import 'package:unittest/unittest.dart'; |
| 13 |
| 14 import '../../reflective_tests.dart'; |
| 15 import 'typed_mocks.dart'; |
| 16 |
| 17 |
| 18 main() { |
| 19 groupSep = ' | '; |
| 20 group('ContextCodec', () { |
| 21 runReflectiveTests(_ContextCodecTest); |
| 22 }); |
| 23 group('ElementCodec', () { |
| 24 runReflectiveTests(_ElementCodecTest); |
| 25 }); |
| 26 group('RelationshipCodec', () { |
| 27 runReflectiveTests(_RelationshipCodecTest); |
| 28 }); |
| 29 group('StringCodec', () { |
| 30 runReflectiveTests(_StringCodecTest); |
| 31 }); |
| 32 } |
| 33 |
| 34 |
| 35 @ReflectiveTestCase() |
| 36 class _ContextCodecTest { |
| 37 ContextCodec codec = new ContextCodec(); |
| 38 |
| 39 void test_encode_decode() { |
| 40 AnalysisContext contextA = new MockAnalysisContext('contextA'); |
| 41 AnalysisContext contextB = new MockAnalysisContext('contextB'); |
| 42 int idA = codec.encode(contextA); |
| 43 int idB = codec.encode(contextB); |
| 44 expect(idA, codec.encode(contextA)); |
| 45 expect(idB, codec.encode(contextB)); |
| 46 expect(codec.decode(idA), contextA); |
| 47 expect(codec.decode(idB), contextB); |
| 48 } |
| 49 |
| 50 void test_remove() { |
| 51 // encode |
| 52 { |
| 53 AnalysisContext context = new MockAnalysisContext('context'); |
| 54 // encode |
| 55 int id = codec.encode(context); |
| 56 expect(id, 0); |
| 57 expect(codec.decode(id), context); |
| 58 // remove |
| 59 codec.remove(context); |
| 60 expect(codec.decode(id), isNull); |
| 61 } |
| 62 // encode again |
| 63 { |
| 64 AnalysisContext context = new MockAnalysisContext('context'); |
| 65 // encode |
| 66 int id = codec.encode(context); |
| 67 expect(id, 1); |
| 68 expect(codec.decode(id), context); |
| 69 } |
| 70 } |
| 71 } |
| 72 |
| 73 |
| 74 @ReflectiveTestCase() |
| 75 class _ElementCodecTest { |
| 76 ElementCodec codec; |
| 77 AnalysisContext context = new MockAnalysisContext('context'); |
| 78 StringCodec stringCodec = new StringCodec(); |
| 79 |
| 80 void setUp() { |
| 81 codec = new ElementCodec(stringCodec); |
| 82 } |
| 83 |
| 84 void test_localLocalVariable() { |
| 85 { |
| 86 Element element = new MockElement(); |
| 87 ElementLocation location = new ElementLocationImpl.con3(['main', 'foo@1', |
| 88 'bar@2']); |
| 89 when(context.getElement(location)).thenReturn(element); |
| 90 when(element.location).thenReturn(location); |
| 91 int id = codec.encode(element); |
| 92 expect(codec.decode(context, id), element); |
| 93 } |
| 94 { |
| 95 Element element = new MockElement(); |
| 96 ElementLocation location = new ElementLocationImpl.con3(['main', 'foo@10', |
| 97 'bar@20']); |
| 98 when(context.getElement(location)).thenReturn(element); |
| 99 when(element.location).thenReturn(location); |
| 100 int id = codec.encode(element); |
| 101 expect(codec.decode(context, id), element); |
| 102 } |
| 103 // check strings, "foo" as a single string, no "foo@1" or "foo@10" |
| 104 expect(stringCodec.nameToIndex, hasLength(3)); |
| 105 expect(stringCodec.nameToIndex, containsPair('main', 0)); |
| 106 expect(stringCodec.nameToIndex, containsPair('foo', 1)); |
| 107 expect(stringCodec.nameToIndex, containsPair('bar', 2)); |
| 108 } |
| 109 |
| 110 void test_localVariable() { |
| 111 { |
| 112 Element element = new MockElement(); |
| 113 ElementLocation location = new ElementLocationImpl.con3(['main', |
| 114 'foo@42']); |
| 115 when(context.getElement(location)).thenReturn(element); |
| 116 when(element.location).thenReturn(location); |
| 117 int id = codec.encode(element); |
| 118 expect(codec.decode(context, id), element); |
| 119 } |
| 120 { |
| 121 Element element = new MockElement(); |
| 122 ElementLocation location = new ElementLocationImpl.con3(['main', |
| 123 'foo@4200']); |
| 124 when(context.getElement(location)).thenReturn(element); |
| 125 when(element.location).thenReturn(location); |
| 126 int id = codec.encode(element); |
| 127 expect(codec.decode(context, id), element); |
| 128 } |
| 129 // check strings, "foo" as a single string, no "foo@42" or "foo@4200" |
| 130 expect(stringCodec.nameToIndex, hasLength(2)); |
| 131 expect(stringCodec.nameToIndex, containsPair('main', 0)); |
| 132 expect(stringCodec.nameToIndex, containsPair('foo', 1)); |
| 133 } |
| 134 |
| 135 void test_notLocal() { |
| 136 Element element = new MockElement(); |
| 137 ElementLocation location = new ElementLocationImpl.con3(['foo', 'bar']); |
| 138 when(element.location).thenReturn(location); |
| 139 when(context.getElement(location)).thenReturn(element); |
| 140 int id = codec.encode(element); |
| 141 expect(codec.encode(element), id); |
| 142 expect(codec.decode(context, id), element); |
| 143 // check strings |
| 144 expect(stringCodec.nameToIndex, hasLength(2)); |
| 145 expect(stringCodec.nameToIndex, containsPair('foo', 0)); |
| 146 expect(stringCodec.nameToIndex, containsPair('bar', 1)); |
| 147 } |
| 148 } |
| 149 |
| 150 |
| 151 @ReflectiveTestCase() |
| 152 class _RelationshipCodecTest { |
| 153 RelationshipCodec codec; |
| 154 StringCodec stringCodec = new StringCodec(); |
| 155 |
| 156 void setUp() { |
| 157 codec = new RelationshipCodec(stringCodec); |
| 158 } |
| 159 |
| 160 void test_all() { |
| 161 Relationship relationship = Relationship.getRelationship('my-relationship'); |
| 162 int id = codec.encode(relationship); |
| 163 expect(codec.decode(id), relationship); |
| 164 } |
| 165 } |
| 166 |
| 167 |
| 168 @ReflectiveTestCase() |
| 169 class _StringCodecTest { |
| 170 StringCodec codec = new StringCodec(); |
| 171 |
| 172 void test_all() { |
| 173 int idA = codec.encode('aaa'); |
| 174 int idB = codec.encode('bbb'); |
| 175 expect(codec.decode(idA), 'aaa'); |
| 176 expect(codec.decode(idB), 'bbb'); |
| 177 } |
| 178 } |
| OLD | NEW |