forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestutil_test.go
59 lines (46 loc) · 1.3 KB
/
testutil_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
51
52
53
54
55
56
57
58
59
package testutil_test
import (
"fmt"
"os"
"testing"
"time"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestDiscardStdout(t *testing.T) {
err := testutil.DiscardStdout()
fmt.Println("Hello, playground")
str := testutil.RestoreStdout()
assert.NoErr(t, err)
assert.Eq(t, "", str)
}
func TestRewriteStdout(t *testing.T) {
testutil.RewriteStdout()
assert.Eq(t, "", testutil.RestoreStdout())
testutil.RewriteStdout()
fmt.Println("Hello, playground")
msg := testutil.RestoreStdout()
assert.Eq(t, "Hello, playground\n", msg)
}
func TestRewriteStderr(t *testing.T) {
testutil.RewriteStderr()
assert.Eq(t, "", testutil.RestoreStderr())
testutil.RewriteStderr()
_, err := fmt.Fprint(os.Stderr, "Hello, playground")
msg := testutil.RestoreStderr()
assert.NoErr(t, err)
assert.Eq(t, "Hello, playground", msg)
}
func TestNewDirEnt(t *testing.T) {
de := testutil.NewDirEnt("testdata/some.txt")
assert.NotEmpty(t, de)
assert.False(t, de.IsDir())
}
// test testutil.SetTimeLocal
func TestSetTimeLocal(t *testing.T) {
testutil.SetTimeLocalUTC()
tt, err := time.ParseInLocation("2006-01-02 15:04:05", "2021-01-01 12:12:12", time.Local)
assert.NoError(t, err)
assert.Eq(t, "2021-01-01 12:12:12", tt.Format("2006-01-02 15:04:05"))
testutil.RestoreTimeLocal()
}