Skip to content

Commit

Permalink
Add ReportGenerationContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Aug 20, 2024
1 parent 3a6ad71 commit fd4e4eb
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 36 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions expectation.composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions expectation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions expectation.message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions expectation.messagematch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions expectation.messagetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down
8 changes: 3 additions & 5 deletions expectation.repeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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))

}

Expand Down
6 changes: 3 additions & 3 deletions expectation.satisfy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions expectation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := "<always fail>"
if e.ok {
c = "<always pass>"
}

return &Report{
TreeOk: treeOk,
TreeOk: ctx.TreeOk,
Ok: e.ok,
Criteria: c,
}
Expand Down
11 changes: 11 additions & 0 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit fd4e4eb

Please sign in to comment.