-
Notifications
You must be signed in to change notification settings - Fork 10
/
Spec_bc_test.go
50 lines (42 loc) · 1.41 KB
/
Spec_bc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package testcase
import (
"bytes"
"testing"
"go.llib.dev/testcase/assert"
"go.llib.dev/testcase/internal/doubles"
"go.llib.dev/testcase/sandbox"
)
func TestSpec_FriendlyVarNotDefined(t *testing.T) {
stub := &doubles.TB{}
s := NewSpec(stub)
willFatalWithMessage := willFatalWithMessageFn(stub)
v1 := Let[string](s, func(t *T) string { return `hello-world` })
v2 := Let[string](s, func(t *T) string { return `hello-world` })
tct := NewTWithSpec(stub, s)
s.Test(`var1 var found`, func(t *T) {
assert.Must(t).Equal(`hello-world`, v1.Get(t))
})
t.Run(`not existing var will panic with friendly msg`, func(t *testing.T) {
msg := willFatalWithMessage(t, func() { tct.vars.Get(tct, `not-exist`) })
assert.Must(t).Contain(msg.String(), `Variable "not-exist" is not found`)
assert.Must(t).Contain(msg.String(), `Did you mean?`)
assert.Must(t).Contain(msg.String(), v1.ID)
assert.Must(t).Contain(msg.String(), v2.ID)
})
}
func isFatalFn(stub *doubles.TB) func(block func()) bool {
return func(block func()) bool {
stub.IsFailed = false
defer func() { stub.IsFailed = false }()
out := sandbox.Run(block)
return !out.OK && stub.Failed()
}
}
func willFatalWithMessageFn(stub *doubles.TB) func(tb testing.TB, blk func()) bytes.Buffer {
isFatal := isFatalFn(stub)
return func(tb testing.TB, blk func()) bytes.Buffer {
stub.Logs = bytes.Buffer{}
assert.Must(tb).True(isFatal(blk))
return stub.Logs
}
}