...

Source file src/github.com/onsi/ginkgo/v2/ginkgo_t_dsl.go

Documentation: github.com/onsi/ginkgo/v2

     1  package ginkgo
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/onsi/ginkgo/v2/internal/testingtproxy"
     7  	"github.com/onsi/ginkgo/v2/types"
     8  )
     9  
    10  /*
    11  GinkgoT() implements an interface that allows third party libraries to integrate with and build on top of Ginkgo.
    12  
    13  GinkgoT() is analogous to *testing.T and implements the majority of *testing.T's methods.  It can be typically be used a a drop-in replacement with third-party libraries that accept *testing.T through an interface.
    14  
    15  GinkgoT() takes an optional offset argument that can be used to get the
    16  correct line number associated with the failure - though you do not need to use this if you call GinkgoHelper() or GinkgoT().Helper() appropriately
    17  
    18  GinkgoT() attempts to mimic the behavior of `testing.T` with the exception of the following:
    19  
    20  - Error/Errorf: failures in Ginkgo always immediately stop execution and there is no mechanism to log a failure without aborting the test.  As such Error/Errorf are equivalent to Fatal/Fatalf.
    21  - Parallel() is a no-op as Ginkgo's multi-process parallelism model is substantially different from go test's in-process model.
    22  
    23  You can learn more here: https://onsi.github.io/ginkgo/#using-third-party-libraries
    24  */
    25  func GinkgoT(optionalOffset ...int) FullGinkgoTInterface {
    26  	offset := 1
    27  	if len(optionalOffset) > 0 {
    28  		offset = optionalOffset[0]
    29  	}
    30  	return testingtproxy.New(
    31  		GinkgoWriter,
    32  		Fail,
    33  		Skip,
    34  		DeferCleanup,
    35  		CurrentSpecReport,
    36  		AddReportEntry,
    37  		GinkgoRecover,
    38  		AttachProgressReporter,
    39  		suiteConfig.RandomSeed,
    40  		suiteConfig.ParallelProcess,
    41  		suiteConfig.ParallelTotal,
    42  		reporterConfig.NoColor,
    43  		offset)
    44  }
    45  
    46  /*
    47  The portion of the interface returned by GinkgoT() that maps onto methods in the testing package's T.
    48  */
    49  type GinkgoTInterface interface {
    50  	Cleanup(func())
    51  	Setenv(kev, value string)
    52  	Error(args ...any)
    53  	Errorf(format string, args ...any)
    54  	Fail()
    55  	FailNow()
    56  	Failed() bool
    57  	Fatal(args ...any)
    58  	Fatalf(format string, args ...any)
    59  	Helper()
    60  	Log(args ...any)
    61  	Logf(format string, args ...any)
    62  	Name() string
    63  	Parallel()
    64  	Skip(args ...any)
    65  	SkipNow()
    66  	Skipf(format string, args ...any)
    67  	Skipped() bool
    68  	TempDir() string
    69  }
    70  
    71  /*
    72  Additional methods returned by GinkgoT() that provide deeper integration points into Ginkgo
    73  */
    74  type FullGinkgoTInterface interface {
    75  	GinkgoTInterface
    76  
    77  	AddReportEntryVisibilityAlways(name string, args ...any)
    78  	AddReportEntryVisibilityFailureOrVerbose(name string, args ...any)
    79  	AddReportEntryVisibilityNever(name string, args ...any)
    80  
    81  	//Prints to the GinkgoWriter
    82  	Print(a ...any)
    83  	Printf(format string, a ...any)
    84  	Println(a ...any)
    85  
    86  	//Provides access to Ginkgo's color formatting, correctly configured to match the color settings specified in the invocation of ginkgo
    87  	F(format string, args ...any) string
    88  	Fi(indentation uint, format string, args ...any) string
    89  	Fiw(indentation uint, maxWidth uint, format string, args ...any) string
    90  
    91  	//Generates a formatted string version of the current spec's timeline
    92  	RenderTimeline() string
    93  
    94  	GinkgoRecover()
    95  	DeferCleanup(args ...any)
    96  
    97  	RandomSeed() int64
    98  	ParallelProcess() int
    99  	ParallelTotal() int
   100  
   101  	AttachProgressReporter(func() string) func()
   102  }
   103  
   104  /*
   105  GinkgoTB() implements a wrapper that exactly matches the testing.TB interface.
   106  
   107  In go 1.18 a new private() function was added to the testing.TB interface. Any function which accepts testing.TB as input needs to be passed in something that directly implements testing.TB.
   108  
   109  This wrapper satisfies the testing.TB interface and intended to be used as a drop-in replacement with third party libraries that accept testing.TB.
   110  
   111  Similar to GinkgoT(), GinkgoTB() takes an optional offset argument that can be used to get the
   112  correct line number associated with the failure - though you do not need to use this if you call GinkgoHelper() or GinkgoT().Helper() appropriately
   113  */
   114  func GinkgoTB(optionalOffset ...int) *GinkgoTBWrapper {
   115  	offset := 2
   116  	if len(optionalOffset) > 0 {
   117  		offset = optionalOffset[0]
   118  	}
   119  	return &GinkgoTBWrapper{GinkgoT: GinkgoT(offset)}
   120  }
   121  
   122  type GinkgoTBWrapper struct {
   123  	testing.TB
   124  	GinkgoT FullGinkgoTInterface
   125  }
   126  
   127  func (g *GinkgoTBWrapper) Cleanup(f func()) {
   128  	g.GinkgoT.Cleanup(f)
   129  }
   130  func (g *GinkgoTBWrapper) Error(args ...any) {
   131  	g.GinkgoT.Error(args...)
   132  }
   133  func (g *GinkgoTBWrapper) Errorf(format string, args ...any) {
   134  	g.GinkgoT.Errorf(format, args...)
   135  }
   136  func (g *GinkgoTBWrapper) Fail() {
   137  	g.GinkgoT.Fail()
   138  }
   139  func (g *GinkgoTBWrapper) FailNow() {
   140  	g.GinkgoT.FailNow()
   141  }
   142  func (g *GinkgoTBWrapper) Failed() bool {
   143  	return g.GinkgoT.Failed()
   144  }
   145  func (g *GinkgoTBWrapper) Fatal(args ...any) {
   146  	g.GinkgoT.Fatal(args...)
   147  }
   148  func (g *GinkgoTBWrapper) Fatalf(format string, args ...any) {
   149  	g.GinkgoT.Fatalf(format, args...)
   150  }
   151  func (g *GinkgoTBWrapper) Helper() {
   152  	types.MarkAsHelper(1)
   153  }
   154  func (g *GinkgoTBWrapper) Log(args ...any) {
   155  	g.GinkgoT.Log(args...)
   156  }
   157  func (g *GinkgoTBWrapper) Logf(format string, args ...any) {
   158  	g.GinkgoT.Logf(format, args...)
   159  }
   160  func (g *GinkgoTBWrapper) Name() string {
   161  	return g.GinkgoT.Name()
   162  }
   163  func (g *GinkgoTBWrapper) Setenv(key, value string) {
   164  	g.GinkgoT.Setenv(key, value)
   165  }
   166  func (g *GinkgoTBWrapper) Skip(args ...any) {
   167  	g.GinkgoT.Skip(args...)
   168  }
   169  func (g *GinkgoTBWrapper) SkipNow() {
   170  	g.GinkgoT.SkipNow()
   171  }
   172  func (g *GinkgoTBWrapper) Skipf(format string, args ...any) {
   173  	g.GinkgoT.Skipf(format, args...)
   174  }
   175  func (g *GinkgoTBWrapper) Skipped() bool {
   176  	return g.GinkgoT.Skipped()
   177  }
   178  func (g *GinkgoTBWrapper) TempDir() string {
   179  	return g.GinkgoT.TempDir()
   180  }
   181  

View as plain text