forked from opendoor-labs/protoc-gen-graphql
-
Notifications
You must be signed in to change notification settings - Fork 3
/
generator_test.go
90 lines (73 loc) · 2.11 KB
/
generator_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
package main_test
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func runProtoc(protoFiles []string, parameter string) error {
args := append([]string{
"-I", "testdata",
"-I", "protobuf",
"--plugin=bin/protoc-gen-graphql",
fmt.Sprintf("--graphql_out=%s:testdata", parameter),
}, protoFiles...)
cmd := exec.Command("protoc", args...)
cmd.Stderr = os.Stderr
return cmd.Run()
}
func itGeneratesTheCorrectOutput(t *testing.T, name, parameter string) {
protoFiles, err := filepath.Glob(filepath.Join("testdata", name, "*.proto"))
if err != nil {
t.Error(err)
}
if err := runProtoc(protoFiles, parameter); err != nil {
t.Error(err)
}
for _, proto := range protoFiles {
graphql, err := ioutil.ReadFile(strings.TrimSuffix(proto, ".proto") + "_pb.graphql")
if err != nil {
t.Error(err)
}
expected, err := ioutil.ReadFile(strings.TrimSuffix(proto, ".proto") + ".golden")
if err != nil {
t.Error(err)
}
if string(graphql) != string(expected) {
t.Errorf("expected %s to equal %s", graphql, expected)
}
}
}
func TestBasicProtobufTypes(t *testing.T) {
itGeneratesTheCorrectOutput(t, "basic", "")
}
func TestMessagesWithCycles(t *testing.T) {
itGeneratesTheCorrectOutput(t, "cycle", "")
}
func TestInputTypesForGrpcServices(t *testing.T) {
itGeneratesTheCorrectOutput(t, "grpc", "")
}
func TestWrappersParameter(t *testing.T) {
itGeneratesTheCorrectOutput(t, "wrappers", "null_wrappers,input_mode=all")
}
func TestProtobufExtensions(t *testing.T) {
itGeneratesTheCorrectOutput(t, "extensions", "root_type_prefix,input_mode=all")
}
func TestFieldNamePreserve(t *testing.T) {
itGeneratesTheCorrectOutput(t, "field_name_preserve", "field_name=preserve,root_type_prefix=")
}
func TestRootTypePrefix(t *testing.T) {
itGeneratesTheCorrectOutput(t, "root_type_prefix", "root_type_prefix=")
}
func TestProto2(t *testing.T) {
itGeneratesTheCorrectOutput(t, "proto2", "")
}
func TestTrimPrefix(t *testing.T) {
itGeneratesTheCorrectOutput(t, "trim_prefix", "trim_prefix=ProtocGenGraphql")
}
func TestEmptyProto(t *testing.T) {
itGeneratesTheCorrectOutput(t, "empty", "")
}