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
|
package gotest
import "testing"
func TestParsePackageName(t *testing.T) {
lines := []string{"// comment", "package mypkg // trailing"}
if got := ParsePackageName(lines); got != "mypkg" {
t.Fatalf("got %q", got)
}
if got := ParsePackageName([]string{"no package"}); got != "" {
t.Fatalf("expected empty package name")
}
}
func TestParsePackageName_TabAfterName(t *testing.T) {
// Covers the tab-trimming branch in ParsePackageName.
lines := []string{"package mypkg\t// tab then comment"}
if got := ParsePackageName(lines); got != "mypkg" {
t.Fatalf("got %q, want %q", got, "mypkg")
}
}
func TestParsePackageName_SpaceAfterName(t *testing.T) {
// Covers the space-trimming branch (no comment, just trailing space).
lines := []string{"package mypkg "}
if got := ParsePackageName(lines); got != "mypkg" {
t.Fatalf("got %q, want %q", got, "mypkg")
}
}
func TestFindFunctionAtLine_NoBody(t *testing.T) {
lines := []string{"func X(a int)", "// comment"}
start, end := FindFunctionAtLine(lines, 0)
if start != 0 || end != 0 {
t.Fatalf("expected single-line prototype, got %d,%d", start, end)
}
}
func TestFindFunctionAtLine_EmptyLines(t *testing.T) {
// Covers the empty-lines early return.
start, end := FindFunctionAtLine([]string{}, 0)
if start != -1 || end != -1 {
t.Fatalf("expected -1,-1 for empty input, got %d,%d", start, end)
}
}
func TestFindFunctionAtLine_NegativeIdx(t *testing.T) {
// Covers the idx < 0 clamping branch.
lines := []string{"func Foo() {", " return", "}"}
start, end := FindFunctionAtLine(lines, -5)
if start != 0 || end != 2 {
t.Fatalf("expected 0,2 got %d,%d", start, end)
}
}
func TestFindFunctionAtLine_IdxBeyondEnd(t *testing.T) {
// Covers the idx >= len(lines) clamping branch.
// The last line contains "func " so the backward scan finds it directly.
lines := []string{"package main", "", "func Last() { }"}
start, end := FindFunctionAtLine(lines, 100)
if start != 2 || end != 2 {
t.Fatalf("expected 2,2 got %d,%d", start, end)
}
}
func TestFindFunctionAtLine_ClosingBraceBeforeFunc(t *testing.T) {
// When scanning backward, hitting '}' before 'func ' means no enclosing function.
lines := []string{"func A() {", "}", " x := 1"}
start, end := FindFunctionAtLine(lines, 2)
if start != -1 || end != -1 {
t.Fatalf("expected -1,-1 got %d,%d", start, end)
}
}
func TestFindFunctionAtLine_NormalFunction(t *testing.T) {
// Covers the normal path: finding a complete function with braces.
lines := []string{
"package main",
"",
"func Hello() {",
" fmt.Println(\"hi\")",
"}",
}
start, end := FindFunctionAtLine(lines, 3)
if start != 2 || end != 4 {
t.Fatalf("expected 2,4 got %d,%d", start, end)
}
}
func TestFindFunctionAtLine_UnclosedBrace(t *testing.T) {
// Covers the branch where opening brace is seen but never closed.
lines := []string{"func Broken() {", " x := 1"}
start, end := FindFunctionAtLine(lines, 0)
if start != 0 || end != -1 {
t.Fatalf("expected 0,-1 for unclosed brace, got %d,%d", start, end)
}
}
func TestFindFunctionAtLine_NestedBraces(t *testing.T) {
// Covers depth tracking with nested braces.
lines := []string{
"func Nested() {",
" if true {",
" x := 1",
" }",
"}",
}
start, end := FindFunctionAtLine(lines, 2)
if start != 0 || end != 4 {
t.Fatalf("expected 0,4 got %d,%d", start, end)
}
}
func TestDeriveFuncName(t *testing.T) {
if got := DeriveFuncName("func Sum(a int) int { return a }"); got != "Sum" {
t.Fatalf("got %q", got)
}
if got := DeriveFuncName("func (t *Type) Method(x int) {}"); got != "Method" {
t.Fatalf("got %q", got)
}
}
func TestDeriveFuncName_NotAFunc(t *testing.T) {
// Covers the early return when line doesn't start with "func ".
if got := DeriveFuncName("var x = 1"); got != "" {
t.Fatalf("expected empty, got %q", got)
}
}
func TestDeriveFuncName_MultiLine(t *testing.T) {
// Covers the firstLine newline-splitting branch.
code := "func Multi() {\n return\n}"
if got := DeriveFuncName(code); got != "Multi" {
t.Fatalf("got %q, want %q", got, "Multi")
}
}
func TestDeriveFuncName_MethodReceiverNoParenAfter(t *testing.T) {
// Covers the case where receiver is parsed but no '(' follows the name.
if got := DeriveFuncName("func (t *T) "); got != "" {
t.Fatalf("expected empty, got %q", got)
}
}
func TestExportName(t *testing.T) {
if got := ExportName("sum"); got != "Sum" {
t.Fatalf("got %q", got)
}
if got := ExportName("Sum"); got != "Sum" {
t.Fatalf("got %q", got)
}
}
func TestExportName_Empty(t *testing.T) {
// Covers the empty-string early return.
if got := ExportName(""); got != "" {
t.Fatalf("expected empty, got %q", got)
}
}
|