Skip to content

Commit

Permalink
Compact sample building for platform conflict description. (dart-lang…
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Jun 15, 2018
1 parent df28881 commit 9e23891
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
11 changes: 11 additions & 0 deletions lib/src/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ String runningDartanalyzerFailed(bool usesFlutter, errorMsg) {
return 'Running `${dict.dartanalyzerShortCmd}` failed with the following output:\n\n'
'```\n$errorMsg\n```\n';
}

/// Build a list like "A, B and 3 more".
String buildSample(Iterable<String> items) {
final total = items.length;
final fullCount = total <= 3 ? total : 2;
var sample = items.take(fullCount).join(', ');
if (total > fullCount) {
sample = '$sample and ${total - fullCount} more.';
}
return sample;
}
23 changes: 8 additions & 15 deletions lib/src/platform.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library pana.platform;

import 'messages.dart' show buildSample;
import 'model.dart'
show ComponentNames, DartPlatform, PlatformNames, PlatformUse;
import 'pubspec.dart';
Expand Down Expand Up @@ -201,17 +202,16 @@ DartPlatform classifyPkgPlatform(
transitiveLibs.keys ?? <String>[],
value: (key) => classifyLibPlatform(transitiveLibs[key]));

String formatConflictSample(String s) {
final components = libraries[s].components.map((s) => '`$s`').join(', ');
return '`$s` (components: $components)';
}

final conflicts =
libraries.keys.where((key) => libraries[key].hasConflict).toList();
if (conflicts.isNotEmpty) {
conflicts.sort();
var sample = conflicts.take(3).map((s) {
final components = libraries[s].components.map((s) => '`$s`').join(', ');
return '`$s` (components: $components)';
}).join(', ');
if (conflicts.length > 3) {
sample = '$sample and ${conflicts.length - 3} more.';
}
final sample = buildSample(conflicts.map(formatConflictSample));
return new DartPlatform.conflict('Conflicting libraries: $sample.');
}

Expand All @@ -235,14 +235,7 @@ DartPlatform classifyPkgPlatform(
);
} else {
flutterConflicts.sort();
var sample = flutterConflicts.take(3).map((s) {
final components =
libraries[s].components.map((s) => '`$s`').join(', ');
return '`$s` (components: $components)';
}).join(', ');
if (flutterConflicts.length > 3) {
sample = '$sample and ${flutterConflicts.length - 3} more.';
}
final sample = buildSample(flutterConflicts.map(formatConflictSample));
return new DartPlatform.conflict(
'References Flutter, but has conflicting libraries: $sample.');
}
Expand Down
39 changes: 39 additions & 0 deletions test/messages_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2018, 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.

import 'package:test/test.dart';

import 'package:pana/src/messages.dart';

void main() {
group('buildSample', () {
String format(int value) => '${value}x$value';

test('no items', () {
expect(buildSample(<int>[].map(format)), '');
});

test('1 item', () {
expect(buildSample(<int>[1].map(format)), '1x1');
});

test('2 items', () {
expect(buildSample(<int>[1, 2].map(format)), '1x1, 2x2');
});

test('3 items', () {
expect(buildSample(<int>[1, 2, 3].map(format)), '1x1, 2x2, 3x3');
});

test('4 items', () {
expect(
buildSample(<int>[1, 2, 3, 4].map(format)), '1x1, 2x2 and 2 more.');
});

test('10 items', () {
expect(buildSample(new List.generate(10, (i) => i + 1).map(format)),
'1x1, 2x2 and 8 more.');
});
});
}

0 comments on commit 9e23891

Please sign in to comment.