Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge the null safety branch into master (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakemac53 authored Jul 22, 2020
1 parent ab40a12 commit 1403538
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 36 deletions.
17 changes: 3 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
language: dart

dart:
- dev
- 2.2.0
- preview/raw/2.10.0-0.2-dev

dart_task:
- test: --platform vm,chrome

matrix:
include:
# Only validate formatting using the dev release
- dart: dev
dart_task: dartfmt
- dart: dev
dart_task:
dartanalyzer: --fatal-warnings --fatal-hints .
- dart: 2.2.0
dart_task:
dartanalyzer: --fatal-warnings .
- dartfmt
- dartanalyzer: --fatal-warnings --fatal-hints .

# Only building master means that we don't run two builds for each pull request.
branches:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.0-nullsafety

* Update to null safety.

## 1.1.0

* Exposed the `FakeTimer` class as a public class.
Expand Down
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ include: package:pedantic/analysis_options.yaml
analyzer:
strong-mode:
implicit-casts: false
enable-experiment:
- non-nullable
errors:
dead_code: error
unused_element: error
Expand Down
29 changes: 15 additions & 14 deletions lib/fake_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ typedef _Microtask = void Function();
/// [`clock.now()`]: https://www.dartdocs.org/documentation/clock/latest/clock/Clock/now.html
///
/// Returns the result of [callback].
T fakeAsync<T>(T Function(FakeAsync async) callback, {DateTime initialTime}) =>
T fakeAsync<T>(T Function(FakeAsync async) callback, {DateTime? initialTime}) =>
FakeAsync(initialTime: initialTime).run(callback);

/// A class that mocks out the passage of time within a [Zone].
Expand All @@ -50,7 +50,7 @@ T fakeAsync<T>(T Function(FakeAsync async) callback, {DateTime initialTime}) =>
/// also be simulated using [elapseBlocking].
class FakeAsync {
/// The value of [clock] within [run].
Clock _clock;
late final Clock _clock;

/// The amount of fake time that's elapsed since this [FakeAsync] was
/// created.
Expand All @@ -60,7 +60,7 @@ class FakeAsync {
/// The fake time at which the current call to [elapse] will finish running.
///
/// This is `null` if there's no current call to [elapse].
Duration _elapsingTo;
Duration? _elapsingTo;

/// Tasks that are scheduled to run when fake time progresses.
final _microtasks = Queue<_Microtask>();
Expand Down Expand Up @@ -98,9 +98,9 @@ class FakeAsync {
///
/// Note: it's usually more convenient to use [fakeAsync] rather than creating
/// a [FakeAsync] object and calling [run] manually.
FakeAsync({DateTime initialTime}) {
initialTime ??= clock.now();
_clock = Clock(() => initialTime.add(elapsed));
FakeAsync({DateTime? initialTime}) {
var nonNullInitialTime = initialTime ?? clock.now();
_clock = Clock(() => nonNullInitialTime.add(elapsed));
}

/// Returns a fake [Clock] whose time can is elapsed by calls to [elapse] and
Expand Down Expand Up @@ -134,8 +134,8 @@ class FakeAsync {
}

_elapsingTo = _elapsed + duration;
_fireTimersWhile((next) => next._nextCall <= _elapsingTo);
_elapseTo(_elapsingTo);
_fireTimersWhile((next) => next._nextCall <= _elapsingTo!);
_elapseTo(_elapsingTo!);
_elapsingTo = null;
}

Expand All @@ -152,7 +152,8 @@ class FakeAsync {
}

_elapsed += duration;
if (_elapsingTo != null && _elapsed > _elapsingTo) _elapsingTo = _elapsed;
var elapsingTo = _elapsingTo;
if (elapsingTo != null && _elapsed > elapsingTo) _elapsingTo = _elapsed;
}

/// Runs [callback] in a [Zone] where all asynchrony is controlled by `this`.
Expand Down Expand Up @@ -202,9 +203,9 @@ class FakeAsync {
/// The [timeout] controls how much fake time may elapse before a [StateError]
/// is thrown. This ensures that a periodic timer doesn't cause this method to
/// deadlock. It defaults to one hour.
void flushTimers({Duration timeout, bool flushPeriodicTimers = true}) {
timeout ??= const Duration(hours: 1);

void flushTimers(
{Duration timeout = const Duration(hours: 1),
bool flushPeriodicTimers = true}) {
var absoluteTimeout = _elapsed + timeout;
_fireTimersWhile((timer) {
if (timer._nextCall > absoluteTimeout) {
Expand Down Expand Up @@ -232,7 +233,7 @@ class FakeAsync {
for (;;) {
if (_timers.isEmpty) break;

var timer = minBy(_timers, (timer) => timer._nextCall);
var timer = minBy(_timers, (FakeTimer timer) => timer._nextCall)!;
if (!predicate(timer)) break;

_elapseTo(timer._nextCall);
Expand Down Expand Up @@ -277,7 +278,7 @@ class FakeTimer implements Timer {

/// The value of [FakeAsync._elapsed] at (or after) which this timer should be
/// fired.
Duration _nextCall;
late Duration _nextCall;

/// The current stack trace when this timer was created.
final creationStackTrace = StackTrace.current;
Expand Down
68 changes: 64 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,77 @@
name: fake_async
version: 1.1.1-dev
version: 1.1.0-nullsafety
description: >-
Fake asynchronous events such as timers and microtasks for deterministic
testing.
homepage: https://github.com/dart-lang/fake_async

environment:
sdk: '>=2.2.0 <3.0.0'
# This must remain a tight constraint until nnbd is stable
sdk: '>=2.10.0-0 <2.10.0'

dependencies:
clock: ^1.0.0
collection: ^1.8.0
clock: '>=1.1.0-nullsafety <1.1.0'
collection: '>=1.15.0-nullsafety <1.15.0'

dev_dependencies:
async: ^2.0.0
test: ^1.0.0


dependency_overrides:
async:
git: git://github.com/dart-lang/async.git
boolean_selector:
git: git://github.com/dart-lang/boolean_selector.git
charcode:
git: git://github.com/dart-lang/charcode.git
clock:
git: git://github.com/dart-lang/clock.git
collection:
git: git://github.com/dart-lang/collection.git
js:
git:
url: git://github.com/dart-lang/sdk.git
path: pkg/js
ref: 2-10-pkgs
matcher:
git: git://github.com/dart-lang/matcher.git
meta:
git:
url: git://github.com/dart-lang/sdk.git
path: pkg/meta
ref: 2-10-pkgs
path:
git: git://github.com/dart-lang/path.git
pedantic:
git: git://github.com/dart-lang/pedantic.git
pool:
git: git://github.com/dart-lang/pool.git
source_maps:
git: git://github.com/dart-lang/source_maps.git
source_map_stack_trace:
git: git://github.com/dart-lang/source_map_stack_trace.git
source_span:
git: git://github.com/dart-lang/source_span.git
stack_trace:
git: git://github.com/dart-lang/stack_trace.git
stream_channel:
git: git://github.com/dart-lang/stream_channel.git
string_scanner:
git: git://github.com/dart-lang/string_scanner.git
term_glyph:
git: git://github.com/dart-lang/term_glyph.git
test_api:
git:
url: git://github.com/dart-lang/test.git
path: pkgs/test_api
test_core:
git:
url: git://github.com/dart-lang/test.git
path: pkgs/test_core
test:
git:
url: git://github.com/dart-lang/test.git
path: pkgs/test
typed_data:
git: git://github.com/dart-lang/typed_data.git
8 changes: 4 additions & 4 deletions test/fake_async_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void main() {

test('should pass the periodic timer itself to callbacks', () {
FakeAsync().run((async) {
Timer constructed;
late Timer constructed;
constructed = Timer.periodic(elapseBy, expectAsync1((passed) {
expect(passed, same(constructed));
}));
Expand Down Expand Up @@ -448,9 +448,9 @@ void main() {
test('should report the number of pending microtasks', () {
FakeAsync().run((async) {
expect(async.microtaskCount, 0);
scheduleMicrotask(() => null);
scheduleMicrotask(() {});
expect(async.microtaskCount, 1);
scheduleMicrotask(() => null);
scheduleMicrotask(() {});
expect(async.microtaskCount, 2);
async.flushMicrotasks();
expect(async.microtaskCount, 0);
Expand Down Expand Up @@ -516,7 +516,7 @@ void main() {
group('timers', () {
test("should become inactive as soon as they're invoked", () {
return FakeAsync().run((async) {
Timer timer;
late Timer timer;
timer = Timer(elapseBy, expectAsync0(() {
expect(timer.isActive, isFalse);
}));
Expand Down

0 comments on commit 1403538

Please sign in to comment.