github.com/blend/go-sdk@v1.20220411.3/assert/context.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package assert
     9  
    10  import "context"
    11  
    12  type testNameKey struct{}
    13  
    14  // WithTestName sets the test name.
    15  func WithTestName(ctx context.Context, id string) context.Context {
    16  	return context.WithValue(ctx, testNameKey{}, id)
    17  }
    18  
    19  // GetTestName gets the test name for a test run context.
    20  func GetTestName(ctx context.Context) string {
    21  	if value := ctx.Value(testNameKey{}); value != nil {
    22  		if typed, ok := value.(string); ok {
    23  			return typed
    24  		}
    25  	}
    26  	return ""
    27  }
    28  
    29  type contextIDKey struct{}
    30  
    31  // WithContextID sets the test context id.
    32  func WithContextID(ctx context.Context, id string) context.Context {
    33  	return context.WithValue(ctx, contextIDKey{}, id)
    34  }
    35  
    36  // GetContextID gets the context id for a test run.
    37  func GetContextID(ctx context.Context) string {
    38  	if value := ctx.Value(contextIDKey{}); value != nil {
    39  		if typed, ok := value.(string); ok {
    40  			return typed
    41  		}
    42  	}
    43  	return ""
    44  }