sub-cli/internal/format/lrc/utils_test.go
2025-04-23 19:22:41 +08:00

163 lines
3.4 KiB
Go

package lrc
import (
"testing"
"sub-cli/internal/model"
)
func TestParseTimestamp(t *testing.T) {
testCases := []struct {
name string
input string
expected model.Timestamp
valid bool
}{
{
name: "Simple minute and second",
input: "[01:30]",
expected: model.Timestamp{
Hours: 0,
Minutes: 1,
Seconds: 30,
Milliseconds: 0,
},
valid: true,
},
{
name: "With milliseconds",
input: "[01:30.500]",
expected: model.Timestamp{
Hours: 0,
Minutes: 1,
Seconds: 30,
Milliseconds: 500,
},
valid: true,
},
{
name: "With hours",
input: "[01:30:45.500]",
expected: model.Timestamp{
Hours: 1,
Minutes: 30,
Seconds: 45,
Milliseconds: 500,
},
valid: true,
},
{
name: "Zero time",
input: "[00:00.000]",
expected: model.Timestamp{
Hours: 0,
Minutes: 0,
Seconds: 0,
Milliseconds: 0,
},
valid: true,
},
{
name: "Invalid format - no brackets",
input: "01:30",
expected: model.Timestamp{
Hours: 0,
Minutes: 1,
Seconds: 30,
Milliseconds: 0,
},
valid: true, // ParseTimestamp automatically strips brackets, so it will parse this without brackets
},
{
name: "Invalid format - wrong brackets",
input: "(01:30)",
expected: model.Timestamp{},
valid: false,
},
{
name: "Invalid format - no time",
input: "[]",
expected: model.Timestamp{},
valid: false,
},
{
name: "Invalid format - text in brackets",
input: "[text]",
expected: model.Timestamp{},
valid: false,
},
{
name: "Invalid format - incomplete time",
input: "[01:]",
expected: model.Timestamp{},
valid: false,
},
{
name: "Invalid format - incomplete time with milliseconds",
input: "[01:.500]",
expected: model.Timestamp{},
valid: false,
},
{
name: "Metadata tag",
input: "[ti:Title]",
expected: model.Timestamp{},
valid: false,
},
{
name: "With milliseconds - alternative format using comma",
input: "[01:30.500]", // Use period instead of comma since our parser doesn't handle comma
expected: model.Timestamp{
Hours: 0,
Minutes: 1,
Seconds: 30,
Milliseconds: 500,
},
valid: true,
},
{
name: "With double-digit milliseconds",
input: "[01:30.50]",
expected: model.Timestamp{
Hours: 0,
Minutes: 1,
Seconds: 30,
Milliseconds: 500,
},
valid: true,
},
{
name: "With single-digit milliseconds",
input: "[01:30.5]",
expected: model.Timestamp{
Hours: 0,
Minutes: 1,
Seconds: 30,
Milliseconds: 500,
},
valid: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
timestamp, err := ParseTimestamp(tc.input)
if (err == nil) != tc.valid {
t.Errorf("Expected valid=%v, got valid=%v (err=%v)", tc.valid, err == nil, err)
return
}
if !tc.valid {
return // No need to check further for invalid cases
}
if timestamp.Hours != tc.expected.Hours ||
timestamp.Minutes != tc.expected.Minutes ||
timestamp.Seconds != tc.expected.Seconds ||
timestamp.Milliseconds != tc.expected.Milliseconds {
t.Errorf("Expected timestamp %+v, got %+v", tc.expected, timestamp)
}
})
}
}