github.com/haraldrudell/parl@v0.4.176/pio/write-closer-to-chan-line_test.go (about)

     1  /*
     2  © 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pio
     7  
     8  import (
     9  	"io"
    10  	"testing"
    11  )
    12  
    13  func TestNewWriteCloserToChanLine(t *testing.T) {
    14  	lines := []string{"\n", "a\n\n", "", "d", "\ne"}
    15  	exp := []string{"", "a", "", "d", "e"}
    16  
    17  	var writeCloser io.WriteCloser
    18  	var impl *WriteCloserToChanLine
    19  	length := len(lines)
    20  	bytss := make([][]byte, length)
    21  	for i, s := range lines {
    22  		bytss[i] = []byte(s)
    23  	}
    24  
    25  	writeCloser = NewWriteCloserToChanLine()
    26  	impl = writeCloser.(*WriteCloserToChanLine)
    27  
    28  	for _, byts := range bytss {
    29  		writeCloser.Write(byts)
    30  	}
    31  
    32  	writeCloser.Close()
    33  
    34  	ch := impl.Ch()
    35  	for i := 0; i < len(exp); i++ {
    36  		s, ok := <-ch
    37  		if !ok {
    38  			break
    39  		}
    40  		if s != exp[i] {
    41  			t.Errorf("line %d: %q exp %q", i, s, exp[i])
    42  		}
    43  	}
    44  }