|
1 | 1 | package headingstyle |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "testing" |
6 | 5 |
|
7 | 6 | "github.com/jeduden/mdsmith/internal/lint" |
8 | 7 | "github.com/yuin/goldmark/ast" |
9 | 8 | "github.com/yuin/goldmark/text" |
10 | 9 | ) |
11 | 10 |
|
12 | | -func TestExploreHeadingWithManualChildren(t *testing.T) { |
13 | | - // Try to craft a heading with Lines().Len() == 0 but with text children |
14 | | - // by creating one manually and calling headingLine |
15 | | - |
| 11 | +func TestHeadingLine_ManualATXWithTextChild(t *testing.T) { |
| 12 | + // A manually constructed ATX heading with Lines().Len()==0 and a Text child; |
| 13 | + // headingLine should fall back to the child segment offset (line 1). |
16 | 14 | src := []byte("# Title\n") |
17 | 15 | f, err := lint.NewFile("test.md", src) |
18 | 16 | if err != nil { |
19 | 17 | t.Fatal(err) |
20 | 18 | } |
21 | | - _ = f |
22 | 19 |
|
23 | | - // Create a heading with no lines but manually attach a text child |
24 | 20 | h := ast.NewHeading(1) |
25 | 21 | textNode := ast.NewText() |
26 | 22 | textNode.Segment = text.NewSegment(2, 7) // "Title" in "# Title\n" |
27 | 23 | h.AppendChild(h, textNode) |
28 | 24 |
|
29 | | - fmt.Printf("Manual heading: Lines=%d, FirstChild=%T\n", h.Lines().Len(), h.FirstChild()) |
30 | 25 | line := headingLine(h, f) |
31 | | - fmt.Printf("headingLine returned: %d\n", line) |
| 26 | + if line < 1 { |
| 27 | + t.Errorf("expected headingLine >= 1, got %d", line) |
| 28 | + } |
32 | 29 | } |
33 | 30 |
|
34 | | -func TestExploreHeadingWithNonTextChild(t *testing.T) { |
35 | | - // Heading with Lines=0 and first child is Emphasis (not Text), wrapping Text |
| 31 | +func TestHeadingLine_ManualATXWithEmphasisChild(t *testing.T) { |
| 32 | + // Heading with Lines==0 and first child is Emphasis wrapping Text; |
| 33 | + // headingLine should still return a valid line number (>= 1). |
36 | 34 | src := []byte("# **bold**\n") |
37 | 35 | f, err := lint.NewFile("test.md", src) |
38 | 36 | if err != nil { |
39 | 37 | t.Fatal(err) |
40 | 38 | } |
41 | 39 |
|
42 | | - // Create heading manually |
43 | 40 | h := ast.NewHeading(1) |
44 | 41 | em := ast.NewEmphasis(2) |
45 | 42 | textNode := ast.NewText() |
46 | | - textNode.Segment = text.NewSegment(3, 7) // "bold" in "# **bold**\n" -- rough |
| 43 | + textNode.Segment = text.NewSegment(3, 7) |
47 | 44 | em.AppendChild(em, textNode) |
48 | 45 | h.AppendChild(h, em) |
49 | 46 |
|
50 | | - fmt.Printf("Manual heading with emphasis: Lines=%d\n", h.Lines().Len()) |
51 | 47 | line := headingLine(h, f) |
52 | | - fmt.Printf("headingLine returned: %d (expected 1 for offset 3)\n", line) |
| 48 | + if line < 1 { |
| 49 | + t.Errorf("expected headingLine >= 1, got %d", line) |
| 50 | + } |
53 | 51 | } |
0 commit comments