...

Source file src/github.com/launchdarkly/go-server-sdk-evaluation/v2/test_helpers_test.go

Documentation: github.com/launchdarkly/go-server-sdk-evaluation/v2

     1  package evaluation
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/launchdarkly/go-sdk-common/v3/ldreason"
     7  	"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
     8  	m "github.com/launchdarkly/go-test-helpers/v3/matchers"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // FailOnAnyPrereqEvent can be used as the prerequisiteFlagEventRecorder parameter to Evaluator.Evaluate()
    13  // in any test where we do not expect any prerequisite events to be generated. It causes an automaitc test
    14  // failure if it receives any events.
    15  func FailOnAnyPrereqEvent(t *testing.T) func(PrerequisiteFlagEvent) {
    16  	return func(e PrerequisiteFlagEvent) {
    17  		assert.Fail(t, "did not expect any prerequisite events", "got event: %+v", e)
    18  	}
    19  }
    20  
    21  // EvalDetailEquals is a custom matcher that works the same as m.Equals, but produces better failure
    22  // output by treating the value as a JSON object rather than a struct.
    23  func EvalDetailEquals(expected ldreason.EvaluationDetail) m.Matcher {
    24  	return m.JSONEqual(expected)
    25  }
    26  
    27  // EvalDetailProps is a shortcut for matching all three fields of an EvaluationDetail.
    28  func EvalDetailProps(variationIndex int, value ldvalue.Value, reason ldreason.EvaluationReason) m.Matcher {
    29  	return EvalDetailEquals(ldreason.NewEvaluationDetail(value, variationIndex, reason))
    30  }
    31  
    32  // EvalDetailError is a shortcut for matching the fields of an EvaluationDetail for a failed evaluation.
    33  func EvalDetailError(errorKind ldreason.EvalErrorKind) m.Matcher {
    34  	return EvalDetailEquals(ldreason.NewEvaluationDetailForError(errorKind, ldvalue.Null()))
    35  }
    36  
    37  // EvalResultDetail is a shortcut for matching all three fields of a successful evaluation result.
    38  func ResultDetailProps(variationIndex int, value ldvalue.Value, reason ldreason.EvaluationReason) m.Matcher {
    39  	return ResultDetail().Should(EvalDetailProps(variationIndex, value, reason))
    40  }
    41  
    42  // EvalResultDetail is a shortcut for matching a Result for a failed evaluation.
    43  func ResultDetailError(errorKind ldreason.EvalErrorKind) m.Matcher {
    44  	return ResultDetail().Should(EvalDetailError(errorKind))
    45  }
    46  
    47  func ResultDetail() m.MatcherTransform {
    48  	return m.Transform("result detail", func(value interface{}) (interface{}, error) { return value.(Result).Detail, nil }).
    49  		EnsureInputValueType(Result{})
    50  }
    51  

View as plain text