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:
Konstantin Shcheglov 2021-12-14 17:11:34 +00:00 committed by Commit Bot
parent efe92fb822
commit 7b31338938
3 changed files with 34 additions and 0 deletions

View file

@ -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';

View 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');
}
}

View file

@ -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');