-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffset_test.go
84 lines (80 loc) · 2.35 KB
/
offset_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
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
package vmap
import (
"testing"
"github.com/iflix/vast"
"github.com/stretchr/testify/assert"
)
func TestOffsetMarshaler(t *testing.T) {
b, err := Offset{}.MarshalText()
if assert.NoError(t, err) {
assert.Equal(t, "0%", string(b))
}
b, err = Offset{Percent: .1}.MarshalText()
if assert.NoError(t, err) {
assert.Equal(t, "10%", string(b))
}
d := vast.Duration(0)
b, err = Offset{Duration: &d}.MarshalText()
if assert.NoError(t, err) {
assert.Equal(t, "00:00:00", string(b))
}
b, err = Offset{Position: 1}.MarshalText()
if assert.NoError(t, err) {
assert.Equal(t, "#1", string(b))
}
b, err = Offset{Position: OffsetStart}.MarshalText()
if assert.NoError(t, err) {
assert.Equal(t, "start", string(b))
}
b, err = Offset{Position: OffsetEnd}.MarshalText()
if assert.NoError(t, err) {
assert.Equal(t, "end", string(b))
}
b, err = Offset{Position: -4}.MarshalText()
assert.EqualError(t, err, "Invalid position: -4")
}
func TestOffsetUnmarshaler(t *testing.T) {
var o Offset
if assert.NoError(t, o.UnmarshalText([]byte("0%"))) {
assert.Nil(t, o.Duration)
assert.Equal(t, float32(0.0), o.Percent)
}
o = Offset{}
if assert.NoError(t, o.UnmarshalText([]byte("10%"))) {
assert.Nil(t, o.Duration)
assert.Equal(t, float32(0.1), o.Percent)
}
o = Offset{}
if assert.NoError(t, o.UnmarshalText([]byte("00:00:00"))) {
if assert.NotNil(t, o.Duration) {
assert.Equal(t, vast.Duration(0), *o.Duration)
}
assert.Equal(t, float32(0), o.Percent)
}
o = Offset{}
if assert.NoError(t, o.UnmarshalText([]byte("#1"))) {
assert.Nil(t, o.Duration)
assert.Equal(t, float32(0), o.Percent)
assert.Equal(t, 1, o.Position)
}
o = Offset{}
if assert.NoError(t, o.UnmarshalText([]byte("start"))) {
assert.Nil(t, o.Duration)
assert.Equal(t, float32(0), o.Percent)
assert.Equal(t, OffsetStart, o.Position)
}
o = Offset{}
if assert.NoError(t, o.UnmarshalText([]byte("end"))) {
assert.Nil(t, o.Duration)
assert.Equal(t, float32(0), o.Percent)
assert.Equal(t, OffsetEnd, o.Position)
}
o = Offset{}
assert.EqualError(t, o.UnmarshalText([]byte("#0")), "invalid offset: #0")
o = Offset{}
assert.EqualError(t, o.UnmarshalText([]byte("#-10")), "invalid offset: #-10")
o = Offset{}
assert.EqualError(t, o.UnmarshalText([]byte("#1.1")), "invalid offset: #1.1")
o = Offset{}
assert.EqualError(t, o.UnmarshalText([]byte("abc%")), "invalid offset: abc%")
}