Skip to content

Commit 3d1a1cc

Browse files
shihanngekhabarov
authored andcommitted
Add unit test for ProcessFile
1 parent 5900a9b commit 3d1a1cc

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

generator/file_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package generator
22

33
import (
44
"bytes"
5+
"io/ioutil"
56
"path/filepath"
67
"runtime"
78
"time"
@@ -124,6 +125,52 @@ var _ = Describe("File", func() {
124125
)
125126
})
126127

128+
Describe("ProcessFile", func() {
129+
Context("when get a header", func() {
130+
var f *descriptor.FileDescriptorProto
131+
132+
BeforeEach(func() {
133+
f = &descriptor.FileDescriptorProto{
134+
Options: &descriptor.FileOptions{},
135+
Name: sp("product.proto"),
136+
Package: sp("pb"),
137+
MessageType: []*descriptor.DescriptorProto{
138+
{
139+
Name: sp("Product"),
140+
Field: []*descriptor.FieldDescriptorProto{
141+
&descriptor.FieldDescriptorProto{
142+
Name: sp("ID"),
143+
Number: nil,
144+
Label: nil,
145+
Type: &typInt64,
146+
TypeName: nil,
147+
Options: &descriptor.FieldOptions{},
148+
},
149+
},
150+
Options: &descriptor.MessageOptions{},
151+
},
152+
},
153+
}
154+
155+
err := proto.SetExtension(f.Options, options.E_GoModelsFilePath, sp("testdata/model.go"))
156+
Expect(err).NotTo(HaveOccurred())
157+
158+
err = proto.SetExtension(f.MessageType[0].Options, options.E_GoStruct, sp("Product"))
159+
Expect(err).NotTo(HaveOccurred())
160+
})
161+
162+
It("returns generated code", func() {
163+
expectedContent, err := ioutil.ReadFile("testdata/processfile.go.golden")
164+
Expect(err).NotTo(HaveOccurred())
165+
166+
absPath, content, err := ProcessFile(f, sp("product"), sp("helper-package"), map[string]MessageOption{}, false, false)
167+
Expect(err).NotTo(HaveOccurred())
168+
Expect(content).To(Equal(string(expectedContent)))
169+
Expect(absPath).To(Equal("product_transformer.go"))
170+
})
171+
})
172+
})
173+
127174
Describe("modelPath", func() {
128175

129176
Context("when there is no option go_models_file_path in file", func() {

generator/testdata/model.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package model
2+
3+
type Product struct {
4+
ID int `db:"id" json:"id"`
5+
}
+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Code generated by protoc-gen-struct-transformer, version: v0.0.1. DO NOT EDIT.
2+
// source file: product.proto
3+
// source package: pb
4+
5+
package product
6+
func PbToProductPtr(src *pb1.Product, opts ...TransformParam) *repo1.Product {
7+
if src == nil {
8+
return nil
9+
}
10+
11+
d := PbToProduct(*src, opts...)
12+
return &d
13+
}
14+
15+
func PbToProductPtrList(src []*pb1.Product, opts ...TransformParam) []*repo1.Product {
16+
resp := make([]*repo1.Product, len(src))
17+
18+
for i, s := range src {
19+
resp[i] = PbToProductPtr(s, opts...)
20+
}
21+
22+
return resp
23+
}
24+
25+
func PbToProductPtrVal(src *pb1.Product, opts ...TransformParam) repo1.Product {
26+
if src == nil {
27+
return repo1.Product{}
28+
}
29+
30+
return PbToProduct(*src, opts...)
31+
}
32+
33+
func PbToProductPtrValList(src []*pb1.Product, opts ...TransformParam) []repo1.Product {
34+
resp := make([]repo1.Product, len(src))
35+
36+
for i, s := range src {
37+
resp[i] = PbToProduct(*s)
38+
}
39+
40+
return resp
41+
}
42+
43+
// PbToProductList is DEPRECATED. Use PbToProductPtrValList instead.
44+
func PbToProductList(src []*pb1.Product, opts ...TransformParam) []repo1.Product {
45+
return PbToProductPtrValList(src)
46+
}
47+
48+
func PbToProduct(src pb1.Product, opts ...TransformParam) repo1.Product {
49+
s := repo1.Product{
50+
ID: int(src.Id ),
51+
}
52+
53+
applyOptions(opts...)
54+
55+
56+
return s
57+
}
58+
59+
func PbToProductValPtr(src pb1.Product, opts ...TransformParam) *repo1.Product {
60+
d := PbToProduct(src, opts...)
61+
return &d
62+
}
63+
64+
func PbToProductValList(src []pb1.Product, opts ...TransformParam) []repo1.Product {
65+
resp := make([]repo1.Product, len(src))
66+
67+
for i, s := range src {
68+
resp[i] = PbToProduct(s, opts...)
69+
}
70+
71+
return resp
72+
}
73+
74+
func ProductToPbPtr(src *repo1.Product, opts ...TransformParam) *pb1.Product {
75+
if src == nil {
76+
return nil
77+
}
78+
79+
d := ProductToPb(*src, opts...)
80+
return &d
81+
}
82+
83+
func ProductToPbPtrList(src []*repo1.Product, opts ...TransformParam) []*pb1.Product {
84+
resp := make([]*pb1.Product, len(src))
85+
86+
for i, s := range src {
87+
resp[i] = ProductToPbPtr(s, opts...)
88+
}
89+
90+
return resp
91+
}
92+
93+
func ProductToPbPtrVal(src *repo1.Product, opts ...TransformParam) pb1.Product {
94+
if src == nil {
95+
return pb1.Product{}
96+
}
97+
98+
return ProductToPb(*src, opts...)
99+
}
100+
101+
func ProductToPbValPtrList(src []repo1.Product, opts ...TransformParam) []*pb1.Product {
102+
resp := make([]*pb1.Product, len(src))
103+
104+
for i, s := range src {
105+
g := ProductToPb(s, opts...)
106+
resp[i] = &g
107+
}
108+
109+
return resp
110+
}
111+
112+
// ProductToPbList is DEPRECATED. Use ProductToPbValPtrList instead.
113+
func ProductToPbList(src []repo1.Product, opts ...TransformParam) []*pb1.Product {
114+
return ProductToPbValPtrList(src)
115+
}
116+
117+
func ProductToPb(src repo1.Product, opts ...TransformParam) pb1.Product {
118+
s := pb1.Product{
119+
Id: int64(src.ID ),
120+
}
121+
122+
applyOptions(opts...)
123+
124+
125+
return s
126+
}
127+
128+
func ProductToPbValPtr(src repo1.Product, opts ...TransformParam) *pb1.Product {
129+
d := ProductToPb(src, opts...)
130+
return &d
131+
}
132+
133+
func ProductToPbValList(src []repo1.Product, opts ...TransformParam) []pb1.Product {
134+
resp := make([]pb1.Product, len(src))
135+
136+
for i, s := range src {
137+
resp[i] = ProductToPb(s, opts...)
138+
}
139+
140+
return resp
141+
}
142+

0 commit comments

Comments
 (0)