diff --git a/CHANGELOG.md b/CHANGELOG.md index deb10f8..5524e15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.1.5 +- Fix code documentation. +- Deprecate `notEmpty`, `executeIf`, 'unwrapped' methods. +- Add new extension `callWhen` on nullable objects. +- Make examples more readable. + ## 0.1.4 - Fix code documentation. - Add LetX extension functions. diff --git a/Makefile b/Makefile index e312c9d..3291c0e 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,3 @@ publish: # Generate test coverage report using the coverage package coverage: dart pub global run coverage:test_with_coverage - -# Tag the release version in git -tag: - git tag -a v$(version) -m "Release version $(version)" && git push origin v$(version) diff --git a/README.md b/README.md index 352aa33..c5e5997 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,25 @@ void main() { ```dart import 'package:nullx/nullx.dart'; +void main() { + const int userAge = 20; + // Unwraps the nullable string and performs an operation on it + callWhen( + condition: () => userAge >= 18, + onMet: () { + // prints 'You are an adult.' + }, + onNotMet: () { + // prints 'You are not an adult.' + }, + ); +} + +``` + +```dart +import 'package:nullx/nullx.dart'; + void main() { const int? nullableInt = null; nullableInt.orZero; // Outputs: 0 diff --git a/example/nullx_example.dart b/example/nullx_example.dart index 8dfad65..b02647e 100644 --- a/example/nullx_example.dart +++ b/example/nullx_example.dart @@ -1,45 +1,54 @@ import 'package:nullx/nullx.dart'; void main() { + /// Variables + /// + /// + // ignore: unnecessary_nullable_for_final_variable_declarations const int? nullableInt = 10; - nullableInt.let((item) => item * 2); - // print(result); // prints: 2 - - final list = [1, null, 3, null]; - list.mapNonNull((item) => item * 2); // prints: [2, 6] + const nonNullInt = 1; + const int? nullInt = null; + // ignore: unnecessary_nullable_for_final_variable_declarations + const String? nullableString = 'example'; + const String emptyString = ''; + const String? nullString = null; + const double? nullDouble = null; + const bool? nullBool = null; - final list2 = [1, null, 3, null]; - list2.mapNonNullIndexed((item, index) => item * index); // prints: [0, 6] + // ignore: unnecessary_nullable_for_final_variable_declarations + final List? nullableIntList = [1, null, 3, null]; + // ignore: unnecessary_nullable_for_final_variable_declarations + final List? nullableStringList = [null, 'one', null, 'two', null]; + // ignore: unnecessary_nullable_for_final_variable_declarations + const List? nullStringList = null; // ignore: unnecessary_nullable_for_final_variable_declarations - List? nullableList = [1, 2, 3]; - nullableList.whatIfNotNullOrEmpty( - (list) => list, - () => 'List is null or empty', // whatIfNot - ); - // prints: [1, 2, 3] + const List? emptyStringList = []; - nullableList.isNullOrEmpty; // prints: false + /// Collections + /// + /// - nullableList = null; - nullableList.isNullOrEmpty; // prints: true + // Maps over the list, applying a function to each non-null element, + nullableIntList?.mapNonNull((item) => item * 2); // prints: [2, 6] - nullableList = []; - nullableList.isNullOrEmpty; // prints: true + // Maps over the list, applying a function to each non-null element and its + // index + // prints: [0, 6] + nullableIntList?.mapNonNullIndexed((item, index) => item * index); - // ignore: unnecessary_nullable_for_final_variable_declarations - const String? nullableString = 'example'; - - // Unwraps the nullable string and performs an operation on it - unwrapped(nullableString, (value) {}); // prints: 'example' + nullableIntList.whatIfNotNullOrEmpty( + (list) => list, + () => 'List is null or empty', // whatIfNot + ); + // prints: [1, 2, 3] - // A list of nullable strings - final List strings = [null, 'one', null, 'two', null]; + nullableIntList.isNullOrEmpty; // prints: false - // Maps over the list, applying a function to each non-null element, - // then filters out null results and converts the result to a list - strings.map((s) => s.letNonNull((s) => s.length)).whereType().toList(); + nullStringList.isNullOrEmpty; // prints: true + emptyStringList.isNullOrEmpty; // prints: true + nullableStringList.isNullOrEmpty; // prints: false // A list of nullable strings final List strs = [null, 'one', null, 'two', null]; @@ -53,53 +62,141 @@ void main() { // A dynamic constant const dynamic value = 'Hello'; - // Tries to cast the dynamic value to a String - final String? name = tryCast(value); - - // Tries to cast the dynamic value to an int - final int? age = tryCast(value); + const int userAge = 20; + // prints: 'You are an adult.' - // Performs an operation on the name if it's not null - name.let((n) => n); + const double? nullableDouble = null; + nullableDouble.orZero; // Outputs: 0.0 + nullableDouble.or(defaultValue: 5.5); // Outputs: 5.5 - // Performs an operation on the age if it's not null - age.let((a) => a); + const bool? nullableBool = null; + nullableBool.orFalse; // Outputs: false + nullableBool.or(defaultValue: true); // Outputs: true - nullableInt.letNonNull((value) => value * 2); // prints: 2 + /// Types + /// + /// - const int userAge = 20; - executeIf( - () => userAge >= 18, - onConditionMet: () { + // Unwraps the nullable string and performs an operation on it + callWhen( + condition: () => userAge >= 18, + onMet: () { // prints 'You are an adult.' }, - onConditionNotMet: () { + onNotMet: () { // prints 'You are not an adult.' }, ); - executeIfAs( + // Unwraps the nullable string and performs an operation on it + final res1 = executeIfAs( () => userAge >= 18, ifTrue: () => 'You are an adult.', ifFalse: () => 'You are not an adult.', ); - // prints: 'You are an adult.' // ignore: avoid_print - notEmpty(nullableString, (s) => print(s)); // prints: 'The value is: example' + print(res1); // prints: 'You are an adult.' - const int? nullableInt2 = null; - nullableInt2.orZero; // Outputs: 0 - nullableInt2.or(defaultValue: 5); // Outputs: 5 + // Unwraps the nullable string and performs an operation on it + nullableString.unwrapped((value) {}); // prints: 'example' - const double? nullableDouble = null; - nullableDouble.orZero; // Outputs: 0.0 - nullableDouble.or(defaultValue: 5.5); // Outputs: 5.5 + // Unwraps the nullable string and performs an operation on it + // ignore: avoid_print + nullableString.notEmpty((s) => print(s)); // prints: 'The value is: example' - const bool? nullableBool = null; - nullableBool.orFalse; // Outputs: false - nullableBool.or(defaultValue: true); // Outputs: true + // Unwraps the nullable string and performs an operation on it + final result = nullableInt.conditionNotNullWith( + isTrue: (item) => item * 2, + isFalse: () => 0, + ); + // ignore: avoid_print + print(result); // prints: 20 + + final res2 = nullableInt.conditionNotNullAs( + condition: (value) => value > 5, + isTrue: (value) => value * 2, + isFalse: () => -1, + ); + + // ignore: avoid_print + print(res2); // prints: 20 + + // Unwraps the nullable string and performs an operation on it + const num = 10; + final res3 = num.takeIf((value) => value > 5); + // ignore: avoid_print + print('takeIf: $res3'); // prints: 10 + + final res4 = num.takeUnless((value) => value > 5); + // ignore: avoid_print + print('takeUnless: $res4'); // prints: 10 + + // Unwraps the nullable string and performs an operation on it + final res5 = nullableInt.let((item) => item * 2); + // ignore: avoid_print + print('let: $res5'); // prints: 20 + + // Unwraps the nullable string and performs an operation on it + final res6 = nonNullInt.run((item) => item * 2); + // ignore: avoid_print + print('run: $res6'); // prints: 2 + + var str = 'Hello'; + str.also((item) => str = item.toUpperCase()); + // ignore: avoid_print + print('also: $str'); // prints: HELLO + + // Maps over the list, applying a function to each non-null element, + // then filters out null results and converts the result to a list + nullableStringList + ?.map((s) => s.letNonNull((s) => s.length)) + .whereType() + .toList(); + + // ignore: avoid_print + print('call isNullOrEmpty on null string: ${nullableString.isNullOrEmpty}'); + + // ignore: avoid_print + print('call isNullOrEmpty on empty string: ${emptyString.isNullOrEmpty}'); + + final res8 = nullString.orDefault('hi'); + // ignore: avoid_print + print('orDefault: $res8'); // prints: 'hi' + + // Unwraps the nullable int + nullInt.orZero; // Outputs: 0 + nullInt.or(defaultValue: 5); // Outputs: 5 + + // Unwraps the nullable double + nullDouble.orZero; // Outputs: 0.0 + nullDouble.or(defaultValue: 5.5); // Outputs: 5.5 + + // Unwraps the nullable bool + nullBool.orFalse; // Outputs: false + nullBool.or(defaultValue: true); // Outputs: true + + /// Utils + /// + /// + + // Tries to cast the dynamic value to a String + final String? name = tryCast(value); + + // Tries to cast the dynamic value to an int + final int? age = tryCast(value); + + // Performs an operation on the name if it's not null + name.let((n) => n); + + // Performs an operation on the age if it's not null + age.let((a) => a); - // A placeholder for future code - todo(); + // Throws a [NotImplementedError] indicating that an operation is + try { + todo(); + } catch (e) { + // ignore: avoid_print + print(e); // prints: An operation is not implemented. + } } diff --git a/lib/src/collections.dart b/lib/src/collections.dart index ec2d928..3201fa4 100644 --- a/lib/src/collections.dart +++ b/lib/src/collections.dart @@ -51,26 +51,30 @@ extension NullableMapExtension on Iterable { } } -/// Extension on `List?` to add a `whatIfNotNullOrEmpty` method. -/// -/// This extension provides a convenient way to handle nullable lists. -/// The `whatIfNotNullOrEmpty` method takes two functions: `whatIf` and -/// `whatIfNot`. -/// -/// If the list is not null and not empty, it applies the `whatIf` function -/// to the list. If the list is null or empty, it calls the `whatIfNot` function. -/// -/// Example usage: -/// -/// ```dart -/// List? nullableList = [1, 2, 3]; -/// nullableList.whatIfNotNullOrEmpty( -/// (list) => print(list), // whatIf -/// () => print('List is null or empty'), // whatIfNot -/// ); -/// // prints: [1, 2, 3] -/// ``` +/// Extension on `List?`. extension WhatIfNotNullOrEmptyExtension on List? { + /// Executes the [whatIf] function if the list is not null and not empty, + /// otherwise executes the [whatIfNot] function. + /// + /// This function is useful when you need to perform an operation on a list + /// that could be null or empty. + /// + /// The [whatIf] function should accept a single argument of type `List`, + /// which represents the non-null and non-empty list. + /// + /// The [whatIfNot] function should be a function that takes no arguments + /// and is called when the list is null or empty. + /// + /// Example usage: + /// + /// ```dart + /// List? nullableList = [1, 2, 3]; + /// nullableList.whatIfNotNullOrEmpty( + /// (list) => print(list), // whatIf + /// () => print('List is null or empty'), // whatIfNot + /// ); + /// // prints: [1, 2, 3] + /// ``` void whatIfNotNullOrEmpty( void Function(List) whatIf, void Function() whatIfNot, diff --git a/lib/src/types.dart b/lib/src/types.dart index 00dc3f4..b6ca262 100644 --- a/lib/src/types.dart +++ b/lib/src/types.dart @@ -12,6 +12,19 @@ /// ``` typedef Closure = R Function(T it); +/// A function type alias for a closure that takes an argument of type [T] and +/// returns void. +/// +/// This is a generic function type where [T] is the type of the argument. +/// +/// Example usage: +/// +/// ```dart +/// VoidClosure printNumber = (int number) { +/// print('Number is: $number'); +/// }; +/// printNumber(5); // prints: 'Number is: 5' +/// ``` typedef VoidClosure = void Function(T value); /// Calls the provided [value] function if the [nullableValue] is not null. @@ -36,6 +49,7 @@ typedef VoidClosure = void Function(T value); /// /// Generic type [T] can be inferred automatically based on the type of the /// provided [nullableValue]. +@Deprecated('use unwrapped extension instead, see [UnwrappedExtension]') void unwrapped(T? nullableValue, VoidClosure value) { if (nullableValue != null) { value(nullableValue); @@ -65,6 +79,7 @@ void unwrapped(T? nullableValue, VoidClosure value) { /// }, /// ); /// ``` +@Deprecated('use callWhen instead') void executeIf( bool Function() condition, { required void Function() onConditionMet, @@ -77,6 +92,34 @@ void executeIf( } } +/// Executes one of two functions based on a condition. +/// The [onMet] function is called when the condition is met, and the [onNotMet] +/// function is called when the condition is not met. +/// +/// Example: +/// ```dart +/// callWhen( +/// condition: () => age >= 18, +/// onMet: () { +/// print('You are an adult.'); +/// }, +/// onNotMet: () { +/// print('You are not an adult.'); +/// }, +/// ); +/// ``` +void callWhen({ + required bool Function() condition, + required void Function() onMet, + required void Function() onNotMet, +}) { + if (condition()) { + onMet(); + } else { + onNotMet(); + } +} + /// Executes one of two functions based on a condition and returns a value /// of type [T]. /// @@ -134,6 +177,7 @@ T executeIfAs( /// /// The provided [value] function should accept a single argument of type [T], /// which represents the non-null and non-empty string value. +@Deprecated('use NotEmptyExtension instead, see [NotEmptyExtension]') void notEmpty(T? nullableValue, VoidClosure value) { if (nullableValue != null && nullableValue.isNotEmpty) { value(nullableValue); @@ -239,10 +283,10 @@ extension ConditionExtension on T? { /// /// Generic type [R] can be any type and is used for the return type of the /// [isTrue] and [isFalse] functions. - R conditionNotNullWith( - R Function(T) isTrue, - R Function() isFalse, - ) { + R conditionNotNullWith({ + required R Function(T) isTrue, + required R Function() isFalse, + }) { if (this != null) { return isTrue(this as T); } else { @@ -291,29 +335,48 @@ extension ConditionExtension on T? { } /// Extension on `T` to add `takeIf` and `takeUnless` methods. -/// -/// These methods provide a convenient way to conditionally return the value. -/// -/// The `takeIf` method takes a predicate function and returns the value if the -/// predicate is true. If the predicate is false, it returns null. -/// -/// The `takeUnless` method is the opposite of `takeIf`. It takes a predicate -/// function and returns the value if the predicate is false. If the predicate -/// is true, it returns null. -/// -/// Example usage: -/// -/// ```dart -/// var value = 5; -/// print(value.takeIf((item) => item > 3)); // prints: 5 -/// print(value.takeUnless((item) => item > 3)); // prints: null -/// ``` extension TakeIfExtension on T { + /// Evaluates the given [predicate] function on the value of type [T] and + /// returns the value if the [predicate] is true, otherwise returns null. + /// + /// This function is useful when you need to conditionally return the value + /// based on a certain condition. + /// + /// The [predicate] function should accept a single argument of type [T], + /// which represents the value, and return a boolean. + /// + /// Example usage: + /// + /// ```dart + /// var value = 5; + /// print(value.takeIf((item) => item > 3)); // prints: 5 + /// ``` + /// + /// @param predicate the function to test on the value + /// @return the value if the [predicate] is true, otherwise null @pragma('vm:prefer-inline') T? takeIf(bool Function(T) predicate) { return predicate(this) ? this : null; } + /// Evaluates the given [predicate] function on the value of type [T] and + /// returns the value if the [predicate] is false, otherwise returns null. + /// + /// This function is useful when you need to conditionally return the value + /// based on a certain condition. + /// + /// The [predicate] function should accept a single argument of type [T], + /// which represents the value, and return a boolean. + /// + /// Example usage: + /// + /// ```dart + /// var value = 5; + /// print(value.takeUnless((item) => item > 3)); // prints: null + /// ``` + /// + /// @param predicate the function to test on the value + /// @return the value if the [predicate] is false, otherwise null @pragma('vm:prefer-inline') T? takeUnless(bool Function(T) predicate) { return !predicate(this) ? this : null; @@ -321,23 +384,29 @@ extension TakeIfExtension on T { } /// Extension on `T?` to add a `let` method. -/// -/// This extension provides a convenient way to apply a transformation to a -/// nullable value. The `let` method takes a function and applies it to the -/// value if it is not null. -/// -/// The function should take a non-null `T` and return a `R`. -/// -/// If the value is null, the `let` method returns null. -/// -/// Example usage: -/// -/// ```dart -/// var nullableInt = 1; -/// var result = nullableInt.let((item) => item * 2); -/// print(result); // prints: 2 -/// ``` extension LetExtension on T? { + /// Applies the [block] function to this value and returns the result if this + /// value is not null. + /// + /// This function is useful when you need to perform a transformation on a + /// nullable value. + /// The [block] function should accept a single argument of type [T], which + /// represents the non-null value, + /// and return a value of type [R]. + /// + /// If this value is null, the `let` method returns null. + /// + /// Example usage: + /// + /// ```dart + /// var nullableInt = 1; + /// var result = nullableInt.let((item) => item * 2); + /// print(result); // prints: 2 + /// ``` + /// + /// @param block the function to apply on the value + /// @return the result of the [block] function if this value is not null, + /// otherwise null @pragma('vm:prefer-inline') R? let(R Function(T) block) { if (this != null) { @@ -347,6 +416,7 @@ extension LetExtension on T? { } } +/// Extension on `T` to add a `run` method. extension LetX on T { /// Extension on `T` where `T` extends `Object` to add a `run` method. /// @@ -369,6 +439,7 @@ extension LetX on T { } } +/// Extension on `T` to add an `also` method. extension AlsoX on T { /// Extension on `T` to add an `also` method. /// @@ -394,23 +465,27 @@ extension AlsoX on T { } /// Extension on `T?` to add a `letNonNull` method. -/// -/// This extension provides a convenient way to apply a transformation to a -/// nullable value. The `letNonNull` method takes a function and applies it to -/// the value if it is not null. -/// -/// The function should take a non-null `T` and return a nullable `R`. -/// -/// If the value is null, the `letNonNull` method returns null. -/// -/// Example usage: -/// -/// ```dart -/// var nullableInt = 1; -/// var result = nullableInt.letNonNull((item) => item * 2); -/// print(result); // prints: 2 -/// ``` extension NullableLetExtension on T? { + /// Applies the [block] function to this value and returns the result if this + /// value is not null. + /// + /// This function is useful when you need to perform a transformation on a + /// nullable value. The [block] function should accept a single argument of + /// type [T], which represents the non-null value, and return a value of type [R]. + /// + /// If this value is null, the `letNonNull` method returns null. + /// + /// Example usage: + /// + /// ```dart + /// var nullableInt = 1; + /// var result = nullableInt.letNonNull((item) => item * 2); + /// print(result); // prints: 2 + /// ``` + /// + /// @param block the function to apply on the value + /// @return the result of the [block] function if this value is not null, + /// otherwise null @pragma('vm:prefer-inline') R? letNonNull(R? Function(T) block) { if (this != null) { diff --git a/pubspec.yaml b/pubspec.yaml index b4208fe..0840c88 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,8 +1,8 @@ name: nullx homepage: https://shtanko.dev description: >- - nullx is a simple package that enhances handling of nullable types, null-checking etc. -version: 0.1.4 + nullx is a collection of elegant extensions for handling null types in Dart. +version: 0.1.5 repository: https://github.com/ashtanko/nullx topics: diff --git a/scripts/git_tag.sh b/scripts/git_tag.sh new file mode 100644 index 0000000..2fb1143 --- /dev/null +++ b/scripts/git_tag.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# This script is used to create a new git tag and push it to the origin. +# The version number for the tag is provided by the user when running the script. +# The script checks if the version number is in the format of major.minor.patch. +# If the version number is valid, a new git tag is created and pushed to the origin. +# If the version number is not valid, an error message is printed and the script exits. + +# Ask for the version +echo "Enter the version:" +# shellcheck disable=SC2162 +read version # Read the version number from the user input + +# Check if the version follows the format of major.minor.patch +if [[ $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + # If the version number is valid, create a git tag and push it to the origin + git tag -a v"$version" -m "Release version $version" + git push origin v"$version" +else + # If the version number is not valid, print an error message + echo "Invalid version format. Please enter a version in the format of major.minor.patch" +fi diff --git a/test/types_test.dart b/test/types_test.dart index 0845d6e..e32ce82 100644 --- a/test/types_test.dart +++ b/test/types_test.dart @@ -2,176 +2,33 @@ import 'package:nullx/nullx.dart'; import 'package:test/test.dart'; void main() { - group('Nullable types tests', () { - setUp(() { - // Additional setup goes here. - }); - - test('Should call the provided function if the nullableBoolean is not null', - () { - // ignore: unnecessary_nullable_for_final_variable_declarations - const bool? nullableBoolean = true; - bool called = false; - - unwrapped(nullableBoolean, (value) { - called = true; - }); - - expect(called, true); - }); - - test('Should not call the provided function if the nullableBoolean is null', - () { - // ignore: avoid_init_to_null - const bool? nullableBoolean = null; - bool called = false; - - unwrapped(nullableBoolean, (value) { - called = true; - }); - - expect(called, false); - }); - - test('Should call the provided function if the nullableInt is not null', - () { - // ignore: unnecessary_nullable_for_final_variable_declarations - const int? nullableInt = 42; - bool called = false; - - unwrapped(nullableInt, (value) { - called = true; - }); - - expect(called, true); - }); - - test('Should not call the provided function if the nullableInt is null', - () { - // ignore: avoid_init_to_null - const int? nullableInt = null; - bool called = false; - - unwrapped(nullableInt, (value) { - called = true; - }); - - expect(called, false); - }); - - test('Should call the provided function if the nullableString is not null', - () { - // ignore: unnecessary_nullable_for_final_variable_declarations - const String? nullableString = 'hello'; - bool called = false; - - unwrapped(nullableString, (value) { - called = true; - }); - - expect(called, true); - }); - - test('Should not call the provided function if the nullableString is null', - () { - // ignore: avoid_init_to_null - const String? nullableString = null; - bool called = false; - - unwrapped(nullableString, (value) { - called = true; - }); - - expect(called, false); - }); - }); - - group('executeIf tests', () { - test('executeIfNotSingleLambdaTest', () { - String? nullableString; - - executeIf( - () => nullableString == null || nullableString!.isEmpty, - onConditionMet: () => nullableString = 'conditionMet', - onConditionNotMet: () => nullableString = 'conditionNotMet', - ); - - expect(nullableString, 'conditionMet'); - - nullableString = ''; - executeIf( - () => nullableString == null || nullableString!.isNotEmpty, - onConditionMet: () => nullableString = 'conditionMet', - onConditionNotMet: () => nullableString = 'conditionNotMet', + group('callWhen', () { + test('calls onMet when condition is true', () { + var onMetCalled = false; + var onNotMetCalled = false; + + callWhen( + condition: () => true, + onMet: () => onMetCalled = true, + onNotMet: () => onNotMetCalled = true, ); - expect(nullableString, 'conditionNotMet'); + expect(onMetCalled, isTrue); + expect(onNotMetCalled, isFalse); }); - test('executeIf - condition met', () { - bool conditionMet = false; + test('calls onNotMet when condition is false', () { + var onMetCalled = false; + var onNotMetCalled = false; - executeIf( - () => true, // Condition always met - onConditionMet: () => conditionMet = true, - onConditionNotMet: () => conditionMet = false, + callWhen( + condition: () => false, + onMet: () => onMetCalled = true, + onNotMet: () => onNotMetCalled = true, ); - expect(conditionMet, true); // Expecting conditionMet to be true - }); - - test('executeIf - condition not met', () { - bool conditionMet = false; - - executeIf( - () => false, // Condition never met - onConditionMet: () => conditionMet = true, - onConditionNotMet: () => conditionMet = false, - ); - - expect(conditionMet, false); // Expecting conditionMet to be false - }); - }); - - group('String types tests', () { - test( - 'Should call the provided function if the nullableString is not null and not empty', - () { - // ignore: unnecessary_nullable_for_final_variable_declarations - const String? nullableString = 'hello'; - bool called = false; - - notEmpty(nullableString, (value) { - called = true; - }); - - expect(called, true); - }); - - test('Should not call the provided function if the nullableString is null', - () { - // ignore: avoid_init_to_null - const String? nullableString = null; - bool called = false; - - notEmpty(nullableString, (value) { - called = true; - }); - - expect(called, false); - }); - - test('Should not call the provided function if the nullableString is empty', - () { - // ignore: unnecessary_nullable_for_final_variable_declarations - const String? nullableString = ''; - bool called = false; - - notEmpty(nullableString, (value) { - called = true; - }); - - expect(called, false); + expect(onMetCalled, isFalse); + expect(onNotMetCalled, isTrue); }); }); @@ -230,8 +87,8 @@ void main() { const String? nullableString = 'notNull'; final int length = nullableString.conditionNotNullWith( - (it) => it.length, - () => 0, + isTrue: (it) => it.length, + isFalse: () => 0, ); expect(length, 7); // Expecting length to be 7 @@ -241,8 +98,8 @@ void main() { String? nullableString; final int length = nullableString.conditionNotNullWith( - (it) => it.length, - () => 0, + isTrue: (it) => it.length, + isFalse: () => 0, ); expect(length, 0); // Expecting length to be 0 @@ -253,8 +110,8 @@ void main() { final CustomObject? nullableObject = CustomObject(42); final int result = nullableObject.conditionNotNullWith( - (obj) => obj.val * 2, - () => -1, + isTrue: (obj) => obj.val * 2, + isFalse: () => -1, ); expect(result, 84); // Expecting result to be 84 @@ -264,8 +121,8 @@ void main() { CustomObject? nullableObject; final int result = nullableObject.conditionNotNullWith( - (obj) => obj.val * 2, - () => -99, + isTrue: (obj) => obj.val * 2, + isFalse: () => -99, ); expect(result, -99); // Expecting result to be 84