-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f80240
commit f691c1e
Showing
5 changed files
with
150 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import "package:fpdart/fpdart.dart"; | ||
import "package:test/test.dart"; | ||
|
||
import "../test_extension.dart"; | ||
|
||
class CustomError implements Exception { | ||
const CustomError(); | ||
} | ||
|
||
void main() { | ||
group( | ||
"Effect context", | ||
() { | ||
group('provideEnv', () { | ||
test('handle throw in closing scope', () async { | ||
final main = Effect<Null, String, int>.succeed(10) | ||
.addFinalizer(Effect.succeedLazy( | ||
() => throw const CustomError(), | ||
)) | ||
.provideEnv(Scope.withEnv(null)); | ||
|
||
final result = await main.runFutureExit(); | ||
|
||
result.expectLeft((value) { | ||
expect(value, isA<Die>()); | ||
if (value is Die) { | ||
expect(value.error, isA<CustomError>()); | ||
} | ||
}); | ||
}); | ||
|
||
test('handle failure in closing scope', () async { | ||
final main = Effect<Null, String, int>.succeed(10) | ||
.addFinalizer(Effect.die(const CustomError())) | ||
.provideEnv(Scope.withEnv(null)); | ||
|
||
final result = await main.runFutureExit(); | ||
|
||
result.expectLeft((value) { | ||
expect(value, isA<Die>()); | ||
if (value is Die) { | ||
expect(value.error, isA<CustomError>()); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
group('provideScope', () { | ||
test('handle throw in closing scope', () async { | ||
final main = Effect<Null, String, int>.succeed(10) | ||
.addFinalizer(Effect.succeedLazy( | ||
() => throw const CustomError(), | ||
)) | ||
.provideScope; | ||
|
||
final result = await main.runFutureExit(); | ||
|
||
result.expectLeft((value) { | ||
expect(value, isA<Die>()); | ||
if (value is Die) { | ||
expect(value.error, isA<CustomError>()); | ||
} | ||
}); | ||
}); | ||
|
||
test('handle failure in closing scope', () async { | ||
final main = Effect<Null, String, int>.succeed(10) | ||
.addFinalizer(Effect.die(const CustomError())) | ||
.provideScope; | ||
|
||
final result = await main.runFutureExit(); | ||
|
||
result.expectLeft((value) { | ||
expect(value, isA<Die>()); | ||
if (value is Die) { | ||
expect(value.error, isA<CustomError>()); | ||
} | ||
}); | ||
}); | ||
}); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:fpdart/fpdart.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
extension EitherTest<L, R> on Either<L, R> { | ||
void expectLeft(Function(L value) onLeft) { | ||
switch (this) { | ||
case Right(value: final value): | ||
fail("Either expected to be Left, Right instead: $value"); | ||
case Left(value: final value): | ||
onLeft(value); | ||
} | ||
} | ||
|
||
void expectRight(Function(R value) onRight) { | ||
switch (this) { | ||
case Right(value: final value): | ||
onRight(value); | ||
case Left(value: final value): | ||
fail("Either expected to be Right, Left instead: $value"); | ||
} | ||
} | ||
} |