Chromium Code Reviews| 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.computer.element; | |
| 6 | |
| 7 import 'package:analysis_server/src/computer/element.dart'; | |
| 8 import 'package:analysis_server/src/constants.dart'; | |
| 9 import 'package:analyzer/src/generated/element.dart' as engine; | |
| 10 import 'package:analyzer/src/generated/utilities_dart.dart' as engine; | |
| 11 import 'package:typed_mock/typed_mock.dart'; | |
| 12 import 'package:unittest/unittest.dart'; | |
| 13 | |
| 14 import '../index/store/typed_mocks.dart'; | |
| 15 import '../reflective_tests.dart'; | |
| 16 | |
| 17 | |
| 18 main() { | |
| 19 groupSep = ' | '; | |
| 20 group('Element', () { | |
| 21 runReflectiveTests(_ElementTest); | |
| 22 }); | |
| 23 group('ElementKind', () { | |
| 24 test('toString', test_ElementKind_toString); | |
| 25 test('valueOf', test_ElementKind_valueOf); | |
| 26 test('valueOfEngine', test_ElementKind_valueOfEngine); | |
| 27 }); | |
| 28 } | |
| 29 | |
| 30 | |
| 31 void test_ElementKind_toString() { | |
| 32 expect(ElementKind.CLASS.toString(), 'CLASS'); | |
| 33 expect(ElementKind.COMPILATION_UNIT.toString(), 'COMPILATION_UNIT'); | |
| 34 } | |
| 35 | |
| 36 | |
| 37 void test_ElementKind_valueOf() { | |
| 38 expect(ElementKind.valueOf('CLASS'), ElementKind.CLASS); | |
|
Brian Wilkerson
2014/06/25 13:50:08
Wouldn't this be better as
expect(ElementKind.val
scheglov
2014/06/25 17:20:46
Done.
| |
| 39 expect(ElementKind.valueOf('CLASS_TYPE_ALIAS'), ElementKind.CLASS_TYPE_ALIAS); | |
| 40 expect(ElementKind.valueOf('COMPILATION_UNIT'), ElementKind.COMPILATION_UNIT); | |
| 41 expect(ElementKind.valueOf('CONSTRUCTOR'), ElementKind.CONSTRUCTOR); | |
| 42 expect(ElementKind.valueOf('FIELD'), ElementKind.FIELD); | |
| 43 expect(ElementKind.valueOf('FUNCTION'), ElementKind.FUNCTION); | |
| 44 expect(ElementKind.valueOf('FUNCTION_TYPE_ALIAS'), | |
| 45 ElementKind.FUNCTION_TYPE_ALIAS); | |
| 46 expect(ElementKind.valueOf('GETTER'), ElementKind.GETTER); | |
| 47 expect(ElementKind.valueOf('LIBRARY'), ElementKind.LIBRARY); | |
| 48 expect(ElementKind.valueOf('METHOD'), ElementKind.METHOD); | |
| 49 expect(ElementKind.valueOf('SETTER'), ElementKind.SETTER); | |
| 50 expect(ElementKind.valueOf('TOP_LEVEL_VARIABLE'), | |
| 51 ElementKind.TOP_LEVEL_VARIABLE); | |
| 52 expect(ElementKind.valueOf('UNIT_TEST_CASE'), ElementKind.UNIT_TEST_CASE); | |
| 53 expect(ElementKind.valueOf('UNIT_TEST_GROUP'), ElementKind.UNIT_TEST_GROUP); | |
| 54 expect(ElementKind.valueOf('UNKNOWN'), ElementKind.UNKNOWN); | |
| 55 expect(ElementKind.valueOf('CLASS'), ElementKind.CLASS); | |
|
Brian Wilkerson
2014/06/25 13:50:08
Duplicate.
scheglov
2014/06/25 17:20:46
Done.
| |
| 56 expect(() { | |
| 57 ElementKind.valueOf('no-such-kind'); | |
| 58 }, throws); | |
| 59 } | |
| 60 | |
| 61 | |
| 62 void test_ElementKind_valueOfEngine() { | |
| 63 expect(ElementKind.valueOfEngine(engine.ElementKind.CLASS), | |
| 64 ElementKind.CLASS); | |
| 65 expect(ElementKind.valueOfEngine(engine.ElementKind.COMPILATION_UNIT), | |
| 66 ElementKind.COMPILATION_UNIT); | |
| 67 expect(ElementKind.valueOfEngine(engine.ElementKind.CONSTRUCTOR), | |
| 68 ElementKind.CONSTRUCTOR); | |
| 69 expect(ElementKind.valueOfEngine(engine.ElementKind.FIELD), | |
| 70 ElementKind.FIELD); | |
| 71 expect(ElementKind.valueOfEngine(engine.ElementKind.FUNCTION), | |
| 72 ElementKind.FUNCTION); | |
| 73 expect(ElementKind.valueOfEngine(engine.ElementKind.FUNCTION_TYPE_ALIAS), | |
| 74 ElementKind.FUNCTION_TYPE_ALIAS); | |
| 75 expect(ElementKind.valueOfEngine(engine.ElementKind.GETTER), | |
| 76 ElementKind.GETTER); | |
| 77 expect(ElementKind.valueOfEngine(engine.ElementKind.LIBRARY), | |
| 78 ElementKind.LIBRARY); | |
| 79 expect(ElementKind.valueOfEngine(engine.ElementKind.METHOD), | |
| 80 ElementKind.METHOD); | |
| 81 expect(ElementKind.valueOfEngine(engine.ElementKind.SETTER), | |
| 82 ElementKind.SETTER); | |
| 83 expect(ElementKind.valueOfEngine(engine.ElementKind.TOP_LEVEL_VARIABLE), | |
| 84 ElementKind.TOP_LEVEL_VARIABLE); | |
| 85 expect(ElementKind.valueOfEngine(engine.ElementKind.ANGULAR_COMPONENT), | |
| 86 ElementKind.UNKNOWN); | |
| 87 } | |
| 88 | |
| 89 | |
| 90 @ReflectiveTestCase() | |
| 91 class _ElementTest { | |
| 92 void test_fromElement_CLASS() { | |
| 93 engine.ClassElement engineElement = new MockClassElement(); | |
|
Brian Wilkerson
2014/06/25 13:50:08
Given how light-weight elements are, why not just
scheglov
2014/06/25 17:20:47
Yes, I'd like to.
But ElementFactory is not a part
| |
| 94 when(engineElement.kind).thenReturn(engine.ElementKind.CLASS); | |
| 95 when(engineElement.nameOffset).thenReturn(1); | |
| 96 when(engineElement.displayName).thenReturn('MyClass'); | |
| 97 when(engineElement.isAbstract).thenReturn(true); | |
| 98 when(engineElement.isDeprecated).thenReturn(true); | |
| 99 when(engineElement.isPrivate).thenReturn(true); | |
| 100 // create notification Element | |
| 101 Element element = new Element.fromEngine(engineElement); | |
| 102 expect(element.kind, ElementKind.CLASS); | |
| 103 expect(element.name, 'MyClass'); | |
| 104 expect(element.offset, 1); | |
| 105 expect(element.length, 'MyClass'.length); | |
| 106 expect(element.flags, Element.FLAG_ABSTRACT | Element.FLAG_DEPRECATED | | |
| 107 Element.FLAG_PRIVATE); | |
| 108 } | |
| 109 | |
| 110 void test_fromElement_CONSTRUCTOR() { | |
| 111 engine.ConstructorElement engineElement = new MockConstructorElement(); | |
| 112 when(engineElement.kind).thenReturn(engine.ElementKind.CONSTRUCTOR); | |
| 113 when(engineElement.nameOffset).thenReturn(1); | |
| 114 when(engineElement.displayName).thenReturn('myConstructor'); | |
| 115 when(engineElement.isConst).thenReturn(true); | |
| 116 when(engineElement.isDeprecated).thenReturn(false); | |
| 117 when(engineElement.isPrivate).thenReturn(false); | |
| 118 when(engineElement.isStatic).thenReturn(false); | |
| 119 when(engineElement.parameters).thenReturn([]); | |
| 120 { | |
| 121 engine.ParameterElement a = new MockParameterElement('int a'); | |
| 122 engine.ParameterElement b = new MockParameterElement('String b'); | |
| 123 when(b.kind).thenReturn(engine.ParameterKind.POSITIONAL); | |
| 124 when(engineElement.parameters).thenReturn([a, b]); | |
| 125 } | |
| 126 { | |
| 127 engine.DartType returnType = new MockDartType('Map<int, String>'); | |
| 128 when(engineElement.returnType).thenReturn(returnType); | |
| 129 } | |
| 130 // create notification Element | |
| 131 Element element = new Element.fromEngine(engineElement); | |
| 132 expect(element.kind, ElementKind.CONSTRUCTOR); | |
| 133 expect(element.name, 'myConstructor'); | |
| 134 expect(element.offset, 1); | |
| 135 expect(element.length, 'myConstructor'.length); | |
| 136 expect(element.parameters, '(int a, [String b])'); | |
| 137 expect(element.returnType, 'Map<int, String>'); | |
| 138 expect(element.flags, Element.FLAG_CONST); | |
| 139 } | |
| 140 | |
| 141 void test_fromElement_FIELD() { | |
| 142 engine.FieldElement engineElement = new MockFieldElement(); | |
| 143 when(engineElement.kind).thenReturn(engine.ElementKind.FIELD); | |
| 144 when(engineElement.nameOffset).thenReturn(1); | |
| 145 when(engineElement.displayName).thenReturn('myField'); | |
| 146 when(engineElement.isConst).thenReturn(true); | |
| 147 when(engineElement.isFinal).thenReturn(true); | |
| 148 when(engineElement.isDeprecated).thenReturn(false); | |
| 149 when(engineElement.isPrivate).thenReturn(false); | |
| 150 when(engineElement.isStatic).thenReturn(true); | |
| 151 // create notification Element | |
| 152 Element element = new Element.fromEngine(engineElement); | |
| 153 expect(element.kind, ElementKind.FIELD); | |
| 154 expect(element.name, 'myField'); | |
| 155 expect(element.offset, 1); | |
| 156 expect(element.length, 'myField'.length); | |
| 157 expect(element.parameters, isNull); | |
| 158 expect(element.returnType, isNull); | |
| 159 expect(element.flags, Element.FLAG_CONST | Element.FLAG_FINAL | Element.FLAG _STATIC); | |
| 160 } | |
| 161 | |
| 162 void test_fromElement_GETTER() { | |
| 163 engine.PropertyAccessorElement engineElement = new MockPropertyAccessorEleme nt(); | |
| 164 when(engineElement.kind).thenReturn(engine.ElementKind.GETTER); | |
| 165 when(engineElement.nameOffset).thenReturn(1); | |
| 166 when(engineElement.displayName).thenReturn('myGetter'); | |
| 167 when(engineElement.isAbstract).thenReturn(false); | |
| 168 when(engineElement.isDeprecated).thenReturn(true); | |
| 169 when(engineElement.isPrivate).thenReturn(false); | |
| 170 when(engineElement.isStatic).thenReturn(false); | |
| 171 when(engineElement.parameters).thenReturn([]); | |
| 172 { | |
| 173 engine.DartType returnType = new MockDartType('String'); | |
| 174 when(engineElement.returnType).thenReturn(returnType); | |
| 175 } | |
| 176 // create notification Element | |
| 177 Element element = new Element.fromEngine(engineElement); | |
| 178 expect(element.kind, ElementKind.GETTER); | |
| 179 expect(element.name, 'myGetter'); | |
| 180 expect(element.offset, 1); | |
| 181 expect(element.length, 'myGetter'.length); | |
| 182 expect(element.parameters, '()'); | |
| 183 expect(element.returnType, 'String'); | |
| 184 expect(element.flags, Element.FLAG_DEPRECATED); | |
| 185 } | |
| 186 | |
| 187 void test_fromElement_METHOD() { | |
| 188 engine.MethodElement engineElement = new MockMethodElement(); | |
| 189 when(engineElement.kind).thenReturn(engine.ElementKind.METHOD); | |
| 190 when(engineElement.nameOffset).thenReturn(1); | |
| 191 when(engineElement.displayName).thenReturn('myMethod'); | |
| 192 when(engineElement.isAbstract).thenReturn(false); | |
| 193 when(engineElement.isDeprecated).thenReturn(false); | |
| 194 when(engineElement.isPrivate).thenReturn(false); | |
| 195 when(engineElement.isStatic).thenReturn(true); | |
| 196 { | |
| 197 engine.ParameterElement a = new MockParameterElement('int a'); | |
| 198 engine.ParameterElement b = new MockParameterElement('String b'); | |
| 199 when(b.kind).thenReturn(engine.ParameterKind.NAMED); | |
| 200 when(engineElement.parameters).thenReturn([a, b]); | |
| 201 } | |
| 202 { | |
| 203 engine.DartType returnType = new MockDartType('List<String>'); | |
| 204 when(engineElement.returnType).thenReturn(returnType); | |
| 205 } | |
| 206 // create notification Element | |
| 207 Element element = new Element.fromEngine(engineElement); | |
| 208 expect(element.kind, ElementKind.METHOD); | |
| 209 expect(element.name, 'myMethod'); | |
| 210 expect(element.offset, 1); | |
| 211 expect(element.length, 'myMethod'.length); | |
| 212 expect(element.parameters, '(int a, {String b})'); | |
| 213 expect(element.returnType, 'List<String>'); | |
| 214 expect(element.flags, Element.FLAG_STATIC); | |
| 215 } | |
| 216 | |
| 217 void test_fromJson() { | |
| 218 var flags = Element.FLAG_DEPRECATED | Element.FLAG_PRIVATE | | |
| 219 Element.FLAG_STATIC; | |
| 220 var json = { | |
| 221 KIND: 'METHOD', | |
| 222 NAME: 'my name', | |
| 223 OFFSET: 1, | |
| 224 LENGTH: 2, | |
| 225 FLAGS: flags, | |
| 226 PARAMETERS: '(int a, String b)', | |
| 227 RETURN_TYPE: 'List<String>' | |
| 228 }; | |
| 229 Element element = new Element.fromJson(json); | |
| 230 expect(element.kind, ElementKind.METHOD); | |
| 231 expect(element.name, 'my name'); | |
| 232 expect(element.offset, 1); | |
| 233 expect(element.length, 2); | |
| 234 expect(element.flags, flags); | |
| 235 expect(element.isAbstract, isFalse); | |
| 236 expect(element.isConst, isFalse); | |
| 237 expect(element.isDeprecated, isTrue); | |
| 238 expect(element.isFinal, isFalse); | |
| 239 expect(element.isPrivate, isTrue); | |
| 240 expect(element.isStatic, isTrue); | |
| 241 } | |
| 242 | |
| 243 void test_toJson() { | |
| 244 var json = { | |
| 245 KIND: 'METHOD', | |
| 246 NAME: 'my name', | |
| 247 OFFSET: 1, | |
| 248 LENGTH: 2, | |
| 249 FLAGS: Element.FLAG_DEPRECATED | Element.FLAG_PRIVATE | | |
| 250 Element.FLAG_STATIC, | |
| 251 PARAMETERS: '(int a, String b)', | |
| 252 RETURN_TYPE: 'List<String>' | |
| 253 }; | |
| 254 Element element = new Element.fromJson(json); | |
| 255 expect(element.toJson(), equals(json)); | |
| 256 } | |
| 257 } | |
| OLD | NEW |