Skip to content

Commit

Permalink
null-safety
Browse files Browse the repository at this point in the history
  • Loading branch information
rspilker committed Jan 5, 2022
1 parent 1ee798c commit dfcaee2
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 60 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0
* Null-safety
* require dart 2.14+

## 1.2.0
* require dart 2.7+
* support (ignore) debug events
Expand Down
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include: package:pedantic/analysis_options.yaml
include: package:lints/recommended.yaml

analyzer:
strong-mode:
Expand Down
20 changes: 10 additions & 10 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import 'dart:convert';
import 'package:testreport/testreport.dart';

void main(List<String> 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<Report> createReport(DateTime when, Stream<String> lines) async {
Expand Down
25 changes: 11 additions & 14 deletions lib/src/api/model.dart
Original file line number Diff line number Diff line change
@@ -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<Suite> 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<Suite> suites, {this.timestamp})
Expand All @@ -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<Test> 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<Test> allTests)
: allTests = List.unmodifiable(allTests);

Expand Down Expand Up @@ -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<Problem> problems;
Expand Down
5 changes: 2 additions & 3 deletions lib/src/api/processor.dart
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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).
///
Expand Down
40 changes: 20 additions & 20 deletions lib/src/impl/processor1.dart
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -9,51 +9,51 @@ import 'package:testreport/src/api/processor.dart';
class Processor1 implements Processor {
static const resultCodes = ['success', 'failure', 'error'];

Map<int, _Suite> suites = SplayTreeMap();
Map<int, _Test> tests = <int, _Test>{};
final DateTime timestamp;
final Map<int, _Suite> suites = SplayTreeMap();
final Map<int, _Test> tests = <int, _Test>{};
final DateTime? timestamp;

Processor1(this.timestamp);

@override
void process(Map<String, dynamic> event) {
var type = event['type'] as String;
final type = event['type'] as String?;
switch (type) {
case 'testStart':
var test = event['test'] as Map<String, dynamic>;
var testCase = _Test()
final test = event['test'] as Map<String, dynamic>;
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':
if (!resultCodes.contains(event['result'])) {
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<String, dynamic>;
final suite = event['suite'] as Map<String, dynamic>;
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':
Expand All @@ -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<Problem> problems = <Problem>[];
List<String> prints = <String>[];
bool hidden;
bool hidden = false;

Test toTestCase() => Test(
name,
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions lib/src/impl/startprocessor.dart
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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<String, dynamic> 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) {
Expand All @@ -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) {
Expand Down
9 changes: 3 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
sdk: '>=2.14.0 <3.0.0'

0 comments on commit dfcaee2

Please sign in to comment.