-
Notifications
You must be signed in to change notification settings - Fork 5
/
testing.go
45 lines (37 loc) · 1.35 KB
/
testing.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
// Copyright 2020 ActiveState Software. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
package termtest
import (
"fmt"
"testing"
expect "github.com/ActiveState/termtest/expect"
"github.com/ActiveState/termtest/internal/osutils/stacktrace"
)
// TestSendObserveFn is an example for a SendObserver function, it reports any error during Send calls to the supplied testing instance
func TestSendObserveFn(t *testing.T) func(string, int, error) {
return func(msg string, num int, err error) {
if err == nil {
return
}
t.Fatalf("Could not send data to terminal\nerror: %v", err)
}
}
// TestExpectObserveFn an example for a ExpectObserver function, it reports any error occurring durint expect calls to the supplied testing instance
func TestExpectObserveFn(t *testing.T) expect.ExpectObserver {
return func(matchers []expect.Matcher, ms *expect.MatchState, err error) {
if err == nil {
return
}
var value string
var sep string
for _, matcher := range matchers {
value += fmt.Sprintf("%s%v", sep, matcher.Criteria())
sep = ", "
}
t.Fatalf(
"Could not meet expectation: Expectation: '%s'\nError: %v at\n%s\n---\nTerminal snapshot:\n%s\n---\nParsed output:\n%+q\n",
value, err, stacktrace.Get().String(), ms.TermState.StringBeforeCursor(), ms.Buf.String(),
)
}
}