-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc_comment_test.go
More file actions
190 lines (176 loc) · 4.14 KB
/
Copy pathdoc_comment_test.go
File metadata and controls
190 lines (176 loc) · 4.14 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package lexical
import "testing"
func TestExtractJavadocBlock(t *testing.T) {
lines := []string{
"package x;",
"",
"/**",
" * Returns the user.",
" * @param id user id",
" */",
"public User get(int id) {",
}
got := Extract(lines, "java", 7)
want := "Returns the user. @param id user id"
if got != want {
t.Fatalf("Javadoc extract = %q, want %q", got, want)
}
}
func TestExtractJavadocSingleLineBlock(t *testing.T) {
lines := []string{
"/** Returns the user. */",
"public User get() {}",
}
got := Extract(lines, "java", 2)
want := "Returns the user."
if got != want {
t.Fatalf("single-line block = %q, want %q", got, want)
}
}
func TestExtractJSDocWithParams(t *testing.T) {
lines := []string{
"/**",
" * Add two numbers.",
" * @param {number} a",
" * @param {number} b",
" */",
"function add(a, b) {",
}
got := Extract(lines, "typescript", 6)
want := "Add two numbers. @param {number} a @param {number} b"
if got != want {
t.Fatalf("JSDoc extract = %q, want %q", got, want)
}
}
func TestExtractCppDoxygenSingleLineBlock(t *testing.T) {
lines := []string{
"/** @brief Computes pi. */",
"double pi();",
}
got := Extract(lines, "cpp", 2)
want := "@brief Computes pi."
if got != want {
t.Fatalf("Doxygen extract = %q, want %q", got, want)
}
}
func TestExtractGoLineComments(t *testing.T) {
lines := []string{
"package main",
"",
"// Greet prints hello.",
"// Use it sparingly.",
"func Greet() {}",
}
got := Extract(lines, "go", 5)
want := "Greet prints hello. Use it sparingly."
if got != want {
t.Fatalf("go line comments = %q, want %q", got, want)
}
}
func TestExtractRustTripleSlash(t *testing.T) {
lines := []string{
"/// Returns the answer.",
"/// Always 42.",
"fn answer() -> i32 { 42 }",
}
got := Extract(lines, "rust", 3)
want := "Returns the answer. Always 42."
if got != want {
t.Fatalf("rust /// = %q, want %q", got, want)
}
}
func TestExtractPythonSingleLineDocstring(t *testing.T) {
lines := []string{
"def add(a, b):",
` """Return the sum."""`,
" return a + b",
}
got := Extract(lines, "python", 1)
want := "Return the sum."
if got != want {
t.Fatalf("python single-line = %q, want %q", got, want)
}
}
func TestExtractPythonMultiLineDoubleQuoted(t *testing.T) {
lines := []string{
"def add(a, b):",
` """`,
" Return the sum.",
" Of two numbers.",
` """`,
" return a + b",
}
got := Extract(lines, "python", 1)
want := "Return the sum. Of two numbers."
if got != want {
t.Fatalf("python multi-line double = %q, want %q", got, want)
}
}
func TestExtractPythonMultiLineSingleQuoted(t *testing.T) {
lines := []string{
"def add(a, b):",
" '''",
" Return the sum.",
" '''",
" return a + b",
}
got := Extract(lines, "python", 1)
want := "Return the sum."
if got != want {
t.Fatalf("python multi-line single = %q, want %q", got, want)
}
}
func TestExtractSkipsAnnotationLines(t *testing.T) {
lines := []string{
"/**",
" * Returns the user.",
" */",
"@Override",
"@Deprecated",
"public User get() {}",
}
got := Extract(lines, "java", 6)
want := "Returns the user."
if got != want {
t.Fatalf("annotation walk-back = %q, want %q", got, want)
}
}
func TestExtractAbortsOnBlankLineGap(t *testing.T) {
lines := []string{
"/** Stale comment. */",
"",
"int x = 5;",
"",
"public User get() {}",
}
got := Extract(lines, "java", 5)
if got != "" {
t.Fatalf("blank-line gap should abort scan, got %q", got)
}
}
func TestExtractEmptyInputs(t *testing.T) {
if Extract(nil, "java", 1) != "" {
t.Fatal("nil lines should return empty")
}
if Extract([]string{"x"}, "java", 0) != "" {
t.Fatal("lineStart 0 should return empty")
}
if Extract([]string{"x"}, "java", 5) != "" {
t.Fatal("out-of-range lineStart should return empty")
}
}
func TestExtractNoCommentReturnsEmpty(t *testing.T) {
lines := []string{
"package x;",
"public User get() {}",
}
if Extract(lines, "java", 2) != "" {
t.Fatal("no comment should return empty")
}
if Extract(lines, "go", 2) != "" {
t.Fatal("no comment go should return empty")
}
if Extract(lines, "python", 1) != "" {
t.Fatal("no docstring should return empty")
}
}