Skip to content

Commit 750994b

Browse files
committed
internal/godoc/dochtml: compare with golden
Add a comparison with a golden file to the test, for more thorough coverage. Delete tests that are obsoleted by having a golden. Factor out golden comparison to a separate package. For golang/go#40850 Change-Id: Iac7b4c0e1c4aadad690d8d5118a6861408614aa2 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/313030 Trust: Jonathan Amsterdam <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]>
1 parent 22d7146 commit 750994b

File tree

5 files changed

+855
-86
lines changed

5 files changed

+855
-86
lines changed

internal/godoc/codec/generate_test.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
"go/ast"
1111
"go/token"
1212
"io"
13-
"io/ioutil"
14-
"path/filepath"
1513
"reflect"
1614
"testing"
1715

1816
"github.com/google/go-cmp/cmp"
17+
"golang.org/x/pkgsite/internal/testing/testhelper"
1918
)
2019

2120
var update = flag.Bool("update", false, "update goldens instead of checking against them")
@@ -57,30 +56,7 @@ func testGenerate(t *testing.T, name string, x interface{}) {
5756
t.Fatal(err)
5857
}
5958
got := buf.String()
60-
if *update {
61-
writeGolden(t, name, got)
62-
} else {
63-
want := readGolden(t, name)
64-
if diff := cmp.Diff(want, got); diff != "" {
65-
t.Errorf("%s: mismatch (-want, +got):\n%s", name, diff)
66-
}
67-
}
68-
}
69-
70-
func writeGolden(t *testing.T, name string, data string) {
71-
filename := filepath.Join("testdata", name+".go")
72-
if err := ioutil.WriteFile(filename, []byte(data), 0644); err != nil {
73-
t.Fatal(err)
74-
}
75-
t.Logf("wrote %s", filename)
76-
}
77-
78-
func readGolden(t *testing.T, name string) string {
79-
data, err := ioutil.ReadFile(filepath.Join("testdata", name+".go"))
80-
if err != nil {
81-
t.Fatal(err)
82-
}
83-
return string(data)
59+
testhelper.CompareWithGolden(t, got, name+".go", *update)
8460
}
8561

