forked from lyft/protoc-gen-star
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow_test.go
100 lines (73 loc) · 2.12 KB
/
workflow_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
98
99
100
package pgs
import (
"bytes"
"io/ioutil"
"testing"
"github.com/golang/protobuf/proto"
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/stretchr/testify/assert"
)
func TestStandardWorkflow_Init(t *testing.T) {
t.Parallel()
req := &plugin_go.CodeGeneratorRequest{FileToGenerate: []string{"foo"}}
b, err := proto.Marshal(req)
assert.NoError(t, err)
mutated := false
g := Init(ProtocInput(bytes.NewReader(b)), MutateParams(func(p Parameters) { mutated = true }))
g.workflow.Init(g)
assert.True(t, mutated)
t.Run("bidi", func(t *testing.T) {
mutated = false
g = Init(ProtocInput(bytes.NewReader(b)), BiDirectional(), MutateParams(func(p Parameters) { mutated = true }))
g.workflow.Init(g)
assert.True(t, mutated)
})
}
func TestStandardWorkflow_Run(t *testing.T) {
t.Parallel()
g := Init()
g.workflow = &standardWorkflow{Generator: g}
g.params = Parameters{}
m := newMockModule()
m.name = "foo"
g.RegisterModule(m)
g.workflow.Run(&graph{})
assert.True(t, m.executed)
}
func TestStandardWorkflow_Persist(t *testing.T) {
t.Parallel()
g := Init(ProtocOutput(ioutil.Discard))
g.workflow = &standardWorkflow{Generator: g}
g.persister = dummyPersister(g.Debugger)
assert.NotPanics(t, func() { g.workflow.Persist(nil) })
}
func TestOnceWorkflow(t *testing.T) {
t.Parallel()
d := &dummyWorkflow{
AST: &graph{},
Artifacts: []Artifact{&CustomFile{}},
}
wf := &onceWorkflow{workflow: d}
ast := wf.Init(nil)
arts := wf.Run(ast)
wf.Persist(arts)
assert.True(t, d.initted)
assert.True(t, d.run)
assert.True(t, d.persisted)
d = &dummyWorkflow{}
wf.workflow = d
assert.Equal(t, ast, wf.Init(nil))
assert.Equal(t, arts, wf.Run(ast))
wf.Persist(arts)
assert.False(t, d.initted)
assert.False(t, d.run)
assert.False(t, d.persisted)
}
type dummyWorkflow struct {
AST AST
Artifacts []Artifact
initted, run, persisted bool
}
func (wf *dummyWorkflow) Init(g *Generator) AST { wf.initted = true; return wf.AST }
func (wf *dummyWorkflow) Run(ast AST) []Artifact { wf.run = true; return wf.Artifacts }
func (wf *dummyWorkflow) Persist(arts []Artifact) { wf.persisted = true }