feat: navigate individual sections
This commit is contained in:
69
internal/note/sections_test.go
Normal file
69
internal/note/sections_test.go
Normal file
@ -0,0 +1,69 @@
|
||||
package note
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSlugifyHeading(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
}{
|
||||
{name: "simple", input: "Glossary", want: "glossary"},
|
||||
{name: "date and title", input: "2026-01-01 - First", want: "2026-01-01-first"},
|
||||
{name: "punctuation", input: "Hello, World!", want: "hello-world"},
|
||||
{name: "trim", input: " spaced out ", want: "spaced-out"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := slugifyHeading(tc.input); got != tc.want {
|
||||
t.Fatalf("expected %q, got %q", tc.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseSections(t *testing.T) {
|
||||
content := "# Title\nIntro\n## Alpha\nA1\nA2\n## Beta\nB1\n"
|
||||
|
||||
sections := ParseSections(content)
|
||||
if len(sections) != 2 {
|
||||
t.Fatalf("expected 2 sections, got %d", len(sections))
|
||||
}
|
||||
|
||||
if sections[0].Heading != "Alpha" {
|
||||
t.Fatalf("expected first heading Alpha, got %q", sections[0].Heading)
|
||||
}
|
||||
if sections[0].ID != "alpha" {
|
||||
t.Fatalf("expected first id alpha, got %q", sections[0].ID)
|
||||
}
|
||||
if want := "## Alpha\nA1\nA2\n"; sections[0].Content != want {
|
||||
t.Fatalf("expected first content %q, got %q", want, sections[0].Content)
|
||||
}
|
||||
|
||||
if sections[1].Heading != "Beta" {
|
||||
t.Fatalf("expected second heading Beta, got %q", sections[1].Heading)
|
||||
}
|
||||
if sections[1].ID != "beta" {
|
||||
t.Fatalf("expected second id beta, got %q", sections[1].ID)
|
||||
}
|
||||
if want := "## Beta\nB1\n"; sections[1].Content != want {
|
||||
t.Fatalf("expected second content %q, got %q", want, sections[1].Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseSections_DuplicateHeadings(t *testing.T) {
|
||||
content := "## Glossary\nTerm A\n## Glossary\nTerm B\n"
|
||||
|
||||
sections := ParseSections(content)
|
||||
if len(sections) != 2 {
|
||||
t.Fatalf("expected 2 sections, got %d", len(sections))
|
||||
}
|
||||
|
||||
if sections[0].ID != "glossary" {
|
||||
t.Fatalf("expected first id glossary, got %q", sections[0].ID)
|
||||
}
|
||||
if sections[1].ID != "glossary-2" {
|
||||
t.Fatalf("expected second id glossary-2, got %q", sections[1].ID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user