github.com/AndrienkoAleksandr/go@v0.0.19/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 "regexp" 9 "strings" 10 ) 11 12 func TestTBHelper(t *T) { 13 var buf strings.Builder 14 ctx := newTestContext(1, allMatcher()) 15 t1 := &T{ 16 common: common{ 17 signal: make(chan bool), 18 w: &buf, 19 }, 20 context: ctx, 21 } 22 t1.Run("Test", testHelper) 23 24 want := `--- FAIL: Test (?s) 25 helperfuncs_test.go:12: 0 26 helperfuncs_test.go:40: 1 27 helperfuncs_test.go:21: 2 28 helperfuncs_test.go:42: 3 29 helperfuncs_test.go:49: 4 30 --- FAIL: Test/sub (?s) 31 helperfuncs_test.go:52: 5 32 helperfuncs_test.go:21: 6 33 helperfuncs_test.go:51: 7 34 helperfuncs_test.go:63: 8 35 --- FAIL: Test/sub2 (?s) 36 helperfuncs_test.go:78: 11 37 helperfuncs_test.go:82: recover 12 38 helperfuncs_test.go:84: GenericFloat64 39 helperfuncs_test.go:85: GenericInt 40 helperfuncs_test.go:71: 9 41 helperfuncs_test.go:67: 10 42 ` 43 lines := strings.Split(buf.String(), "\n") 44 durationRE := regexp.MustCompile(`\(.*\)$`) 45 for i, line := range lines { 46 line = strings.TrimSpace(line) 47 line = durationRE.ReplaceAllString(line, "(?s)") 48 lines[i] = line 49 } 50 got := strings.Join(lines, "\n") 51 if got != want { 52 t.Errorf("got output:\n\n%s\nwant:\n\n%s", got, want) 53 } 54 } 55 56 func TestTBHelperParallel(t *T) { 57 var buf strings.Builder 58 ctx := newTestContext(1, newMatcher(regexp.MatchString, "", "", "")) 59 t1 := &T{ 60 common: common{ 61 signal: make(chan bool), 62 w: &buf, 63 }, 64 context: ctx, 65 } 66 t1.Run("Test", parallelTestHelper) 67 68 lines := strings.Split(strings.TrimSpace(buf.String()), "\n") 69 if len(lines) != 6 { 70 t.Fatalf("parallelTestHelper gave %d lines of output; want 6", len(lines)) 71 } 72 want := "helperfuncs_test.go:21: parallel" 73 if got := strings.TrimSpace(lines[1]); got != want { 74 t.Errorf("got output line %q; want %q", got, want) 75 } 76 } 77 78 type noopWriter int 79 80 func (nw *noopWriter) Write(b []byte) (int, error) { return len(b), nil } 81 82 func BenchmarkTBHelper(b *B) { 83 w := noopWriter(0) 84 ctx := newTestContext(1, allMatcher()) 85 t1 := &T{ 86 common: common{ 87 signal: make(chan bool), 88 w: &w, 89 }, 90 context: ctx, 91 } 92 f1 := func() { 93 t1.Helper() 94 } 95 f2 := func() { 96 t1.Helper() 97 } 98 b.ResetTimer() 99 b.ReportAllocs() 100 for i := 0; i < b.N; i++ { 101 if i&1 == 0 { 102 f1() 103 } else { 104 f2() 105 } 106 } 107 }