@@ -7,6 +7,9 @@ package dochtml
7
7
import (
8
8
"bytes"
9
9
"context"
10
+ "flag"
11
+ "fmt"
12
+
10
13
"go/ast"
11
14
"go/parser"
12
15
"go/token"
@@ -22,10 +25,13 @@ import (
22
25
"golang.org/x/pkgsite/internal/godoc/dochtml/internal/render"
23
26
"golang.org/x/pkgsite/internal/godoc/internal/doc"
24
27
"golang.org/x/pkgsite/internal/testing/htmlcheck"
28
+ "golang.org/x/pkgsite/internal/testing/testhelper"
25
29
)
26
30
27
31
var templateSource = template .TrustedSourceFromConstant ("../../../content/static/html/doc" )
28
32
33
+ var update = flag .Bool ("update" , false , "update goldens instead of checking against them" )
34
+
29
35
var (
30
36
in = htmlcheck .In
31
37
hasAttr = htmlcheck .HasAttr
@@ -41,27 +47,28 @@ var testRenderOptions = RenderOptions{
41
47
}
42
48
43
49
func TestRenderParts (t * testing.T ) {
50
+ ctx := context .Background ()
44
51
LoadTemplates (templateSource )
45
- fset , d := mustLoadPackage ("everydecl" )
46
52
47
- ctx := context . Background ( )
53
+ fset , d := mustLoadPackage ( "everydecl" )
48
54
parts , err := Render (ctx , fset , d , testRenderOptions )
49
55
if err != nil {
50
56
t .Fatal (err )
51
57
}
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 )
56
59
57
- sidenavDoc , err := html .Parse (strings .NewReader (parts .Outline .String ()))
60
+ fset2 , d2 := mustLoadPackage ("deprecated" )
61
+ parts2 , err := Render (ctx , fset2 , d2 , testRenderOptions )
58
62
if err != nil {
59
63
t .Fatal (err )
60
64
}
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 ()))
62
68
if err != nil {
63
69
t .Fatal (err )
64
70
}
71
+
65
72
// Check that there are no duplicate id attributes.
66
73
t .Run ("duplicate ids" , func (t * testing.T ) {
67
74
testDuplicateIDs (t , bodyDoc )
@@ -71,31 +78,6 @@ func TestRenderParts(t *testing.T) {
71
78
testIDsAndKinds (t , bodyDoc )
72
79
})
73
80
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
-
99
81
wantLinks := []render.Link {
100
82
{Href : "https://go.googlesource.com/pkgsite" , Text : "pkgsite repo" },
101
83
{Href : "https://play-with-go.dev" , Text : "Play with Go" },
@@ -105,6 +87,11 @@ func TestRenderParts(t *testing.T) {
105
87
}
106
88
}
107
89
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
+
108
95
func TestExampleRender (t * testing.T ) {
109
96
LoadTemplates (templateSource )
110
97
ctx := context .Background ()
@@ -353,33 +340,6 @@ func TestVersionedPkgPath(t *testing.T) {
353
340
}
354
341
}
355
342
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
-
383
343
func testDuplicateIDs (t * testing.T , htmlDoc * html.Node ) {
384
344
idCounts := map [string ]int {}
385
345
walk (htmlDoc , func (n * html.Node ) {
0 commit comments