-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
67 lines (58 loc) · 1.81 KB
/
main_test.go
File metadata and controls
67 lines (58 loc) · 1.81 KB
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
60
61
62
63
64
65
66
67
package amapretty
import (
"io"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func init() {
timeNow = func() time.Time {
t, _ := time.Parse("2006-01-02 15:04:05", "2023-02-24 05:02:03")
return t
}
runtimeCaller = func(skip int) (pc uintptr, file string, line int, ok bool) {
return uintptr(0), "/Users/username/path/project/main.go", 101, true
}
}
func setOutput() func() string {
r, w, _ := os.Pipe()
output = w
return func() string {
w.Close()
out, _ := io.ReadAll(r)
output = os.Stdout
return string(out)
}
}
func TestPrint(t *testing.T) {
cases := []struct {
name string
expected string
args interface{}
}{
{
name: "with simple string argument",
args: "test",
expected: "[\x1b[1;32mamapretty\x1b[0m] \x1b[1;34m2023-02-24T05:02:03Z\x1b[0m \x1b[1;36m/Users/username/path/project/main.go:101\x1b[0m -- [\n\t\"test\"\n]\n",
},
{
name: "with complex args",
args: []struct{ Name string }{{Name: "One"}, {Name: "Chosen"}},
expected: "[\x1b[1;32mamapretty\x1b[0m] \x1b[1;34m2023-02-24T05:02:03Z\x1b[0m \x1b[1;36m/Users/username/path/project/main.go:101\x1b[0m -- [\n\t[\n\t\t{\n\t\t\t\"Name\": \"One\"\n\t\t},\n\t\t{\n\t\t\t\"Name\": \"Chosen\"\n\t\t}\n\t]\n]\n",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
outputCallbackF := setOutput()
Print(c.args)
assert.Equal(t, c.expected, outputCallbackF())
})
}
}
func TestPrintf(t *testing.T) {
outputCallbackF := setOutput()
Printf("dime: %d, val: %s, time: %v", 123, "none", timeNow().Format(time.RFC3339))
expected := "[\x1b[1;32mamapretty\x1b[0m] \x1b[1;34m2023-02-24T05:02:03Z\x1b[0m \x1b[1;36m/Users/username/path/project/main.go:101\x1b[0m -- [\n\t\"dime: 123, val: none, time: 2023-02-24T05:02:03Z\"\n]\n"
assert.Equal(t, expected, outputCallbackF())
}