-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathformat_test.go
More file actions
144 lines (128 loc) · 3.59 KB
/
Copy pathformat_test.go
File metadata and controls
144 lines (128 loc) · 3.59 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
package mpv
import (
"testing"
"unsafe"
)
// newHeadless returns an initialized mpv instance with no audio/video output.
func newHeadless(t *testing.T) *Mpv {
t.Helper()
m := New()
if err := m.SetPropertyString("vo", "null"); err != nil {
t.Fatalf("SetPropertyString vo: %v", err)
}
if err := m.SetOptionString("ao", "null"); err != nil {
t.Fatalf("SetOptionString ao: %v", err)
}
if err := m.Initialize(); err != nil {
t.Fatalf("Initialize: %v", err)
}
t.Cleanup(m.TerminateDestroy)
return m
}
func TestPropertyRoundTrip(t *testing.T) {
m := newHeadless(t)
t.Run("flag", func(t *testing.T) {
if err := m.SetProperty("pause", FormatFlag, true); err != nil {
t.Fatal(err)
}
v, err := m.GetProperty("pause", FormatFlag)
if err != nil {
t.Fatal(err)
}
if v.(bool) != true {
t.Fatalf("got %v, want true", v)
}
})
t.Run("int64", func(t *testing.T) {
if err := m.SetProperty("ab-loop-count", FormatInt64, int64(3)); err != nil {
t.Fatal(err)
}
v, err := m.GetProperty("ab-loop-count", FormatInt64)
if err != nil {
t.Fatal(err)
}
if v.(int64) != 3 {
t.Fatalf("got %v, want 3", v)
}
})
t.Run("double", func(t *testing.T) {
if err := m.SetProperty("speed", FormatDouble, 1.5); err != nil {
t.Fatal(err)
}
v, err := m.GetProperty("speed", FormatDouble)
if err != nil {
t.Fatal(err)
}
if v.(float64) != 1.5 {
t.Fatalf("got %v, want 1.5", v)
}
})
t.Run("string", func(t *testing.T) {
if err := m.SetProperty("force-media-title", FormatString, "héllo"); err != nil {
t.Fatal(err)
}
v, err := m.GetProperty("force-media-title", FormatString)
if err != nil {
t.Fatal(err)
}
if v.(string) != "héllo" {
t.Fatalf("got %q, want %q", v, "héllo")
}
})
}
func TestStringAccessors(t *testing.T) {
m := New()
if err := m.SetOption("force-media-title", FormatString, "via-option"); err != nil {
t.Fatalf("SetOption FormatString: %v", err)
}
if err := m.SetPropertyString("vo", "null"); err != nil {
t.Fatalf("SetPropertyString vo: %v", err)
}
if err := m.SetOptionString("ao", "null"); err != nil {
t.Fatalf("SetOptionString ao: %v", err)
}
if err := m.Initialize(); err != nil {
t.Fatalf("Initialize: %v", err)
}
t.Cleanup(m.TerminateDestroy)
if got := m.GetPropertyString("force-media-title"); got != "via-option" {
t.Fatalf("GetPropertyString after SetOption = %q, want %q", got, "via-option")
}
if err := m.SetPropertyString("force-media-title", "via-string"); err != nil {
t.Fatalf("SetPropertyString: %v", err)
}
if got := m.GetPropertyString("force-media-title"); got != "via-string" {
t.Fatalf("GetPropertyString = %q, want %q", got, "via-string")
}
if got := m.GetPropertyOsdString("force-media-title"); got != "via-string" {
t.Fatalf("GetPropertyOsdString = %q, want %q", got, "via-string")
}
if got := m.GetPropertyString("does-not-exist"); got != "" {
t.Fatalf("GetPropertyString of missing property = %q, want empty", got)
}
}
func TestToStr(t *testing.T) {
tests := []struct {
name string
in []byte
want string
}{
{"plain", []byte("hello\x00"), "hello"},
{"stops at nul", []byte("hello\x00world\x00"), "hello"},
{"preserves surrounding space", []byte(" hi \x00"), " hi "},
{"preserves newline", []byte("line\n\x00"), "line\n"},
{"empty", []byte("\x00"), ""},
{"utf8", []byte("héllo\x00"), "héllo"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := toStr(unsafe.Pointer(&tc.in[0]))
if got != tc.want {
t.Fatalf("toStr = %q, want %q", got, tc.want)
}
})
}
if got := toStr(nil); got != "" {
t.Fatalf("toStr(nil) = %q, want empty", got)
}
}