package fs import ( "fmt" "strconv" "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{}))) } } func TestNewMailFileErrs(t *testing.T) { var err error // The name has the wrong shape _, err = NewMailFile("pippo") if err != ErrMalformedName { t.Error("Function should have errored") } // The shape is correct, but one of the integer field is instead a string _, err = NewMailFile("aaaa_123.456.myplace,U=1312,FMD5=f2cf513ad46d4d9b9684103e468803a0:2,") switch err.(type) { case *strconv.NumError: // Happy path default: t.Error("Unexpected error") } }