utils: vtt: test + rewrite (#1284)

* utils: vtt: add tests

In lieu of documentation.

* utils: vtt: rewrite for correctness and simplicity

Now handles fractional seconds and negative values correctly.
This commit is contained in:
stashist
2021-05-25 03:25:26 +02:00
committed by GitHub
parent 3d93f7f0fe
commit fc9d70f702
2 changed files with 71 additions and 33 deletions

42
pkg/utils/vtt_test.go Normal file
View File

@@ -0,0 +1,42 @@
package utils
import (
"math"
"testing"
)
func TestZeroTimestamp(t *testing.T) {
if want, got := "00:00:00.000", GetVTTTime(0); want != got {
t.Errorf("TestZeroTimestamp: GetVTTTime(0) = %v; want %v", got, want)
}
}
func TestValidTimestamp(t *testing.T) {
s := 0.1
if want, got := "00:00:00.100", GetVTTTime(s); want != got {
t.Errorf("TestValidTimestamp: GetVTTTime(%v) = %v; want %v", s, got, want)
}
s = ((24+1)*60+1)*60 + 1 + 0.1
if want, got := "25:01:01.100", GetVTTTime(s); want != got {
t.Errorf("TestValidTimestamp: GetVTTTime(%v) = %v; want %v", s, got, want)
}
}
// Negative timestamps are not defined by WebVTT.
func TestNegativeTimestamp(t *testing.T) {
if want, got := "00:00:00.000", GetVTTTime(-1); want != got {
t.Errorf("TestNegativeTimestamp: GetVTTTime(-1) = %v; want %v", got, want)
}
}
func TestInvalidTimestamp(t *testing.T) {
if want, got := "00:00:00.000", GetVTTTime(math.NaN()); want != got {
t.Errorf("TestInvalidTimestamp: GetVTTTime(NaN) = %v; want %v", got, want)
}
if want, got := "00:00:00.000", GetVTTTime(math.Inf(1)); want != got {
t.Errorf("TestInvalidTimestamp: GetVTTTime(Inf) = %v; want %v", got, want)
}
if want, got := "00:00:00.000", GetVTTTime(math.Inf(-1)); want != got {
t.Errorf("TestInvalidTimestamp: GetVTTTime(-Inf) = %v; want %v", got, want)
}
}