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
|
package mbox
import (
"bytes"
"io"
"strings"
"testing"
"time"
)
type testMessage struct {
date string
text string
}
func testWriter(t *testing.T, messages []testMessage) string {
var b bytes.Buffer
wc := NewWriter(&b)
for _, m := range messages {
r := strings.NewReader(m.text)
date, _ := time.Parse(time.RFC1123Z, m.date)
mw, err := wc.CreateMessage("", date)
if err != nil {
t.Fatal(err)
}
_, err = io.Copy(mw, r)
if err != nil {
t.Fatal(err)
}
}
if err := wc.Close(); err != nil {
t.Fatal(err)
}
return b.String()
}
func TestWriter(t *testing.T) {
messages := []testMessage{
{
"Thu, 01 Jan 2015 00:00:01 +0100",
`Date: Thu, 01 Jan 2015 00:00:01 +0100
This is a simple test.
And, by the way, this is how a "From" line is escaped in mboxo format:
From Herp Derp with love.
Bye.`,
},
{
"Thu, 02 Jan 2015 00:00:01 +0100",
`Date: Thu, 02 Jan 2015 00:00:01 +0100` + "\r" + `
` + "\r" + `
This is another simple test.` + "\r" + `
` + "\r" + `
Another line.` + "\r" + `
` + "\r" + `
Bye.`,
},
}
expected := `From ???@??? Wed Dec 31 23:00:01 2014
Date: Thu, 01 Jan 2015 00:00:01 +0100
This is a simple test.
And, by the way, this is how a "From" line is escaped in mboxo format:
>From Herp Derp with love.
Bye.
From ???@??? Thu Jan 1 23:00:01 2015
Date: Thu, 02 Jan 2015 00:00:01 +0100
This is another simple test.
Another line.
Bye.
`
s := testWriter(t, messages)
if s != expected {
t.Error("Invalid mbox output:", s)
}
}
|