1package config
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestRulesInFileOrder(t *testing.T) {
9 c, w := parseString(t, `
10workspace.chat.max-columns = 2
11bind.cmd+c = workspace chat
12rule.games.app-id = steam_app_.*
13rule.games.floating = on
14rule.games.workspace = 3
15rule.chat.app-id = org\.telegram\..*|discord
16rule.chat.workspace = chat
17rule.chat.monitor = DP-2
18rule.chat.width = 1/3
19rule.games.width = 50%
20`)
21 if len(w) != 0 {
22 t.Fatal(w)
23 }
24 if len(c.Rules) != 2 || c.Rules[0].Name != "games" || c.Rules[1].Name != "chat" {
25 t.Fatalf("%+v", c.Rules)
26 }
27 g, ch := c.Rules[0], c.Rules[1]
28 if g.Floating == nil || !*g.Floating || g.Workspace != "3" || g.Width != "50%" || g.Monitor != "" {
29 t.Fatalf("%+v", g)
30 }
31 if ch.Floating != nil || ch.Workspace != "chat" || ch.Monitor != "DP-2" || ch.Width != "1/3" {
32 t.Fatalf("%+v", ch)
33 }
34 if !g.AppID.MatchString("steam_app_1245620") || !ch.AppID.MatchString("discord") || !ch.AppID.MatchString("org.telegram.desktop") {
35 t.Fatal("app-id did not match")
36 }
37}
38
39func TestRuleAppIDIsAnchored(t *testing.T) {
40 c, _ := parseString(t, "rule.a.app-id = steam\nrule.b.app-id = steam|foot\n")
41 for _, tc := range []struct {
42 rule int
43 id string
44 want bool
45 }{
46 {0, "steam", true}, {0, "steam_app_1", false}, {0, "xsteam", false}, {0, "", false},
47 // The alternation is anchored as a whole, not per branch.
48 {1, "foot", true}, {1, "footclient", false}, {1, "steamfoot", false},
49 } {
50 if got := c.Rules[tc.rule].AppID.MatchString(tc.id); got != tc.want {
51 t.Errorf("rule %d on %q = %v, want %v", tc.rule, tc.id, got, tc.want)
52 }
53 }
54}
55
56func TestRuleWarnings(t *testing.T) {
57 for _, tc := range []struct {
58 name, text, warn string
59 line, rules int
60 }{
61 {"bad regex", "rule.a.app-id = (\n", "rule.a.app-id: invalid regular expression", 1, 0},
62 // Closing the anchoring group would match any app ID.
63 {"anchor breakout", "rule.a.app-id = steam)|(.*\n", "rule.a.app-id: invalid regular expression", 1, 0},
64 // Quoting the anchoring group's ")" must warn, not panic.
65 {"quoted anchor", "rule.a.app-id = \\Qsteam\n", "rule.a.app-id: invalid regular expression", 1, 0},
66 {"missing app-id", "rule.a.floating = on\n", "rule.a: no app-id, rule ignored", 1, 0},
67 {"unknown field", "rule.a.app-id = foo\nrule.a.title = x\n", "rule.a.title: unknown key", 2, 1},
68 {"bad floating", "rule.a.app-id = foo\nrule.a.floating = maybe\n", "rule.a.floating: must be on or off", 2, 0},
69 // An invalid duplicate keeps the earlier value and the rule.
70 {"bad duplicate floating", "rule.a.app-id = foo\nrule.a.floating = on\nrule.a.floating = maybe\n", "rule.a.floating: must be on or off", 3, 1},
71 {"bad duplicate app-id", "rule.a.app-id = foo\nrule.a.app-id = (\n", "rule.a.app-id: invalid regular expression", 2, 1},
72 {"bad width", "rule.a.app-id = foo\nrule.a.width = wide\n", "rule.a.width: must be a width", 2, 0},
73 {"bad workspace number", "rule.a.app-id = foo\nrule.a.workspace = 0\n", "rule.a.workspace: workspace number", 2, 0},
74 {"bad workspace name", "rule.a.app-id = foo\nrule.a.workspace = a b\n", "rule.a.workspace: must be", 2, 0},
75 {"empty monitor", "rule.a.app-id = foo\nrule.a.monitor =\n", "rule.a.monitor: needs", 2, 0},
76 {"bad rule name", "rule.a b.app-id = foo\n", "rule name must be", 1, 0},
77 {"undeclared workspace", "rule.a.app-id = foo\nrule.a.workspace = web\n", "rule.a.workspace: no workspace.web.* declared", 2, 1},
78 } {
79 t.Run(tc.name, func(t *testing.T) {
80 c, w := parseString(t, tc.text)
81 if len(w) != 1 || w[0].Line != tc.line || !strings.Contains(w[0].Msg, tc.warn) {
82 t.Fatalf("warnings = %v, want %q at line %d", w, tc.warn, tc.line)
83 }
84 if len(c.Rules) != tc.rules {
85 t.Fatalf("rules = %+v, want %d", c.Rules, tc.rules)
86 }
87 })
88 }
89}
90
91func TestRuleWidthReusesColumnWidth(t *testing.T) {
92 for _, v := range []string{"1", "33%", "1/3", "640px"} {
93 c, w := parseString(t, "rule.a.app-id = foo\nrule.a.width = "+v+"\n")
94 if len(w) != 0 || len(c.Rules) != 1 || c.Rules[0].Width != v {
95 t.Fatalf("%s: %v %+v", v, w, c.Rules)
96 }
97 }
98}
99
100func TestRuleReloadDiff(t *testing.T) {
101 _, a, _ := parseRaw(t, "rule.a.app-id = foo\nrule.b.app-id = bar\n")
102 _, b, _ := parseRaw(t, "rule.b.app-id = bar\nrule.a.app-id = foo\n")
103 _, c, _ := parseRaw(t, "rule.a.app-id = foo\nrule.b.app-id = bar\nrule.b.floating = on\n")
104 if d := diff(a, b); len(d) == 0 {
105 t.Fatal("reordering rules is not a change")
106 }
107 if d := diff(a, c); len(d) != 1 || d[0] != "rule.b.floating" {
108 t.Fatal(d)
109 }
110 if d := diff(a, a); len(d) != 0 {
111 t.Fatal(d)
112 }
113}
114
115func parseRaw(t *testing.T, s string) (cfg any, raw map[string]string, w []Warning) {
116 t.Helper()
117 c, raw, w, err := parse(strings.NewReader(s))
118 if err != nil {
119 t.Fatal(err)
120 }
121 return c, raw, w
122}