OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 import 'dart:async'; |
| 6 import 'package:barback/barback.dart'; |
| 7 import 'package:observe/transform.dart'; |
| 8 import 'package:unittest/compact_vm_config.dart'; |
| 9 import 'package:unittest/unittest.dart'; |
| 10 |
| 11 main() { |
| 12 useCompactVMConfiguration(); |
| 13 |
| 14 group('replaces Observable for ChangeNotifier', () { |
| 15 _testClause('extends ObservableBase', 'extends ChangeNotifierBase'); |
| 16 _testClause('extends Base with ObservableMixin', |
| 17 'extends Base with ChangeNotifierMixin'); |
| 18 _testClause('extends Base<T> with ObservableMixin', |
| 19 'extends Base<T> with ChangeNotifierMixin'); |
| 20 _testClause('extends Base with Mixin, ObservableMixin', |
| 21 'extends Base with Mixin, ChangeNotifierMixin'); |
| 22 _testClause('extends Base with ObservableMixin, Mixin', |
| 23 'extends Base with ChangeNotifierMixin, Mixin'); |
| 24 _testClause('extends Base with Mixin<T>, ObservableMixin', |
| 25 'extends Base with Mixin<T>, ChangeNotifierMixin'); |
| 26 _testClause('extends Base with Mixin, ObservableMixin, Mixin2', |
| 27 'extends Base with Mixin, ChangeNotifierMixin, Mixin2'); |
| 28 _testClause('extends ObservableBase implements Interface', |
| 29 'extends ChangeNotifierBase implements Interface'); |
| 30 _testClause('extends ObservableBase implements Interface<T>', |
| 31 'extends ChangeNotifierBase implements Interface<T>'); |
| 32 _testClause('extends Base with ObservableMixin implements Interface', |
| 33 'extends Base with ChangeNotifierMixin implements Interface'); |
| 34 _testClause( |
| 35 'extends Base with Mixin, ObservableMixin implements I1, I2', |
| 36 'extends Base with Mixin, ChangeNotifierMixin implements I1, I2'); |
| 37 }); |
| 38 |
| 39 group('fixes contructor calls ', () { |
| 40 _testInitializers('this.a', '(a) : __\$a = a'); |
| 41 _testInitializers('{this.a}', '({a}) : __\$a = a'); |
| 42 _testInitializers('[this.a]', '([a]) : __\$a = a'); |
| 43 _testInitializers('this.a, this.b', '(a, b) : __\$a = a, __\$b = b'); |
| 44 _testInitializers('{this.a, this.b}', '({a, b}) : __\$a = a, __\$b = b'); |
| 45 _testInitializers('[this.a, this.b]', '([a, b]) : __\$a = a, __\$b = b'); |
| 46 _testInitializers('this.a, [this.b]', '(a, [b]) : __\$a = a, __\$b = b'); |
| 47 _testInitializers('this.a, {this.b}', '(a, {b}) : __\$a = a, __\$b = b'); |
| 48 }); |
| 49 } |
| 50 |
| 51 _testClause(String clauses, String expected) { |
| 52 test(clauses, () { |
| 53 var className = 'MyClass'; |
| 54 if (clauses.contains('<T>')) className += '<T>'; |
| 55 var code = ''' |
| 56 class $className $clauses { |
| 57 @observable var field; |
| 58 }'''; |
| 59 |
| 60 return _transform(code).then((output) { |
| 61 var classPos = output.indexOf(className) + className.length; |
| 62 var actualClauses = output.substring(classPos, |
| 63 output.indexOf('{')).trim().replaceAll(' ', ' '); |
| 64 expect(actualClauses, expected); |
| 65 }); |
| 66 }); |
| 67 } |
| 68 |
| 69 _testInitializers(String args, String expected) { |
| 70 test(args, () { |
| 71 var constructor = 'MyClass('; |
| 72 var code = ''' |
| 73 class MyClass { |
| 74 @observable var a; |
| 75 @observable var b; |
| 76 MyClass($args); |
| 77 }'''; |
| 78 |
| 79 return _transform(code).then((output) { |
| 80 var begin = output.indexOf(constructor) + constructor.length - 1; |
| 81 var end = output.indexOf(';', begin); |
| 82 if (end == -1) end = output.length; |
| 83 var init = output.substring(begin, end).trim().replaceAll(' ', ' '); |
| 84 expect(init, expected); |
| 85 }); |
| 86 }); |
| 87 } |
| 88 |
| 89 /** Helper that applies the transform by creating mock assets. */ |
| 90 Future<String> _transform(String code) { |
| 91 var id = new AssetId('foo', 'a/b/c.dart'); |
| 92 var asset = new Asset.fromString(id, code); |
| 93 var transformer = new ObservableTransformer(); |
| 94 return transformer.isPrimary(asset).then((isPrimary) { |
| 95 expect(isPrimary, isTrue); |
| 96 var transform = new _MockTransform(asset); |
| 97 return transformer.apply(transform).then((_) { |
| 98 expect(transform.outs, hasLength(1)); |
| 99 expect(transform.outs[0].id, id); |
| 100 return transform.outs.first.readAsString(); |
| 101 }); |
| 102 }); |
| 103 } |
| 104 |
| 105 class _MockTransform implements Transform { |
| 106 List<Asset> outs = []; |
| 107 Asset _asset; |
| 108 AssetId get primaryId => _asset.id; |
| 109 TransformLogger logger = new TransformLogger(false); |
| 110 Future<Asset> get primaryInput => new Future.value(_asset); |
| 111 |
| 112 _MockTransform(this._asset); |
| 113 Future<Asset> getInput(Asset id) { |
| 114 if (id == primaryId) return primaryInput; |
| 115 fail(); |
| 116 } |
| 117 |
| 118 void addOutput(Asset output) { |
| 119 outs.add(output); |
| 120 } |
| 121 } |
OLD | NEW |