| 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 /// Static implementation of smoke services that uses code-generated data and | |
| 6 /// verifies that the results match what we would get with a mirror-based | |
| 7 /// implementation. | |
| 8 library smoke.static_debug; | |
| 9 | |
| 10 export 'package:smoke/static.dart' show StaticConfiguration, Getter, Setter; | |
| 11 import 'package:smoke/static.dart'; | |
| 12 import 'package:smoke/mirrors.dart'; | |
| 13 import 'package:smoke/smoke.dart'; | |
| 14 | |
| 15 import 'src/common.dart' show compareLists; | |
| 16 | |
| 17 /// Set up the smoke package to use a static implementation based on the given | |
| 18 /// [configuration]. | |
| 19 useGeneratedCode(StaticConfiguration configuration) { | |
| 20 configure(new _DebugObjectAccessorService(configuration), | |
| 21 new _DebugTypeInspectorService(configuration), | |
| 22 new _DebugSymbolConverterService(configuration)); | |
| 23 } | |
| 24 | |
| 25 /// Implements [ObjectAccessorService] using a static configuration. | |
| 26 class _DebugObjectAccessorService implements ObjectAccessorService { | |
| 27 GeneratedObjectAccessorService _static; | |
| 28 ReflectiveObjectAccessorService _mirrors; | |
| 29 | |
| 30 _DebugObjectAccessorService(StaticConfiguration configuration) | |
| 31 : _static = new GeneratedObjectAccessorService(configuration), | |
| 32 _mirrors = new ReflectiveObjectAccessorService(); | |
| 33 | |
| 34 read(Object object, Symbol name) => _check('read', [ | |
| 35 object, | |
| 36 name | |
| 37 ], _static.read(object, name), _mirrors.read(object, name)); | |
| 38 | |
| 39 // Note: we can't verify operations with side-effects like write or invoke. | |
| 40 void write(Object object, Symbol name, value) => | |
| 41 _static.write(object, name, value); | |
| 42 | |
| 43 invoke(object, Symbol name, List args, {Map namedArgs, bool adjust: false}) => | |
| 44 _static.invoke(object, name, args, namedArgs: namedArgs, adjust: adjust); | |
| 45 } | |
| 46 | |
| 47 /// Implements [TypeInspectorService] using a static configuration. | |
| 48 class _DebugTypeInspectorService implements TypeInspectorService { | |
| 49 GeneratedTypeInspectorService _static; | |
| 50 ReflectiveTypeInspectorService _mirrors; | |
| 51 | |
| 52 _DebugTypeInspectorService(StaticConfiguration configuration) | |
| 53 : _static = new GeneratedTypeInspectorService(configuration), | |
| 54 _mirrors = new ReflectiveTypeInspectorService(); | |
| 55 | |
| 56 bool isSubclassOf(Type type, Type supertype) => _check('isSubclassOf', [ | |
| 57 type, | |
| 58 supertype | |
| 59 ], _static.isSubclassOf(type, supertype), | |
| 60 _mirrors.isSubclassOf(type, supertype)); | |
| 61 | |
| 62 bool hasGetter(Type type, Symbol name) => _check('hasGetter', [ | |
| 63 type, | |
| 64 name | |
| 65 ], _static.hasGetter(type, name), _mirrors.hasGetter(type, name)); | |
| 66 | |
| 67 bool hasSetter(Type type, Symbol name) => _check('hasSetter', [ | |
| 68 type, | |
| 69 name | |
| 70 ], _static.hasSetter(type, name), _mirrors.hasSetter(type, name)); | |
| 71 | |
| 72 bool hasInstanceMethod(Type type, Symbol name) => _check('hasInstanceMethod', | |
| 73 [type, name], _static.hasInstanceMethod(type, name), | |
| 74 _mirrors.hasInstanceMethod(type, name)); | |
| 75 | |
| 76 bool hasStaticMethod(Type type, Symbol name) => _check('hasStaticMethod', [ | |
| 77 type, | |
| 78 name | |
| 79 ], _static.hasStaticMethod(type, name), _mirrors.hasStaticMethod(type, name)); | |
| 80 | |
| 81 Declaration getDeclaration(Type type, Symbol name) => _check('getDeclaration', | |
| 82 [ | |
| 83 type, | |
| 84 name | |
| 85 ], _static.getDeclaration(type, name), _mirrors.getDeclaration(type, name)); | |
| 86 | |
| 87 List<Declaration> query(Type type, QueryOptions options) => _check('query', [ | |
| 88 type, | |
| 89 options | |
| 90 ], _static.query(type, options), _mirrors.query(type, options)); | |
| 91 } | |
| 92 | |
| 93 /// Implements [SymbolConverterService] using a static configuration. | |
| 94 class _DebugSymbolConverterService implements SymbolConverterService { | |
| 95 GeneratedSymbolConverterService _static; | |
| 96 ReflectiveSymbolConverterService _mirrors; | |
| 97 | |
| 98 _DebugSymbolConverterService(StaticConfiguration configuration) | |
| 99 : _static = new GeneratedSymbolConverterService(configuration), | |
| 100 _mirrors = new ReflectiveSymbolConverterService(); | |
| 101 | |
| 102 String symbolToName(Symbol symbol) => _check('symbolToName', [symbol], | |
| 103 _static.symbolToName(symbol), _mirrors.symbolToName(symbol)); | |
| 104 | |
| 105 Symbol nameToSymbol(String name) => _check('nameToSymbol', [name], | |
| 106 _static.nameToSymbol(name), _mirrors.nameToSymbol(name)); | |
| 107 } | |
| 108 | |
| 109 _check(String operation, List arguments, staticResult, mirrorResult) { | |
| 110 if (staticResult == mirrorResult) return staticResult; | |
| 111 if (staticResult is List && | |
| 112 mirrorResult is List && | |
| 113 compareLists(staticResult, mirrorResult, unordered: true)) { | |
| 114 return staticResult; | |
| 115 } | |
| 116 print('warning: inconsistent result on $operation(${arguments.join(', ')})\n' | |
| 117 'smoke.mirrors result: $mirrorResult\n' | |
| 118 'smoke.static result: $staticResult\n'); | |
| 119 return staticResult; | |
| 120 } | |
| OLD | NEW |