mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
datatypes: fix fsm.set_state() and cleanup fsm module (#16539)
This commit is contained in:
@ -28,11 +28,12 @@ pub fn new() StateMachine {
|
|||||||
return StateMachine{}
|
return StateMachine{}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut s StateMachine) set_state(name string) ? {
|
pub fn (mut s StateMachine) set_state(name string) ! {
|
||||||
if name in s.states {
|
if name in s.states {
|
||||||
s.current_state = name
|
s.current_state = name
|
||||||
|
} else {
|
||||||
|
return error('unknown state: ${name}')
|
||||||
}
|
}
|
||||||
return error('unknown state: ${name}')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut s StateMachine) get_state() string {
|
pub fn (mut s StateMachine) get_state() string {
|
||||||
@ -62,7 +63,7 @@ pub fn (mut s StateMachine) add_transition(from string, to string, condition_han
|
|||||||
s.transitions[from] = [t]
|
s.transitions[from] = [t]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut s StateMachine) run(receiver voidptr) ? {
|
pub fn (mut s StateMachine) run(receiver voidptr) ! {
|
||||||
from_state := s.current_state
|
from_state := s.current_state
|
||||||
mut to_state := s.current_state
|
mut to_state := s.current_state
|
||||||
if transitions := s.transitions[s.current_state] {
|
if transitions := s.transitions[s.current_state] {
|
||||||
|
@ -14,29 +14,29 @@ fn default_setup() (MyReceiver, fsm.StateMachine) {
|
|||||||
return receiver, s
|
return receiver, s
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_statemachine_number_of_callbacks_correct_when_single_transition() ? {
|
fn test_statemachine_number_of_callbacks_correct_when_single_transition() {
|
||||||
mut receiver, mut s := default_setup()
|
mut receiver, mut s := default_setup()
|
||||||
|
|
||||||
s.run(receiver)?
|
s.run(receiver)!
|
||||||
|
|
||||||
assert receiver.data.len == 3
|
assert receiver.data.len == 3
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_statemachine_sequence_works_when_typical() ? {
|
fn test_statemachine_sequence_works_when_typical() {
|
||||||
mut receiver, mut s := default_setup()
|
mut receiver, mut s := default_setup()
|
||||||
|
|
||||||
s.run(receiver)?
|
s.run(receiver)!
|
||||||
|
|
||||||
assert receiver.data[0] == 'on_state_exit: A -> B'
|
assert receiver.data[0] == 'on_state_exit: A -> B'
|
||||||
assert receiver.data[1] == 'on_state_entry: A -> B'
|
assert receiver.data[1] == 'on_state_entry: A -> B'
|
||||||
assert receiver.data[2] == 'on_state_run: A -> B'
|
assert receiver.data[2] == 'on_state_run: A -> B'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_statemachine_works_when_final_state() ? {
|
fn test_statemachine_works_when_final_state() {
|
||||||
mut receiver, mut s := default_setup()
|
mut receiver, mut s := default_setup()
|
||||||
|
|
||||||
// current state `A`, with a possible transition to `B`:
|
// current state `A`, with a possible transition to `B`:
|
||||||
s.run(receiver)? // run should not error here
|
s.run(receiver)! // run should not error here
|
||||||
|
|
||||||
// Note: run will now return error, because for state `B`,
|
// Note: run will now return error, because for state `B`,
|
||||||
// there are no more transitions:
|
// there are no more transitions:
|
||||||
@ -49,7 +49,7 @@ fn test_statemachine_works_when_final_state() ? {
|
|||||||
assert receiver.data[4] == 'on_state_run: B -> B'
|
assert receiver.data[4] == 'on_state_run: B -> B'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_simple_loop() ? {
|
fn test_simple_loop() {
|
||||||
mut receiver, mut s := default_setup()
|
mut receiver, mut s := default_setup()
|
||||||
|
|
||||||
// Add a transition back to `A` too:
|
// Add a transition back to `A` too:
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import flag
|
import flag
|
||||||
|
|
||||||
pub fn read_file(file string) ?[]string {
|
pub fn read_file(file string) ![]string {
|
||||||
if os.is_file(file) {
|
if os.is_file(file) {
|
||||||
text := os.read_lines(file) or {
|
text := os.read_lines(file) or {
|
||||||
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
||||||
@ -33,7 +33,7 @@ pub fn get_transitions(line string) ?string {
|
|||||||
pub fn main() {
|
pub fn main() {
|
||||||
mut fp := flag.new_flag_parser(os.args)
|
mut fp := flag.new_flag_parser(os.args)
|
||||||
file := fp.string('file', `f`, '', 'input V file with transitions to generate graph from.')
|
file := fp.string('file', `f`, '', 'input V file with transitions to generate graph from.')
|
||||||
lines := read_file(file)?
|
lines := read_file(file)!
|
||||||
println('digraph fsm {')
|
println('digraph fsm {')
|
||||||
for line in lines {
|
for line in lines {
|
||||||
if line.contains('add_transition') {
|
if line.contains('add_transition') {
|
||||||
|
Reference in New Issue
Block a user