summaryrefslogtreecommitdiff
path: root/internal/config/enum_test.go
blob: d73396fb0afcae99e52b8473ae1c954784523768 (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
// SPDX-License-Identifier: GPL-3.0-or-later

package config

import (
	"strings"
	"testing"

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

// TestEverySettingValuePrintsAsItselfInAConfig: PrintRule writes settings
// with their String(), so every Conflict and CaseMode value must print as
// the word a configuration uses, and parse back as that same value. A value
// added later without its branch would otherwise print as "Conflict(3)",
// which krino check refuses.
func TestEverySettingValuePrintsAsItselfInAConfig(t *testing.T) {
	for _, c := range []struct {
		typ, form string
		printed   func(int) string
		parsed    func(*Rule) string
	}{
		{"Conflict", "on-conflict", func(i int) string { return Conflict(i).String() },
			func(r *Rule) string { return r.Settings.OnConflict.String() }},
		{"CaseMode", "case", func(i int) string { return CaseMode(i).String() },
			func(r *Rule) string { return r.Settings.Case.String() }},
	} {
		names, err := enumtest.Names("settings.go", c.typ)
		if err != nil {
			t.Fatal(err)
		}
		if len(names) == 0 {
			t.Fatalf("no %s values found", c.typ)
		}
		for i, name := range names {
			word := c.printed(i)
			if strings.ContainsAny(word, "()0123456789") {
				t.Errorf("%s.String() = %q; give it a word a configuration uses", name, word)
				continue
			}
			src := "(path \"/tmp\")\n(rule \"r\" (" + c.form + " " + word + ") (move \"Out\"))\n"
			dir, errs := ParseDir("dl", "dl.conf", []byte(src))
			if len(errs) > 0 {
				t.Errorf("%s prints as %q, which krino check refuses: %v", name, word, errs)
				continue
			}
			if got := c.parsed(dir.Rules[0]); got != word {
				t.Errorf("%s prints as %q but parses back as %q", name, word, got)
			}
		}
	}
}