-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from pierremrtn/async_transition
Support for async transitions
- Loading branch information
Showing
8 changed files
with
209 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: Run tests | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
build: | ||
strategy: | ||
matrix: | ||
working-directory: [ | ||
packages/state_machine_bloc, | ||
examples/flutter_timer_state_machine, | ||
examples/infinite_list_state_machine | ||
] | ||
|
||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: ${{ matrix.working-directory }} | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Flutter | ||
uses: subosito/flutter-action@v2 | ||
with: | ||
channel: stable | ||
|
||
- name: Install dependencies | ||
run: flutter pub get | ||
|
||
- name: Verify formatting | ||
run: dart format --output=none --set-exit-if-changed . | ||
|
||
- name: Run tests | ||
run: flutter test |
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 |
---|---|---|
@@ -1 +1 @@ | ||
3.24.1 | ||
3.24.3 |
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
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
76 changes: 76 additions & 0 deletions
76
packages/state_machine_bloc/test/async_transition_test.dart
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,76 @@ | ||
import 'package:state_machine_bloc/state_machine_bloc.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'utils.dart'; | ||
|
||
abstract class Event {} | ||
|
||
class EventA extends Event {} | ||
|
||
class EventB extends Event {} | ||
|
||
abstract class State { | ||
const State(); | ||
} | ||
|
||
class StateA extends State { | ||
const StateA(); | ||
} | ||
|
||
class StateB extends State { | ||
const StateB(); | ||
} | ||
|
||
class StateC extends State { | ||
const StateC(); | ||
} | ||
|
||
class StateD extends State { | ||
const StateD(); | ||
} | ||
|
||
class DummyStateMachine extends StateMachine<Event, State> { | ||
DummyStateMachine([State? initial]) : super(initial ?? const StateA()) { | ||
define<StateA>(($) => $ | ||
..on((EventA e, s) async { | ||
await Future.delayed(Duration(milliseconds: 300)); | ||
return const StateB(); | ||
}) | ||
..on((EventB e, s) { | ||
return const StateC(); | ||
})); | ||
define<StateB>(); | ||
define<StateC>(); | ||
} | ||
} | ||
|
||
void main() { | ||
group("event receiving tests", () { | ||
test("Transitions are awaited", () async { | ||
final sm = DummyStateMachine(const StateA()); | ||
sm.add(EventA()); | ||
|
||
await wait(); | ||
|
||
expect(sm.state, const StateA()); | ||
|
||
await Future.delayed(Duration(seconds: 1)); | ||
|
||
expect(sm.state, const StateB()); | ||
}); | ||
}); | ||
|
||
test("Transitions are evaluated sequentially", () async { | ||
final sm = DummyStateMachine(const StateA()); | ||
sm.add(EventA()); | ||
sm.add(EventB()); | ||
|
||
await wait(); | ||
|
||
expect(sm.state, const StateA()); | ||
|
||
await Future.delayed(Duration(seconds: 1)); | ||
|
||
expect(sm.state, const StateB()); | ||
}); | ||
} |
79 changes: 79 additions & 0 deletions
79
packages/state_machine_bloc/test/nested_async_transition_test.dart
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,79 @@ | ||
import 'package:state_machine_bloc/state_machine_bloc.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'utils.dart'; | ||
|
||
abstract class Event {} | ||
|
||
class EventA extends Event {} | ||
|
||
class EventB extends Event {} | ||
|
||
abstract class State { | ||
const State(); | ||
} | ||
|
||
class StateA extends State { | ||
const StateA(); | ||
} | ||
|
||
class StateB extends StateA { | ||
const StateB(); | ||
} | ||
|
||
class StateC extends State { | ||
const StateC(); | ||
} | ||
|
||
class StateD extends State { | ||
const StateD(); | ||
} | ||
|
||
class DummyStateMachine extends StateMachine<Event, State> { | ||
DummyStateMachine([State? initial]) : super(initial ?? const StateA()) { | ||
define<StateA>( | ||
($) => $ | ||
..define<StateB>(($) => $ | ||
..on((EventA e, s) async { | ||
await Future.delayed(Duration(milliseconds: 300)); | ||
return const StateC(); | ||
}) | ||
..on((EventB e, s) { | ||
return const StateD(); | ||
})), | ||
); | ||
define<StateC>(); | ||
define<StateD>(); | ||
} | ||
} | ||
|
||
void main() { | ||
group("event receiving tests", () { | ||
test("Nested transitions are awaited", () async { | ||
final sm = DummyStateMachine(const StateB()); | ||
sm.add(EventA()); | ||
|
||
await wait(); | ||
|
||
expect(sm.state, const StateB()); | ||
|
||
await Future.delayed(Duration(seconds: 1)); | ||
|
||
expect(sm.state, const StateC()); | ||
}); | ||
}); | ||
|
||
test("Nested transitions are evaluated sequentially", () async { | ||
final sm = DummyStateMachine(const StateB()); | ||
sm.add(EventA()); | ||
sm.add(EventB()); | ||
|
||
await wait(); | ||
|
||
expect(sm.state, const StateB()); | ||
|
||
await Future.delayed(Duration(seconds: 1)); | ||
|
||
expect(sm.state, const StateC()); | ||
}); | ||
} |