| 1 | package markup |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // COL2, the grammar (columns and icons spec §1.1, §9): the layouts Mapa |
| 9 | // recognises, and the shapes it does not, which parse exactly as they |
| 10 | // did before layouts existed. |
| 11 | |
| 12 | // columnKinds is each block's kind and column, as "kind/column". |
| 13 | func columnKinds(blocks []Block) string { |
| 14 | var out []string |
| 15 | for _, block := range blocks { |
| 16 | out = append(out, block.Kind+"/"+string(rune('0'+block.Column))) |
| 17 | } |
| 18 | return strings.Join(out, " ") |
| 19 | } |
| 20 | |
| 21 | // withoutLayouts parses body as Parse did before layouts: no layout is |
| 22 | // ever recognised. |
| 23 | func withoutLayouts(body string) []Block { |
| 24 | lines := strings.Split(normalise(body), "\n") |
| 25 | offsets := make([]int, len(lines)+1) |
| 26 | for index, line := range lines { |
| 27 | offsets[index+1] = offsets[index] + len(line) + 1 |
| 28 | } |
| 29 | return parseLines(lines, offsets, 0, len(lines), nil) |
| 30 | } |
| 31 | |
| 32 | const twoColumns = ":::: columns\n\n::: column\nLeft.\n\n- one\n- two\n\n" + |
| 33 | ":::\n\n::: column\nRight.\n\n:::\n\n::::" |
| 34 | |
| 35 | func TestALayoutIsRecognisedWhole(t *testing.T) { |
| 36 | cases := map[string]string{ |
| 37 | twoColumns: "columns/0 column/1 paragraph/1 item/1 item/1 " + |
| 38 | "column_end/1 column/2 paragraph/2 column_end/2 columns_end/0", |
| 39 | "Before.\n\n" + twoColumns + "\n\nAfter.": "paragraph/0 columns/0 " + |
| 40 | "column/1 paragraph/1 item/1 item/1 column_end/1 column/2 " + |
| 41 | "paragraph/2 column_end/2 columns_end/0 paragraph/0", |
| 42 | // Tight, empty, three columns, longer fences, CRLF. |
| 43 | "::::: columns\n:::: column\nA\n::::\n:::: column\n::::\n" + |
| 44 | ":::: column\n```\n:::\n```\n::::\n:::::": "columns/0 column/1 " + |
| 45 | "paragraph/1 column_end/1 column/2 column_end/2 column/3 code/3 " + |
| 46 | "column_end/3 columns_end/0", |
| 47 | strings.ReplaceAll(twoColumns, "\n", "\r\n"): "columns/0 column/1 " + |
| 48 | "paragraph/1 item/1 item/1 column_end/1 column/2 paragraph/2 " + |
| 49 | "column_end/2 columns_end/0", |
| 50 | // Text straight before a closer is ended by it. |
| 51 | ":::: columns\n::: column\nA\n> quoted\n:::\n::: column\nB\n:::\n" + |
| 52 | "::::": "columns/0 column/1 paragraph/1 quote/1 column_end/1 " + |
| 53 | "column/2 paragraph/2 column_end/2 columns_end/0", |
| 54 | } |
| 55 | for body, want := range cases { |
| 56 | if got := columnKinds(Parse(body)); got != want { |
| 57 | t.Errorf("%q:\n got %s\nwant %s", body, got, want) |
| 58 | } |
| 59 | } |
| 60 | blocks := Parse(twoColumns) |
| 61 | if blocks[0].Level != 2 || Plain(blocks[0]) != "" || |
| 62 | Words(blocks[1]) != "" { |
| 63 | t.Error("a layout's opener counts no columns, or a fence has words") |
| 64 | } |
| 65 | for _, block := range blocks { |
| 66 | if twoColumns[block.Start:block.End] != block.Source { |
| 67 | t.Errorf("%s at %d:%d is not its source", block.Kind, |
| 68 | block.Start, block.End) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestShapesThatAreNotLayoutsParseAsBefore(t *testing.T) { |
| 74 | col := func(text string) string { return "::: column\n" + text + "\n:::\n" } |
| 75 | open, shut := ":::: columns\n", "::::" |
| 76 | bodies := []string{ |
| 77 | open + col("A") + shut, // one column |
| 78 | open + strings.Repeat(col("A"), 5) + shut, // five |
| 79 | open + "Before.\n" + col("A") + col("B") + shut, // text first |
| 80 | open + col("A") + "Between.\n" + col("B") + shut, // between |
| 81 | open + "::: column\nA\n" + col("B") + shut, // unclosed |
| 82 | open + "::: column\nA\n::::\n" + col("B") + shut, // wrong closer |
| 83 | " " + open + col("A") + col("B") + shut, // indented |
| 84 | open + "::: column width=\"40%\"\nA\n:::\n" + col("B") + shut, |
| 85 | open + "::: {.column}\nA\n:::\n" + col("B") + shut, |
| 86 | // No blank line before, then none after. |
| 87 | "Text.\n" + open + col("A") + col("B") + shut, |
| 88 | open + col("A") + col("B") + shut + "\nText.", |
| 89 | "> " + open + col("A") + col("B") + shut, // in a quote |
| 90 | "- item\n " + open + col("A") + col("B") + shut, // in an item |
| 91 | open + "::: column\n```\nA\n:::\n" + col("B") + shut, // open code |
| 92 | open + col("A\n ::: stray") + col("B") + shut, // fence-like |
| 93 | "```\n" + open + col("A") + col("B") + shut + "\n```", // in code |
| 94 | } |
| 95 | for _, body := range bodies { |
| 96 | got := columnKinds(Parse(body)) |
| 97 | if want := columnKinds(withoutLayouts(body)); got != want { |
| 98 | t.Errorf("%q:\n got %s\nwant %s", body, got, want) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestANestedLayoutGivesTheInnerOne(t *testing.T) { |
| 104 | body := "::::: columns\n\n:::: column\n\n:::: columns\n\n::: column\n" + |
| 105 | "A\n:::\n\n::: column\nB\n:::\n\n::::\n\n::::\n\n" + |
| 106 | ":::: column\nC\n::::\n\n:::::" |
| 107 | got := columnKinds(Parse(body)) |
| 108 | if !strings.Contains(got, "columns/0 column/1 paragraph/1") || |
| 109 | strings.Count(got, "columns/0") != 1 { |
| 110 | t.Fatalf("a nested layout: %s", got) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func TestJoinWritesTheQuartoForm(t *testing.T) { |
| 115 | if got := Join(Parse(twoColumns)); got != twoColumns { |
| 116 | t.Fatalf("Join:\n%q\nwant\n%q", got, twoColumns) |
| 117 | } |
| 118 | tight := ":::: columns\n::: column\nA\n:::\n::: column\n:::\n::::" |
| 119 | want := ":::: columns\n\n::: column\nA\n\n:::\n\n::: column\n:::\n\n::::" |
| 120 | if got := Join(Parse(tight)); got != want { |
| 121 | t.Fatalf("Join of a tight layout:\n%q\nwant\n%q", got, want) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestALayoutRendersAsColumns(t *testing.T) { |
| 126 | got := string(Render(Parse(":::: columns\n::: column\nA\n:::\n"+ |
| 127 | "::: column\n:::\n::::"), nil)) |
| 128 | want := `<div mapa-columns="2"><div mapa-column><p>A</p></div>` + |
| 129 | `<div mapa-column>` + EmptyParagraph + `</div></div>` |
| 130 | if got != want { |
| 131 | t.Fatalf("Render:\n%s\nwant\n%s", got, want) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestFenceLike(t *testing.T) { |
| 136 | for line, want := range map[string]bool{":::": true, " ::: x": true, |
| 137 | "\t::::": true, "::": false, "a :::": false, "\u200b:::": false} { |
| 138 | if FenceLike(line) != want { |
| 139 | t.Errorf("FenceLike(%q) is not %v", line, want) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |