Files
aerc-fork-mirror/worker/middleware/foldermapper_test.go
Koni Marti a791da7ba4 foldermap: make folder-map syntax more expressive
Improve the folder mapping syntax so that prefixes can be removed
completely. The following line in the folder-map file

* = INBOX/*

will strip the INBOX/ prefix from all subfolders of INBOX, e.g. map
"INBOX/Project1" to "Project1".

To prevent a key collision with multiple "*" keys (the folder mapping is
stored as a map internally), a group of "*" will be condensed to one "*",
e.g.

** = INBOX/*

has the same meaning as above.

Also, adjust name translation for folder creation and add tests.

Fixes: https://todo.sr.ht/~rjarry/aerc/176
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
2023-07-14 23:11:42 +02:00

104 lines
2.7 KiB
Go

package middleware
import (
"reflect"
"testing"
)
func TestFolderMap_Apply(t *testing.T) {
tests := []struct {
name string
mapping map[string]string
order []string
input []string
want []string
}{
{
name: "strict single folder mapping",
mapping: map[string]string{"Drafts": "INBOX/Drafts"},
order: []string{"Drafts"},
input: []string{"INBOX/Drafts"},
want: []string{"Drafts"},
},
{
name: "prefix mapping with * suffix",
mapping: map[string]string{"Prefix/": "INBOX/*"},
order: []string{"Prefix/"},
input: []string{"INBOX", "INBOX/Test1", "INBOX/Test2", "Archive"},
want: []string{"INBOX", "Prefix/Test1", "Prefix/Test2", "Archive"},
},
{
name: "remove prefix with * in key",
mapping: map[string]string{"*": "INBOX/*"},
order: []string{"*"},
input: []string{"INBOX", "INBOX/Test1", "INBOX/Test2", "Archive"},
want: []string{"INBOX", "Test1", "Test2", "Archive"},
},
{
name: "remove two prefixes with * in keys",
mapping: map[string]string{
"*": "INBOX/*",
"**": "PROJECT/*",
},
order: []string{"*", "**"},
input: []string{"INBOX", "INBOX/Test1", "INBOX/Test2", "Archive", "PROJECT/sub1", "PROJECT/sub2"},
want: []string{"INBOX", "Test1", "Test2", "Archive", "sub1", "sub2"},
},
{
name: "multiple, sequential mappings",
mapping: map[string]string{
"Archive/existing": "Archive*",
"Archive": "Archivum*",
},
order: []string{"Archive/existing", "Archive"},
input: []string{"Archive", "Archive/sub", "Archivum", "Archivum/year1"},
want: []string{"Archive/existing", "Archive/existing/sub", "Archive", "Archive/year1"},
},
}
for i, test := range tests {
fm := &folderMap{
mapping: test.mapping,
order: test.order,
}
var result []string
for _, in := range test.input {
result = append(result, fm.Apply(in))
}
if !reflect.DeepEqual(result, test.want) {
t.Errorf("test (%d: %s) failed: want '%v' but got '%v'",
i, test.name, test.want, result)
}
}
}
func TestFolderMap_createFolder(t *testing.T) {
tests := []struct {
name string
table map[string]string
input string
want string
}{
{
name: "create normal folder",
table: map[string]string{"Drafts": "INBOX/Drafts"},
input: "INBOX/Drafts2",
want: "INBOX/Drafts2",
},
{
name: "create mapped folder",
table: map[string]string{"Drafts": "INBOX/Drafts"},
input: "Drafts/Sub",
want: "INBOX/Drafts/Sub",
},
}
for i, test := range tests {
result := createFolder(test.table, test.input)
if result != test.want {
t.Errorf("test (%d: %s) failed: want '%v' but got '%v'",
i, test.name, test.want, result)
}
}
}