Skip to content

Commit

Permalink
fix(#118): corrije erro no command
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCostaDeOliveira committed Jan 16, 2025
1 parent 1ebff0a commit 3fcb526
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/core/state/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@ abstract class Command<T> extends ChangeNotifier {
Result<T>? _result;

bool _running = false;
bool _isError = false;
bool _isOk = false;

Command();

bool get isError => result?.asError != null;
bool get isError {
bool tmp = _isError;
_isError = false;
return tmp;
}

bool get isOk => result?.asValue != null;
bool get isOk {
bool tmp = _isOk;
_isOk = false;
return tmp;
}

Result<T>? get result => _result;

Expand All @@ -21,14 +31,24 @@ abstract class Command<T> extends ChangeNotifier {

_result = null;
_running = true;
_isOk = false;
_isError = false;
notifyListeners();

try {
_result = await action();

if (_result?.asError != null) {
_isError = true;
} else {
_isOk = true;
}

} catch (e) {
_result = Result.error(e);
_isError = true;
} finally {
_running = false;
_running = false;
notifyListeners();
}
}
Expand Down
102 changes: 102 additions & 0 deletions test/core/state/command_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import 'package:aranduapp/core/state/command.dart';
import 'package:async/async.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('Successful execution updates state and result', () async {
Command0<String> command = Command0(() async {
await Future.delayed(const Duration(seconds: 1));
return Result.value('Success');
});

expect(command.running, false);
expect(command.result, null);

final future = command.execute();

expect(command.running, true);
await future;

expect(command.running, false);
expect(command.isOk, true);
expect(command.result?.asValue?.value, 'Success');
});

test('Execution with error updates state and sets error', () async {
Command0<String> command = Command0(() async {
throw Exception('An error occurred');
});

expect(command.running, false);
expect(command.result, null);

final future = command.execute();

expect(command.running, true);
await future;

expect(command.running, false);
expect(command.isError, true);
expect(command.result?.asError?.error, isA<Exception>());
});

test('No re-execution when already running', () async {
//TODO: No re-execution when already running
});

test('Notifies listeners on state changes', () async {
Command0<String> command = Command0(() async => Result.value('Success'));

int notifyCount = 0;
command.addListener(() {
notifyCount++;
});

await command.execute();

expect(notifyCount, greaterThan(0));
});

test('Result on second access is ok', () async {
Command0<String> command = Command0(() async => Result.value('Success'));

await command.execute();

final firstAccess = command.isOk;

final secondAccess = command.isOk;

expect(firstAccess, true);
expect(command.result!.asValue!.value, 'Success');
expect(secondAccess, false);
});

test('Result on second access is error', () async {
Command0<String> command = Command0(() async => Result.error('error'));

await command.execute();

final firstAccess = command.isError;

final secondAccess = command.isError;

expect(firstAccess, true);
expect(command.result!.asError!.error, 'error');
expect(secondAccess, false);
});

test('Result on second access is error with exception', () async {
Command0<String> command = Command0(() async => throw Exception('error'));

await command.execute();

final firstAccess = command.isError;

final secondAccess = command.isError;

expect(firstAccess, true);
expect(
command.result?.asError?.error.toString(), equals('Exception: error'));
expect(secondAccess, false);
});
}

0 comments on commit 3fcb526

Please sign in to comment.