|
5 | 5 | package astutil_test |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "fmt" |
8 | 9 | "go/ast" |
9 | 10 | "go/parser" |
10 | 11 | "go/token" |
@@ -57,3 +58,137 @@ func fn() { }` |
57 | 58 | }) |
58 | 59 | } |
59 | 60 | } |
| 61 | + |
| 62 | +func TestDeprecation(t *testing.T) { |
| 63 | + testsCases := []struct { |
| 64 | + name string |
| 65 | + in string |
| 66 | + want string |
| 67 | + }{ |
| 68 | + { |
| 69 | + name: "doc_comment_only_paragraph", |
| 70 | + in: `// Deprecated: Test |
| 71 | + // a whole paragraph. |
| 72 | + type A struct {}`, |
| 73 | + want: "Deprecated: Test\na whole paragraph.\n", |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "doc_comment_any_paragraph", |
| 77 | + in: `// First paragraph |
| 78 | + // |
| 79 | + // Deprecated: Middle |
| 80 | + // paragraph. |
| 81 | + // |
| 82 | + // Last Paragraph |
| 83 | + type A struct {}`, |
| 84 | + want: "Deprecated: Middle\nparagraph.", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "doc_comment_finds_the_first", |
| 88 | + in: `// Deprecated: First |
| 89 | + // |
| 90 | + // Deprecated: second |
| 91 | + type A struct {}`, |
| 92 | + want: "Deprecated: First", |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "doc_comment_not_found_inside_paragraph", |
| 96 | + in: `// First paragraph |
| 97 | + // Deprecated: Middle paragraph. |
| 98 | + type A struct {}`, |
| 99 | + want: "", |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "multi_line_doc_comment_supported_if_no_whitespace", |
| 103 | + in: ` |
| 104 | +/*First paragraph |
| 105 | +
|
| 106 | +Deprecated: Middle paragraph |
| 107 | +
|
| 108 | +Last Paragraph |
| 109 | +*/ |
| 110 | +type A struct {}`, |
| 111 | + want: "Deprecated: Middle paragraph", |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "multi_line_doc_comment_weird_format_not_supported", |
| 115 | + // This is what the go formatter formats when the text starts on |
| 116 | + // the second line of a /* */ comment. |
| 117 | + in: ` |
| 118 | + /* |
| 119 | + First paragraph |
| 120 | +
|
| 121 | + Deprecated: Middle paragraph |
| 122 | +
|
| 123 | + Last Paragraph |
| 124 | + */ |
| 125 | + type A struct {}`, |
| 126 | + // Not found, as the "Deprecated: ..." line has leading whitespace. |
| 127 | + want: "", |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "line_comment_just_deprecated_tag", |
| 131 | + in: `type A interface { |
| 132 | + B(int) int // Deprecated: use 'C()' |
| 133 | + }`, |
| 134 | + want: "Deprecated: use 'C()'\n", |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "line_comment_comment_before_deprecated_tag", |
| 138 | + in: `type A struct { |
| 139 | + b int // This does x. Deprecated: use 'c'. Will cleanup. |
| 140 | + }`, |
| 141 | + want: "Deprecated: use 'c'. Will cleanup.\n", |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "line_comment_finds_first_deprecated_tag", |
| 145 | + in: `type A struct { |
| 146 | + int // Deprecated: use 'c'. Deprecated: use 'd'. |
| 147 | + }`, |
| 148 | + want: "Deprecated: use 'c'. Deprecated: use 'd'.\n", |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "line_comment_doesnt_support_multi_line_comment_type", |
| 152 | + in: `type A struct { |
| 153 | + b int /*Deprecated: use 'c'*/ |
| 154 | + }`, |
| 155 | + // We can't prevent this, as ast.CommentGroup doesn't specify |
| 156 | + // where the comment happened. We won't advertise this. |
| 157 | + want: "Deprecated: use 'c'\n", |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "multiline_comment_cant_have_comment_before_deprecated_tag", |
| 161 | + in: `type A struct { |
| 162 | + b int /* test Deprecated: use 'c' */ |
| 163 | + }`, |
| 164 | + want: "", |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + for _, test := range testsCases { |
| 169 | + t.Run(test.name, func(t *testing.T) { |
| 170 | + src := fmt.Sprintf("package a; \n\n%s", test.in) |
| 171 | + f, err := parser.ParseFile(token.NewFileSet(), "a.go", src, parser.ParseComments) |
| 172 | + if err != nil { |
| 173 | + t.Fatal(err) |
| 174 | + } |
| 175 | + switch len(f.Comments) { |
| 176 | + case 0: |
| 177 | + t.Error("No `ast.CommentGroup` found") |
| 178 | + case 1: |
| 179 | + default: |
| 180 | + t.Errorf("%d `ast.CommentGroup`s found, only want one", len(f.Comments)) |
| 181 | + } |
| 182 | + if got := astutil.Deprecation(f.Comments[0]); got != test.want { |
| 183 | + // align 'got' and 'want' for easier inspection |
| 184 | + t.Errorf("\nfound comment: %q\ngot: %q\nwant: %q", f.Comments[0].Text(), got, test.want) |
| 185 | + } |
| 186 | + }) |
| 187 | + } |
| 188 | + |
| 189 | + t.Run("Deprecation(nil)", func(t *testing.T) { |
| 190 | + if got := astutil.Deprecation(nil); got != "" { |
| 191 | + t.Errorf("Deprecation(nil) = %q, want: \"\"", got) |
| 192 | + } |
| 193 | + }) |
| 194 | +} |
0 commit comments