Skip to content
quarry Artifact [b5125c887c]
Login

Artifact [b5125c887c]

Artifact b5125c887cf25ef9df2e97dafa7712bbf25ac3e0da1f163f0ae0c94fc8c6ca8a:


package markup

import (
	"strings"
	"testing"
)

// Blocks always join back to the source exactly, and fences, <details>
// and indented continuations stay whole across their blank lines.
func TestSplitBlocksKeepsEveryByteAndEveryWhole(t *testing.T) {
	cases := []struct {
		name string
		src  string
		want int
	}{
		{"empty", "", 0},
		{"one paragraph", "hello\nworld\n", 1},
		{"three blocks", "# Title\n\nA paragraph.\n\n- a\n- b\n", 3},
		{"leading and trailing blanks", "\n\nfirst\n\n\nsecond\n\n", 2},
		{"no final newline", "a\n\nb", 2},
		{"fence with blank lines", "intro\n\n```go\nx := 1\n\ny := 2\n```\n\nafter\n", 3},
		{"tilde fence and a longer close", "~~~\none\n\ntwo\n~~~~\n\nafter\n", 2},
		{"poll fence", "```poll\nquestion: Q?\n\n- a\n- b\n```\n", 1},
		{"details", "<details><summary>s</summary>\n\nbody\n\nmore\n\n</details>\n\nafter\n", 2},
		{"list item with a second paragraph", "- one\n\n  still one\n- two\n\nnext\n", 2},
		{"a loose list stays one block", "- one\n\n- two\n\n1. three\n\nafter\n", 2},
		{"a list after a paragraph is its own block", "para\n\n- one\n\n- two\n", 2},
		{"unclosed fence runs to the end", "a\n\n```\ncode\n\nmore\n", 2},
	}
	for _, c := range cases {
		got := SplitBlocks(c.src)
		if strings.Join(got, "") != c.src {
			t.Errorf("FAIL %s: blocks %q do not join back to the source", c.name, got)
		}
		if len(got) != c.want {
			t.Errorf("FAIL %s: %d blocks %q, want %d", c.name, len(got), got, c.want)
		}
	}
}