|
| 1 | +extends VestTest |
| 2 | + |
| 3 | +func get_suite_name() -> String: |
| 4 | + return "RewindableStateMachine" |
| 5 | + |
| 6 | +var state_machine: RewindableStateMachine |
| 7 | +var first_state: RewindableState |
| 8 | +var other_state: RewindableState |
| 9 | + |
| 10 | +func before_case(__): |
| 11 | + state_machine = RewindableStateMachine.new() |
| 12 | + first_state = mock(RewindableState) |
| 13 | + other_state = mock(RewindableState) |
| 14 | + |
| 15 | + # Setup mock answers |
| 16 | + when(first_state.can_enter).then_return(true) |
| 17 | + when(first_state.enter).then_answer(func(__): pass) |
| 18 | + when(first_state.exit).then_answer(func(__): pass) |
| 19 | + when(first_state.tick).then_answer(func(__): pass) |
| 20 | + when(first_state.display_enter).then_answer(func(__): pass) |
| 21 | + when(first_state.display_exit).then_answer(func(__): pass) |
| 22 | + |
| 23 | + when(other_state.can_enter).then_return(true) |
| 24 | + when(other_state.enter).then_answer(func(__): pass) |
| 25 | + when(other_state.exit).then_answer(func(__): pass) |
| 26 | + when(other_state.tick).then_answer(func(__): pass) |
| 27 | + when(other_state.display_enter).then_answer(func(__): pass) |
| 28 | + when(other_state.display_exit).then_answer(func(__): pass) |
| 29 | + |
| 30 | + # Set state names |
| 31 | + first_state.name = "First State" |
| 32 | + other_state.name = "Other State" |
| 33 | + |
| 34 | + # Add states as children - RSM will pick them up when added |
| 35 | + # **NOTE**: Make sure to set owner if spawning states manually, otherwise |
| 36 | + # RSM won't pick them up |
| 37 | + state_machine.add_child(first_state); first_state.owner = state_machine |
| 38 | + state_machine.add_child(other_state); other_state.owner = state_machine |
| 39 | + |
| 40 | + # TODO(vest): TestingSceneTree |
| 41 | + await Vest._scene_tree.process_frame |
| 42 | + Vest._scene_tree.root.add_child(state_machine, true) |
| 43 | + |
| 44 | +func test_should_start_empty(): |
| 45 | + expect_empty(state_machine.state) |
| 46 | + |
| 47 | +func test_should_notify_new_state_on_enter(): |
| 48 | + capture_signal(first_state.on_enter, 3) |
| 49 | + |
| 50 | + # Enter first state |
| 51 | + state_machine.transition("First State") |
| 52 | + |
| 53 | + # Check for event |
| 54 | + expect_not_empty(get_signal_emissions(first_state.on_enter)) |
| 55 | + expect_empty(get_calls_of(first_state.can_enter)) |
| 56 | + expect_equal(get_calls_of(first_state.enter), [[null, 0]]) |
| 57 | + |
| 58 | +func test_on_enter_should_prevent_transition(): |
| 59 | + other_state.on_enter.connect( |
| 60 | + func(_new_state, _tick, prevent): |
| 61 | + prevent.call() |
| 62 | + ) |
| 63 | + |
| 64 | + # Enter first state |
| 65 | + state_machine.transition("First State") |
| 66 | + |
| 67 | + # Try to enter second state |
| 68 | + expect_false(state_machine.transition("Other State"), "Transition should have failed!") |
| 69 | + expect_equal(state_machine.state, "First State") |
| 70 | + |
| 71 | +func test_on_exit_should_prevent_transition(): |
| 72 | + first_state.on_exit.connect( |
| 73 | + func(_new_state, _tick, prevent): |
| 74 | + prevent.call() |
| 75 | + ) |
| 76 | + |
| 77 | + # Enter first state |
| 78 | + state_machine.transition("First State") |
| 79 | + |
| 80 | + # Try to enter second state |
| 81 | + expect_false(state_machine.transition("Other State"), "Transition should have failed!") |
| 82 | + expect_equal(state_machine.state, "First State") |
| 83 | + |
| 84 | +func test_can_enter_should_prevent_transition(): |
| 85 | + state_machine.state = "First State" |
| 86 | + |
| 87 | + # Register a more specific answer so the mock will use that |
| 88 | + when(other_state.can_enter)\ |
| 89 | + .with_args([first_state])\ |
| 90 | + .then_return(func(__): return false) |
| 91 | + |
| 92 | + # Try to enter second state |
| 93 | + expect_false(state_machine.transition("Other State"), "Transition should have failed!") |
| 94 | + expect_equal(state_machine.state, "First State") |
| 95 | + |
| 96 | +func test_should_call_tick(): |
| 97 | + capture_signal(first_state.on_tick, 3) |
| 98 | + |
| 99 | + # Set state |
| 100 | + state_machine.transition("First State") |
| 101 | + |
| 102 | + # Run a rollback tick |
| 103 | + state_machine._rollback_tick(0.16, 0, true) |
| 104 | + |
| 105 | + # Tick should have been called |
| 106 | + expect_equal(get_calls_of(first_state.tick), [[0.16, 0, true]], "Wrong method call!") |
| 107 | + expect_equal(get_signal_emissions(first_state.on_tick), [[0.16, 0, true]], "Wrong signal!") |
| 108 | + |
| 109 | +func test_should_notify_display_state(): |
| 110 | + state_machine.state = "First State" |
| 111 | + |
| 112 | + capture_signal(first_state.on_display_enter, 2) |
| 113 | + capture_signal(first_state.on_display_exit, 2) |
| 114 | + capture_signal(other_state.on_display_enter, 2) |
| 115 | + |
| 116 | + # First loop, display enters First State |
| 117 | + NetworkMocks.in_network_tick_loop(func(): |
| 118 | + state_machine.transition("First State") |
| 119 | + ) |
| 120 | + expect_not_empty(get_signal_emissions(first_state.on_display_enter)) |
| 121 | + expect_not_empty(get_calls_of(first_state.display_enter)) |
| 122 | + |
| 123 | + # Second loop, display enters Other State |
| 124 | + NetworkMocks.in_network_tick_loop(func(): |
| 125 | + state_machine.transition("Other State") |
| 126 | + ) |
| 127 | + |
| 128 | + expect_not_empty(get_signal_emissions(first_state.on_display_exit)) |
| 129 | + expect_not_empty(get_signal_emissions(other_state.on_display_enter)) |
| 130 | + |
| 131 | + expect_not_empty(get_calls_of(first_state.display_exit)) |
| 132 | + expect_not_empty(get_calls_of(other_state.display_enter)) |
| 133 | + |
| 134 | +func after_case(__): |
| 135 | + state_machine.queue_free() |
0 commit comments