-
-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathcmd_device_debug_test.go
More file actions
166 lines (150 loc) · 4.77 KB
/
Copy pathcmd_device_debug_test.go
File metadata and controls
166 lines (150 loc) · 4.77 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"testing"
"time"
"github.com/danielpaulus/go-ios/ios/instruments"
"github.com/docopt/docopt-go"
)
func TestInstrumentsSubcommand(t *testing.T) {
testCases := []struct {
name string
args docopt.Opts
want string
}{
{name: "fps", args: docopt.Opts{"instruments": true, "fps": true}, want: "fps"},
{name: "network", args: docopt.Opts{"instruments": true, "network": true}, want: "network"},
{name: "notifications", args: docopt.Opts{"instruments": true, "notifications": true}, want: "notifications"},
{name: "no subcommand", args: docopt.Opts{"instruments": true}, want: ""},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
if got := instrumentsSubcommand(testCase.args); got != testCase.want {
t.Fatalf("instrumentsSubcommand() = %q, want %q", got, testCase.want)
}
})
}
}
func TestInstrumentsCommandDispatch(t *testing.T) {
for _, subcommand := range []string{"fps", "network", "notifications"} {
args := docopt.Opts{"instruments": true, subcommand: true}
matched := false
dispatchCommand(commandContext{Args: args}, []command{
commandByBool("instruments", func(commandContext) { matched = true }),
})
if !matched {
t.Fatalf("instruments %s did not dispatch to the instruments command", subcommand)
}
}
}
func TestInstrumentsSampleDuration(t *testing.T) {
testCases := []struct {
name string
args docopt.Opts
want time.Duration
wantErr bool
}{
{name: "missing", args: docopt.Opts{}, want: 0},
{name: "empty", args: docopt.Opts{"--duration": ""}, want: 0},
{name: "nil", args: docopt.Opts{"--duration": nil}, want: 0},
{name: "seconds", args: docopt.Opts{"--duration": "5"}, want: 5 * time.Second},
{name: "fractional", args: docopt.Opts{"--duration": "0.5"}, want: 500 * time.Millisecond},
{name: "not a number", args: docopt.Opts{"--duration": "abc"}, wantErr: true},
{name: "negative", args: docopt.Opts{"--duration": "-1"}, wantErr: true},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
got, err := instrumentsSampleDuration(testCase.args)
if testCase.wantErr {
if err == nil {
t.Fatal("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != testCase.want {
t.Fatalf("instrumentsSampleDuration() = %v, want %v", got, testCase.want)
}
})
}
}
func withJSONOutput(t *testing.T, disabled bool) {
t.Helper()
previousDisabled, previousPretty := JSONdisabled, prettyJSON
JSONdisabled, prettyJSON = disabled, false
t.Cleanup(func() {
JSONdisabled, prettyJSON = previousDisabled, previousPretty
})
}
func TestFormatFPSSampleJSON(t *testing.T) {
withJSONOutput(t, false)
sample := instruments.FramesPerSecondSample{CoreAnimationFramesPerSecond: 59.5}
want := `{"fps":59.5}`
if got := formatFPSSample(sample); got != want {
t.Fatalf("formatFPSSample() = %q, want %q", got, want)
}
}
func TestFormatFPSSampleHuman(t *testing.T) {
withJSONOutput(t, true)
sample := instruments.FramesPerSecondSample{CoreAnimationFramesPerSecond: 59.5}
want := "fps=59.50"
if got := formatFPSSample(sample); got != want {
t.Fatalf("formatFPSSample() = %q, want %q", got, want)
}
}
func TestFormatNetworkSampleJSON(t *testing.T) {
withJSONOutput(t, false)
sample := instruments.NetworkSample{
Type: 2,
Data: map[string]interface{}{
"rxBytes": uint64(42),
"interfaceName": "en0",
},
}
want := `{"type":2,"data":{"interfaceName":"en0","rxBytes":42}}`
if got := formatNetworkSample(sample); got != want {
t.Fatalf("formatNetworkSample() = %q, want %q", got, want)
}
}
func TestFormatNetworkSampleHuman(t *testing.T) {
withJSONOutput(t, true)
sample := instruments.NetworkSample{
Type: 2,
Data: map[string]interface{}{
"rxBytes": uint64(42),
"interfaceName": "en0",
},
}
want := "type=2 interfaceName=en0 rxBytes=42"
if got := formatNetworkSample(sample); got != want {
t.Fatalf("formatNetworkSample() = %q, want %q", got, want)
}
}
func TestStreamInstrumentsSamplesStopsAfterDuration(t *testing.T) {
samples := make(chan instruments.FramesPerSecondSample)
done := make(chan struct{})
go func() {
defer close(done)
streamInstrumentsSamples(samples, 10*time.Millisecond, formatFPSSample)
}()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("streamInstrumentsSamples did not stop after the duration elapsed")
}
}
func TestStreamInstrumentsSamplesStopsOnClosedChannel(t *testing.T) {
samples := make(chan instruments.FramesPerSecondSample)
close(samples)
done := make(chan struct{})
go func() {
defer close(done)
streamInstrumentsSamples(samples, 0, formatFPSSample)
}()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("streamInstrumentsSamples did not stop on a closed channel")
}
}