Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create test template for using gotests #327

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
24e4ec5
:white_check_mark: generete missing test file
vankichi Apr 10, 2020
c25e37d
:rewind: remove unnecessary test files
vankichi Apr 13, 2020
8c80e94
:white_check_mark: added missing new test code
vankichi Apr 14, 2020
da8ecba
:wrench: added make gentest for missing test code
vankichi Apr 14, 2020
9130963
:fire: removing files
vankichi Apr 20, 2020
fa6510b
feat: add template file
hlts2 Apr 21, 2020
41db3a1
refactor: adjust space
hlts2 Apr 21, 2020
4807044
refactor: definition position
hlts2 Apr 21, 2020
d980f9e
feat: function test case type
hlts2 Apr 21, 2020
3725095
:white_check_mark: generete missing test file
vankichi Apr 10, 2020
0a3a9f7
:rewind: remove unnecessary test files
vankichi Apr 13, 2020
13de49b
:white_check_mark: added missing new test code
vankichi Apr 14, 2020
f3bbc3d
:fire: removing files
vankichi Apr 20, 2020
4d53b90
feat: add template file
hlts2 Apr 21, 2020
6bde178
refactor: adjust space
hlts2 Apr 21, 2020
4f373b7
refactor: definition position
hlts2 Apr 21, 2020
18d32d2
draft gen-option-test
kevindiu Apr 21, 2020
dd1e3dd
refactor: benchmark script
hlts2 Apr 22, 2020
ebbf0a1
feat: add option
hlts2 Apr 22, 2020
c80f04f
fix: add fill type
hlts2 Apr 22, 2020
e1bcc1f
fix: add message
hlts2 Apr 22, 2020
9179ac3
fix: add message
hlts2 Apr 22, 2020
b787d82
fix: bug params name
hlts2 Apr 22, 2020
0a0ec92
fix: bug of args parameter
hlts2 Apr 22, 2020
c8c8814
fix: commentout test case
hlts2 Apr 22, 2020
6c100d9
fix: test template
hlts2 Apr 23, 2020
d95f55e
:robot: Update license headers and formatting go codes
vdaas-ci Apr 24, 2020
ccd0766
Revert ":robot: Update license headers and formatting go codes"
hlts2 Apr 24, 2020
9b5cbe5
fix: exclude tempalte file
hlts2 Apr 24, 2020
f34dd0b
fix: delete gotests package
hlts2 Apr 24, 2020
14c9162
:robot: Update license headers and formatting go codes
vdaas-ci Apr 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ tensorflow/install: /usr/local/lib/libtensorflow.so
rm -f libtensorflow-cpu-linux-x86_64-$(TENSORFLOW_C_VERSION).tar.gz
ldconfig

.PHONY: gentest
## gentest
gentest:
$(call gen-test)

.PHONY: test
## run tests
test:
Expand Down
24 changes: 24 additions & 0 deletions Makefile.d/functions.mk
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,27 @@ define telepresence
## will be available after telepresence 0.105 released
## --deployment-type "$(SWAP_DEPLOYMENT_TYPE)"
endef

define gen-test
find . -type d | \
grep "./cmd\|./hack\|./internal\|./pkg" | \
grep -v "./cmd/cli\| \
./hack/benchmark/internal/client/ngtd\| \
./hack/benchmark/internal/starter/agent\| \
./hack/benchmark/internal/starter/external\| \
./hack/benchmark/internal/starter/gateway\| \
./hack/license\| \
./hack/swagger\| \
./hack/tools"| \
while read dir; do \
files=`find $${dir} -type f -maxdepth 1 -name "*.go" -not -name '*_test.go' -not -name 'doc.go'`; \
for file in $${files}; do \
path='./assets/test/templates/common'; \
if [[ $${file} =~ .*options?\.go ]] ; then \
path='./assets/test/templates/option'; \
fi; \
echo start generate test code: $${file}; \
gotests -w -template_dir $${path} -all $${file}; \
done; \
done
endef
1 change: 1 addition & 0 deletions assets/test/templates/common/call.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{define "call"}}{{with .Receiver}}{{if not .IsStruct}}test.{{end}}{{Receiver .}}.{{end}}{{.Name}}({{range $i, $el := .Parameters}}{{if $i}}, {{end}}{{if not .IsWriter}}test.args.{{end}}{{Param .}}{{if .Type.IsVariadic}}...{{end}}{{end}}){{end}}
28 changes: 28 additions & 0 deletions assets/test/templates/common/fill.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{define "fill" }}
{{- range . }}
{{ Param . }}: {{ $typ := .Type.String -}}
{{- if eq $typ "int8" -}} 0,
{{- else if eq $typ "uint8" -}} 0,
{{- else if eq $typ "uint16" -}} 0,
{{- else if eq $typ "uint32" -}} 0,
{{- else if eq $typ "uint64" -}} 0,
{{- else if eq $typ "uint" -}} 0,
{{- else if eq $typ "uintptr" -}} 0,
{{- else if eq $typ "int8" -}} 0,
{{- else if eq $typ "int16" -}} 0,
{{- else if eq $typ "int32" -}} 0,
{{- else if eq $typ "int64" -}} 0,
{{- else if eq $typ "int" -}} 0,
{{- else if eq $typ "float32" -}} 0,
{{- else if eq $typ "float64" -}} 0,
{{- else if eq $typ "complex64" -}} 0+0i,
{{- else if eq $typ "complex128" -}} 0+0i,
{{- else if eq $typ "byte" -}} 0,
{{- else if eq $typ "rune" -}} 0,
{{- else if eq $typ "string" -}} "",
{{- else if eq $typ "bool" -}} false,
{{- else if .IsStruct -}} {{.Type.Value}}{},
{{- else -}} nil,
{{- end -}}
{{ end -}}
{{ end }}
154 changes: 154 additions & 0 deletions assets/test/templates/common/function.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{{define "function"}}
{{- $f := . }}

