| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2017, 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 @JS() |
| 6 library js_extend_class_test; |
| 7 |
| 8 import 'dart:html'; |
| 9 |
| 10 import 'package:js/js.dart'; |
| 11 import 'package:js/js_util.dart' as js_util; |
| 12 import 'package:expect/minitest.dart'; |
| 13 |
| 14 @JS('Date') |
| 15 class JSDate { |
| 16 external get jsField; |
| 17 external get jsMethod; |
| 18 } |
| 19 |
| 20 @JS('Date.prototype.jsField') |
| 21 external set datePrototypeJSField(v); |
| 22 |
| 23 @JS('Date.prototype.jsMethod') |
| 24 external set datePrototypeJSMethod(v); |
| 25 |
| 26 // Extending a JS class with a Dart class is only supported by DDC for now. |
| 27 // We extend the Date class instead of a user defined JS class to avoid the |
| 28 // hassle of ensuring the JS class exists before we use it. |
| 29 class DartJsDate extends JSDate { |
| 30 get dartField => 100; |
| 31 int dartMethod(x) { |
| 32 return x * 2; |
| 33 } |
| 34 } |
| 35 |
| 36 main() { |
| 37 // Monkey-patch the JS Date class. |
| 38 datePrototypeJSField = 42; |
| 39 datePrototypeJSMethod = allowInterop((x) => x * 10); |
| 40 |
| 41 group('extend js class', () { |
| 42 test('js class members', () { |
| 43 var bar = new DartJsDate(); |
| 44 expect(bar.jsField, equals(42)); |
| 45 expect(bar.jsMethod(5), equals(50)); |
| 46 |
| 47 expect(bar.dartField, equals(100)); |
| 48 expect(bar.dartMethod(4), equals(8)); |
| 49 }); |
| 50 |
| 51 test('dart subclass members', () { |
| 52 var bar = new DartJsDate(); |
| 53 expect(bar.dartField, equals(100)); |
| 54 expect(bar.dartMethod(4), equals(8)); |
| 55 }); |
| 56 }); |
| 57 } |
| OLD | NEW |