| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Native testing library. Provides support for mock @Native classes and | 5 // Native testing library. Provides support for mock @Native classes and |
| 6 // collects common imports. | 6 // collects common imports. |
| 7 | 7 |
| 8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
| 9 import 'dart:_js_helper' show Native; | 9 import 'dart:_js_helper' show Native; |
| 10 import 'dart:_foreign_helper' show JS; |
| 10 | 11 |
| 11 export "package:expect/expect.dart"; | 12 export "package:expect/expect.dart"; |
| 12 export 'dart:_js_helper' show Creates, Native, JSName, Returns; | 13 export 'dart:_js_helper' show Creates, Native, JSName, Returns; |
| 13 export 'dart:_foreign_helper' show JS; | 14 export 'dart:_foreign_helper' show JS; |
| 14 | 15 |
| 15 void _setup() native r''' | 16 void nativeTesting() { |
| 17 JS('', r''' |
| 16 ((function() { | 18 ((function() { |
| 17 var toStringResultProperty = "_toStringResult"; | 19 var toStringResultProperty = "_toStringResult"; |
| 18 var objectToStringMethod = Object.prototype.toString; | 20 var objectToStringMethod = Object.prototype.toString; |
| 19 | 21 |
| 20 Object.prototype.toString = function() { | 22 Object.prototype.toString = function() { |
| 21 if (this != null) { | 23 if (this != null) { |
| 22 var constructor = this.constructor; | 24 var constructor = this.constructor; |
| 23 if (constructor != null) { | 25 if (constructor != null) { |
| 24 var result = constructor[toStringResultProperty]; | 26 var result = constructor[toStringResultProperty]; |
| 25 if (typeof result == "string") return result; | 27 if (typeof result == "string") return result; |
| 26 } | 28 } |
| 27 } | 29 } |
| 28 return objectToStringMethod.call(this); | 30 return objectToStringMethod.call(this); |
| 29 } | 31 }; |
| 30 | 32 |
| 31 // To mock a @Native class with JavaScript constructor `Foo`, add | 33 // To mock a @Native class with JavaScript constructor `Foo`, add |
| 32 // | 34 // |
| 33 // self.nativeConstructor(Foo); | 35 // self.nativeConstructor(Foo); |
| 34 // | 36 // |
| 35 // to the JavaScript code. | 37 // to the JavaScript code. |
| 36 self.nativeConstructor = function(constructor, opt_name) { | 38 self.nativeConstructor = function(constructor, opt_name) { |
| 37 var toStringResult = "[object " + (opt_name || constructor.name) + "]"; | 39 var toStringResult = "[object " + (opt_name || constructor.name) + "]"; |
| 38 constructor[toStringResultProperty] = toStringResult; | 40 constructor[toStringResultProperty] = toStringResult; |
| 39 } | 41 }; |
| 40 })()); | 42 })()) |
| 41 '''; | 43 '''); |
| 42 | |
| 43 void nativeTesting() { | |
| 44 _setup(); | |
| 45 } | 44 } |
| 46 | 45 |
| 47 @NoInline() | 46 @NoInline() |
| 48 @AssumeDynamic() | 47 @AssumeDynamic() |
| 49 confuse(x) => x; | 48 confuse(x) => x; |
| OLD | NEW |