// 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) } } } }