8662
func TestExportedFields(t *testing.T) {

internal/godoc/dochtml/dochtml_test.go

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ package dochtml
77
import (
88
"bytes"
99
"context"
10+
"flag"
11+
"fmt"
12+
1013
"go/ast"
1114
"go/parser"
1215
"go/token"
@@ -22,10 +25,13 @@ import (
2225
"golang.org/x/pkgsite/internal/godoc/dochtml/internal/render"
2326
"golang.org/x/pkgsite/internal/godoc/internal/doc"
2427
"golang.org/x/pkgsite/internal/testing/htmlcheck"
28+
"golang.org/x/pkgsite/internal/testing/testhelper"
2529
)
2630

2731
var templateSource = template.TrustedSourceFromConstant("../../../content/static/html/doc")
2832

33+
var update = flag.Bool("update", false, "update goldens instead of checking against them")
34+
2935
var (
3036
in = htmlcheck.In
3137
hasAttr = htmlcheck.HasAttr
@@ -41,27 +47,28 @@ var testRenderOptions = RenderOptions{
4147
}
4248

4349
func TestRenderParts(t *testing.T) {
50+
ctx := context.Background()
4451
LoadTemplates(templateSource)
45-
fset, d := mustLoadPackage("everydecl")
4652

47-
ctx := context.Background()
53+
fset, d := mustLoadPackage("everydecl")
4854
parts, err := Render(ctx, fset, d, testRenderOptions)
4955
if err != nil {
5056
t.Fatal(err)
5157
}
52-
bodyDoc, err := html.Parse(strings.NewReader(parts.Body.String()))
53-
if err != nil {
54-
t.Fatal(err)
55-
}
58+
compareWithGolden(t, parts, "everydecl", *update)
5659

57-
sidenavDoc, err := html.Parse(strings.NewReader(parts.Outline.String()))
60+
fset2, d2 := mustLoadPackage("deprecated")
61+
parts2, err := Render(ctx, fset2, d2, testRenderOptions)
5862
if err != nil {
5963
t.Fatal(err)
6064
}
61-
mobileDoc, err := html.Parse(strings.NewReader(parts.MobileOutline.String()))
65+
compareWithGolden(t, parts2, "deprecated", *update)
66+
67+
bodyDoc, err := html.Parse(strings.NewReader(parts.Body.String()))
6268
if err != nil {
6369
t.Fatal(err)
6470
}
71+
6572
// Check that there are no duplicate id attributes.
6673
t.Run("duplicate ids", func(t *testing.T) {
6774
testDuplicateIDs(t, bodyDoc)
@@ -71,31 +78,6 @@ func TestRenderParts(t *testing.T) {
7178
testIDsAndKinds(t, bodyDoc)
7279
})
7380

74-
checker := in(".Documentation-note",
75-
in("h3", hasAttr("id", "pkg-note-BUG"), hasExactText("Bugs ¶")),
76-
in("a", hasHref("#pkg-note-BUG")))
77-
if err := checker(bodyDoc); err != nil {
78-
t.Errorf("note check: %v", err)
79-
}
80-
81-
checker = in(".Documentation-index",
82-
in(".Documentation-indexNote", in("a", hasHref("#pkg-note-BUG"), hasExactText("Bugs"))))
83-
if err := checker(bodyDoc); err != nil {
84-
t.Errorf("note check: %v", err)
85-
}
86-
87-
checker = in(".DocNav-notes",
88-
in("#nav-group-notes", in("li", in("a", hasHref("#pkg-note-BUG"), hasText("Bugs")))))
89-
if err := checker(sidenavDoc); err != nil {
90-
t.Errorf("note check: %v", err)
91-
}
92-
93-
checker = in("",
94-
in("optgroup[label=Notes]", in("option", hasAttr("value", "pkg-note-BUG"), hasExactText("Bugs"))))
95-
if err := checker(mobileDoc); err != nil {
96-
t.Errorf("note check: %v", err)
97-
}
98-
9981
wantLinks := []render.Link{
10082
{Href: "https://go.googlesource.com/pkgsite", Text: "pkgsite repo"},
10183
{Href: "https://play-with-go.dev", Text: "Play with Go"},
@@ -105,6 +87,11 @@ func TestRenderParts(t *testing.T) {
10587
}
10688
}
10789

90+
func compareWithGolden(t *testing.T, parts *Parts, name string, update bool) {
91+
got := fmt.Sprintf("%s\n----\n%s\n----\n%s\n", parts.Body, parts.Outline, parts.MobileOutline)
92+
testhelper.CompareWithGolden(t, got, name+".golden", update)
93+
}
94+
10895
func TestExampleRender(t *testing.T) {
10996
LoadTemplates(templateSource)
11097
ctx := context.Background()
@@ -353,33 +340,6 @@ func TestVersionedPkgPath(t *testing.T) {
353340
}
354341
}
355342

356-
func TestDeprecated(t *testing.T) {
357-
LoadTemplates(templateSource)
358-
ctx := context.Background()
359-
fset, d := mustLoadPackage("deprecated")
360-
parts, err := Render(ctx, fset, d, testRenderOptions)
361-
if err != nil {
362-
t.Fatal(err)
363-
}
364-
// In package deprecated, all non-deprecated symbols begin with "Good" and
365-
// all deprecated ones begin with "Bad".
366-
// There is one const, var, func and type of each.
367-
368-
// The outline only has functions and types, so we should see two "Good"s and no "Bad"s.
369-
outlineString := parts.Outline.String()
370-
for _, want := range []string{"GoodF()", "type GoodT", "GoodM", "NewGoodTGood()"} {
371-
if !strings.Contains(outlineString, want) {
372-
t.Errorf("outline does not have %q but should", want)
373-
}
374-
}
375-
for _, notWant := range []string{"BadF()", "type BadT", "BadM()", "NewGoodTBad()"} {
376-
if strings.Contains(outlineString, notWant) {
377-
t.Errorf("outline has %q but shouldn't", notWant)
378-
}
379-
}
380-
381-
}
382-
383343
func testDuplicateIDs(t *testing.T, htmlDoc *html.Node) {
384344
idCounts := map[string]int{}
385345
walk(htmlDoc, func(n *html.Node) {

0 commit comments

Comments
 (0)