Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
f99b1c87
Unverified
Commit
f99b1c87
authored
Jul 24, 2021
by
Brian Downs
Committed by
GitHub
Jul 24, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add gotests templates (#3709)
add gotests templates
parent
92005c58
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
124 additions
and
0 deletions
+124
-0
call.tmpl
contrib/gotests_templates/call.tmpl
+1
-0
function.tmpl
contrib/gotests_templates/function.tmpl
+107
-0
header.tmpl
contrib/gotests_templates/header.tmpl
+10
-0
inline.tmpl
contrib/gotests_templates/inline.tmpl
+1
-0
inputs.tmpl
contrib/gotests_templates/inputs.tmpl
+1
-0
message.tmpl
contrib/gotests_templates/message.tmpl
+3
-0
results.tmpl
contrib/gotests_templates/results.tmpl
+1
-0
No files found.
contrib/gotests_templates/call.tmpl
0 → 100644
View file @
f99b1c87
{{define "call"}}{{with .Receiver}}{{if not .IsStruct}}tt.{{end}}{{Receiver .}}.{{end}}{{.Name}}({{range $i, $el := .Parameters}}{{if $i}}, {{end}}{{if not .IsWriter}}tt.args.{{end}}{{Param .}}{{if .Type.IsVariadic}}...{{end}}{{end}}){{end}}
contrib/gotests_templates/function.tmpl
0 → 100644
View file @
f99b1c87
{{define "function"}}
{{- $f := .}}
func {{.TestName}}(t *testing.T) {
{{- with .Receiver}}
{{- if .IsStruct}}
{{- if .Fields}}
type fields struct {
{{- range .Fields}}
{{Field .}} {{.Type}}
{{- end}}
}
{{- end}}
{{- end}}
{{- end}}
{{- if .TestParameters}}
type args struct {
{{- range .TestParameters}}
{{Param .}} {{.Type}}
{{- end}}
}
{{- end}}
tests := []struct {
name string
{{- with .Receiver}}
{{- if and .IsStruct .Fields}}
fields fields
{{- else}}
{{Receiver .}} {{.Type}}
{{- end}}
{{- end}}
{{- if .TestParameters}}
args args
{{- end}}
setup func() error // Optional, delete if unused
teardown func() error // Optional, delete if unused
{{- range .TestResults}}
{{Want .}} {{.Type}}
{{- end}}
{{- if .ReturnsError}}
wantErr bool
{{- end}}
}{
// TODO: Add test cases.
// {
// name: "Example Test",
// args: args {
// },
// {{- range .TestResults}}
// want: {{Want .}} {{.Type}}
// {{- end}}
// setup: func() error { return nil },
// teardown: func() error { return nil },
// },
}
for {{if (or .Subtests (not .IsNaked))}} _, tt := {{end}} range tests {
{{- if .Subtests}}
{{- if .Parallel}}tt := tt{{end}}
t.Run(tt.name, func(t *testing.T) {
{{- if .Parallel}}t.Parallel(){{end}}
{{- end}}
{{- with .Receiver}}
{{- if .IsStruct}}
{{Receiver .}} := {{if .Type.IsStar}}&{{end}}{{.Type.Value}}{
{{- range .Fields}}
{{.Name}}: tt.fields.{{Field .}},
{{- end}}
}
{{- end}}
{{- end}}
{{- range .Parameters}}
{{- if .IsWriter}}
{{Param .}} := &bytes.Buffer{}
{{- end}}
{{- end}}
defer tt.teardown()
if err := tt.setup(); err != nil {
t.Errorf("Setup for {{template "message" $f}} failed = %v", err)
return
}
{{- if and (not .OnlyReturnsError) (not .OnlyReturnsOneValue) }}
{{template "results" $f}} {{template "call" $f}}
{{- end}}
{{- if .ReturnsError}}
if {{if .OnlyReturnsError}} err := {{template "call" $f}}; {{end}} (err != nil) != tt.wantErr {
t.Errorf("{{template "message" $f}} error = %v, wantErr %v", {{template "inputs" $f}} err, tt.wantErr)
{{- if .TestResults}}
{{if .Subtests }}return{{else}}continue{{end}}
{{- end}}
}
{{- end}}
{{- range .TestResults}}
{{- if .IsWriter}}
if {{Got .}} := {{Param .}}.String(); {{Got .}} != tt.{{Want .}} {
{{- else if .IsBasicType}}
if {{if $f.OnlyReturnsOneValue}}{{Got .}} := {{template "inline" $f}}; {{end}} {{Got .}} != tt.{{Want .}} {
{{- else}}
if {{if $f.OnlyReturnsOneValue}}{{Got .}} := {{template "inline" $f}}; {{end}} !reflect.DeepEqual({{Got .}}, tt.{{Want .}}) {
{{- end}}
t.Errorf("{{template "message" $f}} {{if $f.ReturnsMultiple}}{{Got .}} {{end}}= %+v\nWant = %+v", {{template "inputs" $f}} {{Got .}}, tt.{{Want .}})
}
{{- end}}
{{- if .Subtests }} }) {{- end -}}
}
}
{{end}}
contrib/gotests_templates/header.tmpl
0 → 100644
View file @
f99b1c87
{{
define
"header"
}}
{{
range
.
Comments
}}{{.}}
{{
end
-}}
package
{{.
Package
}}
import
(
{{
range
.
Imports
}}{{.
Name
}}
{{.
Path
}}
{{
end
}}
)
{{
end
}}
contrib/gotests_templates/inline.tmpl
0 → 100644
View file @
f99b1c87
{{define "inline"}} {{template "call" .}} {{end}}
contrib/gotests_templates/inputs.tmpl
0 → 100644
View file @
f99b1c87
{{define "inputs"}}{{$f := .}}{{if not .Subtests}}tt.name, {{end}}{{if $f.PrintInputs}}{{range $f.Parameters}}tt.args.{{Param .}}, {{end}}{{end}}{{end}}
contrib/gotests_templates/message.tmpl
0 → 100644
View file @
f99b1c87
{{define "message" -}}
{{if not .Subtests}}%q. {{end}}{{with .Receiver}}{{.Type.Value}}.{{end}}{{.Name}}({{if .PrintInputs}}{{range $i, $el := .Parameters}}{{if $i}}, {{end}}%v{{end}}{{end}})
{{- end}}
contrib/gotests_templates/results.tmpl
0 → 100644
View file @
f99b1c87
{{define "results"}} {{range $i, $el := .Results}}{{if $i}}, {{end}}{{Got .}}{{end}}{{if .ReturnsError}}, err{{end}} {{if or .Results .ReturnsError}} := {{end}} {{end}}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment