Merge "Add Errs to TestResult" am: 6bd87750ae

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1619408

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I64298e75715678c124dcc736582de262699599b3
This commit is contained in:
Paul Duffin 2021-03-06 08:48:09 +00:00 committed by Automerger Merge Worker
commit df4a0d874d
1 changed files with 20 additions and 12 deletions

View File

@ -382,15 +382,16 @@ type FixtureErrorHandler interface {
// The supplied result can be used to access the state of the code under test just as the main // The supplied result can be used to access the state of the code under test just as the main
// body of the test would but if any errors other than ones expected are reported the state may // body of the test would but if any errors other than ones expected are reported the state may
// be indeterminate. // be indeterminate.
CheckErrors(result *TestResult, errs []error) CheckErrors(result *TestResult)
} }
type simpleErrorHandler struct { type simpleErrorHandler struct {
function func(result *TestResult, errs []error) function func(result *TestResult)
} }
func (h simpleErrorHandler) CheckErrors(result *TestResult, errs []error) { func (h simpleErrorHandler) CheckErrors(result *TestResult) {
h.function(result, errs) result.Helper()
h.function(result)
} }
// The default fixture error handler. // The default fixture error handler.
@ -400,8 +401,9 @@ func (h simpleErrorHandler) CheckErrors(result *TestResult, errs []error) {
// If the test fails this handler will call `result.FailNow()` which will exit the goroutine within // If the test fails this handler will call `result.FailNow()` which will exit the goroutine within
// which the test is being run which means that the RunTest() method will not return. // which the test is being run which means that the RunTest() method will not return.
var FixtureExpectsNoErrors = FixtureCustomErrorHandler( var FixtureExpectsNoErrors = FixtureCustomErrorHandler(
func(result *TestResult, errs []error) { func(result *TestResult) {
FailIfErrored(result.T, errs) result.Helper()
FailIfErrored(result.T, result.Errs)
}, },
) )
@ -418,8 +420,9 @@ var FixtureExpectsNoErrors = FixtureCustomErrorHandler(
// If the test fails this handler will call `result.FailNow()` which will exit the goroutine within // If the test fails this handler will call `result.FailNow()` which will exit the goroutine within
// which the test is being run which means that the RunTest() method will not return. // which the test is being run which means that the RunTest() method will not return.
func FixtureExpectsAtLeastOneErrorMatchingPattern(pattern string) FixtureErrorHandler { func FixtureExpectsAtLeastOneErrorMatchingPattern(pattern string) FixtureErrorHandler {
return FixtureCustomErrorHandler(func(result *TestResult, errs []error) { return FixtureCustomErrorHandler(func(result *TestResult) {
if !FailIfNoMatchingErrors(result.T, pattern, errs) { result.Helper()
if !FailIfNoMatchingErrors(result.T, pattern, result.Errs) {
result.FailNow() result.FailNow()
} }
}) })
@ -439,13 +442,14 @@ func FixtureExpectsAtLeastOneErrorMatchingPattern(pattern string) FixtureErrorHa
// If the test fails this handler will call `result.FailNow()` which will exit the goroutine within // If the test fails this handler will call `result.FailNow()` which will exit the goroutine within
// which the test is being run which means that the RunTest() method will not return. // which the test is being run which means that the RunTest() method will not return.
func FixtureExpectsAllErrorsToMatchAPattern(patterns []string) FixtureErrorHandler { func FixtureExpectsAllErrorsToMatchAPattern(patterns []string) FixtureErrorHandler {
return FixtureCustomErrorHandler(func(result *TestResult, errs []error) { return FixtureCustomErrorHandler(func(result *TestResult) {
CheckErrorsAgainstExpectations(result.T, errs, patterns) result.Helper()
CheckErrorsAgainstExpectations(result.T, result.Errs, patterns)
}) })
} }
// FixtureCustomErrorHandler creates a custom error handler // FixtureCustomErrorHandler creates a custom error handler
func FixtureCustomErrorHandler(function func(result *TestResult, errs []error)) FixtureErrorHandler { func FixtureCustomErrorHandler(function func(result *TestResult)) FixtureErrorHandler {
return simpleErrorHandler{ return simpleErrorHandler{
function: function, function: function,
} }
@ -546,6 +550,9 @@ type TestResult struct {
fixture *fixture fixture *fixture
Config Config Config Config
// The errors that were reported during the test.
Errs []error
} }
var _ FixtureFactory = (*fixtureFactory)(nil) var _ FixtureFactory = (*fixtureFactory)(nil)
@ -656,9 +663,10 @@ func (f *fixture) RunTest() *TestResult {
testContext: testContext{ctx}, testContext: testContext{ctx},
fixture: f, fixture: f,
Config: f.config, Config: f.config,
Errs: errs,
} }
f.errorHandler.CheckErrors(result, errs) f.errorHandler.CheckErrors(result)
return result return result
} }