| 1 |
// Copyright 2022 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package doc |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"go/parser" |
| 10 |
"go/token" |
| 11 |
"internal/diff" |
| 12 |
"testing" |
| 13 |
) |
| 14 |
|
| 15 |
func TestComment(t *testing.T) { |
| 16 |
fset := token.NewFileSet() |
| 17 |
pkgs, err := parser.ParseDir(fset, "testdata/pkgdoc", nil, parser.ParseComments) |
| 18 |
if err != nil { |
| 19 |
t.Fatal(err) |
| 20 |
} |
| 21 |
if pkgs["pkgdoc"] == nil { |
| 22 |
t.Fatal("missing package pkgdoc") |
| 23 |
} |
| 24 |
pkg := New(pkgs["pkgdoc"], "testdata/pkgdoc", 0) |
| 25 |
|
| 26 |
var ( |
| 27 |
input = "[T] and [U] are types, and [T.M] is a method, but [V] is a broken link. [rand.Int] and [crand.Reader] are things. [G.X] is a field, [G.M1] and [G.M2] are generic methods. [I.F] is an interface method and [I.V] is a broken link.\n" |
| 28 |
wantHTML = `<p><a href="#T">T</a> and <a href="#U">U</a> are types, and <a href="#T.M">T.M</a> is a method, but [V] is a broken link. <a href="/math/rand#Int">rand.Int</a> and <a href="/crypto/rand#Reader">crand.Reader</a> are things. <a href="#G.X">G.X</a> is a field, <a href="#G.M1">G.M1</a> and <a href="#G.M2">G.M2</a> are generic methods. <a href="#I.F">I.F</a> is an interface method and [I.V] is a broken link.` + "\n" |
| 29 |
wantOldHTML = "<p>[T] and [U] are <i>types</i>, and [T.M] is a method, but [V] is a broken link. [rand.Int] and [crand.Reader] are things. [G.X] is a field, [G.M1] and [G.M2] are generic methods. [I.F] is an interface method and [I.V] is a broken link.\n" |
| 30 |
wantMarkdown = "[T](#T) and [U](#U) are types, and [T.M](#T.M) is a method, but \\[V] is a broken link. [rand.Int](/math/rand#Int) and [crand.Reader](/crypto/rand#Reader) are things. [G.X](#G.X) is a field, [G.M1](#G.M1) and [G.M2](#G.M2) are generic methods. [I.F](#I.F) is an interface method and \\[I.V] is a broken link.\n" |
| 31 |
wantText = "T and U are types, and T.M is a method, but [V] is a broken link. rand.Int and\ncrand.Reader are things. G.X is a field, G.M1 and G.M2 are generic methods.\nI.F is an interface method and [I.V] is a broken link.\n" |
| 32 |
wantOldText = "[T] and [U] are types, and [T.M] is a method, but [V] is a broken link.\n[rand.Int] and [crand.Reader] are things. [G.X] is a field, [G.M1] and [G.M2]\nare generic methods. [I.F] is an interface method and [I.V] is a broken link.\n" |
| 33 |
wantSynopsis = "T and U are types, and T.M is a method, but [V] is a broken link." |
| 34 |
wantOldSynopsis = "[T] and [U] are types, and [T.M] is a method, but [V] is a broken link." |
| 35 |
) |
| 36 |
|
| 37 |
if b := pkg.HTML(input); string(b) != wantHTML { |
| 38 |
t.Errorf("%s", diff.Diff("pkg.HTML", b, "want", []byte(wantHTML))) |
| 39 |
} |
| 40 |
if b := pkg.Markdown(input); string(b) != wantMarkdown { |
| 41 |
t.Errorf("%s", diff.Diff("pkg.Markdown", b, "want", []byte(wantMarkdown))) |
| 42 |
} |
| 43 |
if b := pkg.Text(input); string(b) != wantText { |
| 44 |
t.Errorf("%s", diff.Diff("pkg.Text", b, "want", []byte(wantText))) |
| 45 |
} |
| 46 |
if b := pkg.Synopsis(input); b != wantSynopsis { |
| 47 |
t.Errorf("%s", diff.Diff("pkg.Synopsis", []byte(b), "want", []byte(wantText))) |
| 48 |
} |
| 49 |
|
| 50 |
var buf bytes.Buffer |
| 51 |
|
| 52 |
buf.Reset() |
| 53 |
ToHTML(&buf, input, map[string]string{"types": ""}) |
| 54 |
if b := buf.Bytes(); string(b) != wantOldHTML { |
| 55 |
t.Errorf("%s", diff.Diff("ToHTML", b, "want", []byte(wantOldHTML))) |
| 56 |
} |
| 57 |
|
| 58 |
buf.Reset() |
| 59 |
ToText(&buf, input, "", "\t", 80) |
| 60 |
if b := buf.Bytes(); string(b) != wantOldText { |
| 61 |
t.Errorf("%s", diff.Diff("ToText", b, "want", []byte(wantOldText))) |
| 62 |
} |
| 63 |
|
| 64 |
if b := Synopsis(input); b != wantOldSynopsis { |
| 65 |
t.Errorf("%s", diff.Diff("Synopsis", []byte(b), "want", []byte(wantOldText))) |
| 66 |
} |
| 67 |
} |