main mdparser_test.go
Eric Bower  ·  2026-09-14
 1package main
 2
 3import (
 4	"strings"
 5	"testing"
 6)
 7
 8func TestIsMarkdown(t *testing.T) {
 9	tests := []struct {
10		filename string
11		expected bool
12	}{
13		{"README.md", true},
14		{"readme.markdown", true},
15		{"doc.MD", true},
16		{"main.go", false},
17		{"Makefile", false},
18		{"file.txt", false},
19	}
20
21	for _, tt := range tests {
22		t.Run(tt.filename, func(t *testing.T) {
23			if got := isMarkdown(tt.filename); got != tt.expected {
24				t.Errorf("isMarkdown(%q) = %v, expected %v", tt.filename, got, tt.expected)
25			}
26		})
27	}
28}
29
30func TestParseMarkdown(t *testing.T) {
31	input := `# Hello World
32
33This is a paragraph with **bold** and *italic* text.
34
35## Features
36
37- Item 1
38- Item 2
39
40### Code block
41
42` + "```go\nfunc main() {\n\tprintln(\"hello\")\n}\n```" + `
43
44### Table
45
46| Name | Role |
47|------|------|
48| Alice | Admin |
49`
50
51	html, err := ParseMarkdown(input)
52	if err != nil {
53		t.Fatalf("ParseMarkdown returned error: %v", err)
54	}
55
56	// Heading should not be deleted from HTML
57	if !strings.Contains(html, `<h1 id="hello-world">Hello World`) {
58		t.Errorf("expected HTML to contain rendered <h1>, got:\n%s", html)
59	}
60
61	// Heading should have anchor
62	if !strings.Contains(html, `<a class="anchor" href="#hello-world"`) {
63		t.Errorf("expected anchor on heading, got:\n%s", html)
64	}
65
66	// Code block should have chroma classes
67	if !strings.Contains(html, `class="chroma"`) {
68		t.Errorf("expected code block to have chroma class, got:\n%s", html)
69	}
70
71	// Table should be rendered
72	if !strings.Contains(html, `<table>`) {
73		t.Errorf("expected table to be rendered, got:\n%s", html)
74	}
75}