-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
50 lines (41 loc) · 1.21 KB
/
example_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 faket_test
import (
"fmt"
"strings"
"testing"
"github.com/prashantv/faket"
)
func StrContainsInOrder(t testing.TB, s string, contains ...string) {
for _, v := range contains {
i := strings.Index(s, v)
if i < 0 {
// Failed to find element, report error.
t.Errorf("failed to find %q in remaining string %q", v, s)
return
}
// Continue searching for remaining elements after the current found value.
s = s[i+len(v):]
}
}
func ExampleRunTest_failure() {
res := faket.RunTest(func(t testing.TB) {
StrContainsInOrder(t, "help test foo", "test", "helper")
})
fmt.Println("Failed:", res.Failed())
fmt.Println("Logs:", res.Logs().String())
// Output:
// Failed: true
// Logs: example_test.go:16: failed to find "helper" in remaining string " foo"
}
func TestStrContainsInOrder(t *testing.T) {
t.Run("correct order", func(t *testing.T) {
faket.RunTest(func(t testing.TB) {
StrContainsInOrder(t, "test helper function", "test", "helper")
}).MustPass(t)
})
t.Run("incorrect order", func(t *testing.T) {
faket.RunTest(func(t testing.TB) {
StrContainsInOrder(t, "test helper function", "helper", "test")
}).MustFail(t, `failed to find "test" in remaining string " function"`)
})
}