Skip to content

Commit 43f6438

Browse files
require: fix godoc generation for assertions returning a bool
1 parent 65697ce commit 43f6438

File tree

5 files changed

+64
-78
lines changed

5 files changed

+64
-78
lines changed

_codegen/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,48 @@ func (f *testFunc) CommentWithoutT(receiver string) string {
298298
return strings.Replace(f.Comment(), search, replace, -1)
299299
}
300300

301+
func requireComment(comment string) string {
302+
const returnClause = "Returns whether the assertion was successful (true) or not (false)"
303+
const emptyString = ""
304+
305+
comment = strings.ReplaceAll(comment, returnClause+".", emptyString)
306+
comment = strings.ReplaceAll(comment, returnClause, emptyString)
307+
308+
// convert from: "if cond {\n body\n}" to "cond\nbody"
309+
ifBlockReg := regexp.MustCompile(`(?m)^([\t ]*)if (.+) \{\n((?:.*\n)*?)\s*\}`)
310+
311+
comment = ifBlockReg.ReplaceAllStringFunc(comment, func(match string) string {
312+
nestedMatch := ifBlockReg.FindStringSubmatch(match)
313+
indent, cond, body := nestedMatch[1], nestedMatch[2], nestedMatch[3]
314+
out := []string{indent + cond}
315+
316+
for _, line := range strings.Split(
317+
strings.TrimRight(body, "\n"),
318+
"\n",
319+
) {
320+
if t := strings.TrimSpace(line); t != emptyString {
321+
out = append(out, indent+t)
322+
}
323+
}
324+
325+
return strings.Join(out, "\n")
326+
})
327+
328+
final := strings.TrimSpace(comment)
329+
return "// " + strings.ReplaceAll(final, "\n", "\n// ")
330+
}
331+
332+
func (f *testFunc) CommentRequire() string {
333+
comment := strings.ReplaceAll(f.DocInfo.Doc, "assert.", "require.")
334+
return requireComment(comment)
335+
}
336+
337+
func (f *testFunc) CommentRequireWithoutT(receiver string) string {
338+
assertCallRe := regexp.MustCompile(`assert\.(\w+)\(t, `)
339+
comment := assertCallRe.ReplaceAllString(f.DocInfo.Doc, receiver+".$1(")
340+
return requireComment(comment)
341+
}
342+
301343
// Standard header https://go.dev/s/generatedcode.
302344
var headerTemplate = `// Code generated with github.com/stretchr/testify/_codegen; DO NOT EDIT.
303345

require/require.go

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,6 @@ func Greaterf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...in
671671
// body that contains a string.
672672
//
673673
// require.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
674-
//
675-
// Returns whether the assertion was successful (true) or not (false).
676674
func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) {
677675
if h, ok := t.(tHelper); ok {
678676
h.Helper()
@@ -687,8 +685,6 @@ func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method string, url s
687685
// body that contains a string.
688686
//
689687
// require.HTTPBodyContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
690-
//
691-
// Returns whether the assertion was successful (true) or not (false).
692688
func HTTPBodyContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) {
693689
if h, ok := t.(tHelper); ok {
694690
h.Helper()
@@ -703,8 +699,6 @@ func HTTPBodyContainsf(t TestingT, handler http.HandlerFunc, method string, url
703699
// body that does not contain a string.
704700
//
705701
// require.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
706-
//
707-
// Returns whether the assertion was successful (true) or not (false).
708702
func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) {
709703
if h, ok := t.(tHelper); ok {
710704
h.Helper()
@@ -719,8 +713,6 @@ func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method string, ur
719713
// body that does not contain a string.
720714
//
721715
// require.HTTPBodyNotContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
722-
//
723-
// Returns whether the assertion was successful (true) or not (false).
724716
func HTTPBodyNotContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) {
725717
if h, ok := t.(tHelper); ok {
726718
h.Helper()
@@ -734,8 +726,6 @@ func HTTPBodyNotContainsf(t TestingT, handler http.HandlerFunc, method string, u
734726
// HTTPError asserts that a specified handler returns an error status code.
735727
//
736728
// require.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
737-
//
738-
// Returns whether the assertion was successful (true) or not (false).
739729
func HTTPError(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) {
740730
if h, ok := t.(tHelper); ok {
741731
h.Helper()
@@ -749,8 +739,6 @@ func HTTPError(t TestingT, handler http.HandlerFunc, method string, url string,
749739
// HTTPErrorf asserts that a specified handler returns an error status code.
750740
//
751741
// require.HTTPErrorf(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
752-
//
753-
// Returns whether the assertion was successful (true) or not (false).
754742
func HTTPErrorf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) {
755743
if h, ok := t.(tHelper); ok {
756744
h.Helper()
@@ -764,8 +752,6 @@ func HTTPErrorf(t TestingT, handler http.HandlerFunc, method string, url string,
764752
// HTTPRedirect asserts that a specified handler returns a redirect status code.
765753
//
766754
// require.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
767-
//
768-
// Returns whether the assertion was successful (true) or not (false).
769755
func HTTPRedirect(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) {
770756
if h, ok := t.(tHelper); ok {
771757
h.Helper()
@@ -779,8 +765,6 @@ func HTTPRedirect(t TestingT, handler http.HandlerFunc, method string, url strin
779765
// HTTPRedirectf asserts that a specified handler returns a redirect status code.
780766
//
781767
// require.HTTPRedirectf(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
782-
//
783-
// Returns whether the assertion was successful (true) or not (false).
784768
func HTTPRedirectf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) {
785769
if h, ok := t.(tHelper); ok {
786770
h.Helper()
@@ -794,8 +778,6 @@ func HTTPRedirectf(t TestingT, handler http.HandlerFunc, method string, url stri
794778
// HTTPStatusCode asserts that a specified handler returns a specified status code.
795779
//
796780
// require.HTTPStatusCode(t, myHandler, "GET", "/notImplemented", nil, 501)
797-
//
798-
// Returns whether the assertion was successful (true) or not (false).
799781
func HTTPStatusCode(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msgAndArgs ...interface{}) {
800782
if h, ok := t.(tHelper); ok {
801783
h.Helper()
@@ -809,8 +791,6 @@ func HTTPStatusCode(t TestingT, handler http.HandlerFunc, method string, url str
809791
// HTTPStatusCodef asserts that a specified handler returns a specified status code.
810792
//
811793
// require.HTTPStatusCodef(t, myHandler, "GET", "/notImplemented", nil, 501, "error message %s", "formatted")
812-
//
813-
// Returns whether the assertion was successful (true) or not (false).
814794
func HTTPStatusCodef(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msg string, args ...interface{}) {
815795
if h, ok := t.(tHelper); ok {
816796
h.Helper()
@@ -824,8 +804,6 @@ func HTTPStatusCodef(t TestingT, handler http.HandlerFunc, method string, url st
824804
// HTTPSuccess asserts that a specified handler returns a success status code.
825805
//
826806
// require.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil)
827-
//
828-
// Returns whether the assertion was successful (true) or not (false).
829807
func HTTPSuccess(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) {
830808
if h, ok := t.(tHelper); ok {
831809
h.Helper()
@@ -839,8 +817,6 @@ func HTTPSuccess(t TestingT, handler http.HandlerFunc, method string, url string
839817
// HTTPSuccessf asserts that a specified handler returns a success status code.
840818
//
841819
// require.HTTPSuccessf(t, myHandler, "POST", "http://www.google.com", nil, "error message %s", "formatted")
842-
//
843-
// Returns whether the assertion was successful (true) or not (false).
844820
func HTTPSuccessf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) {
845821
if h, ok := t.(tHelper); ok {
846822
h.Helper()
@@ -1387,10 +1363,9 @@ func NoDirExistsf(t TestingT, path string, msg string, args ...interface{}) {
13871363

13881364
// NoError asserts that a function returned a nil error (ie. no error).
13891365
//
1390-
// actualObj, err := SomeFunction()
1391-
// if require.NoError(t, err) {
1392-
// require.Equal(t, expectedObj, actualObj)
1393-
// }
1366+
// actualObj, err := SomeFunction()
1367+
// require.NoError(t, err)
1368+
// require.Equal(t, expectedObj, actualObj)
13941369
func NoError(t TestingT, err error, msgAndArgs ...interface{}) {
13951370
if h, ok := t.(tHelper); ok {
13961371
h.Helper()
@@ -1403,10 +1378,9 @@ func NoError(t TestingT, err error, msgAndArgs ...interface{}) {
14031378

14041379
// NoErrorf asserts that a function returned a nil error (ie. no error).
14051380
//
1406-
// actualObj, err := SomeFunction()
1407-
// if require.NoErrorf(t, err, "error message %s", "formatted") {
1408-
// require.Equal(t, expectedObj, actualObj)
1409-
// }
1381+
// actualObj, err := SomeFunction()
1382+
// require.NoErrorf(t, err, "error message %s", "formatted")
1383+
// require.Equal(t, expectedObj, actualObj)
14101384
func NoErrorf(t TestingT, err error, msg string, args ...interface{}) {
14111385
if h, ok := t.(tHelper); ok {
14121386
h.Helper()
@@ -1515,9 +1489,8 @@ func NotElementsMatchf(t TestingT, listA interface{}, listB interface{}, msg str
15151489

15161490
// NotEmpty asserts that the specified object is NOT [Empty].
15171491
//
1518-
// if require.NotEmpty(t, obj) {
1519-
// require.Equal(t, "two", obj[1])
1520-
// }
1492+
// require.NotEmpty(t, obj)
1493+
// require.Equal(t, "two", obj[1])
15211494
func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) {
15221495
if h, ok := t.(tHelper); ok {
15231496
h.Helper()
@@ -1530,9 +1503,8 @@ func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) {
15301503

15311504
// NotEmptyf asserts that the specified object is NOT [Empty].
15321505
//
1533-
// if require.NotEmptyf(t, obj, "error message %s", "formatted") {
1534-
// require.Equal(t, "two", obj[1])
1535-
// }
1506+
// require.NotEmptyf(t, obj, "error message %s", "formatted")
1507+
// require.Equal(t, "two", obj[1])
15361508
func NotEmptyf(t TestingT, object interface{}, msg string, args ...interface{}) {
15371509
if h, ok := t.(tHelper); ok {
15381510
h.Helper()

require/require.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{ replace .Comment "assert." "require."}}
1+
{{.CommentRequire}}
22
func {{.DocInfo.Name}}(t TestingT, {{.Params}}) {
33
if h, ok := t.(tHelper); ok { h.Helper() }
44
if assert.{{.DocInfo.Name}}(t, {{.ForwardedParams}}) { return }

require/require_forward.go

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,6 @@ func (a *Assertions) Greaterf(e1 interface{}, e2 interface{}, msg string, args .
539539
// body that contains a string.
540540
//
541541
// a.HTTPBodyContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
542-
//
543-
// Returns whether the assertion was successful (true) or not (false).
544542
func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) {
545543
if h, ok := a.t.(tHelper); ok {
546544
h.Helper()
@@ -552,8 +550,6 @@ func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, u
552550
// body that contains a string.
553551
//
554552
// a.HTTPBodyContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
555-
//
556-
// Returns whether the assertion was successful (true) or not (false).
557553
func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) {
558554
if h, ok := a.t.(tHelper); ok {
559555
h.Helper()
@@ -565,8 +561,6 @@ func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string,
565561
// body that does not contain a string.
566562
//
567563
// a.HTTPBodyNotContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
568-
//
569-
// Returns whether the assertion was successful (true) or not (false).
570564
func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) {
571565
if h, ok := a.t.(tHelper); ok {
572566
h.Helper()
@@ -578,8 +572,6 @@ func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string
578572
// body that does not contain a string.
579573
//
580574
// a.HTTPBodyNotContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
581-
//
582-
// Returns whether the assertion was successful (true) or not (false).
583575
func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) {
584576
if h, ok := a.t.(tHelper); ok {
585577
h.Helper()
@@ -590,8 +582,6 @@ func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method strin
590582
// HTTPError asserts that a specified handler returns an error status code.
591583
//
592584
// a.HTTPError(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
593-
//
594-
// Returns whether the assertion was successful (true) or not (false).
595585
func (a *Assertions) HTTPError(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) {
596586
if h, ok := a.t.(tHelper); ok {
597587
h.Helper()
@@ -602,8 +592,6 @@ func (a *Assertions) HTTPError(handler http.HandlerFunc, method string, url stri
602592
// HTTPErrorf asserts that a specified handler returns an error status code.
603593
//
604594
// a.HTTPErrorf(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
605-
//
606-
// Returns whether the assertion was successful (true) or not (false).
607595
func (a *Assertions) HTTPErrorf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) {
608596
if h, ok := a.t.(tHelper); ok {
609597
h.Helper()
@@ -614,8 +602,6 @@ func (a *Assertions) HTTPErrorf(handler http.HandlerFunc, method string, url str
614602
// HTTPRedirect asserts that a specified handler returns a redirect status code.
615603
//
616604
// a.HTTPRedirect(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
617-
//
618-
// Returns whether the assertion was successful (true) or not (false).
619605
func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) {
620606
if h, ok := a.t.(tHelper); ok {
621607
h.Helper()
@@ -626,8 +612,6 @@ func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method string, url s
626612
// HTTPRedirectf asserts that a specified handler returns a redirect status code.
627613
//
628614
// a.HTTPRedirectf(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
629-
//
630-
// Returns whether the assertion was successful (true) or not (false).
631615
func (a *Assertions) HTTPRedirectf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) {
632616
if h, ok := a.t.(tHelper); ok {
633617
h.Helper()
@@ -638,8 +622,6 @@ func (a *Assertions) HTTPRedirectf(handler http.HandlerFunc, method string, url
638622
// HTTPStatusCode asserts that a specified handler returns a specified status code.
639623
//
640624
// a.HTTPStatusCode(myHandler, "GET", "/notImplemented", nil, 501)
641-
//
642-
// Returns whether the assertion was successful (true) or not (false).
643625
func (a *Assertions) HTTPStatusCode(handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msgAndArgs ...interface{}) {
644626
if h, ok := a.t.(tHelper); ok {
645627
h.Helper()
@@ -650,8 +632,6 @@ func (a *Assertions) HTTPStatusCode(handler http.HandlerFunc, method string, url
650632
// HTTPStatusCodef asserts that a specified handler returns a specified status code.
651633
//
652634
// a.HTTPStatusCodef(myHandler, "GET", "/notImplemented", nil, 501, "error message %s", "formatted")
653-
//
654-
// Returns whether the assertion was successful (true) or not (false).
655635
func (a *Assertions) HTTPStatusCodef(handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msg string, args ...interface{}) {
656636
if h, ok := a.t.(tHelper); ok {
657637
h.Helper()
@@ -662,8 +642,6 @@ func (a *Assertions) HTTPStatusCodef(handler http.HandlerFunc, method string, ur
662642
// HTTPSuccess asserts that a specified handler returns a success status code.
663643
//
664644
// a.HTTPSuccess(myHandler, "POST", "http://www.google.com", nil)
665-
//
666-
// Returns whether the assertion was successful (true) or not (false).
667645
func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) {
668646
if h, ok := a.t.(tHelper); ok {
669647
h.Helper()
@@ -674,8 +652,6 @@ func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method string, url st
674652
// HTTPSuccessf asserts that a specified handler returns a success status code.
675653
//
676654
// a.HTTPSuccessf(myHandler, "POST", "http://www.google.com", nil, "error message %s", "formatted")
677-
//
678-
// Returns whether the assertion was successful (true) or not (false).
679655
func (a *Assertions) HTTPSuccessf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) {
680656
if h, ok := a.t.(tHelper); ok {
681657
h.Helper()
@@ -1099,10 +1075,9 @@ func (a *Assertions) NoDirExistsf(path string, msg string, args ...interface{})
10991075

11001076
// NoError asserts that a function returned a nil error (ie. no error).
11011077
//
1102-
// actualObj, err := SomeFunction()
1103-
// if a.NoError(err) {
1104-
// assert.Equal(t, expectedObj, actualObj)
1105-
// }
1078+
// actualObj, err := SomeFunction()
1079+
// a.NoError(err)
1080+
// a.Equal(expectedObj, actualObj)
11061081
func (a *Assertions) NoError(err error, msgAndArgs ...interface{}) {
11071082
if h, ok := a.t.(tHelper); ok {
11081083
h.Helper()
@@ -1112,10 +1087,9 @@ func (a *Assertions) NoError(err error, msgAndArgs ...interface{}) {
11121087

11131088
// NoErrorf asserts that a function returned a nil error (ie. no error).
11141089
//
1115-
// actualObj, err := SomeFunction()
1116-
// if a.NoErrorf(err, "error message %s", "formatted") {
1117-
// assert.Equal(t, expectedObj, actualObj)
1118-
// }
1090+
// actualObj, err := SomeFunction()
1091+
// a.NoErrorf(err, "error message %s", "formatted")
1092+
// a.Equal(expectedObj, actualObj)
11191093
func (a *Assertions) NoErrorf(err error, msg string, args ...interface{}) {
11201094
if h, ok := a.t.(tHelper); ok {
11211095
h.Helper()
@@ -1203,9 +1177,8 @@ func (a *Assertions) NotElementsMatchf(listA interface{}, listB interface{}, msg
12031177

12041178
// NotEmpty asserts that the specified object is NOT [Empty].
12051179
//
1206-
// if a.NotEmpty(obj) {
1207-
// assert.Equal(t, "two", obj[1])
1208-
// }
1180+
// a.NotEmpty(obj)
1181+
// a.Equal("two", obj[1])
12091182
func (a *Assertions) NotEmpty(object interface{}, msgAndArgs ...interface{}) {
12101183
if h, ok := a.t.(tHelper); ok {
12111184
h.Helper()
@@ -1215,9 +1188,8 @@ func (a *Assertions) NotEmpty(object interface{}, msgAndArgs ...interface{}) {
12151188

12161189
// NotEmptyf asserts that the specified object is NOT [Empty].
12171190
//
1218-
// if a.NotEmptyf(obj, "error message %s", "formatted") {
1219-
// assert.Equal(t, "two", obj[1])
1220-
// }
1191+
// a.NotEmptyf(obj, "error message %s", "formatted")
1192+
// a.Equal("two", obj[1])
12211193
func (a *Assertions) NotEmptyf(object interface{}, msg string, args ...interface{}) {
12221194
if h, ok := a.t.(tHelper); ok {
12231195
h.Helper()

require/require_forward.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{.CommentWithoutT "a"}}
1+
{{.CommentRequireWithoutT "a"}}
22
func (a *Assertions) {{.DocInfo.Name}}({{.Params}}) {
33
if h, ok := a.t.(tHelper); ok { h.Helper() }
44
{{.DocInfo.Name}}(a.t, {{.ForwardedParams}})

0 commit comments

Comments
 (0)