Doc and tests for ObjectExtension

This commit is contained in:
Ming Ming 2021-12-20 02:28:10 +08:00
parent 2bddfcbf8c
commit 67c35bcd00
2 changed files with 24 additions and 0 deletions

View file

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

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