Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Not() expectation #364

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions expectation.not.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package testkit

import (
"fmt"

"github.com/dogmatiq/testkit/fact"
)

// Not is an expectation that passes only if the given expectation fails.
func Not(expectation Expectation) Expectation {
return &notExpectation{
caption: fmt.Sprintf("not %s", expectation.Caption()),
expectation: expectation,
}
}

// notExpectation is an [Expectation] that inverts another expectation.
//
// It uses a predicate function to determine whether the not expectation is met.
koden-km marked this conversation as resolved.
Show resolved Hide resolved
type notExpectation struct {
caption string
expectation Expectation
}

func (e *notExpectation) Caption() string {
return e.caption
}

func (e *notExpectation) Predicate(s PredicateScope) (Predicate, error) {
p, err := e.expectation.Predicate(s)
if err != nil {
return nil, err
}

return &notPredicate{
expectation: p,
}, nil
}

// notPredicate is the [Predicate] implementation for [notExpectation].
type notPredicate struct {
expectation Predicate
}

func (p *notPredicate) Notify(f fact.Fact) {
p.expectation.Notify(f)
}

func (p *notPredicate) Ok() bool {
return !p.expectation.Ok()
}

func (p *notPredicate) Done() {
p.expectation.Done()
}

func (p *notPredicate) Report(ctx ReportGenerationContext) *Report {
ctx.IsInverted = !ctx.IsInverted

r := p.expectation.Report(ctx)

rep := &Report{
TreeOk: ctx.TreeOk,
Ok: p.Ok(),
Criteria: "do not " + r.Criteria,
}

return rep
}
106 changes: 106 additions & 0 deletions expectation.not_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package testkit_test

import (
"context"

"github.com/dogmatiq/dogma"
. "github.com/dogmatiq/enginekit/enginetest/stubs"
. "github.com/dogmatiq/testkit"
"github.com/dogmatiq/testkit/internal/testingmock"
g "github.com/onsi/ginkgo/v2"
gm "github.com/onsi/gomega"
)

var _ = g.Context("not expectation", func() {
var (
testingT *testingmock.T
app dogma.Application
test *Test
)

g.BeforeEach(func() {
testingT = &testingmock.T{
FailSilently: true,
}

app = &ApplicationStub{
ConfigureFunc: func(c dogma.ApplicationConfigurer) {
c.Identity("<app>", "00df8612-2fd4-4ae3-9acf-afc2b4daf272")
c.RegisterIntegration(&IntegrationMessageHandlerStub{
ConfigureFunc: func(c dogma.IntegrationConfigurer) {
c.Identity("<integration>", "12dfb90b-e47b-4d49-b834-294b01992ad0")
c.Routes(
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.RecordsEvent[EventStub[TypeA]](),
)
},
HandleCommandFunc: func(
_ context.Context,
s dogma.IntegrationCommandScope,
_ dogma.Command,
) error {
s.RecordEvent(EventA1)
return nil
},
})
},
}

test = Begin(testingT, app).
EnableHandlers("<integration>")
})

testExpectationBehavior := func(
e Expectation,
ok bool,
rm reportMatcher,
) {
test.Expect(ExecuteCommand(CommandA1), e)
rm(testingT)
gm.Expect(testingT.Failed()).To(gm.Equal(!ok))
}

g.Describe("func Not()", func() {
g.DescribeTable(
"expectation behavior",
testExpectationBehavior,
g.Entry(
"it fails when the child expectation passes",
Not(ToRecordEvent(EventA1)),
expectFail,
expectReport(
`✗ do not record a specific 'stubs.EventStub[TypeA]' event`,
),
),
g.Entry(
"it passes when the child expectation fails",
Not(ToRecordEvent(EventA2)),
expectPass,
expectReport(
`✓ do not record a specific 'stubs.EventStub[TypeA]' event`,
),
),
)

g.It("produces the expected caption", func() {
test.Expect(
noop,
Not(ToRecordEvent(EventA2)),
)

gm.Expect(testingT.Logs).To(gm.ContainElement(
"--- expect [no-op] not to record a specific 'stubs.EventStub[TypeA]' event ---",
))
})

g.It("fails the test if the child cannot construct a predicate", func() {
test.Expect(
noop,
Not(failBeforeAction),
)

gm.Expect(testingT.Logs).To(gm.ContainElement("<always fail before action>"))
gm.Expect(testingT.Failed()).To(gm.BeTrue())
})
})
})
2 changes: 1 addition & 1 deletion internal/fixtures/protobuf.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.