This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmachine_test.go
97 lines (74 loc) · 2.21 KB
/
machine_test.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package machine
import (
"encoding/json"
"io/ioutil"
"testing"
"github.com/coinbase/step/utils/to"
"github.com/stretchr/testify/assert"
)
func loadFixture(file string, t *testing.T) *StateMachine {
example_machine, err := ParseFile(file)
assert.NoError(t, err)
return example_machine
}
func execute(json []byte, input interface{}, t *testing.T) (map[string]interface{}, error) {
example_machine, err := FromJSON(json)
assert.NoError(t, err)
example_machine.SetDefaultHandler()
exec, err := example_machine.Execute(input)
return exec.Output, err
}
func executeFixture(file string, input map[string]interface{}, t *testing.T) map[string]interface{} {
example_machine := loadFixture(file, t)
exec, err := example_machine.Execute(input)
assert.NoError(t, err)
return exec.Output
}
//////
// TESTS
//////
func Test_Machine_EmptyStateMachinePassExample(t *testing.T) {
_, err := execute([]byte(EmptyStateMachine), make(map[string]interface{}), t)
assert.NoError(t, err)
}
func Test_Machine_SimplePassExample_With_Execute(t *testing.T) {
json := []byte(`
{
"StartAt": "start",
"States": {
"start": {
"Type": "Pass",
"Result": "b",
"ResultPath": "$.a",
"End": true
}
}
}`)
output, err := execute(json, make(map[string]interface{}), t)
assert.NoError(t, err)
assert.Equal(t, output["a"], "b")
output, err = execute(json, "{}", t)
assert.NoError(t, err)
assert.Equal(t, output["a"], "b")
output, err = execute(json, to.Strp("{}"), t)
assert.NoError(t, err)
assert.Equal(t, output["a"], "b")
}
func Test_Machine_ErrorUnknownState(t *testing.T) {
example_machine := loadFixture("../examples/bad_unknown_state.json", t)
_, err := example_machine.Execute(make(map[string]interface{}))
assert.Error(t, err)
assert.Regexp(t, "Unknown State", err.Error())
}
func Test_Machine_MarshallAllTypes(t *testing.T) {
file := "../examples/all_types.json"
sm, err := ParseFile(file)
assert.NoError(t, err)
sm.SetDefaultHandler()
assert.NoError(t, sm.Validate())
marshalled_json, err := json.Marshal(sm)
assert.NoError(t, err)
raw_json, err := ioutil.ReadFile(file)
assert.NoError(t, err)
assert.JSONEq(t, string(raw_json), string(marshalled_json))
}