diff --git a/CHANGELOG.md b/CHANGELOG.md index 27c2a168..56f8fd2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,18 @@ The format is based on [Keep a Changelog], and this project adheres to [keep a changelog]: https://keepachangelog.com/en/1.0.0/ [semantic versioning]: https://semver.org/spec/v2.0.0.html +## [Unreleased] + +### Added + +- Added `ReportGenerationContext` to hold information required when generating + test reports. + +### Changed + +- **[BC]** Changed `Predicate.Report()` to accept a `ReportGenerationContext` + instead of individual arguments. + ## [0.16.0] - 2024-08-17 ### Changed diff --git a/expectation.composite.go b/expectation.composite.go index e93d88b5..fd97a5f0 100644 --- a/expectation.composite.go +++ b/expectation.composite.go @@ -162,24 +162,22 @@ func (p *compositePredicate) Done() { } } -func (p *compositePredicate) Report(treeOk, isInverted bool) *Report { +func (p *compositePredicate) Report(ctx ReportGenerationContext) *Report { if p.isInverted { - isInverted = !isInverted + ctx.IsInverted = !ctx.IsInverted } m, ok := p.ok() rep := &Report{ - TreeOk: treeOk, + TreeOk: ctx.TreeOk, Ok: ok, Criteria: p.criteria, Outcome: m, } for _, c := range p.children { - rep.Append( - c.Report(treeOk, isInverted), - ) + rep.Append(c.Report(ctx)) } return rep diff --git a/expectation.go b/expectation.go index a57739a6..a6551335 100644 --- a/expectation.go +++ b/expectation.go @@ -42,15 +42,8 @@ type Predicate interface { // Report returns a report describing whether or not the expectation was // met. // - // treeOk is true if the entire "tree" of expectations is considered to have - // passed. This may not be the same value as returned from Ok() when this - // expectation is used as a child of a composite expectation. - // - // isInverted is true if the expectation is inverted, i.e. it is expected - // not be met. - // // The behavior of Report() is undefined if Done() has not been called. - Report(treeOk, isInverted bool) *Report + Report(ReportGenerationContext) *Report } // PredicateScope encapsulates the element's of a Test's state that may be diff --git a/expectation.message.go b/expectation.message.go index 7610a855..2601ee13 100644 --- a/expectation.message.go +++ b/expectation.message.go @@ -139,9 +139,9 @@ func (p *messagePredicate) Ok() bool { func (p *messagePredicate) Done() { } -func (p *messagePredicate) Report(treeOk, isInverted bool) *Report { +func (p *messagePredicate) Report(ctx ReportGenerationContext) *Report { rep := &Report{ - TreeOk: treeOk, + TreeOk: ctx.TreeOk, Ok: p.ok, Criteria: inflect.Sprintf( p.expectedRole, @@ -150,7 +150,7 @@ func (p *messagePredicate) Report(treeOk, isInverted bool) *Report { ), } - if treeOk || p.ok || isInverted { + if p.ok || ctx.TreeOk || ctx.IsInverted { return rep } diff --git a/expectation.messagematch.go b/expectation.messagematch.go index ace6b733..c6ac61a8 100644 --- a/expectation.messagematch.go +++ b/expectation.messagematch.go @@ -249,9 +249,9 @@ func (p *messageMatchPredicate[T]) Done() { } } -func (p *messageMatchPredicate[T]) Report(treeOk, isInverted bool) *Report { +func (p *messageMatchPredicate[T]) Report(ctx ReportGenerationContext) *Report { rep := &Report{ - TreeOk: treeOk, + TreeOk: ctx.TreeOk, Ok: p.ok, Criteria: inflect.Sprintf( p.expectedRole, @@ -268,7 +268,7 @@ func (p *messageMatchPredicate[T]) Report(treeOk, isInverted bool) *Report { ) } - if treeOk || p.ok || isInverted { + if p.ok || ctx.TreeOk || ctx.IsInverted { return rep } diff --git a/expectation.messagetype.go b/expectation.messagetype.go index df3a0d9d..89227df9 100644 --- a/expectation.messagetype.go +++ b/expectation.messagetype.go @@ -134,9 +134,9 @@ func (p *messageTypePredicate) Ok() bool { func (p *messageTypePredicate) Done() { } -func (p *messageTypePredicate) Report(treeOk, isInverted bool) *Report { +func (p *messageTypePredicate) Report(ctx ReportGenerationContext) *Report { rep := &Report{ - TreeOk: treeOk, + TreeOk: ctx.TreeOk, Ok: p.ok, Criteria: inflect.Sprintf( p.expectedRole, @@ -145,7 +145,7 @@ func (p *messageTypePredicate) Report(treeOk, isInverted bool) *Report { ), } - if treeOk || p.ok || isInverted { + if p.ok || ctx.TreeOk || ctx.IsInverted { return rep } diff --git a/expectation.repeat.go b/expectation.repeat.go index cc962364..ff07d2ec 100644 --- a/expectation.repeat.go +++ b/expectation.repeat.go @@ -101,9 +101,9 @@ func (p *repeatPredicate) Done() { } } -func (p *repeatPredicate) Report(treeOk, isInverted bool) *Report { +func (p *repeatPredicate) Report(ctx ReportGenerationContext) *Report { rep := &Report{ - TreeOk: treeOk, + TreeOk: ctx.TreeOk, Ok: true, Criteria: p.criteria, } @@ -127,9 +127,7 @@ func (p *repeatPredicate) Report(treeOk, isInverted bool) *Report { // to the report, as the assumption is the factory produces // potentially thousands of very similar expectations which would // pollute the report and make it harder to find the problem. - rep.Append( - c.Report(treeOk, isInverted), - ) + rep.Append(c.Report(ctx)) } diff --git a/expectation.satisfy.go b/expectation.satisfy.go index 9dab9683..d4e85c9d 100644 --- a/expectation.satisfy.go +++ b/expectation.satisfy.go @@ -93,9 +93,9 @@ func (p *satisfyPredicate) Done() { p.pred(&p.satisfyT) } -func (p *satisfyPredicate) Report(treeOk, isInverted bool) *Report { +func (p *satisfyPredicate) Report(ctx ReportGenerationContext) *Report { rep := &Report{ - TreeOk: treeOk, + TreeOk: ctx.TreeOk, Ok: p.Ok(), Criteria: p.criteria, } @@ -106,7 +106,7 @@ func (p *satisfyPredicate) Report(treeOk, isInverted bool) *Report { rep.Outcome = "the expectation failed" } - if !rep.Ok && isInverted { + if !rep.Ok && ctx.IsInverted { return rep } diff --git a/expectation_test.go b/expectation_test.go index e2e478c9..52745d00 100644 --- a/expectation_test.go +++ b/expectation_test.go @@ -39,14 +39,14 @@ func (e staticExpectation) Predicate(PredicateScope) (Predicate, error) { return func (e staticExpectation) Notify(fact.Fact) {} func (e staticExpectation) Ok() bool { return e.ok } func (e staticExpectation) Done() {} -func (e staticExpectation) Report(treeOk, isInverted bool) *Report { +func (e staticExpectation) Report(ctx ReportGenerationContext) *Report { c := "" if e.ok { c = "" } return &Report{ - TreeOk: treeOk, + TreeOk: ctx.TreeOk, Ok: e.ok, Criteria: c, } diff --git a/report.go b/report.go index 59434bc2..c1b20b7e 100644 --- a/report.go +++ b/report.go @@ -24,6 +24,17 @@ const ( failedMatchesSection = "Failed Matches" ) +// ReportGenerationContext is the context in which a report is generated. +type ReportGenerationContext struct { + // TreeOk is true if the entire "tree" of expectations is considered to have + // passed. This may be different to the individual expectation's outcome. + TreeOk bool + + // IsInverted is true if the expectation is inverted, i.e. it is expected + // NOT to be met. + IsInverted bool +} + // Report is a report on the outcome of an expectation. type Report struct { // TreeOk is true if the "tree" that the expectation belongs to passed. diff --git a/test.go b/test.go index 91af053e..dec72735 100644 --- a/test.go +++ b/test.go @@ -124,15 +124,17 @@ func (t *Test) Expect(act Action, e Expectation) *Test { return t // required when using a mock testingT that does not panic } - treeOk := p.Ok() - rep := p.Report(treeOk, false) + ctx := ReportGenerationContext{ + TreeOk: p.Ok(), + } + rep := p.Report(ctx) buf := &strings.Builder{} fmt.Fprint(buf, "--- TEST REPORT ---\n\n") must.WriteTo(buf, rep) t.testingT.Log(buf.String()) - if !treeOk { + if !ctx.TreeOk { t.testingT.FailNow() }