mirror of
https://github.com/dart-lang/sdk
synced 2024-09-18 21:41:19 +00:00
Add isNotNull and isNull checks.
Change-Id: Ib8fb64e15dcc2458667d48cd6c86c83f6b45d25c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/223840 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Samuel Rawlins <srawlins@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
parent
efe92fb822
commit
7b31338938
|
@ -10,6 +10,7 @@ export 'package:analyzer_utilities/check/check_target.dart';
|
|||
export 'package:analyzer_utilities/check/equality.dart';
|
||||
export 'package:analyzer_utilities/check/int.dart';
|
||||
export 'package:analyzer_utilities/check/iterable.dart';
|
||||
export 'package:analyzer_utilities/check/nullability.dart';
|
||||
export 'package:analyzer_utilities/check/string.dart';
|
||||
export 'package:analyzer_utilities/check/type.dart';
|
||||
|
||||
|
|
21
pkg/analyzer_utilities/lib/check/nullability.dart
Normal file
21
pkg/analyzer_utilities/lib/check/nullability.dart
Normal file
|
@ -0,0 +1,21 @@
|
|||
// Copyright (c) 2021, 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:analyzer_utilities/check/check.dart';
|
||||
|
||||
extension NullabilityExtension<T> on CheckTarget<T?> {
|
||||
void get isNull {
|
||||
if (value != null) {
|
||||
fail('is not null');
|
||||
}
|
||||
}
|
||||
|
||||
CheckTarget<T> get isNotNull {
|
||||
final value = this.value;
|
||||
if (value == null) {
|
||||
fail('is null');
|
||||
}
|
||||
return nest(value, (value) => 'is not null');
|
||||
}
|
||||
}
|
|
@ -97,6 +97,18 @@ void main() {
|
|||
_fails(() => check(<int>{}).isNotEmpty);
|
||||
});
|
||||
});
|
||||
group('nullability', () {
|
||||
const int? notNullable = 0;
|
||||
const int? nullable = null;
|
||||
test('isNotNull', () {
|
||||
check(notNullable).isNotNull;
|
||||
_fails(() => check(nullable).isNotNull.isZero);
|
||||
});
|
||||
test('isNull', () {
|
||||
check(nullable).isNull;
|
||||
_fails(() => check(notNullable).isNull);
|
||||
});
|
||||
});
|
||||
group('String', () {
|
||||
test('contains', () {
|
||||
check('abc').contains('a');
|
||||
|
|
Loading…
Reference in a new issue