@@ -62,42 +62,44 @@ const testTmplString = `func {{.TestFuncName}}(t *testing.T) {
6262
6363 {{- /* Loop over all the test cases. */}}
6464 for _, tt := range tests {
65- {{/* Got variables. */}}
66- {{- if .Results}}{{fieldNames .Results ""}} := {{end}}
67-
68- {{- /* Call expression. In xtest package test, call function by PACKAGE.FUNC. */}}
69- {{- /* TODO(hxjiang): consider any renaming in existing xtest package imports. E.g. import renamedfoo "foo". */}}
70- {{- /* TODO(hxjiang): support add test for methods by calling the right constructor. */}}
71- {{- if .PackageName}}{{.PackageName}}.{{end}}{{.FuncName}}
72-
73- {{- /* Input parameters. */ -}}
74- ({{if eq (len .Args) 1}}tt.arg{{end}}{{if gt (len .Args) 1}}{{fieldNames .Args "tt.args."}}{{end}})
75-
76- {{- /* Handles the returned error before the rest of return value. */}}
77- {{- $last := index .Results (add (len .Results) -1)}}
78- {{- if eq $last.Name "gotErr"}}
79- if gotErr != nil {
80- if !tt.wantErr {
81- t.Errorf("%s: {{$.FuncName}}() failed: %v", tt.name, gotErr)
65+ t.Run(tt.name, func(t *testing.T) {
66+ {{/* Got variables. */}}
67+ {{- if .Results}}{{fieldNames .Results ""}} := {{end}}
68+
69+ {{- /* Call expression. In xtest package test, call function by PACKAGE.FUNC. */}}
70+ {{- /* TODO(hxjiang): consider any renaming in existing xtest package imports. E.g. import renamedfoo "foo". */}}
71+ {{- /* TODO(hxjiang): support add test for methods by calling the right constructor. */}}
72+ {{- if .PackageName}}{{.PackageName}}.{{end}}{{.FuncName}}
73+
74+ {{- /* Input parameters. */ -}}
75+ ({{- if eq (len .Args) 1}}tt.arg{{end}}{{if gt (len .Args) 1}}{{fieldNames .Args "tt.args."}}{{end}})
76+
77+ {{- /* Handles the returned error before the rest of return value. */}}
78+ {{- $last := index .Results (add (len .Results) -1)}}
79+ {{- if eq $last.Name "gotErr"}}
80+ if gotErr != nil {
81+ if !tt.wantErr {
82+ t.Errorf("{{$.FuncName}}() failed: %v", gotErr)
83+ }
84+ return
8285 }
83- return
84- }
85- if tt.wantErr {
86- t.Fatalf("%s: {{$.FuncName}}() succeeded unexpectedly", tt.name)
87- }
88- {{- end}}
89-
90- {{- /* Compare the returned values except for the last returned error. */}}
91- {{- if or (and .Results (ne $last.Name "gotErr")) (and (gt (len .Results) 1) (eq $last.Name "gotErr"))}}
92- // TODO: update the condition below to compare got with tt.want.
93- {{- range $index, $res := .Results}}
94- {{- if ne $res.Name "gotErr"}}
95- if true {
96- t.Errorf("%s: {{$.FuncName}}() = %v, want %v", tt.name, {{.Name}}, tt.{{if eq $index 0}}want{{else}}want{{add $index 1}}{{end}})
97- }
98- {{- end}}
99- {{- end}}
100- {{- end}}
86+ if tt.wantErr {
87+ t.Fatal("{{$.FuncName}}() succeeded unexpectedly")
88+ }
89+ {{- end}}
90+
91+ {{- /* Compare the returned values except for the last returned error. */}}
92+ {{- if or (and .Results (ne $last.Name "gotErr")) (and (gt (len .Results) 1) (eq $last.Name "gotErr"))}}
93+ // TODO: update the condition below to compare got with tt.want.
94+ {{- range $index, $res := .Results}}
95+ {{- if ne $res.Name "gotErr"}}
96+ if true {
97+ t.Errorf("{{$.FuncName}}() = %v, want %v", {{.Name}}, tt.{{if eq $index 0}}want{{else}}want{{add $index 1}}{{end}})
98+ }
99+ {{- end}}
100+ {{- end}}
101+ {{- end}}
102+ })
101103 }
102104}
103105`
0 commit comments