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.async.iteration_test; |
| 16 |
| 17 import 'dart:async'; |
| 18 |
| 19 import 'package:test/test.dart'; |
| 20 import 'package:quiver/async.dart'; |
| 21 |
| 22 main() { |
| 23 group('doWhileAsync', () { |
| 24 test('should process the entier iterable if action returns true', () { |
| 25 var items = []; |
| 26 return doWhileAsync([1, 2, 3], (e) { |
| 27 items.add(e); |
| 28 return new Future(() => true); |
| 29 }).then((r) { |
| 30 expect(items, [1, 2, 3]); |
| 31 expect(r, true); |
| 32 }); |
| 33 }); |
| 34 |
| 35 test('should process the entier iterable until action returns false', () { |
| 36 var items = []; |
| 37 return doWhileAsync([1, 2, 3], (e) { |
| 38 items.add(e); |
| 39 return new Future(() => e < 2); |
| 40 }).then((r) { |
| 41 expect(items, [1, 2]); |
| 42 expect(r, false); |
| 43 }); |
| 44 }); |
| 45 }); |
| 46 |
| 47 group('reduceAsync', () { |
| 48 test('should reduce iterable', () { |
| 49 var items = []; |
| 50 return reduceAsync([1, 2, 3], 0, (v, e) { |
| 51 items.add(e); |
| 52 return new Future(() => v + e); |
| 53 }).then((r) { |
| 54 expect(items, [1, 2, 3]); |
| 55 expect(r, 6); |
| 56 }); |
| 57 }); |
| 58 }); |
| 59 |
| 60 group('forEachAsync', () { |
| 61 test('should schedule one outstanding task by default', () { |
| 62 int pending = 0; |
| 63 var results = []; |
| 64 return forEachAsync([1, 2, 3], (i) { |
| 65 pending++; |
| 66 if (pending > 1) fail("too many pending tasks"); |
| 67 results.add(i); |
| 68 return new Future(() { |
| 69 pending--; |
| 70 }); |
| 71 }).then((_) { |
| 72 expect(results, [1, 2, 3]); |
| 73 }); |
| 74 }); |
| 75 |
| 76 test('should schedule maxTasks tasks', () { |
| 77 int pending = 0; |
| 78 int maxPending = 0; |
| 79 var results = []; |
| 80 return forEachAsync([1, 2, 3, 4], (i) { |
| 81 pending++; |
| 82 if (pending > 3) fail("too many pending tasks"); |
| 83 if (pending > maxPending) maxPending = pending; |
| 84 results.add(i); |
| 85 return new Future(() { |
| 86 pending--; |
| 87 }); |
| 88 }, maxTasks: 3).then((_) { |
| 89 expect(results, [1, 2, 3, 4]); |
| 90 expect(maxPending, 3); |
| 91 }); |
| 92 }); |
| 93 |
| 94 test('should validate maxTasks', () { |
| 95 expect(() => forEachAsync([], (i) {}, maxTasks: null), throws); |
| 96 expect(() => forEachAsync([], (i) {}, maxTasks: 0), throws); |
| 97 }); |
| 98 |
| 99 test('should validate iterable', () { |
| 100 expect(() => forEachAsync(null, (i) {}), throws); |
| 101 }); |
| 102 |
| 103 test('should complete when given no work', () { |
| 104 return forEachAsync([], (i) {}); |
| 105 }); |
| 106 }); |
| 107 } |
OLD | NEW |