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// COL4, the merge over column layouts (columns and icons spec §1.8).
9
10func layoutOf(columns ...string) string {
11 var out strings.Builder
12 out.WriteString(":::: columns\n\n")
13 for _, column := range columns {
14 out.WriteString("::: column\n" + column + "\n\n:::\n\n")
15 }
16 return out.String() + "::::"
17}
18
19func merged(t *testing.T, base, mine, theirs string) ([]Hunk, bool,
20 string) {
21 t.Helper()
22 hunks, clean := Merge3(base, mine, theirs)
23 if !clean {
24 return hunks, false, ""
25 }
26 body, ok := Settle(hunks, nil)
27 if !ok {
28 t.Fatal("a clean merge did not settle")
29 }
30 if !wholeLayouts(body) {
31 t.Fatalf("an invalid layout was settled:\n%s", body)
32 }
33 return hunks, true, body
34}
35
36func TestEditsToDifferentColumnsMerge(t *testing.T) {
37 base := "Top.\n\n" + layoutOf("Left.", "Right.")
38 mine := "Top.\n\n" + layoutOf("Left, Ana.", "Right.")
39 theirs := "Top.\n\n" + layoutOf("Left.", "Right, Bo.")
40 _, clean, body := merged(t, base, mine, theirs)
41 if !clean || body != "Top.\n\n"+layoutOf("Left, Ana.", "Right, Bo.") {
42 t.Fatalf("clean %v:\n%s", clean, body)
43 }
44}
45
46func TestTheSameBlockConflictsInsideItsColumn(t *testing.T) {
47 base := layoutOf("Left.", "Right.")
48 mine := layoutOf("Left, Ana.", "Right.")
49 theirs := layoutOf("Left, Bo.", "Right.")
50 hunks, clean := Merge3(base, mine, theirs)
51 if clean {
52 t.Fatal("two edits to one block merged")
53 }
54 for _, choice := range []Choice{KeepMine, KeepTheirs, KeepBoth} {
55 body, ok := Settle(hunks, []Choice{choice})
56 if !ok || !wholeLayouts(body) ||
57 strings.Count(body, ":::: columns") != 1 {
58 t.Fatalf("choice %d:\n%s", choice, body)
59 }
60 }
61}
62
63func TestAddingAColumnBesideAnEditMergesWithBothBytes(t *testing.T) {
64 base := layoutOf("Left.", "Right.")
65 mine := layoutOf("Left.", "Middle.", "Right.")
66 theirs := layoutOf("Left.", "Right, Bo.")
67 _, clean, body := merged(t, base, mine, theirs)
68 if !clean || body != layoutOf("Left.", "Middle.", "Right, Bo.") {
69 t.Fatalf("clean %v:\n%s", clean, body)
70 }
71}
72
73func TestUndoingALayoutAgainstAddingAColumnIsOneConflict(t *testing.T) {
74 base := layoutOf("Left.", "Right.")
75 mine := "Left.\n\nRight."
76 theirs := layoutOf("Left.", "Middle.", "Right.")
77 hunks, clean := Merge3(base, mine, theirs)
78 if clean {
79 t.Fatal("a broken layout merged cleanly")
80 }
81 for _, choice := range []Choice{KeepMine, KeepTheirs, KeepBoth} {
82 body, ok := Settle(hunks, []Choice{choice})
83 if !ok || !wholeLayouts(body) {
84 t.Fatalf("choice %d gave an invalid body:\n%s", choice, body)
85 }
86 }
87}
88
89func TestABrokenCleanHunkBesideAConflictIsCaught(t *testing.T) {
90 base := "Top.\n\n" + layoutOf("A1.\n\nA2.", "B.")
91 mine := "Top, Ana.\n\nA1.\n\nA2.\n\nB."
92 theirs := "Top, Bo.\n\n" + layoutOf("A1.", "A2.", "B.")
93 hunks, clean := Merge3(base, mine, theirs)
94 if clean {
95 t.Fatal("merged cleanly")
96 }
97 choices := make([]Choice, 0)
98 for _, hunk := range hunks {
99 if hunk.Conflict {
100 choices = append(choices, KeepMine)
101 }
102 }
103 for _, choice := range []Choice{KeepMine, KeepTheirs, KeepBoth} {
104 for at := range choices {
105 choices[at] = choice
106 }
107 body, ok := Settle(hunks, choices)
108 if !ok || !wholeLayouts(body) {
109 t.Fatalf("choice %d gave an invalid body:\n%s", choice, body)
110 }
111 }
112}
113
114func TestTwoRemovedColumnsLeavingOneAreAConflict(t *testing.T) {
115 base := layoutOf("A.", "B.", "C.")
116 mine := layoutOf("B.", "C.")
117 theirs := layoutOf("A.", "C.")
118 if _, clean := Merge3(base, mine, theirs); clean {
119 t.Fatal("a one-column layout merged cleanly")
120 }
121}
122
123// TestEveryMergeOfLayoutEditsHoldsValidLayouts merges every pair of a
124// set of edits to one body, and settles every conflict every way: each
125// result holds only whole layouts (§1.8, rule 2's invariant).
126func TestEveryMergeOfLayoutEditsHoldsValidLayouts(t *testing.T) {
127 base := "Top.\n\n" + layoutOf("A1.\n\nA2.", "B.") + "\n\nEnd."
128 edits := map[string]string{
129 "base": base,
130 "edit top": strings.Replace(base, "Top.", "Top, edited.", 1),
131 "edit A1": strings.Replace(base, "A1.", "A1, edited.", 1),
132 "edit B": strings.Replace(base, "B.", "B, edited.", 1),
133 "add column": "Top.\n\n" + layoutOf("A1.\n\nA2.", "B.", "C.") +
134 "\n\nEnd.",
135 "remove B": "Top.\n\nA1.\n\nA2.\n\nB.\n\nEnd.",
136 "split A": "Top.\n\n" + layoutOf("A1.", "A2.", "B.") + "\n\nEnd.",
137 "undo": "Top.\n\nA1.\n\nA2.\n\nB.\n\nEnd.",
138 "undo, top": "Top, edited.\n\nA1.\n\nA2.\n\nB.\n\nEnd.",
139 "split, top": "Top, again.\n\n" + layoutOf("A1.", "A2.", "B.") +
140 "\n\nEnd.",
141 "move A2": "Top.\n\n" + layoutOf("A1.", "A2.\n\nB.") + "\n\nEnd.",
142 "add layout": base + "\n\n" + layoutOf("X.", "Y."),
143 }
144 for mineName, mine := range edits {
145 for theirsName, theirs := range edits {
146 hunks, _ := Merge3(base, mine, theirs)
147 conflicts := 0
148 for _, hunk := range hunks {
149 if hunk.Conflict {
150 conflicts++
151 }
152 }
153 for _, choices := range everyChoice(conflicts) {
154 body, ok := Settle(hunks, choices)
155 if !ok || !wholeLayouts(body) {
156 t.Errorf("%s against %s, choices %v:\n%s", mineName,
157 theirsName, choices, body)
158 }
159 }
160 }
161 }
162}
163
164// everyChoice is every way to settle count conflicts.
165func everyChoice(count int) [][]Choice {
166 out := [][]Choice{{}}
167 for range count {
168 var next [][]Choice
169 for _, prefix := range out {
170 for _, choice := range []Choice{KeepMine, KeepTheirs, KeepBoth} {
171 next = append(next, append(append([]Choice{}, prefix...),
172 choice))
173 }
174 }
175 out = next
176 }
177 return out
178}
179
180// wholeLayouts reports whether every fence-like line of body is a fence
181// of a whole layout: none is left as text, which is what a broken layout
182// parses as (these bodies type no fence-like text of their own).
183func wholeLayouts(body string) bool {
184 for _, block := range Parse(body) {
185 if IsFence(block.Kind) {
186 continue
187 }
188 for _, line := range strings.Split(block.Source, "\n") {
189 if FenceLike(line) {
190 return false
191 }
192 }
193 }
194 return true
195}
196