-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_hello_world.rs
71 lines (68 loc) · 2.16 KB
/
test_hello_world.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use asl::asl::execution::{
Execution, ExecutionStatus, StateExecutionHandler, StateExecutionOutput,
};
use asl::asl::state_machine::StateMachine;
use asl::asl::types::execution::EmptyContext;
use rstest::rstest;
use serde_json::{json, Value};
use similar_asserts::assert_eq;
struct TaskHandler {}
impl StateExecutionHandler for TaskHandler {
type TaskExecutionError = String;
fn execute_task(
&self,
resource: &str,
input: &Value,
credentials: Option<&Value>,
) -> Result<Option<Value>, Self::TaskExecutionError> {
let result = match resource {
"SayHello" => "Hello",
"SayWorld" => "World",
_ => Err("Unknown resource!")?,
};
println!("{}", result);
Ok(None) // We opt into returning nothing
}
}
#[rstest]
fn execute_hello_world() {
let definition = r#"{
"Comment": "A simple minimal example of the States language",
"StartAt": "State: Hello",
"States": {
"State: Hello": {
"Type": "Task",
"Resource": "SayHello",
"Next": "State: World"
},
"State: World": {
"Type": "Task",
"Resource": "SayWorld",
"End": true
}
}
}"#;
let state_machine = StateMachine::parse(definition).unwrap();
let input = Value::Null;
let execution: Execution<TaskHandler, EmptyContext> =
state_machine.start(&input, TaskHandler {}, EmptyContext {});
// the Execution type implements Iterator, so we can iterate until there are no more states to execute
let steps: Vec<StateExecutionOutput> = execution.collect();
assert_eq!(
steps[0],
StateExecutionOutput {
status: ExecutionStatus::Executing,
state_name: Some("State: Hello".to_string()),
result: Some(json!({})), // returning nothing is the same as returning an empty JSON object
}
);
//
assert_eq!(
steps[1],
StateExecutionOutput {
status: ExecutionStatus::FinishedWithSuccess(Some(json!({}))),
state_name: Some("State: World".to_string()),
result: Some(json!({})),
}
);
}