gotest.tools/gotestsum@v1.11.0/internal/text/testing.go (about)

     1  package text
     2  
     3  import (
     4  	"bufio"
     5  	"io"
     6  	"strings"
     7  
     8  	"gotest.tools/v3/assert"
     9  )
    10  
    11  type TestingT interface {
    12  	Helper()
    13  	assert.TestingT
    14  }
    15  
    16  // ProcessLines from the Reader by passing each one to ops. The output of each
    17  // op is passed to the next. Returns the string created by joining all the
    18  // processed lines.
    19  func ProcessLines(t TestingT, r io.Reader, ops ...func(string) string) string {
    20  	t.Helper()
    21  	out := new(strings.Builder)
    22  	scan := bufio.NewScanner(r)
    23  	for scan.Scan() {
    24  		line := scan.Text()
    25  		for _, op := range ops {
    26  			line = op(line)
    27  		}
    28  		out.WriteString(line + "\n")
    29  	}
    30  	assert.NilError(t, scan.Err())
    31  	return out.String()
    32  }
    33  
    34  func OpRemoveSummaryLineElapsedTime(line string) string {
    35  	if i := strings.Index(line, " in "); i > 0 {
    36  		return line[:i]
    37  	}
    38  	return line
    39  }
    40  
    41  func OpRemoveTestElapsedTime(line string) string {
    42  	if i := strings.Index(line, " (0."); i > 0 && i+8 == len(line) {
    43  		return line[:i]
    44  	}
    45  	return line
    46  }