forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_test.go
48 lines (39 loc) · 1.05 KB
/
log_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
package fyne
import (
"bufio"
"bytes"
"errors"
"log"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func bufferLog(t *testing.T, reason string, err error) []string {
buf := bytes.NewBuffer([]byte{})
write := bufio.NewWriter(buf)
log.SetOutput(write)
LogError(reason, err)
log.SetOutput(os.Stdout)
err = write.Flush()
if err != nil {
t.Error(err)
}
output := strings.TrimSpace(buf.String())
return strings.Split(output, "\n")
}
func TestLogError(t *testing.T) {
err := errors.New("dummy error")
output := bufferLog(t, "Testing errors", err)
assert.Equal(t, 3, len(output))
assert.True(t, strings.Contains(output[0], "Testing errors"))
assert.True(t, strings.Contains(output[1], "Cause"))
assert.True(t, strings.Contains(output[1], "dummy"))
assert.True(t, strings.Contains(output[2], "At"))
}
func TestLogErrorNoErr(t *testing.T) {
output := bufferLog(t, "Testing errors", nil)
assert.Equal(t, 2, len(output))
assert.True(t, strings.Contains(output[0], "Testing errors"))
assert.True(t, strings.Contains(output[1], "At"))
}