Columns (WIP): COL12's recorded Pandoc reading (all of Mapa's layouts and Join's output agree), an exhaustive merge of layout edits, and five mutations
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+544
−5
Makefile+7 −1internal/mapa/markup/merge_columns_test.go+76 −4internal/mapa/markup/pandoc_test.go+163 −0internal/mapa/markup/testdata/pandoc.json+252 −0scripts/mutate/columns.go+45 −0scripts/mutate/main.go+1 −0
diff --git a/Makefile b/Makefile| index a7b1db1..e9fd631 100644 |
| --- a/Makefile |
| +++ b/Makefile |
| @@ -21,7 +21,7 @@ VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null) |
| .PHONY: build release version-check deps test vet fmt-check code \ |
| migration-check mutate browser preview editor golden editor-golden \ |
| - corpus-local credits credits-check ci locale-dates |
| + corpus-local credits credits-check ci locale-dates pandoc-fixture |
| build: |
| CGO_ENABLED=0 go build ./... |
| @@ -122,6 +122,12 @@ golden: |
| CGO_ENABLED=0 go test ./internal/mapa/locale/ -run 'TestDateLabelsAreCurrent$$' -count=1 -update |
| $(MAKE) editor-golden |
| +# pandoc-fixture records how the pandoc on PATH reads every column layout |
| +# case and Join's output for it (columns and icons spec COL12), which the |
| +# markup tests hold Mapa to. CI has no pandoc; commit the result. |
| +pandoc-fixture: |
| + CGO_ENABLED=0 go test ./internal/mapa/markup/ -run 'TestCOL12' -count=1 -pandoc |
| + |
| # locale-dates rewrites internal/mapa/locale/dates.json, the CLDR date |
| # patterns Mapa writes dates with (level-up build note 85), from the ICU |
| # the node on PATH links, then the cases Go and the editor are held to. |
diff --git a/internal/mapa/markup/merge_columns_test.go b/internal/mapa/markup/merge_columns_test.go| index 7a03d97..3059dd4 100644 |
| --- a/internal/mapa/markup/merge_columns_test.go |
| +++ b/internal/mapa/markup/merge_columns_test.go |
| @@ -27,7 +27,7 @@ func merged(t *testing.T, base, mine, theirs string) ([]Hunk, bool, |
| if !ok { |
| t.Fatal("a clean merge did not settle") |
| } |
| - if !ValidLayouts(Parse(body)) { |
| + if !wholeLayouts(body) { |
| t.Fatalf("an invalid layout was settled:\n%s", body) |
| } |
| return hunks, true, body |
| @@ -53,7 +53,7 @@ func TestTheSameBlockConflictsInsideItsColumn(t *testing.T) { |
| } |
| for _, choice := range []Choice{KeepMine, KeepTheirs, KeepBoth} { |
| body, ok := Settle(hunks, []Choice{choice}) |
| - if !ok || !ValidLayouts(Parse(body)) || |
| + if !ok || !wholeLayouts(body) || |
| strings.Count(body, ":::: columns") != 1 { |
| t.Fatalf("choice %d:\n%s", choice, body) |
| } |
| @@ -80,7 +80,7 @@ func TestUndoingALayoutAgainstAddingAColumnIsOneConflict(t *testing.T) { |
| } |
| for _, choice := range []Choice{KeepMine, KeepTheirs, KeepBoth} { |
| body, ok := Settle(hunks, []Choice{choice}) |
| - if !ok || !ValidLayouts(Parse(body)) { |
| + if !ok || !wholeLayouts(body) { |
| t.Fatalf("choice %d gave an invalid body:\n%s", choice, body) |
| } |
| } |
| @@ -105,7 +105,7 @@ func TestABrokenCleanHunkBesideAConflictIsCaught(t *testing.T) { |
| choices[at] = choice |
| } |
| body, ok := Settle(hunks, choices) |
| - if !ok || !ValidLayouts(Parse(body)) { |
| + if !ok || !wholeLayouts(body) { |
| t.Fatalf("choice %d gave an invalid body:\n%s", choice, body) |
| } |
| } |
| @@ -119,3 +119,75 @@ func TestTwoRemovedColumnsLeavingOneAreAConflict(t *testing.T) { |
| t.Fatal("a one-column layout merged cleanly") |
| } |
| } |
| + |
| +// TestEveryMergeOfLayoutEditsHoldsValidLayouts merges every pair of a |
| +// set of edits to one body, and settles every conflict every way: each |
| +// result holds only whole layouts (§1.8, rule 2's invariant). |
| +func TestEveryMergeOfLayoutEditsHoldsValidLayouts(t *testing.T) { |
| + base := "Top.\n\n" + layoutOf("A1.\n\nA2.", "B.") + "\n\nEnd." |
| + edits := map[string]string{ |
| + "base": base, |
| + "edit top": strings.Replace(base, "Top.", "Top, edited.", 1), |
| + "edit A1": strings.Replace(base, "A1.", "A1, edited.", 1), |
| + "edit B": strings.Replace(base, "B.", "B, edited.", 1), |
| + "add column": "Top.\n\n" + layoutOf("A1.\n\nA2.", "B.", "C.") + "\n\nEnd.", |
| + "remove B": "Top.\n\nA1.\n\nA2.\n\nB.\n\nEnd.", |
| + "split A": "Top.\n\n" + layoutOf("A1.", "A2.", "B.") + "\n\nEnd.", |
| + "undo": "Top.\n\nA1.\n\nA2.\n\nB.\n\nEnd.", |
| + "undo, top": "Top, edited.\n\nA1.\n\nA2.\n\nB.\n\nEnd.", |
| + "split, top": "Top, again.\n\n" + layoutOf("A1.", "A2.", "B.") + "\n\nEnd.", |
| + "move A2": "Top.\n\n" + layoutOf("A1.", "A2.\n\nB.") + "\n\nEnd.", |
| + "add layout": base + "\n\n" + layoutOf("X.", "Y."), |
| + } |
| + for mineName, mine := range edits { |
| + for theirsName, theirs := range edits { |
| + hunks, _ := Merge3(base, mine, theirs) |
| + conflicts := 0 |
| + for _, hunk := range hunks { |
| + if hunk.Conflict { |
| + conflicts++ |
| + } |
| + } |
| + for _, choices := range everyChoice(conflicts) { |
| + body, ok := Settle(hunks, choices) |
| + if !ok || !wholeLayouts(body) { |
| + t.Errorf("%s against %s, choices %v:\n%s", mineName, |
| + theirsName, choices, body) |
| + } |
| + } |
| + } |
| + } |
| +} |
| + |
| +// everyChoice is every way to settle count conflicts. |
| +func everyChoice(count int) [][]Choice { |
| + out := [][]Choice{{}} |
| + for range count { |
| + var next [][]Choice |
| + for _, prefix := range out { |
| + for _, choice := range []Choice{KeepMine, KeepTheirs, KeepBoth} { |
| + next = append(next, append(append([]Choice{}, prefix...), |
| + choice)) |
| + } |
| + } |
| + out = next |
| + } |
| + return out |
| +} |
| + |
| +// wholeLayouts reports whether every fence-like line of body is a fence |
| +// of a whole layout: none is left as text, which is what a broken layout |
| +// parses as (these bodies type no fence-like text of their own). |
| +func wholeLayouts(body string) bool { |
| + for _, block := range Parse(body) { |
| + if IsFence(block.Kind) { |
| + continue |
| + } |
| + for _, line := range strings.Split(block.Source, "\n") { |
| + if FenceLike(line) { |
| + return false |
| + } |
| + } |
| + } |
| + return true |
| +} |
diff --git a/internal/mapa/markup/pandoc_test.go b/internal/mapa/markup/pandoc_test.go| new file mode 100644 |
| index 0000000..60820a6 |
| --- /dev/null |
| +++ b/internal/mapa/markup/pandoc_test.go |
| @@ -0,0 +1,163 @@ |
| +package markup |
| + |
| +import ( |
| + "bytes" |
| + "encoding/json" |
| + "flag" |
| + "os" |
| + "os/exec" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// COL12 (columns and icons spec §1.1, §9): Pandoc reads the layouts Mapa |
| +// writes as Mapa does. Pandoc is not in CI, so its reading is recorded: |
| +// make pandoc-fixture runs it over every case in corpus/columns.json and |
| +// over Join's output for each, and writes testdata/pandoc.json. The test |
| +// asserts agreement for every case Mapa recognises as a layout, and for |
| +// Join's output of each; the cases Mapa refuses on purpose (one column, five, |
| +// {.column}, nesting) are recorded, not asserted. |
| + |
| +var pandocUpdate = flag.Bool("pandoc", false, |
| + "rewrite testdata/pandoc.json with the pandoc on PATH") |
| + |
| +const pandocPath = "testdata/pandoc.json" |
| + |
| +// layoutShape is a body's column layouts, top level only, as each |
| +// layout's column count: "2,3" is a layout of two, then one of three. |
| +func layoutShape(blocks []Block) string { |
| + var counts []string |
| + for _, block := range blocks { |
| + if block.Kind == Columns { |
| + counts = append(counts, string(rune('0'+block.Level))) |
| + } |
| + } |
| + return strings.Join(counts, ",") |
| +} |
| + |
| +// pandocShape is the same, read from Pandoc's JSON: each top-level Div |
| +// classed columns, counted by its Divs classed column. |
| +func pandocShape(t *testing.T, body string) string { |
| + t.Helper() |
| + command := exec.Command("pandoc", "-f", "markdown", "-t", "json") |
| + command.Stdin = strings.NewReader(body) |
| + out, err := command.Output() |
| + if err != nil { |
| + t.Fatalf("pandoc: %v", err) |
| + } |
| + var doc struct { |
| + Blocks []json.RawMessage `json:"blocks"` |
| + } |
| + if err := json.Unmarshal(out, &doc); err != nil { |
| + t.Fatal(err) |
| + } |
| + var counts []string |
| + for _, raw := range doc.Blocks { |
| + if count, ok := divColumns(raw); ok { |
| + counts = append(counts, string(rune('0'+count))) |
| + } |
| + } |
| + return strings.Join(counts, ",") |
| +} |
| + |
| +type pandocBlock struct { |
| + T string `json:"t"` |
| + C json.RawMessage `json:"c"` |
| +} |
| + |
| +// divColumns is a Div classed columns and the Divs classed column in it. |
| +func divColumns(raw json.RawMessage) (int, bool) { |
| + classed := func(raw json.RawMessage, class string) ([]json.RawMessage, |
| + bool) { |
| + var block pandocBlock |
| + if json.Unmarshal(raw, &block) != nil || block.T != "Div" { |
| + return nil, false |
| + } |
| + var parts []json.RawMessage |
| + if json.Unmarshal(block.C, &parts) != nil || len(parts) != 2 { |
| + return nil, false |
| + } |
| + var attr []json.RawMessage |
| + var classes []string |
| + if json.Unmarshal(parts[0], &attr) != nil || len(attr) != 3 || |
| + json.Unmarshal(attr[1], &classes) != nil { |
| + return nil, false |
| + } |
| + var inside []json.RawMessage |
| + _ = json.Unmarshal(parts[1], &inside) |
| + return inside, len(classes) == 1 && classes[0] == class |
| + } |
| + inside, ok := classed(raw, "columns") |
| + if !ok { |
| + return 0, false |
| + } |
| + count := 0 |
| + for _, child := range inside { |
| + if _, ok := classed(child, "column"); ok { |
| + count++ |
| + } |
| + } |
| + return count, true |
| +} |
| + |
| +type pandocCase struct { |
| + Name string `json:"name"` |
| + Mapa string `json:"mapa"` |
| + Pandoc string `json:"pandoc"` |
| +} |
| + |
| +func pandocCases(t *testing.T) []corpusBody { |
| + t.Helper() |
| + var bodies []corpusBody |
| + readJSON(t, filepath.Join("testdata", "corpus", "columns.json"), &bodies) |
| + var out []corpusBody |
| + for _, body := range bodies { |
| + out = append(out, body) |
| + joined := Join(Parse(body.Body)) |
| + out = append(out, corpusBody{Name: body.Name + " (Join)", |
| + Body: joined}) |
| + } |
| + return out |
| +} |
| + |
| +func TestCOL12PandocReadsMapasLayouts(t *testing.T) { |
| + cases := pandocCases(t) |
| + if *pandocUpdate { |
| + var recorded []pandocCase |
| + for _, each := range cases { |
| + recorded = append(recorded, pandocCase{Name: each.Name, |
| + Mapa: layoutShape(Parse(each.Body)), |
| + Pandoc: pandocShape(t, each.Body)}) |
| + } |
| + var out bytes.Buffer |
| + encoder := json.NewEncoder(&out) |
| + encoder.SetIndent("", " ") |
| + _ = encoder.Encode(recorded) |
| + if err := os.WriteFile(pandocPath, out.Bytes(), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + return |
| + } |
| + var recorded []pandocCase |
| + readJSON(t, pandocPath, &recorded) |
| + byName := map[string]pandocCase{} |
| + for _, each := range recorded { |
| + byName[each.Name] = each |
| + } |
| + for _, each := range cases { |
| + seen, ok := byName[each.Name] |
| + mapa := layoutShape(Parse(each.Body)) |
| + if !ok || seen.Mapa != mapa { |
| + t.Errorf("%s: pandoc.json is stale: make pandoc-fixture", each.Name) |
| + continue |
| + } |
| + // Asserted wherever Mapa reads a layout, Join's output included; |
| + // text that only looks like fences is Mapa's text and Pandoc's |
| + // divs, and only recorded. |
| + if mapa != "" && seen.Pandoc != mapa { |
| + t.Errorf("%s: Mapa reads %q, Pandoc %q", each.Name, mapa, |
| + seen.Pandoc) |
| + } |
| + } |
| +} |
diff --git a/internal/mapa/markup/testdata/pandoc.json b/internal/mapa/markup/testdata/pandoc.json| new file mode 100644 |
| index 0000000..29fe716 |
| --- /dev/null |
| +++ b/internal/mapa/markup/testdata/pandoc.json |
| @@ -0,0 +1,252 @@ |
| +[ |
| + { |
| + "name": "columns/two", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/two (Join)", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/three-empty", |
| + "mapa": "3", |
| + "pandoc": "3" |
| + }, |
| + { |
| + "name": "columns/three-empty (Join)", |
| + "mapa": "3", |
| + "pandoc": "3" |
| + }, |
| + { |
| + "name": "columns/four", |
| + "mapa": "4", |
| + "pandoc": "4" |
| + }, |
| + { |
| + "name": "columns/four (Join)", |
| + "mapa": "4", |
| + "pandoc": "4" |
| + }, |
| + { |
| + "name": "columns/longer-fences", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/longer-fences (Join)", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/around", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/around (Join)", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/image", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/image (Join)", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/crlf", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/crlf (Join)", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/one", |
| + "mapa": "", |
| + "pandoc": "1" |
| + }, |
| + { |
| + "name": "columns/one (Join)", |
| + "mapa": "", |
| + "pandoc": "1" |
| + }, |
| + { |
| + "name": "columns/five", |
| + "mapa": "", |
| + "pandoc": "5" |
| + }, |
| + { |
| + "name": "columns/five (Join)", |
| + "mapa": "", |
| + "pandoc": "5" |
| + }, |
| + { |
| + "name": "columns/text-first", |
| + "mapa": "", |
| + "pandoc": "0" |
| + }, |
| + { |
| + "name": "columns/text-first (Join)", |
| + "mapa": "", |
| + "pandoc": "0" |
| + }, |
| + { |
| + "name": "columns/between", |
| + "mapa": "", |
| + "pandoc": "1" |
| + }, |
| + { |
| + "name": "columns/between (Join)", |
| + "mapa": "", |
| + "pandoc": "1" |
| + }, |
| + { |
| + "name": "columns/unclosed", |
| + "mapa": "", |
| + "pandoc": "1" |
| + }, |
| + { |
| + "name": "columns/unclosed (Join)", |
| + "mapa": "", |
| + "pandoc": "1" |
| + }, |
| + { |
| + "name": "columns/wrong-closer", |
| + "mapa": "", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/wrong-closer (Join)", |
| + "mapa": "", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/indented", |
| + "mapa": "", |
| + "pandoc": "" |
| + }, |
| + { |
| + "name": "columns/indented (Join)", |
| + "mapa": "", |
| + "pandoc": "" |
| + }, |
| + { |
| + "name": "columns/width", |
| + "mapa": "", |
| + "pandoc": "0" |
| + }, |
| + { |
| + "name": "columns/width (Join)", |
| + "mapa": "", |
| + "pandoc": "0" |
| + }, |
| + { |
| + "name": "columns/braces", |
| + "mapa": "", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/braces (Join)", |
| + "mapa": "", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/no-blank-before", |
| + "mapa": "", |
| + "pandoc": "" |
| + }, |
| + { |
| + "name": "columns/no-blank-before (Join)", |
| + "mapa": "", |
| + "pandoc": "" |
| + }, |
| + { |
| + "name": "columns/no-blank-after", |
| + "mapa": "", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/no-blank-after (Join)", |
| + "mapa": "", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/in-quote", |
| + "mapa": "", |
| + "pandoc": "" |
| + }, |
| + { |
| + "name": "columns/in-quote (Join)", |
| + "mapa": "", |
| + "pandoc": "" |
| + }, |
| + { |
| + "name": "columns/in-item", |
| + "mapa": "", |
| + "pandoc": "" |
| + }, |
| + { |
| + "name": "columns/in-item (Join)", |
| + "mapa": "", |
| + "pandoc": "" |
| + }, |
| + { |
| + "name": "columns/open-code", |
| + "mapa": "", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/open-code (Join)", |
| + "mapa": "", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/fence-like", |
| + "mapa": "", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/fence-like (Join)", |
| + "mapa": "", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/in-code", |
| + "mapa": "", |
| + "pandoc": "" |
| + }, |
| + { |
| + "name": "columns/in-code (Join)", |
| + "mapa": "", |
| + "pandoc": "" |
| + }, |
| + { |
| + "name": "columns/nested", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/nested (Join)", |
| + "mapa": "2", |
| + "pandoc": "2" |
| + }, |
| + { |
| + "name": "columns/stray", |
| + "mapa": "", |
| + "pandoc": "" |
| + }, |
| + { |
| + "name": "columns/stray (Join)", |
| + "mapa": "", |
| + "pandoc": "" |
| + } |
| +] |
diff --git a/scripts/mutate/columns.go b/scripts/mutate/columns.go| new file mode 100644 |
| index 0000000..aa1bc98 |
| --- /dev/null |
| +++ b/scripts/mutate/columns.go |
| @@ -0,0 +1,45 @@ |
| +package main |
| + |
| +// The columns and icons spec's mutations for column layouts (§9): the |
| +// grammar's column count, the merge's second pass, and the server's |
| +// block routes. |
| + |
| +const ( |
| + columnsGo = "internal/mapa/markup/columns.go" |
| + mergeGo = "internal/mapa/markup/merge.go" |
| + markersGo = "internal/mapa/markers.go" |
| + grammarGo = "internal/mapa/grammar.go" |
| + markupPackage = "./internal/mapa/markup/" |
| + secondPassFrom = "\tif texts.anyFence() && !texts.layoutsHold(hunks) {" |
| +) |
| + |
| +var columnMutations = []mutation{ |
| + {gate: "COL2", test: "TestShapesThatAreNotLayoutsParseAsBefore", |
| + pkg: markupPackage, |
| + name: "a one-column layout is recognised", |
| + edits: []edit{{file: columnsGo, |
| + from: "count >= 2 && count <= 4", |
| + to: "count >= 1 && count <= 4"}}}, |
| + {gate: "COL4", |
| + test: "TestEveryMergeOfLayoutEditsHoldsValidLayouts", |
| + pkg: markupPackage, |
| + name: "Merge3 skips the second pass", |
| + edits: []edit{{file: mergeGo, from: secondPassFrom, |
| + to: "\tif false {"}}}, |
| + {gate: "COL4", test: "TestEveryMergeOfLayoutEditsHoldsValidLayouts", |
| + pkg: markupPackage, |
| + name: "Merge3 runs the second pass only when clean", |
| + edits: []edit{{file: mergeGo, from: secondPassFrom, |
| + to: "\tif clean && texts.anyFence() && " + |
| + "!texts.layoutsHold(hunks) {"}}}, |
| + {gate: "COL13", test: "TestCOL13FencesAreNoBlockToMarkOrLink", |
| + name: "MarkBlock accepts a fence", |
| + edits: []edit{{file: markersGo, |
| + from: "\t\t\tmarkup.IsFence(block.Kind) {", |
| + to: "\t\t\tfalse {"}}}, |
| + {gate: "COL13", test: "TestCOL13AnotherGrammarIsRefusedAndTheDraftKept", |
| + name: "any grammar is taken", |
| + edits: []edit{{file: grammarGo, |
| + from: "\t\tif request.PostFormValue(\"grammar\") == grammarField {", |
| + to: "\t\tif true {"}}}, |
| +} |
diff --git a/scripts/mutate/main.go b/scripts/mutate/main.go| index 0810b45..da84093 100644 |
| --- a/scripts/mutate/main.go |
| +++ b/scripts/mutate/main.go |
| @@ -43,6 +43,7 @@ func every() []mutation { |
| all := append([]mutation{}, mutations...) |
| all = append(all, imageMutations...) |
| all = append(all, lookMutations...) |
| + all = append(all, columnMutations...) |
| all = append(all, mapperMutations...) |
| all = append(all, importMutations...) |
| return append(all, menuMutations...) |