goconvey
goconvey copied to clipboard
Basic Custom functions passed to Convey
I was looking to make my tests more readable by not creating a new line for each unimplemented feature on the report.
I couldn't find a way, so I made some simple modifications to the base package that now allow me to write mockups like these with results below, if anyone is interested...
func TestSpec(t *testing.T) {
Convey("Given a new system", t, func() {
Convey("When Conveying new behaviors", func() {
Convey("With nil should work as expected", nil)
Convey("With NotImplemented() should display a warning message", NotImplemented())
Convey("With Warn(...) should display a custom warning message", Warn("Custom Warning"))
Convey("With TODO(...) should display a custom todo message", TODO("My TODO"))
Convey("With TODO(...) inside a function should display neatly as a list below", func() {
Convey("Talk to Peter", TODO(""))
Convey("Clean up code", TODO(""))
})
})
})
}
Results:
Assuming following file can be added somewhere in the package (e.g. warnings.go), the only changes required to the base code are shown on the discovery.go
diff below...
warnings.go
package convey
import "fmt"
var (
warning_fn = func() {}
warning_msg = ""
)
func NotImplemented() *func() {
return Warn("Not implemented")
}
func Warn(msg string) *func() {
return warn("WARNING", msg, &warning_fn)
}
func TODO(msg string) *func() {
return warn("TODO", msg, &warning_fn)
}
func warn(prefix, msg string, fn *func()) *func() {
if msg != "" {
prefix = prefix + ": "
}
warning_msg = "[" + prefix + msg + "]"
return fn
}
discovery.diff
diff --git a/convey/discovery.go b/convey/discovery.go
index eb8d4cb..7593235 100644
--- a/convey/discovery.go
+++ b/convey/discovery.go
@@ -1,5 +1,7 @@
package convey
+import "fmt"
+
type actionSpecifier uint8
const (
@@ -43,6 +45,11 @@ func discover(items []interface{}) *suite {
conveyPanic(parseError)
}
+ if warning_msg != "" {
+ name = fmt.Sprintf("%s %s", name, warning_msg)
+ warning_msg = ""
+ }
+
return newSuite(name, failure, action, test, specifier)
}
func item(items []interface{}) interface{} {
@@ -78,6 +85,10 @@ func parseAction(items []interface{}) (func(C), []interface{}) {
return x, items[1:]
case func():
return func(C) { x() }, items[1:]
+ case *func():
+ if x == &warning_fn {
+ return nil, items[1:]
+ }
}
conveyPanic(parseError)
panic("never get here")