1
1
package gengateway
2
2
3
3
import (
4
+ "path/filepath"
4
5
"strings"
5
6
"testing"
6
7
@@ -9,7 +10,16 @@ import (
9
10
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
10
11
)
11
12
12
- func TestGenerateServiceWithoutBindings (t * testing.T ) {
13
+ func newExampleFileDescriptor () * descriptor.File {
14
+ return newExampleFileDescriptorWithGoPkg (
15
+ & descriptor.GoPackage {
16
+ Path : "example.com/path/to/example/example.pb" ,
17
+ Name : "example_pb" ,
18
+ },
19
+ )
20
+ }
21
+
22
+ func newExampleFileDescriptorWithGoPkg (gp * descriptor.GoPackage ) * descriptor.File {
13
23
msgdesc := & protodescriptor.DescriptorProto {
14
24
Name : proto .String ("ExampleMessage" ),
15
25
}
@@ -39,18 +49,15 @@ func TestGenerateServiceWithoutBindings(t *testing.T) {
39
49
Name : proto .String ("ExampleService" ),
40
50
Method : []* protodescriptor.MethodDescriptorProto {meth , meth1 },
41
51
}
42
- file := descriptor.File {
52
+ return & descriptor.File {
43
53
FileDescriptorProto : & protodescriptor.FileDescriptorProto {
44
54
Name : proto .String ("example.proto" ),
45
55
Package : proto .String ("example" ),
46
56
Dependency : []string {"a.example/b/c.proto" , "a.example/d/e.proto" },
47
57
MessageType : []* protodescriptor.DescriptorProto {msgdesc },
48
58
Service : []* protodescriptor.ServiceDescriptorProto {svc },
49
59
},
50
- GoPkg : descriptor.GoPackage {
51
- Path : "example.com/path/to/example/example.pb" ,
52
- Name : "example_pb" ,
53
- },
60
+ GoPkg : * gp ,
54
61
Messages : []* descriptor.Message {msg },
55
62
Services : []* descriptor.Service {
56
63
{
@@ -76,8 +83,12 @@ func TestGenerateServiceWithoutBindings(t *testing.T) {
76
83
},
77
84
},
78
85
}
86
+ }
87
+
88
+ func TestGenerateServiceWithoutBindings (t * testing.T ) {
89
+ file := newExampleFileDescriptor ()
79
90
g := & generator {}
80
- got , err := g .generate (crossLinkFixture (& file ))
91
+ got , err := g .generate (crossLinkFixture (file ))
81
92
if err != nil {
82
93
t .Errorf ("generate(%#v) failed with %v; want success" , file , err )
83
94
return
@@ -86,3 +97,57 @@ func TestGenerateServiceWithoutBindings(t *testing.T) {
86
97
t .Errorf ("generate(%#v) = %s; does not want to contain %s" , file , got , notwanted )
87
98
}
88
99
}
100
+
101
+ func TestGenerateOutputPath (t * testing.T ) {
102
+ cases := []struct {
103
+ file * descriptor.File
104
+ expected string
105
+ }{
106
+ {
107
+ file : newExampleFileDescriptorWithGoPkg (
108
+ & descriptor.GoPackage {
109
+ Path : "example.com/path/to/example" ,
110
+ Name : "example_pb" ,
111
+ },
112
+ ),
113
+ expected : "example.com/path/to/example" ,
114
+ },
115
+ {
116
+ file : newExampleFileDescriptorWithGoPkg (
117
+ & descriptor.GoPackage {
118
+ Path : "example" ,
119
+ Name : "example_pb" ,
120
+ },
121
+ ),
122
+ expected : "example" ,
123
+ },
124
+ }
125
+
126
+ g := & generator {}
127
+ for _ , c := range cases {
128
+ file := c .file
129
+ gots , err := g .Generate ([]* descriptor.File {crossLinkFixture (file )})
130
+ if err != nil {
131
+ t .Errorf ("Generate(%#v) failed with %v; wants success" , file , err )
132
+ return
133
+ }
134
+
135
+ if len (gots ) != 1 {
136
+ t .Errorf ("Generate(%#v) failed; expects on result got %d" , file , len (gots ))
137
+ return
138
+ }
139
+
140
+ got := gots [0 ]
141
+ if got .Name == nil {
142
+ t .Errorf ("Generate(%#v) failed; expects non-nil Name(%v)" , file , got .Name )
143
+ return
144
+ }
145
+
146
+ gotPath := filepath .Dir (* got .Name )
147
+ expectedPath := c .expected
148
+ if gotPath != expectedPath {
149
+ t .Errorf ("Generate(%#v) failed; got path: %s expected path: %s" , file , gotPath , expectedPath )
150
+ return
151
+ }
152
+ }
153
+ }
0 commit comments