aboutsummaryrefslogtreecommitdiffstats
path: root/lib/templates/functions_test.go
blob: 3dac591e6031e72327db62da0d497f71461b0480 (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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package templates

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/emersion/go-message/mail"
)

func TestTemplates_DifferentNamesFormats(t *testing.T) {
	type testCase struct {
		address mail.Address
		name    string
	}

	cases := []testCase{
		{address: mail.Address{Name: "", Address: "john@doe.com"}, name: "john"},
		{address: mail.Address{Name: "", Address: "bill.john.doe@doe.com"}, name: "bill.john.doe"},
		{address: mail.Address{Name: "John", Address: "john@doe.com"}, name: "John"},
		{address: mail.Address{Name: "John Doe", Address: "john@doe.com"}, name: "John Doe"},
		{address: mail.Address{Name: "Bill John Doe", Address: "john@doe.com"}, name: "Bill John Doe"},
		{address: mail.Address{Name: "Doe, John", Address: "john@doe.com"}, name: "John Doe"},
		{address: mail.Address{Name: "Doe, Bill John", Address: "john@doe.com"}, name: "Bill John Doe"},
		{address: mail.Address{Name: "Schröder, Gerhard", Address: "s@g.de"}, name: "Gerhard Schröder"},
		{address: mail.Address{Name: "Buhl-Freiherr von und zu Guttenberg, Karl-Theodor Maria Nikolaus Johann Jacob Philipp Franz Joseph Sylvester", Address: "long@email.com"}, name: "Karl-Theodor Maria Nikolaus Johann Jacob Philipp Franz Joseph Sylvester Buhl-Freiherr von und zu Guttenberg"},
		{address: mail.Address{Name: "Dr. Őz-Szűcs Villő, MD, PhD, MBA (Üllői úti Klinika, Budapest, Hungary)", Address: "a@b.com"}, name: "Dr. Őz-Szűcs Villő, MD, PhD, MBA (Üllői úti Klinika, Budapest, Hungary)"},
		{address: mail.Address{Name: "International Important Conference, 2023", Address: "a@b.com"}, name: "2023 International Important Conference"},
		{address: mail.Address{Name: "A. B.C. Muscat", Address: "a@b.com"}, name: "A. B.C. Muscat"},
		{address: mail.Address{Name: "Wertram, te, K.W.", Address: "a@b.com"}, name: "Wertram, te, K.W."},
		{address: mail.Address{Name: "Harvard, John, Dr. CDC/MIT/SYSOPSYS", Address: "a@b.com"}, name: "Harvard, John, Dr. CDC/MIT/SYSOPSYS"},
	}

	for _, c := range cases {
		names := names([]*mail.Address{&c.address})
		assert.Len(t, names, 1)
		assert.Equal(t, c.name, names[0])
	}
}

func TestTemplates_DifferentFirstnamesFormats(t *testing.T) {
	type testCase struct {
		address   mail.Address
		firstname string
	}

	cases := []testCase{
		{address: mail.Address{Name: "", Address: "john@doe.com"}, firstname: "john"},
		{address: mail.Address{Name: "", Address: "bill.john.doe@doe.com"}, firstname: "bill"},
		{address: mail.Address{Name: "John", Address: "john@doe.com"}, firstname: "John"},
		{address: mail.Address{Name: "John Doe", Address: "john@doe.com"}, firstname: "John"},
		{address: mail.Address{Name: "Bill John Doe", Address: "john@doe.com"}, firstname: "Bill"},
		{address: mail.Address{Name: "Doe, John", Address: "john@doe.com"}, firstname: "John"},
		{address: mail.Address{Name: "Schröder, Gerhard", Address: "s@g.de"}, firstname: "Gerhard"},
		{address: mail.Address{Name: "Buhl-Freiherr von und zu Guttenberg, Karl-Theodor Maria Nikolaus Johann Jacob Philipp Franz Joseph Sylvester", Address: "long@email.com"}, firstname: "Karl-Theodor"},
		{address: mail.Address{Name: "Dr. Őz-Szűcs Villő, MD, PhD, MBA (Üllői úti Klinika, Budapest, Hungary)", Address: "a@b.com"}, firstname: "Dr."},
		{address: mail.Address{Name: "International Important Conference, 2023", Address: "a@b.com"}, firstname: "2023"},
		{address: mail.Address{Name: "A. B.C. Muscat", Address: "a@b.com"}, firstname: "A."},
		{address: mail.Address{Name: "Wertram, te, K.W.", Address: "a@b.com"}, firstname: "Wertram"},
		{address: mail.Address{Name: "Harvard, John, Dr. CDC/MIT/SYSOPSYS", Address: "a@b.com"}, firstname: "Harvard"},
	}

	for _, c := range cases {
		names := firstnames([]*mail.Address{&c.address})
		assert.Len(t, names, 1)
		assert.Equal(t, c.firstname, names[0])
	}
}

