-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathdocs_test.go
More file actions
187 lines (159 loc) · 4.86 KB
/
docs_test.go
File metadata and controls
187 lines (159 loc) · 4.86 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
package ecspresso_test
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/kayac/ecspresso/v2"
)
func TestParseSections(t *testing.T) {
content := "# Title\n\nIntro text.\n\n## Section A\n\nBody A.\n\n### Subsection A1\n\nBody A1.\n\n## Section B\n\nBody B.\n"
sections := ecspresso.ParseSections(content)
if len(sections) != 4 {
t.Fatalf("expected 4 sections, got %d", len(sections))
}
want := []struct {
level int
title string
line int
}{
{1, "Title", 1},
{2, "Section A", 5},
{3, "Subsection A1", 9},
{2, "Section B", 13},
}
for i, w := range want {
s := sections[i]
if s.Level != w.level {
t.Errorf("section[%d] level: want %d, got %d", i, w.level, s.Level)
}
if s.Title != w.title {
t.Errorf("section[%d] title: want %q, got %q", i, w.title, s.Title)
}
if s.Line != w.line {
t.Errorf("section[%d] line: want %d, got %d", i, w.line, s.Line)
}
}
// verify content of first section includes intro text
if !strings.Contains(sections[0].Content, "Intro text.") {
t.Errorf("section[0] content should contain 'Intro text.', got %q", sections[0].Content)
}
// verify section A does not contain subsection A1 content
if strings.Contains(sections[1].Content, "Body A1.") {
t.Errorf("section[1] should not contain 'Body A1.' (leaf section)")
}
}
func TestParseSectionsCodeBlock(t *testing.T) {
content := "## Real Section\n\n```yaml\n# this is a comment not a heading\nfoo: bar\n```\n\n## Next Section\n\nBody.\n"
sections := ecspresso.ParseSections(content)
if len(sections) != 2 {
t.Fatalf("expected 2 sections (code block # should be ignored), got %d", len(sections))
}
if sections[0].Title != "Real Section" {
t.Errorf("section[0] title: want %q, got %q", "Real Section", sections[0].Title)
}
if sections[1].Title != "Next Section" {
t.Errorf("section[1] title: want %q, got %q", "Next Section", sections[1].Title)
}
// verify the code block content is in the first section
if !strings.Contains(sections[0].Content, "# this is a comment not a heading") {
t.Errorf("section[0] should contain code block content")
}
}
func TestParseSectionsEmpty(t *testing.T) {
sections := ecspresso.ParseSections("")
if len(sections) != 0 {
t.Errorf("expected 0 sections for empty content, got %d", len(sections))
}
}
func TestParseSectionsReadme(t *testing.T) {
content := ecspresso.ReadmeContent
sections := ecspresso.ParseSections(content)
if len(sections) == 0 {
t.Fatal("expected non-zero sections from README.md")
}
// verify well-known headings exist
titles := make(map[string]bool)
for _, s := range sections {
titles[s.Title] = true
}
for _, expected := range []string{"ecspresso", "Install", "Usage", "Plugins"} {
if !titles[expected] {
t.Errorf("expected heading %q not found in README.md sections", expected)
}
}
// verify first section is the top-level heading
if sections[0].Level != 1 || sections[0].Title != "ecspresso" {
t.Errorf("first section should be '# ecspresso', got level=%d title=%q", sections[0].Level, sections[0].Title)
}
}
func TestDocsSearch(t *testing.T) {
content := ecspresso.ReadmeContent
sections := ecspresso.ParseSections(content)
query := "fargate"
lowerQuery := strings.ToLower(query)
var matched []string
for _, s := range sections {
if strings.Contains(strings.ToLower(s.Content), lowerQuery) {
matched = append(matched, s.Title)
}
}
if len(matched) == 0 {
t.Fatal("expected at least one section matching 'fargate'")
}
// verify well-known fargate sections
found := false
for _, title := range matched {
if strings.Contains(strings.ToLower(title), "fargate") {
found = true
break
}
}
if !found {
t.Errorf("expected a section with 'fargate' in the title among matched sections: %v", matched)
}
}
func TestDocsSearchNoMatch(t *testing.T) {
content := "# Title\n\nSome content.\n"
sections := ecspresso.ParseSections(content)
query := "zzz_nonexistent_keyword_zzz"
lowerQuery := strings.ToLower(query)
var matched int
for _, s := range sections {
if strings.Contains(strings.ToLower(s.Content), lowerQuery) {
matched++
}
}
if matched != 0 {
t.Errorf("expected 0 matches for non-existent keyword, got %d", matched)
}
}
func TestDocsOptionDefault(t *testing.T) {
want := &ecspresso.DocsOption{
Article: "readme",
List: false,
Index: false,
Search: "",
JSON: false,
}
args := []string{"docs"}
_, opts, _, err := ecspresso.ParseCLIv2(args)
if err != nil {
t.Fatal(err)
}
got := opts.ForSubCommand("docs")
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("DocsOption mismatch (-want +got):\n%s", diff)
}
}
func TestDocsListText(t *testing.T) {
err := ecspresso.Docs(t.Context(), ecspresso.DocsOption{List: true})
if err != nil {
t.Fatal(err)
}
}
func TestDocsListJSON(t *testing.T) {
err := ecspresso.Docs(t.Context(), ecspresso.DocsOption{List: true, JSON: true})
if err != nil {
t.Fatal(err)
}
}