mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-01-22 16:56:19 +01:00
Doc and tests for ObjectExtension
This commit is contained in:
parent
2bddfcbf8c
commit
67c35bcd00
2 changed files with 24 additions and 0 deletions
|
@ -1,15 +1,18 @@
|
|||
import 'dart:async';
|
||||
|
||||
extension ObjectExtension<T> on T {
|
||||
/// Run [fn] with this, and return this
|
||||
T apply(void Function(T obj) fn) {
|
||||
fn(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// Run [fn] with this, and return this
|
||||
Future<T> applyFuture(FutureOr<void> Function(T obj) fn) async {
|
||||
await fn(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// Run [fn] with this, and return the results of [fn]
|
||||
U run<U>(U Function(T obj) fn) => fn(this);
|
||||
}
|
||||
|
|
21
test/object_extension_test.dart
Normal file
21
test/object_extension_test.dart
Normal file
|
@ -0,0 +1,21 @@
|
|||
import 'package:nc_photos/object_extension.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group("ObjectExtension", () {
|
||||
test("apply", () {
|
||||
const obj = Object();
|
||||
expect(obj.apply((obj) => 1), obj);
|
||||
});
|
||||
|
||||
test("applyFuture", () async {
|
||||
const obj = Object();
|
||||
expect(await obj.applyFuture((obj) async => 1), obj);
|
||||
});
|
||||
|
||||
test("run", () {
|
||||
const obj = Object();
|
||||
expect(obj.run((obj) => 1), 1);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue