diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-16 03:10:55 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-16 03:10:55 +0200 |
| commit | 1fc1611fa99993cab5dc8bf0844183285296e3b2 (patch) | |
| tree | c5c9b8b5abac5b5d4c0d56ed90b0580184cc4383 /internal/gotest | |
| parent | 12090f25a3677291863dbb80277bdad3eaec0324 (diff) | |
Release v0.24.0v0.24.0
Bring unit test coverage from ~75% to 85.1% project-wide. All internal
packages now exceed 80% coverage. Refactored cmd entrypoints to extract
testable run() functions with injectable seams.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/gotest')
| -rw-r--r-- | internal/gotest/heuristics_test.go | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/internal/gotest/heuristics_test.go b/internal/gotest/heuristics_test.go index 831262d..6597238 100644 --- a/internal/gotest/heuristics_test.go +++ b/internal/gotest/heuristics_test.go @@ -12,6 +12,22 @@ func TestParsePackageName(t *testing.T) { } } +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) @@ -20,6 +36,81 @@ func TestFindFunctionAtLine_NoBody(t *testing.T) { } } +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) @@ -29,6 +120,28 @@ func TestDeriveFuncName(t *testing.T) { } } +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) @@ -37,3 +150,10 @@ func TestExportName(t *testing.T) { 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) + } +} |
