aboutsummaryrefslogtreecommitdiff
path: root/gui/internal/model/forms_test.go
blob: 46b582018795111cc97935bb006007e30dcb7444 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// SPDX-License-Identifier: GPL-3.0-or-later

package model

import (
	"strings"
	"testing"

	"git.labunix.xyz/krino/internal/config"
)

// formsFile is a directory file with comments in the places that matter:
// above a rule (its own), inside a rule, and free-standing.
const formsFile = `(path "~/dl")

(exclude (name "^keep-"))

;; invoices go to their year
(rule "invoices"
  (when (and (type pdf) (content "invoice")))
  (move "Invoices/{mtime:%Y}")
  (stop))

(rule "images"
  (when (type image))   ; anything the type table calls an image
  (move "Images")
  (stop))

;; last resort

(rule "rest"
  (move "Other"))
`

// openForms puts text in an editor over the sandbox's file.
func openForms(t *testing.T, text string) *Rules {
	t.Helper()
	e, _ := sandboxDir(t, formsFile, map[string]string{"a.pdf": "one"})
	r, err := OpenRules(e, "dl")
	if err != nil {
		t.Fatal(err)
	}
	if text != "" {
		r.SetText(text)
	}
	return r
}

// TestFormsLists: every editable form of the file, in file order, named the
// way the list shows it.
func TestFormsLists(t *testing.T) {
	r := openForms(t, "")
	forms, err := r.Forms()
	if err != nil {
		t.Fatal(err)
	}
	var got []string
	for _, f := range forms {
		got = append(got, f.Label)
	}
	want := []string{`(exclude (name "^keep-"))`, "invoices", "images", "rest"}
	if strings.Join(got, "|") != strings.Join(want, "|") {
		t.Errorf("forms = %v, want %v", got, want)
	}
	if forms[0].Kind != ExcludeForm || forms[1].Kind != RuleForm {
		t.Errorf("kinds = %v, %v", forms[0].Kind, forms[1].Kind)
	}
	if forms[1].Rule == nil || forms[1].Rule.Name != "invoices" {
		t.Errorf("rule form carries no rule: %+v", forms[1])
	}
}

// TestDeleteFormTakesItsComments: a rule's block is its form plus the
// comment lines directly above it with no blank line between (GUI design
// §5.1), so deleting it does not leave its comment stranded - while a
// comment separated by a blank line stays.
func TestDeleteFormTakesItsComments(t *testing.T) {
	r := openForms(t, "")
	if err := r.DeleteForm(1); err != nil { // invoices, with its comment
		t.Fatal(err)
	}
	if strings.Contains(r.Text, "invoices go to their year") {
		t.Errorf("the rule's own comment was left behind:\n%s", r.Text)
	}
	if strings.Contains(r.Text, `(rule "invoices"`) {
		t.Errorf("the rule is still there:\n%s", r.Text)
	}
	// The free-standing comment above "rest" is separated by a blank line,
	// so deleting that rule leaves it alone.
	forms, err := r.Forms()
	if err != nil {
		t.Fatal(err)
	}
	if err := r.DeleteForm(len(forms) - 1); err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(r.Text, ";; last resort") {
		t.Errorf("a comment separated by a blank line was taken too:\n%s", r.Text)
	}
	if diags := r.Check(); len(diags) > 0 {
		t.Errorf("the file no longer loads: %v", diags)
	}
}

// TestMoveForm: a rule moves with its comments, and the order of everything
// else is kept.
func TestMoveForm(t *testing.T) {
	r := openForms(t, "")
	if err := r.MoveForm(2, -1); err != nil { // images, up past invoices
		t.Fatal(err)
	}
	forms, err := r.Forms()
	if err != nil {
		t.Fatal(err)
	}
	var got []string
	for _, f := range forms {
		got = append(got, f.Label)
	}
	want := []string{`(exclude (name "^keep-"))`, "images", "invoices", "rest"}
	if strings.Join(got, "|") != strings.Join(want, "|") {
		t.Fatalf("after moving up: %v, want %v", got, want)
	}
	// The comment travelled with its rule, and still sits above it.
	iInvoices := strings.Index(r.Text, `(rule "invoices"`)
	iComment := strings.Index(r.Text, ";; invoices go to their year")
	if iComment < 0 || iComment > iInvoices {
		t.Errorf("the comment did not travel with its rule:\n%s", r.Text)
	}
	if strings.Index(r.Text, `(rule "images"`) > iComment {
		t.Errorf("images did not move above invoices:\n%s", r.Text)
	}
	if diags := r.Check(); len(diags) > 0 {
		t.Errorf("the file no longer loads: %v", diags)
	}
	// Moving the first form up, or the last down, does nothing.
	before := r.Text
	if err := r.MoveForm(0, -1); err != nil {
		t.Fatal(err)
	}
	if err := r.MoveForm(len(forms)-1, 1); err != nil {
		t.Fatal(err)
	}
	if r.Text != before {
		t.Error("moving past the ends changed the file")
	}
}

// TestAddRuleAfter: a new rule lands after the one selected, is the form
// the printer writes, and the file still loads.
func TestAddRuleAfter(t *testing.T) {
	r := openForms(t, "")
	i, err := r.AddRuleAfter(1, "new one")
	if err != nil {
		t.Fatal(err)
	}
	forms, err := r.Forms()
	if err != nil {
		t.Fatal(err)
	}
	if i != 2 || forms[2].Label != "new one" {
		t.Fatalf("new rule at %d: %v", i, labels(forms))
	}
	if diags := r.Check(); len(diags) > 0 {
		t.Errorf("the file no longer loads: %v", diags)
	}
}

