[pkg] use package:lints when analyzing pkg/smith, pkg/expect

Change-Id: Iaaf2f8a1583ea94fadb8cb03fd83dc9ed38b2b95
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250771
Reviewed-by: Nate Bosch <nbosch@google.com>
Commit-Queue: Devon Carew <devoncarew@google.com>
This commit is contained in:
Devon Carew
2022-07-06 22:29:24 +00:00
committed by Commit Bot
parent 580597c2eb
commit 317e3463a6
9 changed files with 141 additions and 172 deletions
+1
View File
@@ -0,0 +1 @@
include: package:lints/core.yaml
+94 -138
View File
@@ -2,10 +2,8 @@
// 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.
/**
* This library contains an Expect class with static methods that can be used
* for simple unit-tests.
*/
/// This library contains an Expect class with static methods that can be used
/// for simple unit-tests.
library expect;
import 'package:meta/meta.dart';
@@ -16,26 +14,22 @@ bool get hasUnsoundNullSafety => const <Null>[] is List<Object>;
/// Whether the program is running with sound null safety.
bool get hasSoundNullSafety => !hasUnsoundNullSafety;
/**
* Expect is used for tests that do not want to make use of the
* Dart unit test library - for example, the core language tests.
* Third parties are discouraged from using this, and should use
* the expect() function in the unit test library instead for
* test assertions.
*/
/// Expect is used for tests that do not want to make use of the
/// Dart unit test library - for example, the core language tests.
/// Third parties are discouraged from using this, and should use
/// the expect() function in the unit test library instead for
/// test assertions.
class Expect {
/**
* Return a slice of a string.
*
* The slice will contain at least the substring from [start] to the lower of
* [end] and `start + length`.
* If the result is no more than `length - 10` characters long,
* context may be added by extending the range of the slice, by decreasing
* [start] and increasing [end], up to at most length characters.
* If the start or end of the slice are not matching the start or end of
* the string, ellipses are added before or after the slice.
* Characters other than printable ASCII are escaped.
*/
/// Return a slice of a string.
///
/// The slice will contain at least the substring from [start] to the lower of
/// [end] and `start + length`.
/// If the result is no more than `length - 10` characters long,
/// context may be added by extending the range of the slice, by decreasing
/// [start] and increasing [end], up to at most length characters.
/// If the start or end of the slice are not matching the start or end of
/// the string, ellipses are added before or after the slice.
/// Characters other than printable ASCII are escaped.
static String _truncateString(String string, int start, int end, int length) {
if (end - start > length) {
end = start + length;
@@ -48,7 +42,7 @@ class Expect {
if (start < 0) start = 0;
if (end > string.length) end = string.length;
}
StringBuffer buf = new StringBuffer();
StringBuffer buf = StringBuffer();
if (start > 0) buf.write("...");
_escapeSubstring(buf, string, 0, string.length);
if (end < string.length) buf.write("...");
@@ -58,7 +52,7 @@ class Expect {
/// Return the string with characters that are not printable ASCII characters
/// escaped as either "\xXX" codes or "\uXXXX" codes.
static String _escapeString(String string) {
StringBuffer buf = new StringBuffer();
StringBuffer buf = StringBuffer();
_escapeSubstring(buf, string, 0, string.length);
return buf.toString();
}
@@ -85,16 +79,14 @@ class Expect {
}
}
/**
* Find the difference between two strings.
*
* This finds the first point where two strings differ, and returns
* a text describing the difference.
*
* For small strings (length less than 20) nothing is done, and "" is
* returned. Small strings can be compared visually, but for longer strings
* only a slice containing the first difference will be shown.
*/
/// Find the difference between two strings.
///
/// This finds the first point where two strings differ, and returns
/// a text describing the difference.
///
/// For small strings (length less than 20) nothing is done, and "" is
/// returned. Small strings can be compared visually, but for longer strings
/// only a slice containing the first difference will be shown.
static String _stringDifference(String expected, String actual) {
if (expected.length < 20 && actual.length < 20) return "";
for (int i = 0; i < expected.length && i < actual.length; i++) {
@@ -115,9 +107,7 @@ class Expect {
return "";
}
/**
* Checks whether the expected and actual values are equal (using `==`).
*/
/// Checks whether the expected and actual values are equal (using `==`).
static void equals(dynamic expected, dynamic actual, [String reason = ""]) {
if (expected == actual) return;
String msg = _getMessage(reason);
@@ -132,64 +122,50 @@ class Expect {
_fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails.");
}
/**
* Checks whether the actual value is a bool and its value is true.
*/
/// Checks whether the actual value is a bool and its value is true.
static void isTrue(dynamic actual, [String reason = ""]) {
if (_identical(actual, true)) return;
String msg = _getMessage(reason);
_fail("Expect.isTrue($actual$msg) fails.");
}
/**
* Checks whether the actual value is a bool and its value is false.
*/
/// Checks whether the actual value is a bool and its value is false.
static void isFalse(dynamic actual, [String reason = ""]) {
if (_identical(actual, false)) return;
String msg = _getMessage(reason);
_fail("Expect.isFalse($actual$msg) fails.");
}
/**
* Checks whether [actual] is null.
*/
/// Checks whether [actual] is null.
static void isNull(dynamic actual, [String reason = ""]) {
if (null == actual) return;
String msg = _getMessage(reason);
_fail("Expect.isNull(actual: <$actual>$msg) fails.");
}
/**
* Checks whether [actual] is not null.
*/
/// Checks whether [actual] is not null.
static void isNotNull(dynamic actual, [String reason = ""]) {
if (null != actual) return;
String msg = _getMessage(reason);
_fail("Expect.isNotNull(actual: <$actual>$msg) fails.");
}
/**
* Checks whether the Iterable [actual] is empty.
*/
/// Checks whether the Iterable [actual] is empty.
static void isEmpty(Iterable actual, [String reason = ""]) {
if (actual.isEmpty) return;
String msg = _getMessage(reason);
_fail("Expect.isEmpty(actual: <$actual>$msg) fails.");
}
/**
* Checks whether the Iterable [actual] is not empty.
*/
/// Checks whether the Iterable [actual] is not empty.
static void isNotEmpty(Iterable actual, [String reason = ""]) {
if (actual.isNotEmpty) return;
String msg = _getMessage(reason);
_fail("Expect.isNotEmpty(actual: <$actual>$msg) fails.");
}
/**
* Checks whether the expected and actual values are identical
* (using `identical`).
*/
/// Checks whether the expected and actual values are identical
/// (using `identical`).
static void identical(dynamic expected, dynamic actual,
[String reason = ""]) {
if (_identical(expected, actual)) return;
@@ -205,17 +181,15 @@ class Expect {
"fails.");
}
/**
* Finds equivalence classes of objects (by index) wrt. identity.
*
* Returns a list of lists of identical object indices per object.
* That is, `objects[i]` is identical to objects with indices in
* `_findEquivalences(objects)[i]`.
*
* Uses `[]` for objects that are only identical to themselves.
*/
/// Finds equivalence classes of objects (by index) wrt. identity.
///
/// Returns a list of lists of identical object indices per object.
/// That is, `objects[i]` is identical to objects with indices in
/// `_findEquivalences(objects)[i]`.
///
/// Uses `[]` for objects that are only identical to themselves.
static List<List<int>> _findEquivalences(List<dynamic> objects) {
var equivalences = new List<List<int>>.generate(objects.length, (_) => []);
var equivalences = List<List<int>>.generate(objects.length, (_) => []);
for (int i = 0; i < objects.length; i++) {
if (equivalences[i].isNotEmpty) continue;
var o = objects[i];
@@ -261,7 +235,7 @@ class Expect {
var equivalences = _findEquivalences(objects);
var first = equivalences[0];
if (first.isNotEmpty && first.length == objects.length) return;
var buffer = new StringBuffer("Expect.allIdentical([");
var buffer = StringBuffer("Expect.allIdentical([");
_writeEquivalences(objects, equivalences, buffer);
buffer
..write("]")
@@ -270,19 +244,15 @@ class Expect {
_fail(buffer.toString());
}
/**
* Checks whether the expected and actual values are *not* identical
* (using `identical`).
*/
/// Checks whether the expected and actual values are *not* identical
/// (using `identical`).
static void notIdentical(var unexpected, var actual, [String reason = ""]) {
if (!_identical(unexpected, actual)) return;
String msg = _getMessage(reason);
_fail("Expect.notIdentical(expected and actual: <$actual>$msg) fails.");
}
/**
* Checks that no two [objects] are `identical`.
*/
/// Checks that no two [objects] are `identical`.
static void allDistinct(List<dynamic> objects, [String reason = ""]) {
String msg = _getMessage(reason);
var equivalences = _findEquivalences(objects);
@@ -295,7 +265,7 @@ class Expect {
}
}
if (!hasEquivalence) return;
var buffer = new StringBuffer("Expect.allDistinct([");
var buffer = StringBuffer("Expect.allDistinct([");
_writeEquivalences(objects, equivalences, buffer);
buffer
..write("]")
@@ -310,11 +280,9 @@ class Expect {
_fail("Expect.fail('$msg')");
}
/**
* Failure if the difference between expected and actual is greater than the
* given tolerance. If no tolerance is given, tolerance is assumed to be the
* value 4 significant digits smaller than the value given for expected.
*/
/// Failure if the difference between expected and actual is greater than the
/// given tolerance. If no tolerance is given, tolerance is assumed to be the
/// value 4 significant digits smaller than the value given for expected.
static void approxEquals(num expected, num actual,
[num tolerance = -1, String reason = ""]) {
if (tolerance < 0) {
@@ -335,12 +303,10 @@ class Expect {
"fails.");
}
/**
* Checks that all elements in [expected] and [actual] are equal `==`.
* This is different than the typical check for identity equality `identical`
* used by the standard list implementation. It should also produce nicer
* error messages than just calling `Expect.equals(expected, actual)`.
*/
/// Checks that all elements in [expected] and [actual] are equal `==`.
/// This is different than the typical check for identity equality `identical`
/// used by the standard list implementation. It should also produce nicer
/// error messages than just calling `Expect.equals(expected, actual)`.
static void listEquals(List expected, List actual, [String reason = ""]) {
String msg = _getMessage(reason);
int n = (expected.length < actual.length) ? expected.length : actual.length;
@@ -360,11 +326,9 @@ class Expect {
}
}
/**
* Checks that all [expected] and [actual] have the same set of keys (using
* the semantics of [Map.containsKey] to determine what "same" means. For
* each key, checks that the values in both maps are equal using `==`.
*/
/// Checks that all [expected] and [actual] have the same set of keys (using
/// the semantics of [Map.containsKey] to determine what "same" means. For
/// each key, checks that the values in both maps are equal using `==`.
static void mapEquals(Map expected, Map actual, [String reason = ""]) {
String msg = _getMessage(reason);
@@ -385,10 +349,8 @@ class Expect {
}
}
/**
* Specialized equality test for strings. When the strings don't match,
* this method shows where the mismatch starts and ends.
*/
/// Specialized equality test for strings. When the strings don't match,
/// this method shows where the mismatch starts and ends.
static void stringEquals(String expected, String actual,
[String reason = ""]) {
if (expected == actual) return;
@@ -506,23 +468,21 @@ class Expect {
}
}
/**
* Checks that every element of [expected] is also in [actual], and that
* every element of [actual] is also in [expected].
*/
/// Checks that every element of [expected] is also in [actual], and that
/// every element of [actual] is also in [expected].
static void setEquals(Iterable expected, Iterable actual,
[String reason = ""]) {
final missingSet = new Set.from(expected);
final missingSet = Set.from(expected);
missingSet.removeAll(actual);
final extraSet = new Set.from(actual);
final extraSet = Set.from(actual);
extraSet.removeAll(expected);
if (extraSet.isEmpty && missingSet.isEmpty) return;
String msg = _getMessage(reason);
StringBuffer sb = new StringBuffer("Expect.setEquals($msg) fails");
StringBuffer sb = StringBuffer("Expect.setEquals($msg) fails");
// Report any missing items.
if (!missingSet.isEmpty) {
if (missingSet.isNotEmpty) {
sb.write('\nExpected collection does not contain: ');
}
@@ -531,7 +491,7 @@ class Expect {
}
// Report any extra items.
if (!extraSet.isEmpty) {
if (extraSet.isNotEmpty) {
sb.write('\nExpected collection should not contain: ');
}
@@ -541,11 +501,9 @@ class Expect {
_fail(sb.toString());
}
/**
* Checks that [expected] is equivalent to [actual].
*
* If the objects are iterables or maps, recurses into them.
*/
/// Checks that [expected] is equivalent to [actual].
///
/// If the objects are iterables or maps, recurses into them.
static void deepEquals(dynamic expected, dynamic actual) {
// Early exit check for equality.
if (expected == actual) return;
@@ -595,30 +553,28 @@ class Expect {
static bool _defaultCheck([dynamic _]) => true;
/**
* Verifies that [computation] throws a [T].
*
* Calls the [computation] function and fails if that call doesn't throw,
* throws something which is not a [T], or throws a [T] which does not
* satisfy the optional [check] function.
*
* Returns the accepted thrown [T] object, if one is caught.
* This value can be checked further, instead of checking it in the [check]
* function. For example, to check the content of the thrown object,
* you could write this:
* ```
* var e = Expect.throws<MyException>(myThrowingFunction);
* Expect.isTrue(e.myMessage.contains("WARNING"));
* ```
* The type variable can be omitted, in which case it defaults to [Object],
* and the (sub-)type of the object can be checked in [check] instead.
* This was traditionally done before Dart had generic methods.
*
* If `computation` fails another test expectation
* (i.e., throws an [ExpectException]),
* that exception cannot be caught and accepted by [Expect.throws].
* The test is still considered failing.
*/
/// Verifies that [computation] throws a [T].
///
/// Calls the [computation] function and fails if that call doesn't throw,
/// throws something which is not a [T], or throws a [T] which does not
/// satisfy the optional [check] function.
///
/// Returns the accepted thrown [T] object, if one is caught.
/// This value can be checked further, instead of checking it in the [check]
/// function. For example, to check the content of the thrown object,
/// you could write this:
/// ```
/// var e = Expect.throws<MyException>(myThrowingFunction);
/// Expect.isTrue(e.myMessage.contains("WARNING"));
/// ```
/// The type variable can be omitted, in which case it defaults to [Object],
/// and the (sub-)type of the object can be checked in [check] instead.
/// This was traditionally done before Dart had generic methods.
///
/// If `computation` fails another test expectation
/// (i.e., throws an [ExpectException]),
/// that exception cannot be caught and accepted by [Expect.throws].
/// The test is still considered failing.
static T throws<T extends Object>(void Function() computation,
[bool Function(T error)? check, String? reason]) {
// TODO(vsm): Make check and reason nullable or change call sites.
@@ -739,7 +695,7 @@ class Expect {
@alwaysThrows
static Never _fail(String message) {
throw new ExpectException(message);
throw ExpectException(message);
}
}
+25 -25
View File
@@ -25,17 +25,17 @@ import 'dart:async';
import 'package:expect/expect.dart';
typedef dynamic _Action();
typedef void _ExpectationFunction(dynamic actual);
typedef _Action = dynamic Function();
typedef _ExpectationFunction = void Function(dynamic actual);
final List<_Group> _groups = [new _Group()];
final List<_Group> _groups = [_Group()];
final Object isFalse = new _Expectation(Expect.isFalse);
final Object isNotNull = new _Expectation(Expect.isNotNull);
final Object isNull = new _Expectation(Expect.isNull);
final Object isTrue = new _Expectation(Expect.isTrue);
final Object isFalse = _Expectation(Expect.isFalse);
final Object isNotNull = _Expectation(Expect.isNotNull);
final Object isNull = _Expectation(Expect.isNull);
final Object isTrue = _Expectation(Expect.isTrue);
final Object returnsNormally = new _Expectation((actual) {
final Object returnsNormally = _Expectation((actual) {
try {
(actual as _Action)();
} catch (error) {
@@ -43,39 +43,39 @@ final Object returnsNormally = new _Expectation((actual) {
}
});
final Object throws = new _Expectation((actual) {
final Object throws = _Expectation((actual) {
Expect.throws(actual as _Action);
});
final Object throwsArgumentError = new _Expectation((actual) {
final Object throwsArgumentError = _Expectation((actual) {
Expect.throws(actual as _Action, (error) => error is ArgumentError);
});
final Object throwsNoSuchMethodError = new _Expectation((actual) {
final Object throwsNoSuchMethodError = _Expectation((actual) {
Expect.throws(actual as _Action, (error) => error is NoSuchMethodError);
});
final Object throwsRangeError = new _Expectation((actual) {
final Object throwsRangeError = _Expectation((actual) {
Expect.throws(actual as _Action, (error) => error is RangeError);
});
final Object throwsStateError = new _Expectation((actual) {
final Object throwsStateError = _Expectation((actual) {
Expect.throws(actual as _Action, (error) => error is StateError);
});
final Object throwsUnsupportedError = new _Expectation((actual) {
final Object throwsUnsupportedError = _Expectation((actual) {
Expect.throws(actual as _Action, (error) => error is UnsupportedError);
});
/// The test runner should call this once after running a test file.
void finishTests() {
_groups.clear();
_groups.add(new _Group());
_groups.add(_Group());
}
void group(String description, body()) {
// TODO(rnystrom): Do something useful with the description.
_groups.add(new _Group());
_groups.add(_Group());
try {
var result = body();
@@ -138,48 +138,48 @@ void fail(String message) {
Expect.fail(message);
}
Object equals(dynamic value) => new _Expectation((actual) {
Object equals(dynamic value) => _Expectation((actual) {
Expect.deepEquals(value, actual);
});
Object notEquals(dynamic value) => new _Expectation((actual) {
Object notEquals(dynamic value) => _Expectation((actual) {
Expect.notEquals(value, actual);
});
Object unorderedEquals(dynamic value) => new _Expectation((actual) {
Object unorderedEquals(dynamic value) => _Expectation((actual) {
Expect.setEquals(value as Iterable, actual as Iterable);
});
Object predicate(bool fn(dynamic value), [String description = ""]) =>
new _Expectation((actual) {
_Expectation((actual) {
Expect.isTrue(fn(actual), description);
});
Object inInclusiveRange(num min, num max) => new _Expectation((actual) {
Object inInclusiveRange(num min, num max) => _Expectation((actual) {
var actualNum = actual as num;
if (actualNum < min || actualNum > max) {
fail("Expected $actualNum to be in the inclusive range [$min, $max].");
}
});
Object greaterThan(num value) => new _Expectation((actual) {
Object greaterThan(num value) => _Expectation((actual) {
var actualNum = actual as num;
if (actualNum <= value) {
fail("Expected $actualNum to be greater than $value.");
}
});
Object same(dynamic value) => new _Expectation((actual) {
Object same(dynamic value) => _Expectation((actual) {
Expect.identical(value, actual);
});
Object closeTo(num value, num tolerance) => new _Expectation((actual) {
Object closeTo(num value, num tolerance) => _Expectation((actual) {
Expect.approxEquals(value, actual as num, tolerance);
});
/// Succeeds if the actual value is any of the given strings. Unlike matcher's
/// [anyOf], this only works with strings and requires an explicit list.
Object anyOf(List<String> expected) => new _Expectation((actual) {
Object anyOf(List<String> expected) => _Expectation((actual) {
for (var string in expected) {
if (actual == string) return;
}
+4
View File
@@ -15,3 +15,7 @@ environment:
dependencies:
meta: any
smith: any
# Use 'any' constraints here; we get our versions from the DEPS file.
dev_dependencies:
lints: any
+8 -8
View File
@@ -5,11 +5,11 @@
import "package:expect/expect.dart";
main() {
var o1 = new Object();
var o2 = new Object();
var o3 = new Object();
var c1 = new C(0);
var c2 = new C(0);
var o1 = Object();
var o2 = Object();
var o3 = Object();
var c1 = C(0);
var c2 = C(0);
// Successful checks.
Expect.notIdentical(o1, o2, "msg");
@@ -28,7 +28,7 @@ main() {
c2,
[1]
], "msg");
Expect.allDistinct(new List.generate(100, (_) => new Object()));
Expect.allDistinct(List.generate(100, (_) => Object()));
fails((msg) {
Expect.notIdentical(o1, o1, msg);
@@ -64,14 +64,14 @@ main() {
Expect.allDistinct([o1, o2, o3, o3], msg);
});
fails((msg) {
var list = new List.generate(100, (_) => new Object());
var list = List.generate(100, (_) => Object());
list.add(list[0]);
Expect.allDistinct(list, msg);
});
}
class C {
final x;
final Object x;
const C(this.x);
int get hashCode => x.hashCode;
bool operator ==(Object other) => other is C && x == other.x;
+2
View File
@@ -1,3 +1,5 @@
include: package:lints/recommended.yaml
analyzer:
language:
strict-casts: true
+5
View File
@@ -432,12 +432,14 @@ class Configuration {
return true;
}
@override
bool operator ==(Object other) =>
other is Configuration && name == other.name && optionsEqual(other);
int _toBinary(List<bool> bits) =>
bits.fold(0, (sum, bit) => (sum << 1) ^ (bit ? 1 : 0));
@override
int get hashCode =>
name.hashCode ^
architecture.hashCode ^
@@ -468,6 +470,7 @@ class Configuration {
useQemu
]);
@override
String toString() {
var buffer = StringBuffer();
buffer.write(name);
@@ -576,6 +579,7 @@ class Architecture extends NamedEnum {
static const x64 = Architecture._('x64');
static const x64c = Architecture._('x64c');
static const arm = Architecture._('arm');
// ignore: constant_identifier_names
static const arm_x64 = Architecture._('arm_x64');
static const arm64 = Architecture._('arm64');
static const arm64c = Architecture._('arm64c');
@@ -988,5 +992,6 @@ abstract class NamedEnum {
const NamedEnum(this.name);
@override
String toString() => name;
}
+1
View File
@@ -9,3 +9,4 @@ environment:
# Use 'any' constraints here; we get our versions from the DEPS file.
dev_dependencies:
expect: any
lints: any
+1 -1
View File
@@ -14,7 +14,7 @@ void expectParseError(String name, Map<String, dynamic> options, String error) {
}
}
void expectFormatError(String error, test()) {
void expectFormatError(String error, Function() test) {
try {
test();
} on FormatException catch (e) {