github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/uio/progress_test.go (about)

     1  // Copyright 2019 the u-root 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 uio
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  )
    11  
    12  func TestProgressReader(t *testing.T) {
    13  	input := bytes.NewBufferString("01234567890123456789")
    14  	stdout := &bytes.Buffer{}
    15  	pr := ProgressReader{
    16  		R:        input,
    17  		Symbol:   "#",
    18  		Interval: 4,
    19  		W:        stdout,
    20  	}
    21  
    22  	// Read one byte at a time.
    23  	output := make([]byte, 1)
    24  	pr.Read(output)
    25  	pr.Read(output)
    26  	pr.Read(output)
    27  	if len(stdout.Bytes()) != 0 {
    28  		t.Errorf("found %q, but expected no bytes to be written", stdout)
    29  	}
    30  	pr.Read(output)
    31  	if stdout.String() != "#" {
    32  		t.Errorf("found %q, expected %q to be written", stdout.String(), "#")
    33  	}
    34  
    35  	// Read 9 bytes all at once.
    36  	output = make([]byte, 9)
    37  	pr.Read(output)
    38  	if stdout.String() != "###" {
    39  		t.Errorf("found %q, expected %q to be written", stdout.String(), "###")
    40  	}
    41  	if string(output) != "456789012" {
    42  		t.Errorf("found %q, expected %q to be written", string(output), "456789012")
    43  	}
    44  }