writer_test.go 876 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2019 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. package iotest
  5. import (
  6. "bytes"
  7. "testing"
  8. )
  9. var truncateWriterTests = []struct {
  10. in string
  11. want string
  12. trunc int64
  13. n int
  14. }{
  15. {"hello", "", -1, 5},
  16. {"world", "", 0, 5},
  17. {"abcde", "abc", 3, 5},
  18. {"edcba", "edcba", 7, 5},
  19. }
  20. func TestTruncateWriter(t *testing.T) {
  21. for _, tt := range truncateWriterTests {
  22. buf := new(bytes.Buffer)
  23. tw := TruncateWriter(buf, tt.trunc)
  24. n, err := tw.Write([]byte(tt.in))
  25. if err != nil {
  26. t.Errorf("Unexpected error %v for\n\t%+v", err, tt)
  27. }
  28. if g, w := buf.String(), tt.want; g != w {
  29. t.Errorf("got %q, expected %q", g, w)
  30. }
  31. if g, w := n, tt.n; g != w {
  32. t.Errorf("read %d bytes, but expected to have read %d bytes for\n\t%+v", g, w, tt)
  33. }
  34. }
  35. }