papero/fs/maildir_test.go

77 lines
1.9 KiB
Go

package fs
import (
"fmt"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestFlagsFromString(t *testing.T) {
flags, err := flagsFromString("RTDSF")
if err != nil {
t.Errorf("Flag parsing errored unexpectedly: %s\n", err)
}
expect := []Flag{FlagDraft, FlagFlagged, FlagAnswered, FlagSeen, FlagDeleted}
if !cmp.Equal(flags, expect) {
t.Errorf("Flags mismatch: %s", cmp.Diff(expect, flags))
}
_, err = flagsFromString("RDX")
if err != ErrUnknownFlag {
t.Error("Flag parsing should have errored")
}
_, err = flagsFromString("DDF")
if err != ErrMalformedName {
t.Error("Flag parsing should have errored")
}
}
func TestMailFileString(t *testing.T) {
clock := mockClock{}
timestamp := clock.Now()
progressive := 4
pid := 1312
hostname := "myplace"
name := "main_account" // known md5sum: f2cf513ad46d4d9b9684103e468803a0
uid := 33243
mf := &MailFile{timestamp: timestamp, progressive: progressive, pid: pid, hostname: hostname}
mf.SetFlags([]Flag{FlagSeen, FlagAnswered})
mf.SetMd5FromName(name)
mf.SetUid(uid)
expect := fmt.Sprintf(
"%d_%d.%d.%s,U=%d,FMD5=f2cf513ad46d4d9b9684103e468803a0:2,RS",
timestamp.Unix(),
progressive,
pid,
hostname,
uid)
result := fmt.Sprint(mf)
if expect != result {
t.Errorf("Mismatch\n\tExpect: %s\n\tResult: %s\n", expect, result)
t.Logf("diff: %s", cmp.Diff(expect, result))
}
}
func TestNewMailFile(t *testing.T) {
mf, err := NewMailFile("12345_0.1312.myplace,U=666,FMD5=f2cf513ad46d4d9b9684103e468803a0:2,")
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
expect := &MailFile{
timestamp: time.Unix(12345, 0),
progressive: 0,
pid: 1312,
hostname: "myplace",
uid: 666,
md5: "f2cf513ad46d4d9b9684103e468803a0",
flags: []Flag{},
}
if !cmp.Equal(mf, expect, cmp.AllowUnexported(MailFile{})) {
t.Errorf("MailFile mismatch: %s", cmp.Diff(expect, mf, cmp.AllowUnexported(MailFile{})))
}
}