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_typed_interop_lazy_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('someProperty') |
| 15 external get foo; |
| 16 |
| 17 @JS('baz.bar') |
| 18 external get bar; |
| 19 |
| 20 @JS('baz.LazyClass') |
| 21 class LazyClass { |
| 22 external factory LazyClass(a); |
| 23 external get a; |
| 24 } |
| 25 |
| 26 @anonymous |
| 27 @JS('some.bogus.ignored.js.path') |
| 28 class AnonClass { |
| 29 external factory AnonClass({a}); |
| 30 external get a; |
| 31 } |
| 32 |
| 33 @anonymous |
| 34 @JS() |
| 35 class AnonClass2 { |
| 36 external factory AnonClass2({b}); |
| 37 external get b; |
| 38 } |
| 39 |
| 40 abstract class Foo<T> { |
| 41 T get obj; |
| 42 } |
| 43 |
| 44 // This class would cause compile time issues if JS classes are not properly laz
y. |
| 45 class FooImpl extends Foo<LazyClass> { |
| 46 LazyClass get obj => new LazyClass(100); |
| 47 } |
| 48 |
| 49 main() { |
| 50 group('lazy property', () { |
| 51 test('simple', () { |
| 52 expect(foo, isNull); |
| 53 js_util.setProperty(window, 'someProperty', 42); |
| 54 expect(foo, equals(42)); |
| 55 }); |
| 56 |
| 57 test('nested', () { |
| 58 js_util.setProperty(window, 'baz', js_util.newObject()); |
| 59 expect(bar, isNull); |
| 60 js_util.setProperty(window, 'baz', js_util.jsify({'bar': 100})); |
| 61 expect(bar, equals(100)); |
| 62 }); |
| 63 }); |
| 64 |
| 65 group('lazy class', () { |
| 66 test('type literal', () { |
| 67 // Fine because we can determine the class literals are equal without |
| 68 // having to determine what (non-existant) JS type they correspond to. |
| 69 var x = LazyClass; |
| 70 var y = LazyClass; |
| 71 expect(x == y, isTrue); |
| 72 }); |
| 73 |
| 74 test('reference in type parameter', () { |
| 75 var o = new FooImpl(); |
| 76 expect(o is Foo<LazyClass>, isTrue); |
| 77 }); |
| 78 |
| 79 test('create instance', () { |
| 80 document.body.append(new ScriptElement() |
| 81 ..type = 'text/javascript' |
| 82 ..innerHtml = r""" |
| 83 window.baz = {}; |
| 84 |
| 85 baz.LazyClass = function LazyClass(a) { |
| 86 this.a = a; |
| 87 }; |
| 88 """); |
| 89 var l = new LazyClass(42); |
| 90 expect(l.a, equals(42)); |
| 91 expect(l is LazyClass, isTrue); |
| 92 expect(l is AnonClass, isTrue); |
| 93 expect((l as AnonClass) == l, isTrue); |
| 94 expect((l as AnonClass2) == l, isTrue); |
| 95 var anon = new AnonClass(a: 42); |
| 96 expect(anon is! LazyClass, isTrue); |
| 97 expect(anon is AnonClass, isTrue); |
| 98 expect(anon is AnonClass2, isTrue); |
| 99 |
| 100 // Sanity check that is and as are not broken. |
| 101 expect(new Object() is! LazyClass, isTrue); |
| 102 expect(new Object() is! AnonClass, isTrue); |
| 103 expect(new Object() is! AnonClass2, isTrue); |
| 104 |
| 105 expect(<AnonClass>[] is List<AnonClass>, isTrue); |
| 106 // TODO(jacobr): why doesn't this test pass? |
| 107 // expect(<AnonClass>[] is List<AnonClass2>, isTrue); |
| 108 expect(<int>[] is! List<AnonClass>, isTrue); |
| 109 expect(<AnonClass>[] is! List<LazyClass>, isTrue); |
| 110 expect(<int>[] is! List<LazyClass>, isTrue); |
| 111 expect(<LazyClass>[] is List<LazyClass>, isTrue); |
| 112 }); |
| 113 }); |
| 114 } |
OLD | NEW |