1
2
3
4
5 package textproto
6
7 import (
8 "bufio"
9 "strings"
10 "testing"
11 )
12
13 func TestPrintfLine(t *testing.T) {
14 var buf strings.Builder
15 w := NewWriter(bufio.NewWriter(&buf))
16 err := w.PrintfLine("foo %d", 123)
17 if s := buf.String(); s != "foo 123\r\n" || err != nil {
18 t.Fatalf("s=%q; err=%s", s, err)
19 }
20 }
21
22 func TestDotWriter(t *testing.T) {
23 var buf strings.Builder
24 w := NewWriter(bufio.NewWriter(&buf))
25 d := w.DotWriter()
26 n, err := d.Write([]byte("abc\n.def\n..ghi\n.jkl\n."))
27 if n != 21 || err != nil {
28 t.Fatalf("Write: %d, %s", n, err)
29 }
30 d.Close()
31 want := "abc\r\n..def\r\n...ghi\r\n..jkl\r\n..\r\n.\r\n"
32 if s := buf.String(); s != want {
33 t.Fatalf("wrote %q", s)
34 }
35 }
36
37 func TestDotWriterCloseEmptyWrite(t *testing.T) {
38 var buf strings.Builder
39 w := NewWriter(bufio.NewWriter(&buf))
40 d := w.DotWriter()
41 n, err := d.Write([]byte{})
42 if n != 0 || err != nil {
43 t.Fatalf("Write: %d, %s", n, err)
44 }
45 d.Close()
46 want := "\r\n.\r\n"
47 if s := buf.String(); s != want {
48 t.Fatalf("wrote %q; want %q", s, want)
49 }
50 }
51
52 func TestDotWriterCloseNoWrite(t *testing.T) {
53 var buf strings.Builder
54 w := NewWriter(bufio.NewWriter(&buf))
55 d := w.DotWriter()
56 d.Close()
57 want := "\r\n.\r\n"
58 if s := buf.String(); s != want {
59 t.Fatalf("wrote %q; want %q", s, want)
60 }
61 }
62
View as plain text