Skip to content

Commit

Permalink
internal/godoc/dochtml: compare with golden
Browse files Browse the repository at this point in the history
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 <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
  • Loading branch information
jba committed Apr 23, 2021
1 parent 22d7146 commit 750994b
Show file tree
Hide file tree
Showing 5 changed files with 855 additions and 86 deletions.
28 changes: 2 additions & 26 deletions internal/godoc/codec/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
"go/ast"
"go/token"
"io"
"io/ioutil"
"path/filepath"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
"golang.org/x/pkgsite/internal/testing/testhelper"
)

var update = flag.Bool("update", false, "update goldens instead of checking against them")
Expand Down Expand Up @@ -57,30 +56,7 @@ func testGenerate(t *testing.T, name string, x interface{}) {
t.Fatal(err)
}
got := buf.String()
if *update {
writeGolden(t, name, got)
} else {
want := readGolden(t, name)
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("%s: mismatch (-want, +got):\n%s", name, diff)
}
}
}

func writeGolden(t *testing.T, name string, data string) {
filename := filepath.Join("testdata", name+".go")
if err := ioutil.WriteFile(filename, []byte(data), 0644); err != nil {
t.Fatal(err)
}
t.Logf("wrote %s", filename)
}

func readGolden(t *testing.T, name string) string {
data, err := ioutil.ReadFile(filepath.Join("testdata", name+".go"))
if err != nil {
t.Fatal(err)
}
return string(data)
testhelper.CompareWithGolden(t, got, name+".go", *update)
}

func TestExportedFields(t *testing.T) {
Expand Down
80 changes: 20 additions & 60 deletions internal/godoc/dochtml/dochtml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package dochtml
import (
"bytes"
"context"
"flag"
"fmt"

"go/ast"
"go/parser"
"go/token"
Expand All @@ -22,10 +25,13 @@ import (
"golang.org/x/pkgsite/internal/godoc/dochtml/internal/render"
"golang.org/x/pkgsite/internal/godoc/internal/doc"
"golang.org/x/pkgsite/internal/testing/htmlcheck"
"golang.org/x/pkgsite/internal/testing/testhelper"
)

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

var update = flag.Bool("update", false, "update goldens instead of checking against them")

var (
in = htmlcheck.In
hasAttr = htmlcheck.HasAttr
Expand All @@ -41,27 +47,28 @@ var testRenderOptions = RenderOptions{
}

func TestRenderParts(t *testing.T) {
ctx := context.Background()
LoadTemplates(templateSource)
fset, d := mustLoadPackage("everydecl")

ctx := context.Background()
fset, d := mustLoadPackage("everydecl")
parts, err := Render(ctx, fset, d, testRenderOptions)
if err != nil {
t.Fatal(err)
}
bodyDoc, err := html.Parse(strings.NewReader(parts.Body.String()))
if err != nil {
t.Fatal(err)
}
compareWithGolden(t, parts, "everydecl", *update)

sidenavDoc, err := html.Parse(strings.NewReader(parts.Outline.String()))
fset2, d2 := mustLoadPackage("deprecated")
parts2, err := Render(ctx, fset2, d2, testRenderOptions)
if err != nil {
t.Fatal(err)
}
mobileDoc, err := html.Parse(strings.NewReader(parts.MobileOutline.String()))
compareWithGolden(t, parts2, "deprecated", *update)

bodyDoc, err := html.Parse(strings.NewReader(parts.Body.String()))
if err != nil {
t.Fatal(err)
}

// Check that there are no duplicate id attributes.
t.Run("duplicate ids", func(t *testing.T) {
testDuplicateIDs(t, bodyDoc)
Expand All @@ -71,31 +78,6 @@ func TestRenderParts(t *testing.T) {
testIDsAndKinds(t, bodyDoc)
})

checker := in(".Documentation-note",
in("h3", hasAttr("id", "pkg-note-BUG"), hasExactText("Bugs ¶")),
in("a", hasHref("#pkg-note-BUG")))
if err := checker(bodyDoc); err != nil {
t.Errorf("note check: %v", err)
}

checker = in(".Documentation-index",
in(".Documentation-indexNote", in("a", hasHref("#pkg-note-BUG"), hasExactText("Bugs"))))
if err := checker(bodyDoc); err != nil {
t.Errorf("note check: %v", err)
}

checker = in(".DocNav-notes",
in("#nav-group-notes", in("li", in("a", hasHref("#pkg-note-BUG"), hasText("Bugs")))))
if err := checker(sidenavDoc); err != nil {
t.Errorf("note check: %v", err)
}

checker = in("",
in("optgroup[label=Notes]", in("option", hasAttr("value", "pkg-note-BUG"), hasExactText("Bugs"))))
if err := checker(mobileDoc); err != nil {
t.Errorf("note check: %v", err)
}

wantLinks := []render.Link{
{Href: "https://go.googlesource.com/pkgsite", Text: "pkgsite repo"},
{Href: "https://play-with-go.dev", Text: "Play with Go"},
Expand All @@ -105,6 +87,11 @@ func TestRenderParts(t *testing.T) {
}
}

func compareWithGolden(t *testing.T, parts *Parts, name string, update bool) {
got := fmt.Sprintf("%s\n----\n%s\n----\n%s\n", parts.Body, parts.Outline, parts.MobileOutline)
testhelper.CompareWithGolden(t, got, name+".golden", update)
}

func TestExampleRender(t *testing.T) {
LoadTemplates(templateSource)
ctx := context.Background()
Expand Down Expand Up @@ -353,33 +340,6 @@ func TestVersionedPkgPath(t *testing.T) {
}
}

func TestDeprecated(t *testing.T) {
LoadTemplates(templateSource)
ctx := context.Background()
fset, d := mustLoadPackage("deprecated")
parts, err := Render(ctx, fset, d, testRenderOptions)
if err != nil {
t.Fatal(err)
}
// In package deprecated, all non-deprecated symbols begin with "Good" and
// all deprecated ones begin with "Bad".
// There is one const, var, func and type of each.

// The outline only has functions and types, so we should see two "Good"s and no "Bad"s.
outlineString := parts.Outline.String()
for _, want := range []string{"GoodF()", "type GoodT", "GoodM", "NewGoodTGood()"} {
if !strings.Contains(outlineString, want) {
t.Errorf("outline does not have %q but should", want)
}
}
for _, notWant := range []string{"BadF()", "type BadT", "BadM()", "NewGoodTBad()"} {
if strings.Contains(outlineString, notWant) {
t.Errorf("outline has %q but shouldn't", notWant)
}
}

}

func testDuplicateIDs(t *testing.T, htmlDoc *html.Node) {
idCounts := map[string]int{}
walk(htmlDoc, func(n *html.Node) {
Expand Down
Loading

0 comments on commit 750994b

Please sign in to comment.