-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefparser_test.go
More file actions
126 lines (109 loc) · 3.69 KB
/
refparser_test.go
File metadata and controls
126 lines (109 loc) · 3.69 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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
// TestGenerateReferenceMarkdown_Nonexistent verifies an error is returned
// when the input directory does not exist.
func TestGenerateReferenceMarkdown_Nonexistent(t *testing.T) {
outFile := filepath.Join(t.TempDir(), "out.md")
err := generateReferenceMarkdown("does_not_exist", outFile)
if err == nil || !strings.Contains(err.Error(), "input directory does not exist") {
t.Fatalf("expected missing input dir error, got %v", err)
}
}
// TestGenerateReferenceMarkdown_Basic generates docs for a small sample package
// and checks that key sections and declarations are present.
func TestGenerateReferenceMarkdown_Basic(t *testing.T) {
inDir := t.TempDir()
src := `
// Package sample provides example functionality.
package sample
// Pi is the mathematical constant.
const Pi = 3.14
// Name holds the default name.
var Name = "test"
// Hello returns a greeting.
func Hello(who string) string {
return "Hello, " + who
}
// Counter counts things.
type Counter int
// Increment increases the counter.
func (c *Counter) Increment() {
*c++
}
`
if err := os.WriteFile(filepath.Join(inDir, "sample.go"), []byte(src), 0644); err != nil {
t.Fatalf("failed to write sample.go: %v", err)
}
outFile := filepath.Join(t.TempDir(), "REF.md")
if err := generateReferenceMarkdown(inDir, outFile); err != nil {
t.Fatalf("generateReferenceMarkdown failed: %v", err)
}
data, err := os.ReadFile(outFile)
if err != nil {
t.Fatalf("reading output file: %v", err)
}
md := string(data)
if !strings.Contains(md, "# Reference") {
t.Error("missing title '# Reference'")
}
for _, sec := range []string{"Overview", "Constants", "Variables", "Functions", "Types"} {
if !strings.Contains(md, "- ["+sec+"]") {
t.Errorf("TOC missing section %q", sec)
}
}
if !strings.Contains(md, "### `Pi`") || !strings.Contains(md, "const Pi = 3.14") {
t.Error("missing or malformed const Pi documentation")
}
if !strings.Contains(md, "### `Name`") || !strings.Contains(md, "var Name = \"test\"") {
t.Error("missing or malformed var Name documentation")
}
if !strings.Contains(md, "### `Hello`") || !strings.Contains(md, "func Hello(who string) string") {
t.Error("missing or malformed func Hello documentation")
}
if !strings.Contains(md, "### `Counter`") || !strings.Contains(md, "type Counter int") {
t.Error("missing or malformed type Counter documentation")
}
if !strings.Contains(md, "#### `Increment`") || !strings.Contains(md, "func (c *Counter) Increment()") {
t.Error("missing or malformed method Increment documentation")
}
}
// TestGenerateAnchor ensures generateAnchor produces GitHub-style slugs.
func TestGenerateAnchor(t *testing.T) {
cases := map[string]string{
"Simple Text": "simple-text",
"`Code` Example": "code-example",
"Mixed_Case 123": "mixedcase-123",
"Trailing - dash": "trailing-dash",
"***Stars***": "stars",
"Multiple---dashes": "multiple-dashes",
" leading-space": "leading-space",
"trailing-space ": "trailing-space",
}
for input, want := range cases {
if got := generateAnchor(input); got != want {
t.Errorf("generateAnchor(%q) = %q; want %q", input, got, want)
}
}
}
// TestFormatDocText ensures formatDocText converts paragraphs and preserves code blocks.
func TestFormatDocText(t *testing.T) {
raw := `This is a paragraph.
It has two lines.
indented code
More text after code.
- list item one
- list item two
`
out := formatDocText(raw)
if !strings.Contains(out, "```go") {
t.Error("expected code fence for indented block")
}
if !strings.Contains(out, "- list item one") || !strings.Contains(out, "- list item two") {
t.Error("list items not formatted correctly")
}
}