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 "strings"
5 "testing"
6)
7
8// Level-up §6.4, D13: a merged body changes bytes only in the blocks
9// mine changed and at their seams. Everything else is theirs, byte for
10// byte.
11
12const spacedBase = "# Plan\n\nOne.\n\nTwo.\n\nThree.\n\nFour."
13
14func mergedClean(t *testing.T, base, mine, theirs string) string {
15 t.Helper()
16 hunks, clean := Merge3(base, mine, theirs)
17 if !clean {
18 t.Fatalf("a conflict merging\n%q\n%q", mine, theirs)
19 }
20 body, ok := Settle(hunks, nil)
21 if !ok {
22 t.Fatal("a clean merge did not settle")
23 }
24 return body
25}
26
27func TestSettleKeepsTheirSpacingOutsideMineEdits(t *testing.T) {
28 theirs := "\n# Plan\n\n\n\nOne.\n\n \nTwo.\n\nThree, theirs." +
29 "\n\n\t\n\nFour."
30 mine := "# Plan\n\nOne.\n\nTwo.\n\nThree.\n\nFour, mine."
31 got := mergedClean(t, spacedBase, mine, theirs)
32 want := "\n# Plan\n\n\n\nOne.\n\n \nTwo.\n\nThree, theirs." +
33 "\n\nFour, mine."
34 if got != want {
35 t.Fatalf("got %q\nwant %q", got, want)
36 }
37}
38
39func TestSettleKeepsTheirBytesWhenMineChangedNothing(t *testing.T) {
40 theirs := "# Plan\n\n\n\nOne, theirs.\n\nTwo.\n\nThree.\n\n\nFour."
41 if got := mergedClean(t, spacedBase, spacedBase, theirs); got != theirs {
42 t.Fatalf("got %q", got)
43 }
44}
45
46func TestSettleKeepsMineOwnSeamsBetweenMineBlocks(t *testing.T) {
47 mine := "# Plan\n\nOne.\n\nNew A.\n\n\n\nNew B.\n\nTwo.\n\nThree." +
48 "\n\nFour."
49 theirs := "# Plan\n\n\nOne.\n\nTwo.\n\nThree.\n\n\n\nFour, theirs."
50 got := mergedClean(t, spacedBase, mine, theirs)
51 want := "# Plan\n\n\nOne.\n\nNew A.\n\n\n\nNew B.\n\nTwo.\n\nThree." +
52 "\n\n\n\nFour, theirs."
53 if got != want {
54 t.Fatalf("got %q\nwant %q", got, want)
55 }
56}
57
58func TestSettleKeepsATightListTight(t *testing.T) {
59 base := "Intro.\n\n- a\n- b\n- c"
60 mine := "Intro.\n\n- a\n- b, mine\n- c"
61 theirs := "Intro, theirs.\n\n\n- a\n- b\n- c"
62 got := mergedClean(t, base, mine, theirs)
63 if got != "Intro, theirs.\n\n\n- a\n- b, mine\n- c" {
64 t.Fatalf("got %q", got)
65 }
66}
67
68// Only the bytes inside the blocks mine changed, and the seams beside
69// them, differ from theirs.
70func TestSettleChangesOnlyMineBlocksAndTheirSeams(t *testing.T) {
71 theirs := "# Plan\n\n\n\nOne.\n\n \nTwo.\n\nThree.\n\n\n\nFour."
72 mine := "# Plan\n\nOne.\n\nTwo, mine.\n\nThree.\n\nFour."
73 got := mergedClean(t, spacedBase, mine, theirs)
74 head, tail := "# Plan\n\n\n\nOne.", "Three.\n\n\n\nFour."
75 if !strings.HasPrefix(got, head) || !strings.HasSuffix(got, tail) ||
76 !strings.Contains(got, "Two, mine.") {
77 t.Fatalf("got %q", got)
78 }
79}
80
81// The structure check: a result that would not parse back to the
82// settled blocks falls back to Join. Mine's two new blocks are
83// neighbours in mine, so keeping bytes would put mine's own wide seam
84// between them; and mine's unclosed fence, no longer last, would then
85// swallow theirs' block. Join's bytes differ from the kept ones, so the
86// test fails if the check is skipped (review: it passed without it).
87func TestSettleFallsBackToJoinWhenTheBytesWouldNotParse(t *testing.T) {
88 base := "A.\n\nB."
89 mine := "A.\n\nX.\n\n\n\n```\nmine code"
90 theirs := "A.\n\nB, theirs."
91 hunks, clean := Merge3(base, mine, theirs)
92 if clean {
93 t.Fatal("two edits to one block merged silently")
94 }
95 got, ok := Settle(hunks, []Choice{KeepBoth})
96 if !ok {
97 t.Fatal("a chosen conflict did not settle")
98 }
99 settled := []Block{{Kind: Paragraph, Source: "A."},
100 {Kind: Paragraph, Source: "X."},
101 {Kind: Code, Source: "```\nmine code"},
102 {Kind: Paragraph, Source: "B, theirs."}}
103 if want := Join(settled); got != want {
104 t.Fatalf("got %q, want Join's %q", got, want)
105 }
106 kept := "A.\n\nX.\n\n\n\n```\nmine code\n\nB, theirs."
107 if sameStructure(Parse(kept), settled) || kept == got {
108 t.Fatal("the vacuous case: the kept bytes parse as settled")
109 }
110}
111
112func TestAConflictsSourcesAreItsExactBytes(t *testing.T) {
113 base := "One.\n\nTwo.\n\nThree."
114 mine := "One.\n\nTwo,\nmine. \n\nThree."
115 theirs := "One.\n\nTwo, theirs.\n\nThree."
116 hunks, _ := Merge3(base, mine, theirs)
117 for _, hunk := range hunks {
118 if !hunk.Conflict {
119 continue
120 }
121 was, ours, others := hunk.Sources()
122 if was != "Two." || ours != "Two,\nmine. " ||
123 others != "Two, theirs." || hunk.MineAt != 1 {
124 t.Fatalf("sources %q %q %q at %d", was, ours, others,
125 hunk.MineAt)
126 }
127 return
128 }
129 t.Fatal("no conflict")
130}
131