// TestReplaceForm: editing one form rewrites that form's text and nothing
// else - every comment and every other line stays byte for byte.
func TestReplaceForm(t *testing.T) {
	r := openForms(t, "")
	forms, err := r.Forms()
	if err != nil {
		t.Fatal(err)
	}
	rule := *forms[1].Rule // invoices
	rule.Actions = []config.Action{{Kind: config.Move, Arg: "Faktury"}}
	if err := r.ReplaceForm(1, config.PrintRule(&rule)); err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(r.Text, `(move "Faktury")`) {
		t.Errorf("the edit did not land:\n%s", r.Text)
	}
	if strings.Contains(r.Text, `(move "Invoices/{mtime:%Y}")`) {
		t.Errorf("the old action is still there:\n%s", r.Text)
	}
	for _, keep := range []string{";; invoices go to their year", ";; last resort",
		"; anything the type table calls an image", `(exclude (name "^keep-"))`} {
		if !strings.Contains(r.Text, keep) {
			t.Errorf("replacing one form lost %q:\n%s", keep, r.Text)
		}
	}
	if diags := r.Check(); len(diags) > 0 {
		t.Errorf("the file no longer loads: %v", diags)
	}
}

// TestFormHasComments: a form with a comment inside it cannot be rewritten
// from a form editor without losing it, so the caller is warned; a
// semicolon inside a string is not a comment (GUI design §5.1).
func TestFormHasComments(t *testing.T) {
	r := openForms(t, "")
	forms, err := r.Forms()
	if err != nil {
		t.Fatal(err)
	}
	if got, err := r.FormHasComments(2); err != nil || !got { // images
		t.Errorf("FormHasComments(images) = %v, %v; want true", got, err)
	}
	if got, err := r.FormHasComments(1); err != nil || got { // invoices
		t.Errorf("FormHasComments(invoices) = %v, %v; want false", got, err)
	}
	_ = forms

	r.SetText("(path \"~/dl\")\n(rule \"semi\" (when (name \"a;b\")) (move \"Out\"))\n")
	if got, err := r.FormHasComments(0); err != nil || got {
		t.Errorf("a semicolon inside a string counted as a comment: %v, %v", got, err)
	}
}

// TestFormsRefusesBrokenText: the forms list comes from parsing, so text
// that does not parse has no forms to show, and says so.
func TestFormsRefusesBrokenText(t *testing.T) {
	r := openForms(t, "(path \"~/dl\")\n(rule \"unclosed\"\n")
	if _, err := r.Forms(); err == nil {
		t.Error("Forms answered for text that does not parse")
	}
	if err := r.DeleteForm(0); err == nil {
		t.Error("DeleteForm acted on text that does not parse")
	}
}

func labels(forms []Form) []string {
	var out []string
	for _, f := range forms {
		out = append(out, f.Label)
	}
	return out
}

// TestDeleteFormLeavesItsNeighbourAlone: a form's block is whole lines, so
// deleting one of two forms written on the same line took both. The dialog
// names one; the other vanished, the file still parsed, the check said "no
// errors" and Save lit - so an (exclude ...) could disappear silently and
// the next run would sort the files it had been protecting.
func TestDeleteFormLeavesItsNeighbourAlone(t *testing.T) {
	r := openForms(t, "(path \"~/dl\")\n\n"+
		"(exclude (name \"^keep-\")) (exclude (name \"^hold-\"))\n\n"+
		"(rule \"rest\" (move \"Other\"))\n")
	before, err := r.Forms()
	if err != nil {
		t.Fatal(err)
	}
	if len(before) != 3 {
		t.Fatalf("fixture has %d forms, want 3 (two excludes and one rule)", len(before))
	}
	if err := r.DeleteForm(0); err != nil { // the first exclude
		t.Fatal(err)
	}
	if strings.Contains(r.Text, "^keep-") {
		t.Error("the exclude that was asked for is still there")
	}
	if !strings.Contains(r.Text, "^hold-") {
		t.Errorf("deleting the first exclude took the second with it:\n%s", r.Text)
	}
	if !strings.Contains(r.Text, `(rule "rest"`) {
		t.Errorf("the rule is gone too:\n%s", r.Text)
	}
	after, err := r.Forms()
	if err != nil {
		t.Fatalf("the text no longer parses: %v\n%s", err, r.Text)
	}
	if len(after) != 2 {
		t.Errorf("%d forms left, want 2:\n%s", len(after), r.Text)
	}
}

// TestDeleteFormTakesTheWholeLineWhenItIsAlone: the ordinary case is
// unchanged - a form on its own lines goes with its line, leaving no blank
// where it was.
func TestDeleteFormTakesTheWholeLineWhenItIsAlone(t *testing.T) {
	r := openForms(t, "(path \"~/dl\")\n\n(exclude (name \"^keep-\"))\n\n(rule \"rest\" (move \"Other\"))\n")
	if err := r.DeleteForm(0); err != nil {
		t.Fatal(err)
	}
	if strings.Contains(r.Text, "^keep-") {
		t.Error("the exclude is still there")
	}
	if strings.Contains(r.Text, "\n\n\n") {
		t.Errorf("a blank line was left where the form was:\n%q", r.Text)
	}
}