| 12 | } |
| 13 | |
| 14 | func TestParser(t *testing.T) { |
| 15 | t.Run("Heading", func(t *testing.T) { |
| 16 | doc := NewParser("# Hello").Parse() |
| 17 | require.Equal(t, DocumentNode, doc.Type()) |
| 18 | require.Len(t, doc.Children(), 1) |
| 19 | heading := doc.Children()[0] |
| 20 | require.Equal(t, HeadingNode, heading.Type()) |
| 21 | require.Equal(t, "# Hello", doc.String(0)) |
| 22 | }) |
| 23 | |
| 24 | t.Run("Paragraph", func(t *testing.T) { |
| 25 | doc := NewParser("Hello world").Parse() |
| 26 | require.Len(t, doc.Children(), 1) |
| 27 | para := doc.Children()[0] |
| 28 | require.Equal(t, ParagraphNode, para.Type()) |
| 29 | require.Equal(t, "Hello world", doc.String(0)) |
| 30 | }) |
| 31 | |
| 32 | t.Run("HorizontalLine", func(t *testing.T) { |
| 33 | doc := NewParser("---").Parse() |
| 34 | require.Len(t, doc.Children(), 1) |
| 35 | sep := doc.Children()[0] |
| 36 | require.Equal(t, HorizontalLineNode, sep.Type()) |
| 37 | require.Equal(t, "---", doc.String(0)) |
| 38 | }) |
| 39 | |
| 40 | t.Run("HardBreak", func(t *testing.T) { |
| 41 | doc := NewParser("Line one \nLine two").Parse() |
| 42 | require.Equal(t, "Line one\nLine two", doc.String(0)) |
| 43 | }) |
| 44 | |
| 45 | t.Run("List", func(t *testing.T) { |
| 46 | doc := NewParser("- one\n- two").Parse() |
| 47 | require.Len(t, doc.Children(), 1) |
| 48 | list := doc.Children()[0] |
| 49 | require.Equal(t, ListNode, list.Type()) |
| 50 | require.Equal(t, "- one\n- two", doc.String(0)) |
| 51 | }) |
| 52 | |
| 53 | t.Run("InlineFormatting", func(t *testing.T) { |
| 54 | t.Run("Emphasis", func(t *testing.T) { |
| 55 | doc := NewParser("*em*").Parse() |
| 56 | require.Len(t, doc.Children(), 1) |
| 57 | para := doc.Children()[0].(*baseNode) |
| 58 | require.Equal(t, ParagraphNode, para.Type()) |
| 59 | children := para.Children() |
| 60 | require.Len(t, children, 1) |
| 61 | require.Equal(t, EmphasisNode, children[0].Type()) |
| 62 | require.Equal(t, "*em*", doc.String(0)) |
| 63 | }) |
| 64 | |
| 65 | t.Run("Strong", func(t *testing.T) { |
| 66 | doc := NewParser("**bold**").Parse() |
| 67 | require.Len(t, doc.Children(), 1) |
| 68 | para := doc.Children()[0].(*baseNode) |
| 69 | require.Equal(t, ParagraphNode, para.Type()) |
| 70 | children := para.Children() |
| 71 | require.Len(t, children, 1) |