| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 import 'dart:collection'; | 5 import 'dart:collection'; |
| 6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
| 7 | 7 |
| 8 class MyList<E> extends Object with ListMixin<E> implements List<E> { | 8 class MyList<E> extends Object with ListMixin<E> implements List<E> { |
| 9 List<E> _list; | 9 List<E> _list; |
| 10 | 10 |
| 11 MyList(List<E> this._list); | 11 MyList(List<E> this._list); |
| 12 | 12 |
| 13 int get length => _list.length; | 13 int get length => _list.length; |
| 14 | 14 |
| 15 void set length(int x) { | 15 void set length(int x) { |
| 16 _list.length = x; | 16 _list.length = x; |
| 17 } | 17 } |
| 18 | 18 |
| 19 E operator[](int idx) => _list[idx]; | 19 E operator [](int idx) => _list[idx]; |
| 20 | 20 |
| 21 void operator[]=(int idx, E value) { | 21 void operator []=(int idx, E value) { |
| 22 _list[idx] = value; | 22 _list[idx] = value; |
| 23 } | 23 } |
| 24 } | 24 } |
| 25 | 25 |
| 26 class MyNoSuchMethodList<E> extends Object |
| 27 with ListMixin<E> |
| 28 implements List<E> { |
| 29 List<E> _list; |
| 30 |
| 31 MyNoSuchMethodList(List<E> this._list); |
| 32 |
| 33 noSuchMethod(Invocation invocation) { |
| 34 if (invocation.memberName == #length) { |
| 35 if (invocation.isGetter) return _list.length; |
| 36 if (invocation.isSetter) { |
| 37 _list.length = invocation.positionalArguments.first; |
| 38 return; |
| 39 } |
| 40 return super.noSuchMethod(invocation); |
| 41 } |
| 42 if (invocation.memberName == new Symbol("[]") && |
| 43 invocation.positionalArguments.length == 1) { |
| 44 return _list[invocation.positionalArguments.first]; |
| 45 } |
| 46 if (invocation.memberName == new Symbol("[]=") && |
| 47 invocation.positionalArguments.length == 2) { |
| 48 _list[invocation.positionalArguments.first] = |
| 49 invocation.positionalArguments[1]; |
| 50 return; |
| 51 } |
| 52 return super.noSuchMethod(invocation); |
| 53 } |
| 54 } |
| 55 |
| 26 void testRetainWhere() { | 56 void testRetainWhere() { |
| 27 List<int> list = <int>[1, 2, 3]; | 57 List<int> list = <int>[1, 2, 3]; |
| 28 list.retainWhere((x) => x % 2 == 0); | 58 list.retainWhere((x) => x % 2 == 0); |
| 29 Expect.equals(1, list.length); | 59 Expect.equals(1, list.length); |
| 30 Expect.equals(2, list.first); | 60 Expect.equals(2, list.first); |
| 31 | 61 |
| 32 list = new MyList<int>([1, 2, 3]); | 62 list = new MyList<int>([1, 2, 3]); |
| 33 list.retainWhere((x) => x % 2 == 0); | 63 list.retainWhere((x) => x % 2 == 0); |
| 34 Expect.equals(1, list.length); | 64 Expect.equals(1, list.length); |
| 35 Expect.equals(2, list.first); | 65 Expect.equals(2, list.first); |
| 66 |
| 67 list = new MyNoSuchMethodList<int>([1, 2, 3]); |
| 68 list.retainWhere((x) => x % 2 == 0); |
| 69 Expect.equals(1, list.length); |
| 70 Expect.equals(2, list.first); |
| 71 |
| 72 // Equivalent tests where the type of the List is known statically. |
| 73 { |
| 74 var l = new MyList<int>([1, 2, 3]); |
| 75 l.retainWhere((x) => x % 2 == 0); |
| 76 Expect.equals(1, l.length); |
| 77 Expect.equals(2, l.first); |
| 78 } |
| 79 |
| 80 { |
| 81 var l = new MyNoSuchMethodList<int>([1, 2, 3]); |
| 82 l.retainWhere((x) => x % 2 == 0); |
| 83 Expect.equals(1, l.length); |
| 84 Expect.equals(2, l.first); |
| 85 } |
| 36 } | 86 } |
| 37 | 87 |
| 38 void main() { | 88 void main() { |
| 39 testRetainWhere(); | 89 testRetainWhere(); |
| 40 } | 90 } |
| OLD | NEW |