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 /** | |
| 6 * Tests for utility class used to extract debugger data defined in | |
| 7 * native_DOMImplementation.dart | |
| 8 */ | |
| 9 library utilsTest; | |
| 10 | |
| 11 import 'package:expect/expect.dart'; | |
| 12 import 'package:unittest/html_config.dart'; | |
| 13 import 'package:unittest/unittest.dart'; | |
| 14 import 'dart:html'; | |
| 15 | |
| 16 import 'sample_library.dart'; | |
| 17 | |
| 18 import 'dart:mirrors'; | |
| 19 | |
| 20 /** | |
| 21 * Wrapper around the [dart:html] [_Utils] class that lets us invoke its | |
| 22 * methods from a different library. | |
|
vsm
2014/04/25 00:01:14
Is this just for testing purposes?
| |
| 23 */ | |
| 24 class UtilsWrapper { | |
| 25 ClassMirror _receiver; | |
| 26 | |
| 27 UtilsWrapper() { | |
| 28 var html = currentMirrorSystem().libraries[Uri.parse('dart:html')]; | |
| 29 _receiver = html.declarations[MirrorSystem.getSymbol('_Utils', html)]; | |
| 30 } | |
| 31 | |
| 32 noSuchMethod(Invocation msg) => | |
| 33 _receiver.invoke(msg.memberName, | |
| 34 msg.positionalArguments, | |
| 35 msg.namedArguments).reflectee; | |
| 36 } | |
| 37 | |
| 38 /** | |
| 39 * Helper class to convert the packed property structure used by Dartium back | |
| 40 * to a more human readable form. | |
| 41 */ | |
| 42 class _Property { | |
| 43 _Property(List input, int offset) : | |
| 44 name = input[offset], | |
| 45 setter = input[offset + 1], | |
| 46 getter = input[offset + 2], | |
| 47 value = input[offset + 3], | |
| 48 hasValue = input[offset + 4], | |
| 49 writable = input[offset + 5], | |
| 50 isMethod = input[offset + 6], | |
| 51 isOwn = input[offset + 7], | |
| 52 wasThrown = input[offset + 8]; | |
| 53 | |
| 54 final String name; | |
| 55 final Function setter; | |
| 56 final Function getter; | |
| 57 final value; | |
| 58 final bool hasValue; | |
| 59 final bool writable; | |
| 60 final bool isMethod; | |
| 61 final bool isOwn; | |
| 62 final bool wasThrown; | |
| 63 } | |
| 64 | |
| 65 var _Utils = new UtilsWrapper(); | |
| 66 | |
| 67 _Property findObjectClassProperty(o, String name) => | |
| 68 findProperty(_Utils.getObjectClassProperties(o, true, false), name); | |
| 69 | |
| 70 _Property findProperty(List properties, String name) { | |
| 71 for (int i = 0; i < properties.length; i += 9) { | |
| 72 if (properties[i] == name) return new _Property(properties, i); | |
| 73 } | |
| 74 return null; | |
| 75 } | |
| 76 | |
| 77 main() { | |
| 78 useHtmlConfiguration(true); | |
| 79 | |
| 80 var sampleLibraryUrl = reflectClass(Foo).owner.uri.toString(); | |
| 81 | |
| 82 group('completions', () { | |
| 83 | |
| 84 test('object', () { | |
| 85 expect(_Utils.getObjectCompletions(new Foo(), sampleLibraryUrl), | |
| 86 orderedEquals(['_fooPrivate1', '_fooPrivate2', 'field1', 'field2', | |
| 87 'hashCode', 'member1', 'member2', 'noSuchMethod', 'prop1', | |
| 88 'prop2', 'runtimeType', 'toString'])); | |
| 89 | |
| 90 // Should not display any privates as a different library is specified. | |
| 91 expect(_Utils.getObjectCompletions(new Foo(), 'dart:html'), | |
| 92 orderedEquals(['field1', 'field2', 'hashCode', 'member1', 'member2', | |
| 93 'noSuchMethod', 'prop1', 'prop2', 'runtimeType', 'toString'])); | |
| 94 | |
| 95 expect(_Utils.getObjectCompletions(new Object(), sampleLibraryUrl), | |
| 96 orderedEquals( | |
| 97 ['hashCode', 'noSuchMethod', 'runtimeType', 'toString'])); | |
| 98 | |
| 99 // We don't want to make the test depend on exactly what private members | |
| 100 // Object has but it is safe to assume that Object will have some private | |
| 101 // members. | |
| 102 expect(_Utils.getObjectCompletions(new Object(), 'dart:core').length, | |
| 103 greaterThan(4)); | |
| 104 }); | |
| 105 | |
| 106 test('class', () { | |
| 107 expect(_Utils.getObjectCompletions(Foo, sampleLibraryUrl), | |
|
vsm
2014/04/25 00:01:14
Test privacy here as well?
| |
| 108 orderedEquals(['staticField1', 'staticField2', 'staticMember1'])); | |
| 109 expect(_Utils.getObjectCompletions(Bar, sampleLibraryUrl), | |
| 110 orderedEquals(['barConst1', 'barStaticMethod', 'barStaticProp'])); | |
| 111 }); | |
| 112 | |
| 113 test('library', () { | |
| 114 expect(_Utils.getLibraryCompletions('dart:core'), | |
| 115 contains('Object')); | |
| 116 | |
| 117 var completions = _Utils.getLibraryCompletions(sampleLibraryUrl); | |
| 118 expect(completions, contains('Foo')); | |
| 119 expect(completions, contains('exampleStaticMethod')); | |
| 120 expect(completions, contains('_examplePrivateStaticMethod')); | |
| 121 expect(completions, contains('_exampleProp2')); | |
| 122 expect(completions, contains('exampleProp2')); | |
| 123 // Contained only in the imported dart:html library. | |
| 124 expect(completions, isNot(contains('Node'))); | |
| 125 expect(completions, isNot(contains('js'))); | |
| 126 | |
| 127 completions = _Utils.getLibraryCompletionsIncludingImports(sampleLibraryUr l); | |
|
vsm
2014/04/25 00:01:14
length
| |
| 128 expect(completions, contains('Node')); | |
| 129 expect(completions, contains('js')); | |
| 130 }); | |
| 131 }); | |
| 132 | |
| 133 group('getProperties', () { | |
| 134 | |
| 135 test('treat property as field', () { | |
| 136 expect(_Utils.treatPropertyAsField( | |
| 137 reflectClass(Foo).declarations[MirrorSystem.getSymbol('prop1')]), | |
| 138 isFalse); | |
| 139 | |
| 140 // Insure that dart:html properties are treated as fields. | |
| 141 expect(_Utils.treatPropertyAsField( | |
| 142 reflectClass(Node).declarations[MirrorSystem.getSymbol('text')]), | |
| 143 true); | |
| 144 }); | |
| 145 | |
| 146 test('describe function', () { | |
| 147 var b = new Bar(); | |
| 148 expect(_Utils.describeFunction(b.barMember1), equals('barMember1() => 42;' )); | |
|
vsm
2014/04/25 00:01:14
length
| |
| 149 // Invocation trampoline for function barMember1. | |
| 150 var barMember1 = findObjectClassProperty(b, 'barMember1').value; | |
| 151 expect(_Utils.describeFunction(barMember1), equals('barMember1() => 42;')) ; | |
|
vsm
2014/04/25 00:01:14
length
| |
| 152 | |
| 153 var details = _Utils.getInvocationTrampolineDetails(barMember1); | |
| 154 expect(details[0], equals(57)); // Line number. | |
| 155 expect(details[1], equals(3)); // Column number. | |
| 156 expect(details[2], endsWith('sample_library.dart')); // File name. | |
| 157 expect(details[3], equals('barMember1')); // Method name. | |
| 158 }); | |
| 159 | |
| 160 test('object properties', () { | |
| 161 var o = new Bar(); | |
| 162 var property; | |
| 163 var properties = _Utils.getObjectProperties(o, true, false); | |
| 164 | |
| 165 property = findProperty(properties, 'barField1'); | |
| 166 expect(property.value, equals(9)); | |
| 167 expect(property.setter, isNull); | |
| 168 expect(property.getter, isNull); | |
| 169 expect(property.hasValue, isTrue); | |
| 170 expect(property.writable, isTrue); | |
| 171 expect(property.isMethod, isFalse); | |
| 172 expect(property.isOwn, isTrue); | |
| 173 expect(property.wasThrown, isFalse); | |
| 174 | |
| 175 property = findProperty(properties, 'barFinalField'); | |
| 176 expect(property.value, equals(12)); | |
| 177 expect(property.setter, isNull); | |
| 178 expect(property.getter, isNull); | |
| 179 expect(property.hasValue, isTrue); | |
| 180 expect(property.writable, isFalse); | |
| 181 expect(property.isMethod, isFalse); | |
| 182 expect(property.isOwn, isTrue); | |
| 183 expect(property.wasThrown, isFalse); | |
| 184 | |
| 185 // verify that properties don't show up. | |
| 186 expect(findProperty(properties, "barProp1"), isNull); | |
| 187 | |
| 188 properties = _Utils.getObjectClassProperties(o, true, true); | |
| 189 property = findProperty(properties, 'barProp1'); | |
| 190 expect(property.value, isNull); | |
| 191 expect(property.setter, isNull); | |
| 192 expect(property.getter(), equals('bar1')); | |
| 193 expect(property.hasValue, isFalse); | |
| 194 expect(property.writable, isFalse); | |
| 195 expect(property.isMethod, isFalse); | |
| 196 expect(property.isOwn, isTrue); | |
| 197 expect(property.wasThrown, isFalse); | |
| 198 | |
| 199 property = findProperty(properties, 'barProp2'); | |
| 200 expect(property.value, isNull); | |
| 201 expect(property.writable, isTrue); | |
| 202 expect(property.setter is Function, isTrue); | |
| 203 expect(property.getter(), equals(11)); | |
| 204 property.setter(55); | |
| 205 expect(property.getter(), equals(55)); | |
| 206 expect(property.hasValue, isFalse); | |
| 207 expect(property.isMethod, isFalse); | |
| 208 expect(property.isOwn, isTrue); | |
| 209 expect(property.wasThrown, isFalse); | |
| 210 }); | |
| 211 | |
| 212 test('class properties', () { | |
| 213 var properties = _Utils.getClassProperties(Bar, true, false); | |
| 214 var property = findProperty(properties, 'barStaticMethod'); | |
| 215 expect(property.value(10), equals(30)); | |
| 216 expect(property.setter, isNull); | |
| 217 expect(property.getter, isNull); | |
| 218 expect(property.hasValue, isTrue); | |
| 219 expect(property.writable, isFalse); | |
| 220 expect(property.isMethod, isTrue); | |
| 221 expect(property.isOwn, isTrue); | |
| 222 expect(property.wasThrown, isFalse); | |
| 223 | |
| 224 property = findProperty(properties, 'barConst1'); | |
| 225 expect(property.value, equals(9)); | |
| 226 expect(property.setter, isNull); | |
| 227 expect(property.getter, isNull); | |
| 228 expect(property.hasValue, isTrue); | |
| 229 expect(property.writable, isFalse); | |
| 230 expect(property.isMethod, isFalse); | |
| 231 expect(property.isOwn, isTrue); | |
| 232 expect(property.wasThrown, isFalse); | |
| 233 | |
| 234 properties = _Utils.getClassProperties(Bar, true, true); | |
| 235 expect(findProperty(properties, 'barStaticMethod'), isNull); | |
| 236 | |
| 237 property = findProperty(properties, 'barStaticProp'); | |
| 238 expect(property.value, isNull); | |
| 239 expect(property.setter, isNull); | |
| 240 expect(property.getter(), equals('staticProp')); | |
| 241 expect(property.hasValue, isFalse); | |
| 242 expect(property.writable, isFalse); | |
| 243 expect(property.isMethod, isFalse); | |
| 244 expect(property.isOwn, isTrue); | |
| 245 expect(property.wasThrown, isFalse); | |
| 246 }); | |
| 247 | |
| 248 test('safe get property', () { | |
| 249 expect(_Utils.getObjectPropertySafe(new Bar(), "_barProp2"), equals(11)); | |
| 250 | |
| 251 // Warning: this assumes that the VM continues to have a field named | |
| 252 // _cid on Object. This case is good to test as it verifies we walk | |
| 253 // up the class hierarchy attempting to guess the library of the | |
| 254 // private field. | |
|
vsm
2014/04/25 00:01:14
It might be better to just import a second sample
| |
| 255 expect(_Utils.getObjectPropertySafe(new Bar(), "_cid") is int, isTrue); | |
| 256 }); | |
| 257 | |
| 258 test('library properties', () { | |
| 259 var properties = _Utils.getLibraryProperties(sampleLibraryUrl, true, false ); | |
|
vsm
2014/04/25 00:01:14
length
| |
| 260 var property = findProperty(properties, 'exampleStaticMethod'); | |
| 261 expect(property.value(10), equals(20)); | |
| 262 expect(property.setter, isNull); | |
| 263 expect(property.getter, isNull); | |
| 264 expect(property.hasValue, isTrue); | |
| 265 expect(property.writable, isFalse); | |
| 266 expect(property.isMethod, isTrue); | |
| 267 expect(property.isOwn, isTrue); | |
| 268 expect(property.wasThrown, isFalse); | |
| 269 | |
| 270 property = findProperty(properties, 'exampleField'); | |
| 271 expect(property.value, equals(8)); | |
| 272 expect(property.setter, isNull); | |
| 273 expect(property.getter, isNull); | |
| 274 expect(property.hasValue, isTrue); | |
| 275 expect(property.writable, isTrue); | |
| 276 expect(property.isMethod, isFalse); | |
| 277 expect(property.isOwn, isTrue); | |
| 278 expect(property.wasThrown, isFalse); | |
| 279 | |
| 280 property = findProperty(properties, 'exampleFinalField'); | |
| 281 expect(property.value, equals(16)); | |
| 282 expect(property.setter, isNull); | |
| 283 expect(property.getter, isNull); | |
| 284 expect(property.hasValue, isTrue); | |
| 285 expect(property.writable, isFalse); | |
| 286 expect(property.isMethod, isFalse); | |
| 287 expect(property.isOwn, isTrue); | |
| 288 expect(property.wasThrown, isFalse); | |
| 289 | |
| 290 property = findProperty(properties, 'Foo'); | |
| 291 expect(property.value.toString(), "Foo"); | |
| 292 expect(property.value is Type, isTrue); | |
| 293 expect(property.setter, isNull); | |
| 294 expect(property.getter, isNull); | |
| 295 expect(property.hasValue, isTrue); | |
| 296 expect(property.writable, isFalse); | |
| 297 expect(property.isMethod, isFalse); | |
| 298 expect(property.isOwn, isTrue); | |
| 299 expect(property.wasThrown, isFalse); | |
| 300 | |
| 301 properties = _Utils.getLibraryProperties(sampleLibraryUrl, true, true); | |
| 302 property = findProperty(properties, 'exampleProp1'); | |
| 303 expect(property.value, isNull); | |
| 304 expect(property.setter, isNull); | |
| 305 expect(property.getter(), equals(30)); | |
| 306 expect(property.hasValue, isFalse); | |
| 307 expect(property.writable, isFalse); | |
| 308 expect(property.isMethod, isFalse); | |
| 309 expect(property.isOwn, isTrue); | |
| 310 expect(property.wasThrown, isFalse); | |
| 311 | |
| 312 property = findProperty(properties, 'exampleProp2'); | |
| 313 expect(property.value, isNull); | |
| 314 expect(property.writable, isTrue); | |
| 315 expect(property.setter is Function, isTrue); | |
| 316 expect(property.getter(), equals(10)); | |
| 317 property.setter(20); | |
| 318 expect(property.getter(), equals(20)); | |
| 319 expect(property.hasValue, isFalse); | |
| 320 expect(property.isMethod, isFalse); | |
| 321 expect(property.isOwn, isTrue); | |
| 322 expect(property.wasThrown, isFalse); | |
| 323 }); | |
| 324 }); | |
| 325 } | |
| OLD | NEW |