File: extractdoc_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (80 lines) | stat: -rw-r--r-- 1,896 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package analysisinternal_test

import (
	"testing"

	"golang.org/x/tools/internal/analysisinternal"
)

func TestExtractDoc(t *testing.T) {
	const multi = `// Copyright

//+build tag

// Package foo
//
// # Irrelevant heading
//
// This is irrelevant doc.
//
// # Analyzer nocolon
//
// This one has the wrong form for this line.
//
// # Analyzer food
//
// food: reports dining opportunities
//
// This is the doc for analyzer 'food'.
//
// # Analyzer foo
//
// foo: reports diagnostics
//
// This is the doc for analyzer 'foo'.
//
// # Analyzer bar
//
// bar: reports drinking opportunities
//
// This is the doc for analyzer 'bar'.
package blah

var x = syntax error
`

	for _, test := range []struct {
		content, name string
		want          string // doc or "error: %w" string
	}{
		{"", "foo",
			"error: empty Go source file"},
		{"//foo", "foo",
			"error: not a Go source file"},
		{"//foo\npackage foo", "foo",
			"error: package doc comment contains no 'Analyzer foo' heading"},
		{multi, "foo",
			"reports diagnostics\n\nThis is the doc for analyzer 'foo'."},
		{multi, "bar",
			"reports drinking opportunities\n\nThis is the doc for analyzer 'bar'."},
		{multi, "food",
			"reports dining opportunities\n\nThis is the doc for analyzer 'food'."},
		{multi, "nope",
			"error: package doc comment contains no 'Analyzer nope' heading"},
		{multi, "nocolon",
			"error: 'Analyzer nocolon' heading not followed by 'nocolon: summary...' line"},
	} {
		got, err := analysisinternal.ExtractDoc(test.content, test.name)
		if err != nil {
			got = "error: " + err.Error()
		}
		if test.want != got {
			t.Errorf("ExtractDoc(%q) returned <<%s>>, want <<%s>>, given input <<%s>>",
				test.name, got, test.want, test.content)
		}
	}
}