From dfcaee21c2422e6adebabd1dc733d67c2b8f549c Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Wed, 5 Jan 2022 02:47:29 +0100 Subject: [PATCH] null-safety --- CHANGELOG.md | 4 ++++ analysis_options.yaml | 2 +- example/example.dart | 20 ++++++++-------- lib/src/api/model.dart | 25 +++++++++----------- lib/src/api/processor.dart | 5 ++-- lib/src/impl/processor1.dart | 40 ++++++++++++++++---------------- lib/src/impl/startprocessor.dart | 12 +++++----- pubspec.yaml | 9 +++---- 8 files changed, 57 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 288f3d8..37dc3e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.0.0 +* Null-safety +* require dart 2.14+ + ## 1.2.0 * require dart 2.7+ * support (ignore) debug events diff --git a/analysis_options.yaml b/analysis_options.yaml index ef3ba33..53d95cb 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,4 +1,4 @@ -include: package:pedantic/analysis_options.yaml +include: package:lints/recommended.yaml analyzer: strong-mode: diff --git a/example/example.dart b/example/example.dart index b16b045..7c17451 100644 --- a/example/example.dart +++ b/example/example.dart @@ -4,17 +4,17 @@ import 'dart:convert'; import 'package:testreport/testreport.dart'; void main(List args) async { - var file = File(args[0]); - var lines = LineSplitter().bind(utf8.decoder.bind(file.openRead())); - var report = await createReport(file.lastModifiedSync(), lines); + final file = File(args[0]); + final lines = LineSplitter().bind(utf8.decoder.bind(file.openRead())); + final report = await createReport(file.lastModifiedSync(), lines); - report.suites.forEach((s) { - s.problems.forEach((t) { - t.problems.forEach((p) { - print('${s.path} - ${t.name}: ${p.message}'); - }); - }); - }); + for (final suite in report.suites) { + for (final test in suite.problems) { + for (final problem in test.problems) { + print('${suite.path} - ${test.name}: ${problem.message}'); + } + } + } } Future createReport(DateTime when, Stream lines) async { diff --git a/lib/src/api/model.dart b/lib/src/api/model.dart index 9494a4c..136c7f4 100644 --- a/lib/src/api/model.dart +++ b/lib/src/api/model.dart @@ -1,17 +1,17 @@ -// Copyright (c) 2016-2020, TOPdesk. Please see the AUTHORS file for details. +// Copyright (c) 2016-2021, TOPdesk. Please see the AUTHORS file for details. // All rights reserved. Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. /// Indicates a test that has not yet been been finished. -const int unfinished = -1; +const unfinished = -1; /// Describes the results of a test run. class Report { /// The [Suite]s in the report. final Iterable suites; - /// The timestamp of the test. Might be `null`. - final DateTime timestamp; + /// The timestamp of the test. + final DateTime? timestamp; /// Create a report with the given [suites] and [timestamp]. Report(Iterable suites, {this.timestamp}) @@ -22,24 +22,21 @@ class Report { /// /// Based on [Suite](https://github.com/dart-lang/test/blob/master/pkgs/test/doc/json_reporter.md#suite). class Suite { - static final bool Function(Test t) _skips = (t) => !t.isHidden && t.isSkipped; - static final bool Function(Test t) _problems = - (t) => !t.isHidden && t.problems.isNotEmpty; - static final bool Function(Test t) _tests = (t) => !t.isHidden; - static final bool Function(Test t) _hidden = (t) => t.isHidden; + bool _skips(Test t) => !t.isHidden && t.isSkipped; + bool _problems(Test t) => !t.isHidden && t.problems.isNotEmpty; + bool _tests(Test t) => !t.isHidden; + bool _hidden(Test t) => t.isHidden; /// The path to the suite's file. final String path; /// The platform on which the suite is running. - /// - /// Might be `null`. - final String platform; + final String? platform; /// All [Test]s contained by this Suite, including the hidden tests. final Iterable allTests; - /// Create a Suite wit hthe given [path], [platform] and [allTests]. + /// Create a Suite with the given [path], [platform] and [allTests]. Suite(this.path, this.platform, Iterable allTests) : allTests = List.unmodifiable(allTests); @@ -73,7 +70,7 @@ class Test { final int duration; /// Indicates why was the test skipped. - final String skipReason; + final String? skipReason; /// [Problem]s occurred during the test. final Iterable problems; diff --git a/lib/src/api/processor.dart b/lib/src/api/processor.dart index 20ea507..e5e2464 100644 --- a/lib/src/api/processor.dart +++ b/lib/src/api/processor.dart @@ -1,4 +1,4 @@ -// Copyright (c) 2016-2020, TOPdesk. Please see the AUTHORS file for details. +// Copyright (c) 2016-2021, TOPdesk. Please see the AUTHORS file for details. // All rights reserved. Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -7,10 +7,9 @@ import 'package:testreport/src/impl/startprocessor.dart'; /// The Processor consumes events emitted by the [json reporter](https://github.com/dart-lang/test/blob/master/pkgs/test/doc/json_reporter.md). abstract class Processor { - Processor._(); /// Creates a Processor for the given [timestamp]. - factory Processor({DateTime timestamp}) => StartProcessor(timestamp); + factory Processor({DateTime? timestamp}) => StartProcessor(timestamp); /// Processes a single [event](https://github.com/dart-lang/test/blob/master/pkgs/test/doc/json_reporter.md#events). /// diff --git a/lib/src/impl/processor1.dart b/lib/src/impl/processor1.dart index cef128a..a7eb6c6 100644 --- a/lib/src/impl/processor1.dart +++ b/lib/src/impl/processor1.dart @@ -1,4 +1,4 @@ -// Copyright (c) 2016-2019, TOPdesk. Please see the AUTHORS file for details. +// Copyright (c) 2016-2021, TOPdesk. Please see the AUTHORS file for details. // All rights reserved. Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -9,25 +9,25 @@ import 'package:testreport/src/api/processor.dart'; class Processor1 implements Processor { static const resultCodes = ['success', 'failure', 'error']; - Map suites = SplayTreeMap(); - Map tests = {}; - final DateTime timestamp; + final Map suites = SplayTreeMap(); + final Map tests = {}; + final DateTime? timestamp; Processor1(this.timestamp); @override void process(Map event) { - var type = event['type'] as String; + final type = event['type'] as String?; switch (type) { case 'testStart': - var test = event['test'] as Map; - var testCase = _Test() + final test = event['test'] as Map; + final testCase = _Test() ..startTime = event['time'] as int ..name = test['name'] as String - ..skipReason = test['metadata']['skipReason'] as String; + ..skipReason = test['metadata']['skipReason'] as String?; tests[test['id'] as int] = testCase; - suites[test['suiteID']].tests.add(testCase); + suites[test['suiteID']]!.tests.add(testCase); break; case 'testDone': @@ -35,25 +35,25 @@ class Processor1 implements Processor { throw ArgumentError("Unknown result in '$event'"); } - tests[event['testID'] as int] + tests[event['testID'] as int]! ..endTime = event['time'] as int ..hidden = event['hidden'] as bool; break; case 'suite': - var suite = event['suite'] as Map; + final suite = event['suite'] as Map; suites[suite['id'] as int] = _Suite() ..path = suite['path'] as String ..platform = suite['platform'] as String; break; case 'error': - tests[event['testID']].problems.add(Problem(event['error'] as String, + tests[event['testID']]!.problems.add(Problem(event['error'] as String, event['stackTrace'] as String, event['isFailure'] as bool)); break; case 'print': - tests[event['testID'] as int].prints.add(event['message'] as String); + tests[event['testID'] as int]!.prints.add(event['message'] as String); break; case 'done': @@ -75,13 +75,13 @@ class Processor1 implements Processor { } class _Test { - String name; - int startTime; + String name = ''; + int startTime = unfinished; int endTime = unfinished; - String skipReason; + String? skipReason; List problems = []; List prints = []; - bool hidden; + bool hidden = false; Test toTestCase() => Test( name, @@ -94,9 +94,9 @@ class _Test { } class _Suite { - String path; - String platform; - List<_Test> tests = <_Test>[]; + String path = ''; + String platform = ''; + final List<_Test> tests = <_Test>[]; Suite toTestSuite() => Suite( path, diff --git a/lib/src/impl/startprocessor.dart b/lib/src/impl/startprocessor.dart index 81f37f7..03ee3c6 100644 --- a/lib/src/impl/startprocessor.dart +++ b/lib/src/impl/startprocessor.dart @@ -1,4 +1,4 @@ -// Copyright (c) 2016-2018, TOPdesk. Please see the AUTHORS file for details. +// Copyright (c) 2016-2021, TOPdesk. Please see the AUTHORS file for details. // All rights reserved. Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -7,14 +7,14 @@ import 'package:testreport/src/api/processor.dart'; import 'package:testreport/src/impl/processor1.dart'; class StartProcessor implements Processor { - Processor _delegate; - final DateTime timestamp; + Processor? _delegate; + final DateTime? timestamp; StartProcessor(this.timestamp); @override void process(Map event) { - var type = event['type'] as String; + final type = event['type'] as String?; if (type == null) throw ArgumentError("No type in '$event'"); if (type == 'start') { if (_delegate == null) { @@ -26,13 +26,13 @@ class StartProcessor implements Processor { if (_delegate == null) { throw StateError('not started'); } - _delegate.process(event); + _delegate!.process(event); } @override Report get report { if (_delegate == null) throw StateError('not started'); - return _delegate.report; + return _delegate!.report; } Processor _createDelegate(String version) { diff --git a/pubspec.yaml b/pubspec.yaml index f7f2e36..8fc53e4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -6,13 +6,10 @@ description: > and provide an API to the test results. homepage: https://github.com/TOPdesk/dart-testreport -documentation: - -dependencies: dev_dependencies: - test: any - pedantic: ^1.9.0 + test: ^1.20.1 + lints: ^1.0.1 environment: - sdk: '>=2.7.0 <3.0.0' \ No newline at end of file + sdk: '>=2.14.0 <3.0.0'