This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
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
c0d81f8
commit b0eb4f6
Showing
3 changed files
with
84 additions
and
0 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
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,16 @@ | ||
import 'result.dart'; | ||
|
||
extension ResultExtensions<T> on Result<T> { | ||
/// Returns the value if the result [isValue]. | ||
/// | ||
/// If the result [isError], it throws the corresponding error and stacktrace. | ||
T get requireValue { | ||
if (isValue) return asValue!.value; | ||
Error.throwWithStackTrace(asError!.error, asError!.stackTrace); | ||
} | ||
|
||
/// Returns the value if the result [isValue]. | ||
/// | ||
/// Returns null otherwise | ||
T? get valueOrNull => asValue?.value; | ||
} |
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,67 @@ | ||
import 'package:async/async.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('test requiredValue', () { | ||
test( | ||
'return value when result is value', | ||
() { | ||
final result = Result.value(10); | ||
expect(result.requireValue, 10); | ||
}, | ||
); | ||
|
||
test( | ||
'return nullable value when result is nullable value', | ||
() { | ||
final result = Result<int?>.value(null); | ||
expect(result.requireValue, null); | ||
}, | ||
); | ||
|
||
test( | ||
'throw exception when result is error', | ||
() async { | ||
final result = Result.error('error'); | ||
expect(() => result.requireValue, throwsA(anything)); | ||
}, | ||
); | ||
|
||
test( | ||
'throw the corresponding and stacktrace exception when result is error', | ||
() async { | ||
final error = Exception(); | ||
final stacktrace = StackTrace.current; | ||
final result = Result.error(error,stacktrace); | ||
|
||
expect( | ||
() => result.requireValue, | ||
throwsA( | ||
predicate((err) { | ||
return err == error; | ||
}), | ||
), | ||
); | ||
}, | ||
); | ||
}); | ||
|
||
group('test valueOrNull', () { | ||
test('return null when result is error', (){ | ||
final result = Result.error('error'); | ||
expect(result.valueOrNull, null); | ||
}); | ||
|
||
test('return value when result is value ', (){ | ||
final result = Result.value(10); | ||
expect(result.valueOrNull, 10); | ||
}); | ||
|
||
test('return nullable value when result is nullable value ', (){ | ||
final result = Result<int?>.value(null); | ||
expect(result.valueOrNull, null); | ||
}); | ||
|
||
}); | ||
|
||
} |