Source file src/testing/iotest/writer_test.go

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

View as plain text