From a9793712606dd72b256bcbb0fad0858aa0e72d67 Mon Sep 17 00:00:00 2001 From: Stephen Shirley Date: Thu, 13 Oct 2016 16:27:51 +0200 Subject: [PATCH] SoMsg proof of concept. Adds a variant of So() that allows you to specify a message that is reported on error. --- convey/context.go | 11 +++++++++++ convey/doc.go | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/convey/context.go b/convey/context.go index 2c75c2d7..3cfbd590 100644 --- a/convey/context.go +++ b/convey/context.go @@ -177,6 +177,17 @@ func (ctx *context) So(actual interface{}, assert assertion, expected ...interfa } } +func (ctx *context) SoMsg(msg string, actual interface{}, assert assertion, expected ...interface{}) { + if result := assert(actual, expected...); result == assertionSuccess { + ctx.assertionReport(reporting.NewSuccessReport()) + return + } else { + ctx.reporter.Enter(reporting.NewScopeReport(msg)) + defer ctx.reporter.Exit() + ctx.assertionReport(reporting.NewFailureReport(result)) + } +} + func (ctx *context) Reset(action func()) { /* TODO: Failure mode configuration */ ctx.resets = append(ctx.resets, action) diff --git a/convey/doc.go b/convey/doc.go index 2562ce4c..b277179c 100644 --- a/convey/doc.go +++ b/convey/doc.go @@ -24,6 +24,7 @@ type C interface { FocusConvey(items ...interface{}) So(actual interface{}, assert assertion, expected ...interface{}) + SoMsg(msg string, actual interface{}, assert assertion, expected ...interface{}) SkipSo(stuff ...interface{}) Reset(action func()) @@ -125,6 +126,10 @@ func So(actual interface{}, assert assertion, expected ...interface{}) { mustGetCurrentContext().So(actual, assert, expected...) } +func SoMsg(msg string, actual interface{}, assert assertion, expected ...interface{}) { + mustGetCurrentContext().SoMsg(msg, actual, assert, expected...) +} + // SkipSo is analagous to So except that the assertion that would have been passed // to So is not executed and the reporter is notified that the assertion was skipped. func SkipSo(stuff ...interface{}) {