oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package markup
2
3import (
4 "bytes"
5 "encoding/json"
6 "flag"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11 "testing"
12)
13
14// COL12 (columns and icons spec §1.1, §9): Pandoc reads the layouts Mapa
15// writes as Mapa does. Pandoc is not in CI, so its reading is recorded:
16// make pandoc-fixture runs it over every case in corpus/columns.json and
17// over Join's output for each, and writes testdata/pandoc.json. The test
18// asserts agreement for every case Mapa recognises as a layout, and for
19// Join's output of each; the cases Mapa refuses on purpose (one column, five,
20// {.column}, nesting) are recorded, not asserted.
21
22var pandocUpdate = flag.Bool("pandoc", false,
23 "rewrite testdata/pandoc.json with the pandoc on PATH")
24
25const pandocPath = "testdata/pandoc.json"
26
27// layoutShape is a body's column layouts, top level only, as each
28// layout's column count: "2,3" is a layout of two, then one of three.
29func layoutShape(blocks []Block) string {
30 var counts []string
31 for _, block := range blocks {
32 if block.Kind == Columns {
33 counts = append(counts, string(rune('0'+block.Level)))
34 }
35 }
36 return strings.Join(counts, ",")
37}
38
39// pandocShape is the same, read from Pandoc's JSON: each top-level Div
40// classed columns, counted by its Divs classed column.
41func pandocShape(t *testing.T, body string) string {
42 t.Helper()
43 command := exec.Command("pandoc", "-f", "markdown", "-t", "json")
44 command.Stdin = strings.NewReader(body)
45 out, err := command.Output()
46 if err != nil {
47 t.Fatalf("pandoc: %v", err)
48 }
49 var doc struct {
50 Blocks []json.RawMessage `json:"blocks"`
51 }
52 if err := json.Unmarshal(out, &doc); err != nil {
53 t.Fatal(err)
54 }
55 var counts []string
56 for _, raw := range doc.Blocks {
57 if count, ok := divColumns(raw); ok {
58 counts = append(counts, string(rune('0'+count)))
59 }
60 }
61 return strings.Join(counts, ",")
62}
63
64type pandocBlock struct {
65 T string `json:"t"`
66 C json.RawMessage `json:"c"`
67}
68
69// divColumns is a Div classed columns and the Divs classed column in it.
70func divColumns(raw json.RawMessage) (int, bool) {
71 classed := func(raw json.RawMessage, class string) ([]json.RawMessage,
72 bool) {
73 var block pandocBlock
74 if json.Unmarshal(raw, &block) != nil || block.T != "Div" {
75 return nil, false
76 }
77 var parts []json.RawMessage
78 if json.Unmarshal(block.C, &parts) != nil || len(parts) != 2 {
79 return nil, false
80 }
81 var attr []json.RawMessage
82 var classes []string
83 if json.Unmarshal(parts[0], &attr) != nil || len(attr) != 3 ||
84 json.Unmarshal(attr[1], &classes) != nil {
85 return nil, false
86 }
87 var inside []json.RawMessage
88 _ = json.Unmarshal(parts[1], &inside)
89 return inside, len(classes) == 1 && classes[0] == class
90 }
91 inside, ok := classed(raw, "columns")
92 if !ok {
93 return 0, false
94 }
95 count := 0
96 for _, child := range inside {
97 if _, ok := classed(child, "column"); ok {
98 count++
99 }
100 }
101 return count, true
102}
103
104type pandocCase struct {
105 Name string `json:"name"`
106 Mapa string `json:"mapa"`
107 Pandoc string `json:"pandoc"`
108}
109
110func pandocCases(t *testing.T) []corpusBody {
111 t.Helper()
112 var bodies []corpusBody
113 readJSON(t, filepath.Join("testdata", "corpus", "columns.json"), &bodies)
114 var out []corpusBody
115 for _, body := range bodies {
116 out = append(out, body)
117 joined := Join(Parse(body.Body))
118 out = append(out, corpusBody{Name: body.Name + " (Join)",
119 Body: joined})
120 }
121 return out
122}
123
124func TestCOL12PandocReadsMapasLayouts(t *testing.T) {
125 cases := pandocCases(t)
126 if *pandocUpdate {
127 var recorded []pandocCase
128 for _, each := range cases {
129 recorded = append(recorded, pandocCase{Name: each.Name,
130 Mapa: layoutShape(Parse(each.Body)),
131 Pandoc: pandocShape(t, each.Body)})
132 }
133 var out bytes.Buffer
134 encoder := json.NewEncoder(&out)
135 encoder.SetIndent("", " ")
136 _ = encoder.Encode(recorded)
137 if err := os.WriteFile(pandocPath, out.Bytes(), 0o644); err != nil {
138 t.Fatal(err)
139 }
140 return
141 }
142 var recorded []pandocCase
143 readJSON(t, pandocPath, &recorded)
144 byName := map[string]pandocCase{}
145 for _, each := range recorded {
146 byName[each.Name] = each
147 }
148 for _, each := range cases {
149 seen, ok := byName[each.Name]
150 mapa := layoutShape(Parse(each.Body))
151 if !ok || seen.Mapa != mapa {
152 t.Errorf("%s: pandoc.json is stale: make pandoc-fixture", each.Name)
153 continue
154 }
155 // Asserted wherever Mapa reads a layout, Join's output included;
156 // text that only looks like fences is Mapa's text and Pandoc's
157 // divs, and only recorded.
158 if mapa != "" && seen.Pandoc != mapa {
159 t.Errorf("%s: Mapa reads %q, Pandoc %q", each.Name, mapa,
160 seen.Pandoc)
161 }
162 }
163}
164