...

Source file src/net/textproto/writer_test.go

Documentation: net/textproto

		 1  // Copyright 2010 The Go Authors. All rights reserved.
		 2  // Use of this source code is governed by a BSD-style
		 3  // license that can be found in the LICENSE file.
		 4  
		 5  package textproto
		 6  
		 7  import (
		 8  	"bufio"
		 9  	"bytes"
		10  	"testing"
		11  )
		12  
		13  func TestPrintfLine(t *testing.T) {
		14  	var buf bytes.Buffer
		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 bytes.Buffer
		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 bytes.Buffer
		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 bytes.Buffer
		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