Commit dd60766

mo khan <mo@mokhan.ca>
2021-10-13 17:45:45
feat: add a context/spec flavoured runner
1 parent 3d6bad2
Changed files (3)
spec/spec.go
@@ -0,0 +1,34 @@
+package spec
+
+import "testing"
+
+// establish context (e.g. "when creating a new sparkle")
+// because of (e.g. "with an empty body")
+// it behaves like (e.g. "it returns an error message")
+type Context struct {
+	because func() interface{}
+	its     []ItFunc
+	Result  interface{}
+}
+type ContextFunc func(*Context)
+type BecauseFunc func() interface{}
+type ItFunc func(*testing.T)
+
+func Establish(t *testing.T, x ContextFunc) {
+	context := Context{
+		its: []ItFunc{},
+	}
+	x(&context)
+	context.Result = context.because()
+	for _, it := range context.its {
+		it(t)
+	}
+}
+
+func (c *Context) Because(b func() interface{}) {
+	c.because = b
+}
+
+func (c *Context) It(it ItFunc) {
+	c.its = append(c.its, it)
+}
spec/spec_test.go
@@ -0,0 +1,19 @@
+package spec
+
+import (
+	"testing"
+)
+
+func TestCalculator(t *testing.T) {
+	t.Run("when adding 1 + 1", func(t *testing.T) {
+		Establish(t, func(x *Context) {
+			x.Because(func() interface{} { return 1 + 1 })
+
+			x.It(func(t *testing.T) {
+				if x.Result != 2 {
+					t.Errorf("Expected: 2, Got: %d", x.Result)
+				}
+			})
+		})
+	})
+}
go.sum