blob: 946346c98ee9b339e946be675cf7b274e2111878 [file] [log] [blame]
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// @assertion Future<T> firstWhere (bool test(T element), T orElse()})
///
/// Finds the first element of this stream matching test.
///
/// Returns a future that is completed with the first element of this stream for
/// which test returns true.
///
/// If no such element is found before this stream is done, and an orElse
/// function is provided, the result of calling orElse becomes the value of the
/// future. If orElse throws, the returned future is completed with that error.
///
/// If this stream emits an error before the first matching element, the
/// returned future is completed with that error, and processing stops.
///
/// Stops listening to this stream after the first matching element or error has
/// been received.
///
/// Internally the method cancels its subscription after the first element that
/// matches the predicate. This means that single-subscription (non-broadcast)
/// streams are closed and cannot be reused after a call to this method.
///
/// If an error occurs, or if this stream ends without finding a match and with
/// no orElse function provided, the returned future is completed with an error.
///
/// @description Checks that if element is found, it is passed to the resulting
/// future. [orElse] is provided.
/// @author kaigorodov
library firstWhere_A01_t02;
import "dart:async";
import "../../../Utils/expect.dart";
void check(Stream<int?> s, bool test(int? element), Object? expected) {
asyncStart();
bool orElseUsed = false;
Future f = s.firstWhere(test, orElse: () {
orElseUsed = true;
return 42;
});
f.then((var actual) {
Expect.equals(expected, actual);
Expect.isFalse(orElseUsed);
asyncEnd();
});
}
void test(CreateStreamFunction create) {
check(create([1, 2, 3]), (int? element) => true, 1);
check(create([1, 2, 3]), (int? element) => element != null, 1);
check(create([1, 2, 3, null]), (int? element) => element == null, null);
check(create([1, 2, 3]), (int? element) => element! > 2, 3);
check(create(new Iterable.generate(10, (int? index) => index! * 5)),
(int? element) => element != 30, 0);
check(create(new Iterable.generate(10, (int index) => index * 5)),
(int? element) => element == 30, 30);
}