Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Result | refactoring #76

Merged
merged 9 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/analyze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
# - uses: subosito/flutter-action@v2
with:
channel: 'stable'
version: 3.3.10
version: 3.13.6
# - run: |
# sudo apt-get update -y
# sudo apt-get install -y ninja-build libgtk-3-dev
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
# - uses: subosito/flutter-action@v2
with:
channel: 'stable'
version: 3.3.10
version: 3.13.6
# - run: |
# sudo apt-get update -y
# sudo apt-get install -y ninja-build libgtk-3-dev
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: flutter-actions/setup-flutter@v2
with:
channel: 'stable'
version: 3.3.10
version: 3.13.6

- name: Clearing project
run: flutter clean
Expand Down
3 changes: 3 additions & 0 deletions lib/hmi_core_result_new.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library hmi_core_result_new;

export 'src/core/result_new/result.dart';
5 changes: 5 additions & 0 deletions lib/src/core/result/result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import 'package:hmi_core/src/core/error/failure.dart';

///
/// Result contains or data of type T or error
@Deprecated(
'Use implementation from hmi_core_result_new. '
'The current implementation will be removed as soon as '
'the new one if fully integrated with other libraries and projects.',
)
class Result<T> {
final T? _data;
final Failure? _error;
Expand Down
21 changes: 21 additions & 0 deletions lib/src/core/result_new/result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:hmi_core/src/core/error/failure.dart';
///
/// Containing either value of type [V] or error of type [E]
sealed class Result<V, E> {}
///
/// Containing either value of type [V] or [Failure]
typedef ResultF<V> = Result<V, Failure>;
///
/// Successful [Result] with [value].
final class Ok<V, E> implements Result<V, E> {
final V value;
Minyewoo marked this conversation as resolved.
Show resolved Hide resolved
/// Successful [Result] with [value].
const Ok(this.value);
}
///
/// Unsuccessful [Result] with [error].
final class Err<V, E> implements Result<V, E> {
final E error;
Minyewoo marked this conversation as resolved.
Show resolved Hide resolved
/// Unsuccessful [Result] with [error].
const Err(this.error);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 0.0.5
homepage: https://github.com/a-givertzman/hmi_core

environment:
sdk: '>=2.18.2 <3.0.0'
sdk: '>=3.0.0 <4.0.0'
flutter: ">=1.17.0"

dependencies:
Expand Down
2 changes: 2 additions & 0 deletions test/unit/core/result/result_constructor_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result.dart';
Expand Down
2 changes: 2 additions & 0 deletions test/unit/core/result/result_data_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result.dart';
Expand Down
2 changes: 2 additions & 0 deletions test/unit/core/result/result_error_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result.dart';
Expand Down
2 changes: 2 additions & 0 deletions test/unit/core/result/result_fold_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result.dart';
Expand Down
2 changes: 2 additions & 0 deletions test/unit/core/result/result_has_data_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result.dart';
Expand Down
2 changes: 2 additions & 0 deletions test/unit/core/result/result_has_error_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result.dart';
Expand Down
25 changes: 25 additions & 0 deletions test/unit/core/result_new/new_result_constructor_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result_new.dart';

import 'test_data.dart';

void main() {
group('Result constructor of', () {
test('Ok completes if data is provided', () {
for(final value in testData) {
expect(() => Ok(value), returnsNormally);
}
});
test('Err completes if error is provided', () {
for(final value in testData) {
expect(
() => Err(
Failure(message: value, stackTrace: StackTrace.current),
),
returnsNormally,
);
}
});
});
}
59 changes: 59 additions & 0 deletions test/unit/core/result_new/new_result_fold_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result_new.dart';
import 'test_data.dart';

void main() {
group('Result fold', () {
test('matches Ok if data is provided', () {
for (final value in testData) {
final Result<dynamic, Failure> result = Ok(value);
final foldResult = switch(result) {
Ok(:final value) => value,
Err(:final error) => error,
};
expect(foldResult, value);
}
});
test('matches Err if no data', () {
for (final error in testData) {
final Result result = Err(error);
final foldResult = switch(result) {
Ok(:final value) => value,
Err(:final error) => error,
};
expect(foldResult, error);
}
});
test('works perfectly even if data is void', () {
for (final value in testData) {
// ignore: void_checks
final Result<void, Failure> result = Ok(value);
expect(
() {
return switch(result) {
Ok(:final value) => value,
Err(:final error) => error,
};
},
returnsNormally,
);
}
});
test('works perfectly even if error is void', () {
for (final error in testData) {
// ignore: void_checks
final Result<dynamic, void> result = Err(error);
expect(
() {
return switch(result) {
Ok(:final value) => value,
Err(:final error) => error,
};
},
returnsNormally,
);
}
});
});
}
27 changes: 27 additions & 0 deletions test/unit/core/result_new/test_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
final class ForTestOptionOnly {
final String value;
const ForTestOptionOnly(this.value);
}
const _maxInt64 = 0x7FFFFFFFFFFFFFFF;
const _minInt64 = -0x8000000000000000;
const testData = [
_maxInt64,
_minInt64,
0,
353465,
45,
-3456,
ForTestOptionOnly('value'),
ForTestOptionOnly(''),
ForTestOptionOnly('123'),
true,
false,
double.maxFinite,
double.minPositive
-double.maxFinite,
0.0,
1.234,
234.4,
-0.45676,
-346.786,
];
Loading