github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/testing/helper_test.go (about)

     1  // Copyright 2017 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 testing
     6  
     7  import (
     8  	"bytes"
     9  	"regexp"
    10  	"strings"
    11  )
    12  
    13  func TestTBHelper(t *T) {
    14  	var buf bytes.Buffer
    15  	ctx := newTestContext(1, newMatcher(regexp.MatchString, "", ""))
    16  	t1 := &T{
    17  		common: common{
    18  			signal: make(chan bool),
    19  			w:      &buf,
    20  		},
    21  		context: ctx,
    22  	}
    23  	t1.Run("Test", testHelper)
    24  
    25  	want := `--- FAIL: Test (?s)
    26  helperfuncs_test.go:12: 0
    27  helperfuncs_test.go:33: 1
    28  helperfuncs_test.go:21: 2
    29  helperfuncs_test.go:35: 3
    30  helperfuncs_test.go:42: 4
    31  helperfuncs_test.go:47: 5
    32  --- FAIL: Test/sub (?s)
    33  helperfuncs_test.go:50: 6
    34  helperfuncs_test.go:21: 7
    35  helperfuncs_test.go:53: 8
    36  `
    37  	lines := strings.Split(buf.String(), "\n")
    38  	durationRE := regexp.MustCompile(`\(.*\)$`)
    39  	for i, line := range lines {
    40  		line = strings.TrimSpace(line)
    41  		line = durationRE.ReplaceAllString(line, "(?s)")
    42  		lines[i] = line
    43  	}
    44  	got := strings.Join(lines, "\n")
    45  	if got != want {
    46  		t.Errorf("got output:\n\n%s\nwant:\n\n%s", got, want)
    47  	}
    48  }
    49  
    50  func TestTBHelperParallel(t *T) {
    51  	var buf bytes.Buffer
    52  	ctx := newTestContext(1, newMatcher(regexp.MatchString, "", ""))
    53  	t1 := &T{
    54  		common: common{
    55  			signal: make(chan bool),
    56  			w:      &buf,
    57  		},
    58  		context: ctx,
    59  	}
    60  	t1.Run("Test", parallelTestHelper)
    61  
    62  	lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
    63  	if len(lines) != 6 {
    64  		t.Fatalf("parallelTestHelper gave %d lines of output; want 6", len(lines))
    65  	}
    66  	want := "helperfuncs_test.go:21: parallel"
    67  	if got := strings.TrimSpace(lines[1]); got != want {
    68  		t.Errorf("got output line %q; want %q", got, want)
    69  	}
    70  }