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

182 lines
3.3 KiB
Go

package srt
import (
"testing"
"sub-cli/internal/model"
)
func TestParseSRTTimestamp(t *testing.T) {
testCases := []struct {
input string
expected model.Timestamp
}{
{
input: "00:00:01,000",
expected: model.Timestamp{
Hours: 0,
Minutes: 0,
Seconds: 1,
Milliseconds: 0,
},
},
{
input: "01:02:03,456",
expected: model.Timestamp{
Hours: 1,
Minutes: 2,
Seconds: 3,
Milliseconds: 456,
},
},
{
input: "10:20:30,789",
expected: model.Timestamp{
Hours: 10,
Minutes: 20,
Seconds: 30,
Milliseconds: 789,
},
},
{
// Test invalid format
input: "invalid",
expected: model.Timestamp{},
},
{
// Test with dot instead of comma
input: "01:02:03.456",
expected: model.Timestamp{
Hours: 1,
Minutes: 2,
Seconds: 3,
Milliseconds: 456,
},
},
}
for _, tc := range testCases {
result := parseSRTTimestamp(tc.input)
if result.Hours != tc.expected.Hours ||
result.Minutes != tc.expected.Minutes ||
result.Seconds != tc.expected.Seconds ||
result.Milliseconds != tc.expected.Milliseconds {
t.Errorf("parseSRTTimestamp(%s) = %+v, want %+v",
tc.input, result, tc.expected)
}
}
}
func TestFormatSRTTimestamp(t *testing.T) {
testCases := []struct {
input model.Timestamp
expected string
}{
{
input: model.Timestamp{
Hours: 0,
Minutes: 0,
Seconds: 1,
Milliseconds: 0,
},
expected: "00:00:01,000",
},
{
input: model.Timestamp{
Hours: 1,
Minutes: 2,
Seconds: 3,
Milliseconds: 456,
},
expected: "01:02:03,456",
},
{
input: model.Timestamp{
Hours: 10,
Minutes: 20,
Seconds: 30,
Milliseconds: 789,
},
expected: "10:20:30,789",
},
}
for _, tc := range testCases {
result := formatSRTTimestamp(tc.input)
if result != tc.expected {
t.Errorf("formatSRTTimestamp(%+v) = %s, want %s",
tc.input, result, tc.expected)
}
}
}
func TestIsEntryTimeStampUnset(t *testing.T) {
testCases := []struct {
entry model.SRTEntry
expected bool
}{
{
entry: model.SRTEntry{
StartTime: model.Timestamp{
Hours: 0,
Minutes: 0,
Seconds: 0,
Milliseconds: 0,
},
},
expected: true,
},
{
entry: model.SRTEntry{
StartTime: model.Timestamp{
Hours: 0,
Minutes: 0,
Seconds: 1,
Milliseconds: 0,
},
},
expected: false,
},
{
entry: model.SRTEntry{
StartTime: model.Timestamp{
Hours: 0,
Minutes: 1,
Seconds: 0,
Milliseconds: 0,
},
},
expected: false,
},
{
entry: model.SRTEntry{
StartTime: model.Timestamp{
Hours: 1,
Minutes: 0,
Seconds: 0,
Milliseconds: 0,
},
},
expected: false,
},
{
entry: model.SRTEntry{
StartTime: model.Timestamp{
Hours: 0,
Minutes: 0,
Seconds: 0,
Milliseconds: 1,
},
},
expected: false,
},
}
for i, tc := range testCases {
result := isEntryTimeStampUnset(tc.entry)
if result != tc.expected {
t.Errorf("Case %d: isEntryTimeStampUnset(%+v) = %v, want %v",
i, tc.entry, result, tc.expected)
}
}
}