OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 library quiver.collection.delegates.iterable_test; |
| 16 |
| 17 import 'package:quiver/collection.dart'; |
| 18 import 'package:test/test.dart'; |
| 19 |
| 20 class MyIterable extends DelegatingIterable<String> { |
| 21 final Iterable<String> _delegate; |
| 22 |
| 23 MyIterable(this._delegate); |
| 24 |
| 25 Iterable<String> get delegate => _delegate; |
| 26 } |
| 27 |
| 28 void main() { |
| 29 group('DelegatingIterable', () { |
| 30 DelegatingIterable<String> delegatingIterable; |
| 31 |
| 32 setUp(() { |
| 33 delegatingIterable = new MyIterable(['a', 'b', 'cc']); |
| 34 }); |
| 35 |
| 36 test('any', () { |
| 37 expect(delegatingIterable.any((e) => e == 'b'), isTrue); |
| 38 expect(delegatingIterable.any((e) => e == 'd'), isFalse); |
| 39 }); |
| 40 |
| 41 test('contains', () { |
| 42 expect(delegatingIterable.contains('b'), isTrue); |
| 43 expect(delegatingIterable.contains('d'), isFalse); |
| 44 }); |
| 45 |
| 46 test('elementAt', () { |
| 47 expect(delegatingIterable.elementAt(1), equals('b')); |
| 48 }); |
| 49 |
| 50 test('every', () { |
| 51 expect(delegatingIterable.every((e) => e is String), isTrue); |
| 52 expect(delegatingIterable.every((e) => e == 'b'), isFalse); |
| 53 }); |
| 54 |
| 55 test('expand', () { |
| 56 expect(delegatingIterable.expand((e) => e.codeUnits), |
| 57 equals([97, 98, 99, 99])); |
| 58 }); |
| 59 |
| 60 test('first', () { |
| 61 expect(delegatingIterable.first, equals('a')); |
| 62 }); |
| 63 |
| 64 test('firstWhere', () { |
| 65 expect(delegatingIterable.firstWhere((e) => e == 'b'), equals('b')); |
| 66 expect(delegatingIterable.firstWhere((e) => e == 'd', orElse: () => 'e'), |
| 67 equals('e')); |
| 68 }); |
| 69 |
| 70 test('fold', () { |
| 71 expect(delegatingIterable.fold('z', (p, e) => p + e), equals('zabcc')); |
| 72 }); |
| 73 |
| 74 test('forEach', () { |
| 75 final s = new StringBuffer(); |
| 76 delegatingIterable.forEach((e) => s.write(e)); |
| 77 expect(s.toString(), equals('abcc')); |
| 78 }); |
| 79 |
| 80 test('isEmpty', () { |
| 81 expect(delegatingIterable.isEmpty, isFalse); |
| 82 expect(new MyIterable([]).isEmpty, isTrue); |
| 83 }); |
| 84 |
| 85 test('isNotEmpty', () { |
| 86 expect(delegatingIterable.isNotEmpty, isTrue); |
| 87 expect(new MyIterable([]).isNotEmpty, isFalse); |
| 88 }); |
| 89 |
| 90 test('forEach', () { |
| 91 final it = delegatingIterable.iterator; |
| 92 expect(it.current, isNull); |
| 93 expect(it.moveNext(), isTrue); |
| 94 expect(it.current, equals('a')); |
| 95 expect(it.moveNext(), isTrue); |
| 96 expect(it.current, equals('b')); |
| 97 expect(it.moveNext(), isTrue); |
| 98 expect(it.current, equals('cc')); |
| 99 expect(it.moveNext(), isFalse); |
| 100 expect(it.current, isNull); |
| 101 }); |
| 102 |
| 103 test('join', () { |
| 104 expect(delegatingIterable.join(), equals('abcc')); |
| 105 expect(delegatingIterable.join(','), equals('a,b,cc')); |
| 106 }); |
| 107 |
| 108 test('last', () { |
| 109 expect(delegatingIterable.last, equals('cc')); |
| 110 }); |
| 111 |
| 112 test('lastWhere', () { |
| 113 expect(delegatingIterable.lastWhere((e) => e == 'b'), equals('b')); |
| 114 expect(delegatingIterable.lastWhere((e) => e == 'd', orElse: () => 'e'), |
| 115 equals('e')); |
| 116 }); |
| 117 |
| 118 test('length', () { |
| 119 expect(delegatingIterable.length, equals(3)); |
| 120 }); |
| 121 |
| 122 test('map', () { |
| 123 expect(delegatingIterable.map((e) => e.toUpperCase()), |
| 124 equals(['A', 'B', 'CC'])); |
| 125 }); |
| 126 |
| 127 test('reduce', () { |
| 128 expect(delegatingIterable.reduce((value, element) => value + element), |
| 129 equals('abcc')); |
| 130 }); |
| 131 |
| 132 test('single', () { |
| 133 expect(() => delegatingIterable.single, throws); |
| 134 expect(new MyIterable(['a']).single, equals('a')); |
| 135 }); |
| 136 |
| 137 test('singleWhere', () { |
| 138 expect(delegatingIterable.singleWhere((e) => e == 'b'), equals('b')); |
| 139 expect(() => delegatingIterable.singleWhere((e) => e == 'd'), throws); |
| 140 }); |
| 141 |
| 142 test('skip', () { |
| 143 expect(delegatingIterable.skip(1), equals(['b', 'cc'])); |
| 144 }); |
| 145 |
| 146 test('skipWhile', () { |
| 147 expect( |
| 148 delegatingIterable.skipWhile((e) => e == 'a'), equals(['b', 'cc'])); |
| 149 }); |
| 150 |
| 151 test('take', () { |
| 152 expect(delegatingIterable.take(1), equals(['a'])); |
| 153 }); |
| 154 |
| 155 test('skipWhile', () { |
| 156 expect(delegatingIterable.takeWhile((e) => e == 'a'), equals(['a'])); |
| 157 }); |
| 158 |
| 159 test('toList', () { |
| 160 expect(delegatingIterable.toList(), equals(['a', 'b', 'cc'])); |
| 161 }); |
| 162 |
| 163 test('toSet', () { |
| 164 expect(delegatingIterable.toSet(), |
| 165 equals(new Set<String>.from(['a', 'b', 'cc']))); |
| 166 }); |
| 167 |
| 168 test('where', () { |
| 169 expect( |
| 170 delegatingIterable.where((e) => e.length == 1), equals(['a', 'b'])); |
| 171 }); |
| 172 }); |
| 173 } |
OLD | NEW |