Skip to content

Commit

Permalink
Merge pull request #22 from netglade/feat/if-empty-and-blank
Browse files Browse the repository at this point in the history
Add if empty and blank, add tests, remove callbacks
  • Loading branch information
tenhobi authored Feb 7, 2024
2 parents 263acff + e4ebbf0 commit c973323
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 42 deletions.
4 changes: 4 additions & 0 deletions packages/netglade_utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.0
- Add `ifEmpty` and `ifBlank` to string extensions.
- Remove typedefs. (private)

## 1.1.0
- Add `isNotNullNorEmpty` to `String?` extension.
- Fix implementation and doc comments in `string_extensions`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'dart:async';

import 'package:clock/clock.dart';
import 'package:netglade_utils/src/typedefs/typedefs.dart';

typedef OnTakingTooLongCallback = void Function();

extension FutureExtensions<T> on Future<T> {
// ignore: comment_references, see https://github.com/dart-lang/linter/issues/2079
/// If [this] future taking longer than [duration] to execute - [callback] is called.
Future<T> onTakingTooLong(Duration duration, VoidCallback callback) async {
Future<T> onTakingTooLong(Duration duration, OnTakingTooLongCallback callback) async {
final timer = Timer(duration, callback);

try {
Expand Down
21 changes: 21 additions & 0 deletions packages/netglade_utils/lib/src/extensions/string_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ extension StringExtensions on String {
String lastNCharacters(int limit) {
return characters.getRange(max(0, length - limit)).toString();
}

String? ifEmpty([String? defaultValue]) {
return isEmpty ? defaultValue : this;
}

String? ifBlank([String? defaultValue]) {
return isBlank ? defaultValue : this;
}
}

extension NullableStringExtensions on String? {
Expand All @@ -42,6 +50,11 @@ extension NullableStringExtensions on String? {
return this?.isBlank ?? true;
}

/// Returns negation of [isNullOrBlank].
bool get isNotNullNorBlank {
return !isNullOrBlank;
}

/// Returns negation of [isBlank].
///
/// * String? is blank when it has a value and contains only whitespaces.
Expand All @@ -58,4 +71,12 @@ extension NullableStringExtensions on String? {
bool get isNotNullNorEmpty {
return !isNullOrEmpty;
}

String? ifEmpty([String? defaultValue]) {
return (this?.isEmpty ?? false) ? defaultValue : this;
}

String? ifBlank([String? defaultValue]) {
return (this?.isBlank ?? false) ? defaultValue : this;
}
}
9 changes: 4 additions & 5 deletions packages/netglade_utils/lib/src/testing/mock_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'dart:async';

import 'package:mocktail/mocktail.dart';
import 'package:netglade_utils/src/result/result.dart';
import 'package:netglade_utils/src/typedefs/typedefs.dart';

extension FutureVoidAnswer on When<Future<void>> {
void thenAnswerWithVoid() => thenAnswer((_) async => <void>{});
Expand Down Expand Up @@ -46,14 +45,14 @@ extension StreamSubscriptionAnswer<T> on When<StreamSubscription<T>> {
void thenAnswerWithProvidedCallback() => thenAnswer((i) {
final callback = i.positionalArguments.singleOrNull;

// ignore: no-empty-block, it needs to be empty
return Stream.fromIterable(<T>[]).listen(callback != null ? callback as Setter<T> : (_) {});
// ignore: no-empty-block, it needs to be empty, prefer-typedefs-for-callbacks, private API
return Stream.fromIterable(<T>[]).listen(callback != null ? callback as void Function(T value) : (_) {});
});

void thenAnswerWithCustomStream(Stream<T> stream) => thenAnswer((i) {
final callback = i.positionalArguments.singleOrNull;

// ignore: no-empty-block, it needs to be empty
return stream.listen(callback != null ? callback as Setter<T> : (_) {});
// ignore: no-empty-block, it needs to be empty, prefer-typedefs-for-callbacks, private API
return stream.listen(callback != null ? callback as void Function(T value) : (_) {});
});
}
7 changes: 0 additions & 7 deletions packages/netglade_utils/lib/src/typedefs/callbacks.dart

This file was deleted.

1 change: 0 additions & 1 deletion packages/netglade_utils/lib/src/typedefs/typedefs.dart

This file was deleted.

2 changes: 1 addition & 1 deletion packages/netglade_utils/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: netglade_utils
version: 1.1.0
version: 1.2.0
description: Dart utils used internally at netglade.
repository: https://github.com/netglade/flutter_core/tree/main/packages/netglade_utils
issue_tracker: https://github.com/netglade/flutter_core/issues
Expand Down
186 changes: 160 additions & 26 deletions packages/netglade_utils/test/src/extensions/string_extensions_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: prefer_const_declarations, unnecessary_nullable_for_final_variable_declarations, omit_local_variable_types

import 'package:netglade_utils/netglade_utils.dart';
import 'package:test/test.dart';

Expand All @@ -23,127 +25,259 @@ void main() {
group('isBlank', () {
group('on String', () {
test('empty message is isBlank', () {
expect(''.isBlank, isTrue);
final String value = '';
expect(value.isBlank, isTrue);
});

test('message with whitespaces is isBlank', () {
expect(' '.isBlank, isTrue);
final String value = ' ';
expect(value.isBlank, isTrue);
});

test('message with non-whitespace characters is not isBlank', () {
expect('xxx'.isBlank, isFalse);
final String value = 'xxx';
expect(value.isBlank, isFalse);
});
});

group('on String?', () {
test('empty message is isBlank', () {
expect(''.isBlank, isTrue);
final String? value = '';
expect(value.isBlank, isTrue);
});

test('null message is not isBlank', () {
expect(null.isBlank, isFalse);
final String? value = null;
expect(value.isBlank, isFalse);
});

test('message with whitespaces is isBlank', () {
expect(' '.isBlank, isTrue);
final String? value = ' ';
expect(value.isBlank, isTrue);
});

test('message with non-whitespace characters is not isBlank', () {
expect('xxx'.isBlank, isFalse);
final String? value = 'xxx';
expect(value.isBlank, isFalse);
});
});
});

group('isNotBlank', () {
group('on String', () {
test('empty message is not isNotBlank', () {
expect(''.isNotBlank, isFalse);
final String value = '';
expect(value.isNotBlank, isFalse);
});

test('message with whitespaces is not isNotBlank', () {
expect(' '.isNotBlank, isFalse);
final String value = ' ';
expect(value.isNotBlank, isFalse);
});

test('message with non-whitespace characters is isNotBlank', () {
expect('xxx'.isNotBlank, isTrue);
final String value = 'xxx';
expect(value.isNotBlank, isTrue);
});
});

group('on String?', () {
test('empty message is not isNotBlank', () {
expect(''.isNotBlank, isFalse);
final String? value = '';
expect(value.isNotBlank, isFalse);
});

test('message is null is isNotBlank', () {
expect(null.isNotBlank, isTrue);
final String? value = null;
expect(value.isNotBlank, isTrue);
});

test('message has whitespaces is not isNotBlank', () {
expect(' '.isNotBlank, isFalse);
final String? value = ' ';
expect(value.isNotBlank, isFalse);
});

test('message has non-whitespace characters is isNotBlank', () {
expect('xxx'.isNotBlank, isTrue);
final String? value = 'xxx';
expect(value.isNotBlank, isTrue);
});
});
});

group('isNullOrBlank', () {
group('on String?', () {
test('empty message is isNullOrBlank', () {
expect(''.isNullOrBlank, isTrue);
final String? value = '';
expect(value.isNullOrBlank, isTrue);
});

test('null message is isNullOrBlank', () {
expect(null.isNullOrBlank, isTrue);
final String? value = null;
expect(value.isNullOrBlank, isTrue);
});

test('message with whitespaces is isNullOrBlank', () {
expect(' '.isNullOrBlank, isTrue);
final String? value = ' ';
expect(value.isNullOrBlank, isTrue);
});

test('message with non-whitespace characters is not isNullOrBlank', () {
expect('xxx'.isNullOrBlank, isFalse);
final String? value = 'xxx';
expect(value.isNullOrBlank, isFalse);
});
});
});

group('isNullOrEmpty', () {
group('on String?', () {
test('empty message is isNullOrEmpty', () {
expect(''.isNullOrEmpty, isTrue);
final String? value = '';
expect(value.isNullOrEmpty, isTrue);
});

test('null message is isNullOrEmpty', () {
expect(null.isNullOrEmpty, isTrue);
final String? value = null;
expect(value.isNullOrEmpty, isTrue);
});

test('message with whitespaces is not isNullOrEmpty', () {
expect(' '.isNullOrEmpty, isFalse);
final String? value = ' ';
expect(value.isNullOrEmpty, isFalse);
});

test('message with non-whitespace characters is not isNullOrEmpty', () {
expect('xxx'.isNullOrEmpty, isFalse);
final String? value = 'xxx';
expect(value.isNullOrEmpty, isFalse);
});
});
});

group('isNotNullNorEmpty', () {
group('on String?', () {
test('empty message is not isNotNullNorEmpty', () {
expect(''.isNotNullNorEmpty, isFalse);
final String? value = '';
expect(value.isNotNullNorEmpty, isFalse);
});

test('null message is not isNotNullNorEmpty', () {
expect(null.isNotNullNorEmpty, isFalse);
final String? value = null;
expect(value.isNotNullNorEmpty, isFalse);
});

test('message with whitespaces is isNotNullNorEmpty', () {
expect(' '.isNotNullNorEmpty, isTrue);
final String? value = ' ';
expect(value.isNotNullNorEmpty, isTrue);
});

test('message with non-whitespace characters is isNotNullNorEmpty', () {
expect('xxx'.isNotNullNorEmpty, isTrue);
final String? value = 'xxx';
expect(value.isNotNullNorEmpty, isTrue);
});
});
});

group('isNotNullNorBlank', () {
group('on String?', () {
test('empty message is not isNotNullNorBlank', () {
final String? value = '';
expect(value.isNotNullNorBlank, isFalse);
});

test('null message is not isNotNullNorBlank', () {
final String? value = null;
expect(value.isNotNullNorBlank, isFalse);
});

test('message with whitespaces is not isNotNullNorBlank', () {
final String? value = ' ';
expect(value.isNotNullNorBlank, isFalse);
});

test('message with non-whitespace characters is isNotNullNorBlank', () {
final String? value = 'xxx';
expect(value.isNotNullNorBlank, isTrue);
});
});
});

group('ifEmpty', () {
group('on String', () {
test('empty message', () {
final String value = '';
expect(value.ifEmpty('aaa'), equals('aaa'));
});

test('message with whitespaces', () {
final String value = ' ';
expect(value.ifEmpty('aaa'), equals(value));
});

test('message with non-whitespace characters', () {
final String value = 'xxx';
expect(value.ifEmpty('aaa'), equals(value));
});
});

group('on String?', () {
test('empty message', () {
final String? value = '';
expect(value.ifEmpty('aaa'), equals('aaa'));
});

test('null message', () {
final String? value = null;
expect(value.ifEmpty('aaa'), equals(value));
});

test('message with whitespaces', () {
final String? value = ' ';
expect(value.ifEmpty('aaa'), equals(value));
});

test('message with non-whitespace characters', () {
final String? value = 'xxx';
expect(value.ifEmpty('aaa'), equals(value));
});
});
});

group('ifBlank', () {
group('on String', () {
test('empty message', () {
final String value = '';
expect(value.ifBlank('aaa'), equals('aaa'));
});

test('message with whitespaces', () {
final String value = ' ';
expect(value.ifBlank('aaa'), equals('aaa'));
});

test('message with non-whitespace characters', () {
final String value = 'xxx';
expect(value.ifBlank('aaa'), equals(value));
});
});

group('on String?', () {
test('empty message', () {
final String? value = '';
expect(value.ifBlank('aaa'), equals('aaa'));
});

test('null message', () {
final String? value = null;
expect(value.ifBlank('aaa'), equals(value));
});

test('message with whitespaces', () {
final String? value = ' ';
expect(value.ifBlank('aaa'), equals('aaa'));
});

test('message with non-whitespace characters', () {
final String? value = 'xxx';
expect(value.ifBlank('aaa'), equals(value));
});
});
});
Expand Down

0 comments on commit c973323

Please sign in to comment.