diff --git a/.travis.yml b/.travis.yml index 581bd4b..187244c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/CHANGELOG.md b/CHANGELOG.md index 54b4366..93b7a4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.1.0-nullsafety + +* Update to null safety. + ## 1.1.0 * Exposed the `FakeTimer` class as a public class. diff --git a/analysis_options.yaml b/analysis_options.yaml index 2bf511a..55139b7 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -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 diff --git a/lib/fake_async.dart b/lib/fake_async.dart index 5a79ffb..52dd3ba 100644 --- a/lib/fake_async.dart +++ b/lib/fake_async.dart @@ -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 Function(FakeAsync async) callback, {DateTime initialTime}) => +T fakeAsync(T Function(FakeAsync async) callback, {DateTime? initialTime}) => FakeAsync(initialTime: initialTime).run(callback); /// A class that mocks out the passage of time within a [Zone]. @@ -50,7 +50,7 @@ T fakeAsync(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. @@ -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>(); @@ -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 @@ -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; } @@ -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`. @@ -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) { @@ -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); @@ -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; diff --git a/pubspec.yaml b/pubspec.yaml index ef71b26..7e05dfd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/fake_async_test.dart b/test/fake_async_test.dart index d609533..a32c35c 100644 --- a/test/fake_async_test.dart +++ b/test/fake_async_test.dart @@ -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)); })); @@ -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); @@ -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); }));