52 lines
708 B
Go
52 lines
708 B
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestParse(t *testing.T) {
|
|
const input = `From user1
|
|
|
|
Body 1
|
|
|
|
From user2
|
|
|
|
Body 2 with From in it.
|
|
|
|
From user3
|
|
|
|
Body 3
|
|
`
|
|
wants := []string{
|
|
`From user1
|
|
|
|
Body 1
|
|
`,
|
|
`From user2
|
|
|
|
Body 2 with From in it.
|
|
`,
|
|
`From user3
|
|
|
|
Body 3
|
|
`,
|
|
}
|
|
msgN := 0
|
|
testWriteMessage := func(got []byte) error {
|
|
want := wants[msgN]
|
|
if !bytes.Equal([]byte(want), got) {
|
|
t.Errorf("msg[%d] = %q, want %q", msgN, got, want)
|
|
}
|
|
msgN++
|
|
return nil
|
|
}
|
|
if err := parse(strings.NewReader(input), testWriteMessage); err != nil {
|
|
t.Errorf("parse() failed: %v", err)
|
|
}
|
|
if msgN != len(wants) {
|
|
t.Errorf("Parsed %d messages, expected %d", msgN, len(wants))
|
|
}
|
|
}
|