func TestTemplates_InternalRearrangeNamesWithComma(t *testing.T) {
	type testCase struct {
		source string
		res    string
	}

	cases := []testCase{
		{source: "John.Doe", res: "John.Doe"},
		{source: "John Doe", res: "John Doe"},
		{source: "John Bill Doe", res: "John Bill Doe"},
		{source: "Doe, John Bill", res: "John Bill Doe"},
		{source: "Doe, John-Bill", res: "John-Bill Doe"},
		{source: "Doe John, Bill", res: "Bill Doe John"},
		{source: "Schröder, Gerhard", res: "Gerhard Schröder"},
		// do not touch names with more than one comma
		{source: "One, Two, Three", res: "One, Two, Three"},
		{source: "One, Two, Three, Four", res: "One, Two, Three, Four"},
	}

	for _, c := range cases {
		res := rearrangeNameWithComma(c.source)
		assert.Equal(t, c.res, res)
	}
}

func TestTemplates_DifferentInitialsFormats(t *testing.T) {
	type testCase struct {
		address  mail.Address
		initials string
	}

	cases := []testCase{
		{address: mail.Address{Name: "", Address: "john@doe.com"}, initials: "j"},
		{address: mail.Address{Name: "", Address: "bill.john.doe@doe.com"}, initials: "b"},
		{address: mail.Address{Name: "John", Address: "john@doe.com"}, initials: "J"},
		{address: mail.Address{Name: "John Doe", Address: "john@doe.com"}, initials: "JD"},
		{address: mail.Address{Name: "Bill John Doe", Address: "john@doe.com"}, initials: "BJD"},
		{address: mail.Address{Name: "Doe, John", Address: "john@doe.com"}, initials: "JD"},
		{address: mail.Address{Name: "Doe, John Bill", Address: "john@doe.com"}, initials: "JBD"},
		{address: mail.Address{Name: "Schröder, Gerhard", Address: "s@g.de"}, initials: "GS"},
		{address: mail.Address{Name: "Buhl-Freiherr von und zu Guttenberg, Karl-Theodor Maria Nikolaus Johann Jacob Philipp Franz Joseph Sylvester", Address: "long@email.com"}, initials: "KMNJJPFJSBvuzG"},
		{address: mail.Address{Name: "Dr. Őz-Szűcs Villő, MD, PhD, MBA (Üllői úti Klinika, Budapest, Hungary)", Address: "a@b.com"}, initials: "DŐVMPM(úKBH"},
		{address: mail.Address{Name: "International Important Conference, 2023", Address: "a@b.com"}, initials: "2IIC"},
		{address: mail.Address{Name: "A. B.C. Muscat", Address: "a@b.com"}, initials: "ABM"},
		{address: mail.Address{Name: "Wertram, te, K.W.", Address: "a@b.com"}, initials: "WtK"},
		{address: mail.Address{Name: "Harvard, John, Dr. CDC/MIT/SYSOPSYS", Address: "a@b.com"}, initials: "HJDC"},
	}

	for _, c := range cases {
		intls := initials([]*mail.Address{&c.address})
		assert.Len(t, intls, 1)
		assert.Equal(t, c.initials, intls[0])
	}
}

func TestTemplates_Head(t *testing.T) {
	type testCase struct {
		head   uint
		input  string
		output string
	}
	cases := []testCase{
		{head: 3, input: "abcde", output: "abc"},
		{head: 10, input: "abcde", output: "abcde"},
	}

	for _, c := range cases {
		out := head(c.head, c.input)
		assert.Equal(t, c.output, out)
	}
}

func TestTemplates_Tail(t *testing.T) {
	type testCase struct {
		tail   uint
		input  string
		output string
	}
	cases := []testCase{
		{tail: 2, input: "abcde", output: "de"},
		{tail: 8, input: "abcde", output: "abcde"},
	}

	for _, c := range cases {
		out := tail(c.tail, c.input)
		assert.Equal(t, c.output, out)
	}
}