| 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 null; | 
|  | 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 null; | 
|  | 51     } | 
|  | 52     return super.noSuchMethod(invocation); | 
|  | 53   } | 
|  | 54 } | 
|  | 55 | 
|  | 56 // Class that behaves like a list but does not implement List. | 
|  | 57 class MyIndexableNoSuchMethod<E> { | 
|  | 58   List<E> _list; | 
|  | 59 | 
|  | 60   MyIndexableNoSuchMethod(List<E> this._list); | 
|  | 61 | 
|  | 62   noSuchMethod(Invocation invocation) { | 
|  | 63     if (invocation.memberName == #length) { | 
|  | 64       if (invocation.isGetter) return _list.length; | 
|  | 65       if (invocation.isSetter) { | 
|  | 66         _list.length = invocation.positionalArguments.first; | 
|  | 67         return null; | 
|  | 68       } | 
|  | 69       return super.noSuchMethod(invocation); | 
|  | 70     } | 
|  | 71     if (invocation.memberName == new Symbol("prototype")) { | 
|  | 72       return 42; | 
|  | 73     } | 
|  | 74 | 
|  | 75     if (invocation.memberName == new Symbol("[]") && | 
|  | 76         invocation.positionalArguments.length == 1) { | 
|  | 77       return _list[invocation.positionalArguments.first]; | 
|  | 78     } | 
|  | 79     if (invocation.memberName == new Symbol("[]=") && | 
|  | 80         invocation.positionalArguments.length == 2) { | 
|  | 81       _list[invocation.positionalArguments.first] = | 
|  | 82           invocation.positionalArguments[1]; | 
|  | 83       return null; | 
|  | 84     } | 
|  | 85     return super.noSuchMethod(invocation); | 
|  | 86   } | 
|  | 87 } | 
|  | 88 | 
| 26 void testRetainWhere() { | 89 void testRetainWhere() { | 
| 27   List<int> list = <int>[1, 2, 3]; | 90   List<int> list = <int>[1, 2, 3]; | 
| 28   list.retainWhere((x) => x % 2 == 0); | 91   list.retainWhere((x) => x % 2 == 0); | 
| 29   Expect.equals(1, list.length); | 92   Expect.equals(1, list.length); | 
| 30   Expect.equals(2, list.first); | 93   Expect.equals(2, list.first); | 
|  | 94   Expect.equals(2, list[0]); | 
| 31 | 95 | 
| 32   list = new MyList<int>([1, 2, 3]); | 96   list = new MyList<int>([1, 2, 3]); | 
| 33   list.retainWhere((x) => x % 2 == 0); | 97   list.retainWhere((x) => x % 2 == 0); | 
| 34   Expect.equals(1, list.length); | 98   Expect.equals(1, list.length); | 
| 35   Expect.equals(2, list.first); | 99   Expect.equals(2, list.first); | 
|  | 100   Expect.equals(2, list[0]); | 
|  | 101 | 
|  | 102   list = new MyNoSuchMethodList<int>([1, 2, 3]); | 
|  | 103   list.retainWhere((x) => x % 2 == 0); | 
|  | 104   Expect.equals(1, list.length); | 
|  | 105   Expect.equals(2, list.first); | 
|  | 106   Expect.equals(2, list[0]); | 
|  | 107 | 
|  | 108   // Equivalent tests where the type of the List is known statically. | 
|  | 109   { | 
|  | 110     var l = new MyList<int>([1, 2, 3]); | 
|  | 111     l.retainWhere((x) => x % 2 == 0); | 
|  | 112     Expect.equals(1, l.length); | 
|  | 113     Expect.equals(2, l.first); | 
|  | 114     Expect.equals(2, l[0]); | 
|  | 115   } | 
|  | 116 | 
|  | 117   { | 
|  | 118     var l = new MyNoSuchMethodList<int>([1, 2, 3]); | 
|  | 119     l.retainWhere((x) => x % 2 == 0); | 
|  | 120     Expect.equals(1, l.length); | 
|  | 121     Expect.equals(2, l.first); | 
|  | 122     Expect.equals(2, l[0]); | 
|  | 123   } | 
|  | 124 | 
|  | 125   // Equivalent tests where the type of the List is not known. | 
|  | 126   { | 
|  | 127     dynamic l = new MyList<int>([1, 2, 3]); | 
|  | 128     l.retainWhere((x) => x % 2 == 0); | 
|  | 129     Expect.equals(1, l.length); | 
|  | 130     Expect.equals(2, l.first); | 
|  | 131     Expect.equals(2, l[0]); | 
|  | 132   } | 
|  | 133 | 
|  | 134   { | 
|  | 135     dynamic l = new MyNoSuchMethodList<int>([1, 2, 3]); | 
|  | 136     l.retainWhere((x) => x % 2 == 0); | 
|  | 137     Expect.equals(1, l.length); | 
|  | 138     Expect.equals(2, l.first); | 
|  | 139     Expect.equals(2, l[0]); | 
|  | 140   } | 
|  | 141 | 
|  | 142   { | 
|  | 143     dynamic indexable = new MyIndexableNoSuchMethod<int>([1,2,3]); | 
|  | 144     Expect.equals(3, indexable.length); | 
|  | 145     Expect.equals(1, indexable[0]); | 
|  | 146     Expect.equals(3, indexable[2]); | 
|  | 147     indexable.length = 2; | 
|  | 148     Expect.equals(2, indexable.length); | 
|  | 149     Expect.equals(42, indexable.prototype); | 
|  | 150   } | 
| 36 } | 151 } | 
| 37 | 152 | 
| 38 void main() { | 153 void main() { | 
| 39   testRetainWhere(); | 154   testRetainWhere(); | 
| 40 } | 155 } | 
| OLD | NEW | 
|---|