| 1 | // Copyright 2023 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 analyzerutil_test |
| 6 | |
| 7 | import ( |
| 8 | "testing" |
| 9 | |
| 10 | "golang.org/x/tools/internal/analysis/analyzerutil" |
| 11 | ) |
| 12 | |
| 13 | func TestExtractDoc(t *testing.T) { |
| 14 | const multi = `// Copyright |
| 15 | |
| 16 | //+build tag |
| 17 | |
| 18 | // Package foo |
| 19 | // |
| 20 | // # Irrelevant heading |
| 21 | // |
| 22 | // This is irrelevant doc. |
| 23 | // |
| 24 | // # Analyzer nocolon |
| 25 | // |
| 26 | // This one has the wrong form for this line. |
| 27 | // |
| 28 | // # Analyzer food |
| 29 | // |
| 30 | // food: reports dining opportunities |
| 31 | // |
| 32 | // This is the doc for analyzer 'food'. |
| 33 | // |
| 34 | // # Analyzer foo |
| 35 | // |
| 36 | // foo: reports diagnostics |
| 37 | // |
| 38 | // This is the doc for analyzer 'foo'. |
| 39 | // |
| 40 | // # Analyzer bar |
| 41 | // |
| 42 | // bar: reports drinking opportunities |
| 43 | // |
| 44 | // This is the doc for analyzer 'bar'. |
| 45 | package blah |
| 46 | |
| 47 | var x = syntax error |
| 48 | ` |
| 49 | |
| 50 | for _, test := range []struct { |
| 51 | content, name string |
| 52 | want string // doc or "error: %w" string |
| 53 | }{ |
| 54 | {"", "foo", |
| 55 | "error: empty Go source file"}, |
| 56 | {"//foo", "foo", |
| 57 | "error: not a Go source file"}, |
| 58 | {"//foo\npackage foo", "foo", |
| 59 | "error: package doc comment contains no 'Analyzer foo' heading"}, |
| 60 | {multi, "foo", |
| 61 | "reports diagnostics\n\nThis is the doc for analyzer 'foo'."}, |
| 62 | {multi, "bar", |
| 63 | "reports drinking opportunities\n\nThis is the doc for analyzer 'bar'."}, |
| 64 | {multi, "food", |
| 65 | "reports dining opportunities\n\nThis is the doc for analyzer 'food'."}, |
| 66 | {multi, "nope", |
| 67 | "error: package doc comment contains no 'Analyzer nope' heading"}, |
| 68 | {multi, "nocolon", |
| 69 | "error: 'Analyzer nocolon' heading not followed by 'nocolon: summary...' line"}, |
| 70 | } { |
| 71 | got, err := analyzerutil.ExtractDoc(test.content, test.name) |
| 72 | if err != nil { |
| 73 | got = "error: " + err.Error() |
| 74 | } |
| 75 | if test.want != got { |
| 76 | t.Errorf("ExtractDoc(%q) returned <<%s>>, want <<%s>>, given input <<%s>>", |
| 77 | test.name, got, test.want, test.content) |
| 78 | } |
| 79 | } |
| 80 | } |