caching_iterable_test.dart 3.18 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
6
import 'package:flutter_test/flutter_test.dart';
7

8
int yieldCount = 0;
9 10 11 12 13 14 15

Iterable<int> range(int start, int end) sync* {
  assert(yieldCount == 0);
  for (int index = start; index <= end; index += 1) {
    yieldCount += 1;
    yield index;
  }
16
}
17 18 19 20 21 22

void main() {
  setUp(() {
    yieldCount = 0;
  });

23
  test('The Caching Iterable: length caches', () {
24
    final Iterable<int> i = CachingIterable<int>(range(1, 5).iterator);
25 26 27 28 29 30 31 32 33 34 35 36 37 38
    expect(yieldCount, equals(0));
    expect(i.length, equals(5));
    expect(yieldCount, equals(5));

    expect(i.length, equals(5));
    expect(yieldCount, equals(5));

    expect(i.last, equals(5));
    expect(yieldCount, equals(5));

    expect(i, equals(<int>[1, 2, 3, 4, 5]));
    expect(yieldCount, equals(5));
  });

39
  test('The Caching Iterable: laziness', () {
40
    final Iterable<int> i = CachingIterable<int>(range(1, 5).iterator);
41 42 43 44 45 46 47 48 49 50 51 52
    expect(yieldCount, equals(0));

    expect(i.first, equals(1));
    expect(yieldCount, equals(1));

    expect(i.firstWhere((int i) => i == 3), equals(3));
    expect(yieldCount, equals(3));

    expect(i.last, equals(5));
    expect(yieldCount, equals(5));
  });

53
  test('The Caching Iterable: where and map', () {
54
    final Iterable<int> integers = CachingIterable<int>(range(1, 5).iterator);
55 56
    expect(yieldCount, equals(0));

57
    final Iterable<int> evens = integers.where((int i) => i.isEven);
58 59 60 61 62 63 64 65
    expect(yieldCount, equals(0));

    expect(evens.first, equals(2));
    expect(yieldCount, equals(2));

    expect(integers.first, equals(1));
    expect(yieldCount, equals(2));

66
    expect(evens.map<int>((int i) => i + 1), equals(<int>[3, 5]));
67 68 69 70 71 72 73 74
    expect(yieldCount, equals(5));

    expect(evens, equals(<int>[2, 4]));
    expect(yieldCount, equals(5));

    expect(integers, equals(<int>[1, 2, 3, 4, 5]));
    expect(yieldCount, equals(5));
  });
75 76

  test('The Caching Iterable: take and skip', () {
77
    final Iterable<int> integers = CachingIterable<int>(range(1, 5).iterator);
78 79
    expect(yieldCount, equals(0));

80
    final Iterable<int> secondTwo = integers.skip(1).take(2);
81 82 83 84 85

    expect(yieldCount, equals(0));
    expect(secondTwo, equals(<int>[2, 3]));
    expect(yieldCount, equals(3));

86
    final Iterable<int> result = integers.takeWhile((int i) => i < 4).skipWhile((int i) => i < 3);
87 88 89 90 91 92

    expect(result, equals(<int>[3]));
    expect(yieldCount, equals(4));
    expect(integers, equals(<int>[1, 2, 3, 4, 5]));
    expect(yieldCount, equals(5));
  });
93 94

  test('The Caching Iterable: expand', () {
95
    final Iterable<int> integers = CachingIterable<int>(range(1, 5).iterator);
96 97
    expect(yieldCount, equals(0));

98
    final Iterable<int> expanded1 = integers.expand<int>((int i) => <int>[i, i]);
99 100 101 102 103

    expect(yieldCount, equals(0));
    expect(expanded1, equals(<int>[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]));
    expect(yieldCount, equals(5));

104
    final Iterable<int> expanded2 = integers.expand<int>((int i) => <int>[i, i]);
105 106 107 108 109

    expect(yieldCount, equals(5));
    expect(expanded2, equals(<int>[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]));
    expect(yieldCount, equals(5));
  });
110
}