github.com/mangodowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/testing/helperfuncs_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 "sync"
     8  
     9  // The line numbering of this file is important for TestTBHelper.
    10  
    11  func notHelper(t *T, msg string) {
    12  	t.Error(msg)
    13  }
    14  
    15  func helper(t *T, msg string) {
    16  	t.Helper()
    17  	t.Error(msg)
    18  }
    19  
    20  func notHelperCallingHelper(t *T, msg string) {
    21  	helper(t, msg)
    22  }
    23  
    24  func helperCallingHelper(t *T, msg string) {
    25  	t.Helper()
    26  	helper(t, msg)
    27  }
    28  
    29  func testHelper(t *T) {
    30  	// Check combinations of directly and indirectly
    31  	// calling helper functions.
    32  	notHelper(t, "0")
    33  	helper(t, "1")
    34  	notHelperCallingHelper(t, "2")
    35  	helperCallingHelper(t, "3")
    36  
    37  	// Check a function literal closing over t that uses Helper.
    38  	fn := func(msg string) {
    39  		t.Helper()
    40  		t.Error(msg)
    41  	}
    42  	fn("4")
    43  
    44  	// Check that calling Helper from inside this test entry function
    45  	// doesn't have an effect.
    46  	t.Helper()
    47  	t.Error("5")
    48  
    49  	t.Run("sub", func(t *T) {
    50  		helper(t, "6")
    51  		notHelperCallingHelper(t, "7")
    52  		t.Helper()
    53  		t.Error("8")
    54  	})
    55  }
    56  
    57  func parallelTestHelper(t *T) {
    58  	var wg sync.WaitGroup
    59  	for i := 0; i < 5; i++ {
    60  		wg.Add(1)
    61  		go func() {
    62  			notHelperCallingHelper(t, "parallel")
    63  			wg.Done()
    64  		}()
    65  	}
    66  	wg.Wait()
    67  }