Debian Codegraph @

config/style_test.go

raw
1 package config
2
3 import (
4 "testing"
5
6 "github.com/emersion/go-message/mail"
7 "github.com/go-ini/ini"
8 )
9
10 const multiHeaderStyleset string = `
11 msglist_*.fg = salmon
12 msglist_*.From,~^"Bob Foo".fg = khaki
13 msglist_*.From,~^"Bob Foo".selected.fg = palegreen
14 msglist_*.Subject,~PATCH.From,~^"Bob Foo".fg = coral
15 msglist_*.From,~^"Bob Foo".Subject,~PATCH.X-Baz,exact.X-Clacks-Overhead,~Pratchett$.fg = plum
16 msglist_*.From,~^"Bob Foo".Subject,~PATCH.X-Clacks-Overhead,~Pratchett$.fg = pink
17 msglist_*.From,~^"Bob Foo".List-ID,~/lists\.sr\.ht/.fg = pink
18 `
19
20 func TestStyleMultiHeaderPattern(t *testing.T) {
21 ini, err := ini.Load([]byte(multiHeaderStyleset))
22 if err != nil {
23 t.Errorf("failed to load styleset: %v", err)
24 }
25
26 ss := NewStyleSet()
27 err = ss.ParseStyleSet(ini)
28 if err != nil {
29 t.Errorf("failed to parse styleset: %v", err)
30 }
31
32 t.Run("default color", func(t *testing.T) {
33 var h mail.Header
34 h.SetAddressList("From", []*mail.Address{{"Alice Foo", "[email protected]"}})
35
36 s := ss.Get(STYLE_MSGLIST_DEFAULT, &h)
37 if s.Foreground != colorNames["salmon"] {
38 t.Errorf("expected:#%v got:#%v", colorNames["salmon"], s.Foreground)
39 }
40 })
41
42 t.Run("single header", func(t *testing.T) {
43 var h mail.Header
44 h.SetAddressList("From", []*mail.Address{{"Bob Foo", "[email protected]"}})
45
46 s := ss.Get(STYLE_MSGLIST_DEFAULT, &h)
47 if s.Foreground != colorNames["khaki"] {
48 t.Errorf("expected:#%v got:#%v", colorNames["khaki"], s.Foreground)
49 }
50 })
51
52 t.Run("two headers", func(t *testing.T) {
53 var h mail.Header
54 h.SetAddressList("From", []*mail.Address{{"Bob Foo", "[email protected]"}})
55 h.SetSubject("[PATCH] tests")
56
57 s := ss.Get(STYLE_MSGLIST_DEFAULT, &h)
58 if s.Foreground != colorNames["coral"] {
59 t.Errorf("expected:#%x got:#%x", colorNames["coral"], s.Foreground)
60 }
61 })
62
63 t.Run("multiple headers", func(t *testing.T) {
64 var h mail.Header
65 h.SetAddressList("From", []*mail.Address{{"Bob Foo", "[email protected]"}})
66 h.SetSubject("[PATCH] tests")
67 h.SetText("X-Clacks-Overhead", "GNU Terry Pratchett")
68
69 s := ss.Get(STYLE_MSGLIST_DEFAULT, &h)
70 if s.Foreground != colorNames["pink"] {
71 t.Errorf("expected:#%x got:#%x", colorNames["pink"], s.Foreground)
72 }
73 })
74
75 t.Run("preserves order-sensitivity", func(t *testing.T) {
76 var h mail.Header
77 h.SetAddressList("From", []*mail.Address{{"Bob Foo", "[email protected]"}})
78 h.SetSubject("[PATCH] tests")
79 h.SetText("X-Clacks-Overhead", "GNU Terry Pratchett")
80 h.SetText("X-Baz", "exact")
81
82 s := ss.Get(STYLE_MSGLIST_DEFAULT, &h)
83
84 // The "pink" entry comes later, so will overrule the more exact
85 // match with color "plum"
86 if s.Foreground != colorNames["pink"] {
87 t.Errorf("expected:#%x got:#%x", colorNames["pink"], s.Foreground)
88 }
89 })
90
91 t.Run("handles uris in regular expressions", func(t *testing.T) {
92 var h mail.Header
93 h.SetAddressList("From", []*mail.Address{{"Bob Foo", "[email protected]"}})
94 h.SetText("List-ID", "List-ID: ~rjarry/aerc-discuss <~rjarry/aerc-discuss.lists.sr.ht>")
95
96 s := ss.Get(STYLE_MSGLIST_DEFAULT, &h)
97 if s.Foreground != colorNames["pink"] {
98 t.Errorf("expected:#%x got:#%x", colorNames["pink"], s.Foreground)
99 }
100 })
101 }