-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose_test.go
More file actions
95 lines (90 loc) · 2.27 KB
/
Copy pathcompose_test.go
File metadata and controls
95 lines (90 loc) · 2.27 KB
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
package main
import "testing"
func TestParseAddrList(t *testing.T) {
cases := []struct {
name string
in any
want []string
}{
{"nil", nil, []string{}},
{"comma string", "a@b.fr, c@d.fr", []string{"a@b.fr", "c@d.fr"}},
{"empty entries dropped", "a@b.fr,, c@d.fr", []string{"a@b.fr", "c@d.fr"}},
{"semicolon string", "a@b.fr;c@d.fr", []string{"a@b.fr", "c@d.fr"}},
{"display name", "Contact <contact@lacure.enbauges.fr>", []string{"contact@lacure.enbauges.fr"}},
{"[]any", []any{"a@b.fr", "c@d.fr"}, []string{"a@b.fr", "c@d.fr"}},
{"[]any mixed types", []any{"a@b.fr", 42}, []string{"a@b.fr"}},
{"unsupported type", 123, []string{}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := parseAddrList(c.in)
if len(got) != len(c.want) {
t.Fatalf("got %v, want %v", got, c.want)
}
for i := range got {
if got[i] != c.want[i] {
t.Fatalf("got %v, want %v", got, c.want)
}
}
})
}
}
func TestLooksLikeEmail(t *testing.T) {
cases := []struct {
in string
want bool
}{
{"contact@lacure.enbauges.fr", true},
{"a@b.co", true},
{"no-at-sign", false},
{"@b.co", false},
{"a@", false},
{"a@@b.co", false},
{"a@b", false},
{"a@.co", false},
{"a@b.co.", false},
{"a b@c.co", false},
{"a@b.co,c@d.co", false},
}
for _, c := range cases {
if got := looksLikeEmail(c.in); got != c.want {
t.Errorf("looksLikeEmail(%q) = %v, want %v", c.in, got, c.want)
}
}
}
func TestSafeFilename(t *testing.T) {
cases := []struct {
in string
want string
}{
{"devis.pdf", "devis.pdf"},
{"../../etc/passwd", "passwd"},
{`C:\Users\x\file.txt`, "file.txt"},
{"...hidden", "hidden"},
{"", "attachment"},
{" ", "attachment"},
}
for _, c := range cases {
if got := safeFilename(c.in); got != c.want {
t.Errorf("safeFilename(%q) = %q, want %q", c.in, got, c.want)
}
}
if got := safeFilename(string(make([]byte, 300))); len(got) > 200 {
t.Errorf("safeFilename did not cap length: got %d chars", len(got))
}
}
func TestHumanBytes(t *testing.T) {
cases := []struct {
in int64
want string
}{
{500, "500 B"},
{2048, "2 KB"},
{5 * 1024 * 1024, "5.0 MB"},
}
for _, c := range cases {
if got := humanBytes(c.in); got != c.want {
t.Errorf("humanBytes(%d) = %q, want %q", c.in, got, c.want)
}
}
}