forked from favadi/protoc-go-inject-tag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
181 lines (162 loc) · 4.82 KB
/
main_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bytes"
"io/ioutil"
"log"
"os"
"regexp"
"testing"
)
var (
testInputFile = "./pb/test.pb.go"
testInputFileTemp = "./pb/test.pb.go_tmp"
)
func TestTagFromComment(t *testing.T) {
var tests = []struct {
comment string
tag string
}{
{comment: `//@inject_tag: valid:"abc"`, tag: `valid:"abc"`},
{comment: `// @inject_tag: valid:"abcd"`, tag: `valid:"abcd"`},
{comment: `// @inject_tag: valid:"xyz"`, tag: `valid:"xyz"`},
{comment: `// fdsafsa`, tag: ""},
{comment: `//@inject_tag:`, tag: ""},
{comment: `// @inject_tag: json:"abc" yaml:"abc`, tag: `json:"abc" yaml:"abc`},
}
for _, test := range tests {
result := tagFromComment(test.comment)
if result != test.tag {
t.Errorf("expected tag: %q, got: %q", test.tag, result)
}
}
}
func TestParseWriteFile(t *testing.T) {
expectedTag := `valid:"ip" yaml:"ip" json:"overrided"`
areas, err := parseFile(testInputFile, []string{})
if err != nil {
t.Fatal(err)
}
if len(areas) != 3 {
t.Fatalf("expected 3 area to replace, got: %d", len(areas))
}
area := areas[0]
t.Logf("area: %v", area)
if area.InjectTag != expectedTag {
t.Errorf("expected tag: %q, got: %q", expectedTag, area.InjectTag)
}
// make a copy of test file
contents, err := ioutil.ReadFile(testInputFile)
if err != nil {
t.Fatal(err)
}
if err = ioutil.WriteFile(testInputFileTemp, contents, 0644); err != nil {
t.Fatal(err)
}
defer os.Remove(testInputFileTemp)
if err = writeFile(testInputFileTemp, areas); err != nil {
t.Fatal(err)
}
// check if file contains custom tag
contents, err = ioutil.ReadFile(testInputFileTemp)
if err != nil {
t.Fatal(err)
}
expectedExpr := "Address[ \t]+string[ \t]+`protobuf:\"bytes,1,opt,name=Address,proto3\" json:\"overrided\" valid:\"ip\" yaml:\"ip\"`"
matched, err := regexp.Match(expectedExpr, contents)
if err != nil || matched != true {
t.Error("file doesn't contains custom tag after writing")
t.Log(string(contents))
}
}
func TestNewTagItems(t *testing.T) {
var tests = []struct {
tag string
items tagItems
}{
{
tag: `valid:"ip" yaml:"ip, required" json:"overrided"`,
items: []tagItem{
{key: "valid", value: `"ip"`},
{key: "yaml", value: `"ip, required"`},
{key: "json", value: `"overrided"`},
},
},
{
tag: `validate:"omitempty,oneof=a b c d"`,
items: []tagItem{
{key: "validate", value: `"omitempty,oneof=a b c d"`},
},
},
}
for _, test := range tests {
for i, item := range newTagItems(test.tag) {
if item.key != test.items[i].key || item.value != test.items[i].value {
t.Errorf("wrong tag item for tag %s, expected %v, got: %v",
test.tag, test.items[i], item)
}
}
}
}
func TestContinueParsingWhenSkippingFields(t *testing.T) {
expectedTags := []string{`valid:"ip" yaml:"ip" json:"overrided"`, `xml:"-"`, `xml:"-"`, `xml:"-"`, `valid:"http|https"`, `valid:"nonzero"`, `xml:"-"`, `xml:"-"`, `xml:"-"`}
areas, err := parseFile(testInputFile, []string{"xml"})
if err != nil {
t.Fatal(err)
}
if len(areas) != 9 {
t.Fatalf("expected 3 areas to replace, got: %d", len(areas))
}
for i, a := range areas {
if a.InjectTag != expectedTags[i] {
t.Errorf("expected tag: %q, got: %q", expectedTags[i], a.InjectTag)
}
}
// make a copy of test file
contents, err := ioutil.ReadFile(testInputFile)
if err != nil {
t.Fatal(err)
}
if err = ioutil.WriteFile(testInputFileTemp, contents, 0644); err != nil {
t.Fatal(err)
}
defer os.Remove(testInputFileTemp)
if err = writeFile(testInputFileTemp, areas); err != nil {
t.Fatal(err)
}
// check if file contains 3 three custom tags
contents, err = ioutil.ReadFile(testInputFileTemp)
if err != nil {
t.Fatal(err)
}
expectedExprs := []string{
"Address[ \t]+string[ \t]+`protobuf:\"bytes,1,opt,name=Address,proto3\" json:\"overrided\" valid:\"ip\" yaml:\"ip\"`",
"Address[ \t]+string[ \t]+`protobuf:\"bytes,1,opt,name=Address,proto3\" json:\"overrided\" valid:\"ip\" yaml:\"ip\"`",
"Scheme[ \t]+string[ \t]+`protobuf:\"bytes,1,opt,name=scheme,proto3\" json:\"scheme,omitempty\" valid:\"http|https\"`",
"Port[ \t]+int32[ \t]+`protobuf:\"varint,3,opt,name=port,proto3\" json:\"port,omitempty\" valid:\"nonzero\"`",
"XXX_NoUnkeyedLiteral[ \t]+struct{}[ \t]+`json:\"-\" xml:\"-\"`",
"XXX_unrecognized[ \t]+[]byte[ \t]+`json:\"-\" xml:\"-\"`",
"XXX_sizecache[ \t]+int32[ \t]+`json:\"-\" xml:\"-\"`",
}
for i, expr := range expectedExprs {
matched, err := regexp.Match(expr, contents)
if err != nil || matched != true {
t.Errorf("file doesn't contains custom tag #%d after writing", i+1)
t.Log(string(contents))
break
}
}
}
func TestVerbose(t *testing.T) {
b := new(bytes.Buffer)
log.SetOutput(b)
verbose = false
logf("test")
if len(b.Bytes()) > 0 {
t.Errorf("verbose should be off")
}
verbose = true
logf("test")
if len(b.Bytes()) == 0 {
t.Errorf("verbose should be on")
}
}