func {{ .TestName }}(t *testing.T) {
{{- if .TestParameters }}
type args struct {
{{- range .TestParameters }}
{{ Param . }} {{ .Type }}
{{- end}}
}
{{- end }}
{{- $hasFields := false -}}
{{- with .Receiver }}
{{- if .IsStruct }}
{{- if .Fields}}
type fields struct { {{ $hasFields = true }}
{{- range .Fields }}
{{ Field . }} {{ .Type }}
{{- end }}
}
{{- end }}
{{- end }}
{{- end }}
type want struct {
{{- range .TestResults }}
{{ Want . }} {{ .Type }}
{{- end }}
{{- if .ReturnsError }}
err error
{{- end }}
}
type test struct {
name string
{{- if .TestParameters }}
args args
{{- end }}
{{- with .Receiver}}
{{- if and .IsStruct .Fields}}
fields fields
{{- else}}
{{Receiver .}} {{.Type}}
{{- end}}
{{- end}}
want want
checkFunc func(want,
{{- range .TestResults }} {{ .Type }}, {{- end }}
{{- if .ReturnsError }} error {{- end }} ) error
beforeFunc func({{- if .TestParameters }} args {{- end}})
afterFunc func({{- if .TestParameters }} args {{- end}})
}
defaultCheckFunc := func(w want,
{{- range .TestResults }} {{ Got . }} {{ .Type }}, {{- end }}
{{- if .ReturnsError }} err error {{- end }} ) error {

{{- if .ReturnsError }}
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
{{- end }}
{{- range .TestResults }} {{ $want := Want . }} {{ $got := Got . }}
if !reflect.DeepEqual({{ $got }}, w.{{ $want }}) {
return errors.Errorf("got = %v, want %v", {{ $got }}, w.{{ $want }})
}
{{- end }}
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
{{- if .TestParameters }}
args: args {
{{- template "fill" .TestParameters }}
},
{{- end }}
{{- if $hasFields }}
fields: fields {
{{- template "fill" .Receiver.Fields }}
},
{{- end }}
want: want{},
checkFunc: defaultCheckFunc,
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
{{- if .TestParameters }}
args: args {
{{- template "fill" .TestParameters }}
},
{{- end }}
{{- if $hasFields }}
fields: fields {
{{- template "fill" .Receiver.Fields }}
},
{{- end }}
want: want{},
checkFunc: defaultCheckFunc,
}
}(),
*/
}

for {{- if (or .Subtests (not .IsNaked)) }} _, test := {{- end }} range tests {
{{- if .Subtests }}
t.Run(test.name, func(tt *testing.T) {
if test.beforeFunc != nil {
test.beforeFunc({{- if .TestParameters }} test.args {{- end }})
}
if test.afterFunc != nil {
defer test.afterFunc({{- if .TestParameters }} test.args {{- end }})
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

{{- with .Receiver }}
{{- if .IsStruct }}
{{ Receiver . }} := {{- if .Type.IsStar }}&{{- end }}{{ .Type.Value }} {
{{- range .Fields }}
{{ Field . }}: test.fields.{{ Field . }},
{{- end }}
}
{{- end }}
{{- end }}

{{- range .Parameters }}
{{- if .IsWriter }}
{{ Param . }} := &bytes.Buffer{}
{{- end }}
{{- end }}

{{ $len := len .Results }}
{{- if or (ge $len 1) (.OnlyReturnsOneValue) (.OnlyReturnsError) }}
{{ template "results" $f }} := {{ template "call" $f }}
if err := test.checkFunc(test.want, {{ template "results" $f }} ); err != nil {
tt.Errorf("error = %v", err)
}
{{ else }}
{{ template "call" $f }}
if err := test.checkFunc(test.want); err != nil {
tt.Errorf("error = %v", err)
}
{{- end }}
})
{{- end }}
}
}
{{- end }}
10 changes: 10 additions & 0 deletions assets/test/templates/common/header.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{define "header"}}
{{range .Comments}}{{.}}
{{end -}}
package {{.Package}}

import (
{{range .Imports}}{{.Name}} {{.Path}}
{{end}}
)
{{end}}
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions assets/test/templates/common/results.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{define "results"}}{{- if .OnlyReturnsError }}err{{ else }}{{range $i, $el := .Results}}{{if $i}}, {{end}}{{Got .}}{{end}}{{if .ReturnsError}}, err{{end}}
{{- end }}{{end}}
1 change: 1 addition & 0 deletions assets/test/templates/option/call.tmpl
1 change: 1 addition & 0 deletions assets/test/templates/option/fill.tmpl
Loading