OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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_function_getter_test; |
| 7 |
| 8 import 'dart:html'; |
| 9 |
| 10 import 'package:js/js.dart'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 import 'package:unittest/html_config.dart'; |
| 13 import 'package:unittest/html_individual_config.dart'; |
| 14 |
| 15 _injectJs() { |
| 16 document.body.append(new ScriptElement() |
| 17 ..type = 'text/javascript' |
| 18 ..innerHtml = r""" |
| 19 var bar = { }; |
| 20 |
| 21 bar.instanceMember = function() { |
| 22 if (this !== bar) { |
| 23 throw 'Unexpected this!'; |
| 24 } |
| 25 return arguments.length; |
| 26 }; |
| 27 |
| 28 bar.staticMember = function() { |
| 29 return arguments.length * 2; |
| 30 }; |
| 31 |
| 32 bar.add = function(a, b) { |
| 33 return a + b; |
| 34 }; |
| 35 |
| 36 var foo = { 'bar' : bar }; |
| 37 """); |
| 38 } |
| 39 |
| 40 typedef int AddFn(int x, int y); |
| 41 |
| 42 @JS() |
| 43 abstract class Bar { |
| 44 external Function get staticMember; |
| 45 external Function get instanceMember; |
| 46 external AddFn get add; |
| 47 } |
| 48 |
| 49 @JS() |
| 50 abstract class Foo { |
| 51 external Bar get bar; |
| 52 } |
| 53 |
| 54 @JS() |
| 55 external Foo get foo; |
| 56 |
| 57 main() { |
| 58 _injectJs(); |
| 59 |
| 60 useHtmlIndividualConfiguration(); |
| 61 |
| 62 group('call getter as function', () { |
| 63 test('member function', () { |
| 64 expect(foo.bar.instanceMember(), equals(0)); |
| 65 expect(foo.bar.instanceMember(0), equals(1)); |
| 66 expect(foo.bar.instanceMember(0,0), equals(2)); |
| 67 expect(foo.bar.instanceMember(0,0,0,0,0,0), equals(6)); |
| 68 var instanceMember = foo.bar.instanceMember; |
| 69 expect(() => instanceMember(), throws); |
| 70 expect(() => instanceMember(0), throws); |
| 71 expect(() => instanceMember(0,0), throws); |
| 72 expect(() => instanceMember(0,0,0,0,0,0), throws); |
| 73 }); |
| 74 |
| 75 test('static function', () { |
| 76 expect(foo.bar.staticMember(), equals(0)); |
| 77 expect(foo.bar.staticMember(0), equals(2)); |
| 78 expect(foo.bar.staticMember(0,0), equals(4)); |
| 79 expect(foo.bar.staticMember(0,0,0,0,0,0), equals(12)); |
| 80 var staticMember = foo.bar.staticMember; |
| 81 expect(staticMember(), equals(0)); |
| 82 expect(staticMember(0), equals(2)); |
| 83 expect(staticMember(0,0), equals(4)); |
| 84 expect(staticMember(0,0,0,0,0,0), equals(12)); |
| 85 }); |
| 86 |
| 87 test('typedef function', () { |
| 88 expect(foo.bar.add(4,5), equals(9)); |
| 89 // This test would work in dart2js but wouldn't work yet in Dartium. |
| 90 // expect(() => foo.bar.add(4,5,10), throws); |
| 91 }); |
| 92 }); |
| 93 } |
OLD | NEW |