-
-
Notifications
You must be signed in to change notification settings - Fork 49
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
Showing
12 changed files
with
219 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
extends StateChartTestBase | ||
|
||
# Test that when automatic transition works when a compound state is left | ||
func test_automatic_transition_leaving_compound_state(): | ||
var root := compound_state("root") | ||
var a := compound_state("a", root) | ||
|
||
var a1 := atomic_state("a1", a) | ||
var a2 := atomic_state("a2", a) | ||
|
||
var b := atomic_state("b", root) | ||
|
||
transition(a, b, "") | ||
|
||
await finish_setup() | ||
|
||
# because we have the automatic transition to b, we should be in b | ||
# right after the setup phase | ||
assert_active(b) | ||
|
||
assert_inactive(a) | ||
assert_inactive(a1) | ||
assert_inactive(a2) | ||
|
||
|
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,36 @@ | ||
extends StateChartTestBase | ||
|
||
func test_event_consumption(): | ||
var root := compound_state("root") | ||
var a := compound_state("a", root) | ||
var b := compound_state("b", root) | ||
# add a transition from a to b reacting on the "switch" event | ||
var t1 := transition(a, b, "switch") | ||
|
||
var a1 := atomic_state("a1", a) | ||
var a2 := atomic_state("a2", a) | ||
# add a transition from a1 to a2 reacting on the "switch" event | ||
var t2 := transition(a1, a2, "switch") | ||
|
||
await finish_setup() | ||
assert_active(a) | ||
assert_active(a1) | ||
|
||
watch_signals(t1) | ||
watch_signals(t2) | ||
|
||
# when i send the "switch" event... | ||
send_event("switch") | ||
|
||
# then | ||
# the transition from a1 to a2 should be taken, consuming the event | ||
assert_active(a) | ||
assert_active(a2) | ||
assert_inactive(a1) | ||
assert_inactive(b) | ||
|
||
# the transition t2 should have emitted a "taken" signal | ||
assert_signal_emitted(t2, "taken") | ||
|
||
# the transition t1 should not have emitted a "taken" signal | ||
assert_signal_not_emitted(t1, "taken") |
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,19 @@ | ||
extends StateChartTestBase | ||
|
||
# Tests that a history state used as an initial state will work. | ||
func test_history_state_as_initial_state(): | ||
var root := compound_state("root") | ||
|
||
var a := compound_state("a", root) | ||
atomic_state("a1", a) | ||
var a2 := atomic_state("a2", a) | ||
var h := history_state("h", a, a2) | ||
|
||
# overwrite the initial state of a to be h | ||
a.initial_state = a.get_path_to(h) | ||
|
||
await finish_setup() | ||
|
||
# now a2 should be active, because h was initial state and the default | ||
# state of h is a2 | ||
assert_active(a2) |
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,47 @@ | ||
extends StateChartTestBase | ||
|
||
# This tests a condition state where the target states are immediate children of the source state | ||
# but the source state holds all the transitions. | ||
func test_immediate_condition_state(): | ||
var root := compound_state("root") | ||
var a := atomic_state("a", root) | ||
var b := atomic_state("b", root) | ||
var c := atomic_state("c", root) | ||
|
||
|
||
transition(root, a, "", "0", expression_guard("state == 'a'")) | ||
transition(root, b, "", "0", expression_guard("state == 'b'")) | ||
transition(root, c, "", "0", expression_guard("state == 'c'")) | ||
|
||
set_initial_expression_properties({"state": "a"}) | ||
|
||
await finish_setup() | ||
|
||
# root state is active | ||
assert_active(a) | ||
assert_inactive(b) | ||
assert_inactive(c) | ||
|
||
# WHEN: i change the property to enter state b | ||
set_expression_property("state", "b") | ||
|
||
# THEN: state b is active | ||
assert_active(b) | ||
assert_inactive(a) | ||
assert_inactive(c) | ||
|
||
# WHEN: i change the property to enter state c | ||
set_expression_property("state", "c") | ||
|
||
# THEN: state c is active | ||
assert_active(c) | ||
assert_inactive(a) | ||
assert_inactive(b) | ||
|
||
# WHEN: i change the property to enter state a | ||
set_expression_property("state", "a") | ||
|
||
# THEN: state a is active | ||
assert_active(a) | ||
assert_inactive(b) | ||
assert_inactive(c) |
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,30 @@ | ||
extends StateChartTestBase | ||
|
||
func test_parallel_state_event_handling(): | ||
var root := parallel_state("root") | ||
|
||
var a := compound_state("a", root) | ||
var a1 := atomic_state("a1", a) | ||
var a2 := atomic_state("a2", a) | ||
|
||
var b := compound_state("b", root) | ||
var b1 := atomic_state("b1", b) | ||
var b2 := atomic_state("b2", b) | ||
|
||
transition(a1, a2, "to_a2") | ||
transition( b1, b2, "", "0", state_is_active_guard(a2)) | ||
|
||
await finish_setup() | ||
|
||
assert_active(a1) | ||
assert_active(b1) | ||
|
||
# when i send an event to switch to a2, b1 should automatically switch to b2 | ||
send_event("to_a2") | ||
|
||
assert_active(a2) | ||
# this only works because b2 is a child of b which is a parallel state, so b's transition | ||
# will be triggered even if a1 consumes the event | ||
assert_active(b2) | ||
|
||
|
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,36 @@ | ||
extends StateChartTestBase | ||
|
||
# This tests that property changes are immediately visible to all guards. | ||
# see https://github.com/derkork/godot-statecharts/issues/82#issuecomment-1963417766 | ||
func test_property_changes_immediately_visible(): | ||
var root := compound_state("root") | ||
var a := atomic_state("a", root) | ||
var b := atomic_state("b", root) | ||
var c := atomic_state("c", root) | ||
|
||
var d := atomic_state("d", root) | ||
var e := atomic_state("e", root) | ||
|
||
transition(a, b, "to_b") | ||
transition(b, c) | ||
transition(c, d, "", "0", expression_guard("x > 0")) | ||
transition(c, e, "", "0", expression_guard("x = 0")) | ||
|
||
|
||
a.state_entered.connect(func(): set_expression_property("x", 0)) | ||
b.state_entered.connect(func(): set_expression_property("x", 1)) | ||
await finish_setup() | ||
|
||
# root state is active | ||
assert_active(a) | ||
|
||
# when I transition to b | ||
send_event("to_b") | ||
|
||
# then b is entered, which sets x to 1 | ||
# then c is entered and because x is 1, d is entered | ||
assert_active(d) | ||
assert_inactive(e) | ||
assert_inactive(a) | ||
assert_inactive(b) | ||
assert_inactive(c) |