This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
/
graphqltest.go
68 lines (56 loc) · 1.75 KB
/
graphqltest.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
package graphql
import (
"fmt"
"github.com/gogo/protobuf/gogoproto"
"github.com/gogo/protobuf/plugin/testgen"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
"github.com/opsee/protobuf/opseeproto"
"strings"
)
type test struct {
*generator.Generator
}
func init() {
testgen.RegisterTestPlugin(NewTest)
}
func NewTest(g *generator.Generator) testgen.TestPlugin {
return &test{g}
}
func (p *test) Generate(imports generator.PluginImports, file *generator.FileDescriptor) bool {
used := false
if opseeproto.GetGraphQLFile(file.FileDescriptorProto) != true {
return used
}
testingPkg := imports.NewImport("testing")
randPkg := imports.NewImport("math/rand")
timePkg := imports.NewImport("time")
ci := 0
for _, message := range file.Messages() {
if message.DescriptorProto.GetOptions().GetMapEntry() {
continue
}
messageGQL := strings.TrimSpace(p.Comments(fmt.Sprintf("4,%d", ci)))
ccTypeName := generator.CamelCaseSlice(message.TypeName())
ci++
if gogoproto.HasTestGen(file.FileDescriptorProto, message.DescriptorProto) {
used = true
p.P(`func Test`, ccTypeName, `GraphQL(t *`, testingPkg.Use(), `.T) {`)
p.In()
p.P(`popr := `, randPkg.Use(), `.New(`, randPkg.Use(), `.NewSource(`, timePkg.Use(), `.Now().UnixNano()))`)
p.P(`_ = NewPopulated`, ccTypeName, `(popr, false)`)
p.P(`objdesc := "`, messageGQL, `"`)
p.P(`pdesc := `, p.graphQLTypeVarName(message), `.PrivateDescription`)
p.P(`if pdesc != objdesc {`)
p.In()
p.P(`t.Fatalf("String want %v got %v", objdesc, pdesc)`)
p.Out()
p.P(`}`)
p.Out()
p.P(`}`)
}
}
return used
}
func (p *test) graphQLTypeVarName(obj generator.Object) string {
return fmt.Sprint(p.DefaultPackageName(obj), "GraphQL", generator.CamelCaseSlice(obj.TypeName()), "Type")
}