-
Notifications
You must be signed in to change notification settings - Fork 464
/
plugin_test.go
152 lines (124 loc) · 4.6 KB
/
plugin_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
package gendoc_test
import (
"regexp"
"testing"
"github.com/golang/protobuf/proto"
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
. "github.com/pseudomuto/protoc-gen-doc"
"github.com/pseudomuto/protokit/utils"
"github.com/stretchr/testify/require"
)
func TestParseOptionsForBuiltinTemplates(t *testing.T) {
results := map[string]string{
"docbook": "output.xml",
"html": "output.html",
"json": "output.json",
"markdown": "output.md",
}
for kind, file := range results {
req := new(plugin_go.CodeGeneratorRequest)
req.Parameter = proto.String(kind + "," + file)
options, err := ParseOptions(req)
require.NoError(t, err)
renderType, err := NewRenderType(kind)
require.NoError(t, err)
require.Equal(t, renderType, options.Type)
require.Equal(t, file, options.OutputFile)
require.Empty(t, options.TemplateFile)
}
}
func TestParseOptionsForSourceRelative(t *testing.T) {
req := new(plugin_go.CodeGeneratorRequest)
req.Parameter = proto.String("markdown,index.md,source_relative")
options, err := ParseOptions(req)
require.NoError(t, err)
require.Equal(t, options.SourceRelative, true)
req.Parameter = proto.String("markdown,index.md,default")
options, err = ParseOptions(req)
require.NoError(t, err)
require.Equal(t, options.SourceRelative, false)
req.Parameter = proto.String("markdown,index.md")
options, err = ParseOptions(req)
require.NoError(t, err)
require.Equal(t, options.SourceRelative, false)
}
func TestParseOptionsForCustomTemplate(t *testing.T) {
req := new(plugin_go.CodeGeneratorRequest)
req.Parameter = proto.String("/path/to/template.tmpl,/base/name/only/output.md")
options, err := ParseOptions(req)
require.NoError(t, err)
require.Equal(t, RenderTypeHTML, options.Type)
require.Equal(t, "/path/to/template.tmpl", options.TemplateFile)
require.Equal(t, "output.md", options.OutputFile)
}
func TestParseOptionsForExcludePatterns(t *testing.T) {
req := new(plugin_go.CodeGeneratorRequest)
req.Parameter = proto.String(":google/*,notgoogle/*")
options, err := ParseOptions(req)
require.NoError(t, err)
require.Len(t, options.ExcludePatterns, 2)
pattern0, _ := regexp.Compile("google/*")
pattern1, _ := regexp.Compile("notgoogle/*")
require.Equal(t, pattern0.String(), options.ExcludePatterns[0].String())
require.Equal(t, pattern1.String(), options.ExcludePatterns[1].String())
}
func TestParseOptionsWithInvalidValues(t *testing.T) {
badValues := []string{
"markdown",
"html",
"/some/path.tmpl",
"more,than,1,comma",
"markdown,index.md,unknown",
}
for _, value := range badValues {
req := new(plugin_go.CodeGeneratorRequest)
req.Parameter = proto.String(value)
_, err := ParseOptions(req)
require.Error(t, err)
}
}
func TestRunPluginForBuiltinTemplate(t *testing.T) {
set, _ := utils.LoadDescriptorSet("fixtures", "fileset.pb")
req := utils.CreateGenRequest(set, "Booking.proto", "Vehicle.proto", "nested/Book.proto")
req.Parameter = proto.String("markdown,/base/name/only/output.md")
plugin := new(Plugin)
resp, err := plugin.Generate(req)
require.NoError(t, err)
require.Len(t, resp.File, 1)
require.Equal(t, "output.md", resp.File[0].GetName())
require.NotEmpty(t, resp.File[0].GetContent())
}
func TestRunPluginForCustomTemplate(t *testing.T) {
set, _ := utils.LoadDescriptorSet("fixtures", "fileset.pb")
req := utils.CreateGenRequest(set, "Booking.proto", "Vehicle.proto", "nested/Book.proto")
req.Parameter = proto.String("resources/html.tmpl,/base/name/only/output.html")
plugin := new(Plugin)
resp, err := plugin.Generate(req)
require.NoError(t, err)
require.Len(t, resp.File, 1)
require.Equal(t, "output.html", resp.File[0].GetName())
require.NotEmpty(t, resp.File[0].GetContent())
}
func TestRunPluginWithInvalidOptions(t *testing.T) {
req := new(plugin_go.CodeGeneratorRequest)
req.Parameter = proto.String("html")
plugin := new(Plugin)
_, err := plugin.Generate(req)
require.Error(t, err)
}
func TestRunPluginForSourceRelative(t *testing.T) {
set, _ := utils.LoadDescriptorSet("fixtures", "fileset.pb")
req := utils.CreateGenRequest(set, "Booking.proto", "Vehicle.proto", "nested/Book.proto")
req.Parameter = proto.String("markdown,index.md,source_relative")
plugin := new(Plugin)
resp, err := plugin.Generate(req)
require.NoError(t, err)
require.Len(t, resp.File, 2)
expected := map[string]int{"index.md": 1, "nested/index.md": 1}
require.Contains(t, expected, resp.File[0].GetName())
delete(expected, resp.File[0].GetName())
require.Contains(t, expected, resp.File[1].GetName())
delete(expected, resp.File[1].GetName())
require.NotEmpty(t, resp.File[0].GetContent())
require.NotEmpty(t, resp.File[1].GetContent())
}