forked from pseudomuto/protoc-gen-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_test.go
111 lines (89 loc) · 3.01 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
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/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 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",
}
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) {
req := new(plugin_go.CodeGeneratorRequest)
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) {
req := new(plugin_go.CodeGeneratorRequest)
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)
}