caching_iterable_test.dart 3.17 KB
Newer Older
1 2 3 4 5
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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

7
import '../flutter_test_alternative.dart';
8 9 10 11 12 13 14 15 16

int yieldCount;

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

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

24
  test('The Caching Iterable: length caches', () {
25
    final Iterable<int> i = CachingIterable<int>(range(1, 5).iterator);
26 27 28 29 30 31 32 33 34 35 36 37 38 39
    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));
  });

40
  test('The Caching Iterable: laziness', () {
41
    final Iterable<int> i = CachingIterable<int>(range(1, 5).iterator);
42 43 44 45 46 47 48 49 50 51 52 53
    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));
  });

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

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

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

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

67
    expect(evens.map<int>((int i) => i + 1), equals(<int>[3, 5]));
68 69 70 71 72 73 74 75
    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));
  });
76 77

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

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

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

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

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

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

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

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

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

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