148 lines
4.6 KiB
Go
148 lines
4.6 KiB
Go
package ass
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"sub-cli/internal/model"
|
|
)
|
|
|
|
func TestParse(t *testing.T) {
|
|
// Create temporary test file
|
|
content := `[Script Info]
|
|
ScriptType: v4.00+
|
|
Title: Test ASS File
|
|
PlayResX: 640
|
|
PlayResY: 480
|
|
|
|
[V4+ Styles]
|
|
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
|
|
Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1
|
|
Style: Bold,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,1,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1
|
|
|
|
[Events]
|
|
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
|
|
Dialogue: 0,0:00:01.00,0:00:04.00,Default,,0,0,0,,This is the first subtitle line.
|
|
Dialogue: 0,0:00:05.00,0:00:08.00,Bold,,0,0,0,,This is the second subtitle line with bold style.
|
|
Comment: 0,0:00:09.00,0:00:12.00,Default,,0,0,0,,This is a comment.
|
|
`
|
|
tempDir := t.TempDir()
|
|
testFile := filepath.Join(tempDir, "test.ass")
|
|
if err := os.WriteFile(testFile, []byte(content), 0644); err != nil {
|
|
t.Fatalf("Failed to create test file: %v", err)
|
|
}
|
|
|
|
// Test parsing
|
|
assFile, err := Parse(testFile)
|
|
if err != nil {
|
|
t.Fatalf("Parse failed: %v", err)
|
|
}
|
|
|
|
// Verify results
|
|
// Script info
|
|
if assFile.ScriptInfo["Title"] != "Test ASS File" {
|
|
t.Errorf("Title mismatch: expected 'Test ASS File', got '%s'", assFile.ScriptInfo["Title"])
|
|
}
|
|
if assFile.ScriptInfo["ScriptType"] != "v4.00+" {
|
|
t.Errorf("Script type mismatch: expected 'v4.00+', got '%s'", assFile.ScriptInfo["ScriptType"])
|
|
}
|
|
|
|
// Styles
|
|
if len(assFile.Styles) != 3 {
|
|
t.Errorf("Expected 3 styles, got %d", len(assFile.Styles))
|
|
} else {
|
|
// Find Bold style
|
|
var boldStyle *model.ASSStyle
|
|
for i, style := range assFile.Styles {
|
|
if style.Name == "Bold" {
|
|
boldStyle = &assFile.Styles[i]
|
|
break
|
|
}
|
|
}
|
|
|
|
if boldStyle == nil {
|
|
t.Errorf("Bold style not found")
|
|
} else {
|
|
boldValue, exists := boldStyle.Properties["Bold"]
|
|
if !exists || boldValue != "1" {
|
|
t.Errorf("Bold style Bold property mismatch: expected '1', got '%s'", boldValue)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Events
|
|
if len(assFile.Events) != 3 {
|
|
t.Errorf("Expected 3 events, got %d", len(assFile.Events))
|
|
} else {
|
|
// Check first dialogue line
|
|
if assFile.Events[0].Type != "Dialogue" {
|
|
t.Errorf("First event type mismatch: expected 'Dialogue', got '%s'", assFile.Events[0].Type)
|
|
}
|
|
if assFile.Events[0].StartTime.Seconds != 1 || assFile.Events[0].StartTime.Milliseconds != 0 {
|
|
t.Errorf("First event start time mismatch: expected 00:00:01.00, got %d:%02d:%02d.%03d",
|
|
assFile.Events[0].StartTime.Hours, assFile.Events[0].StartTime.Minutes,
|
|
assFile.Events[0].StartTime.Seconds, assFile.Events[0].StartTime.Milliseconds)
|
|
}
|
|
if assFile.Events[0].Text != "This is the first subtitle line." {
|
|
t.Errorf("First event text mismatch: expected 'This is the first subtitle line.', got '%s'", assFile.Events[0].Text)
|
|
}
|
|
|
|
// Check second dialogue line (bold style)
|
|
if assFile.Events[1].Style != "Bold" {
|
|
t.Errorf("Second event style mismatch: expected 'Bold', got '%s'", assFile.Events[1].Style)
|
|
}
|
|
|
|
// Check comment line
|
|
if assFile.Events[2].Type != "Comment" {
|
|
t.Errorf("Third event type mismatch: expected 'Comment', got '%s'", assFile.Events[2].Type)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParse_EdgeCases(t *testing.T) {
|
|
// Test empty file
|
|
tempDir := t.TempDir()
|
|
emptyFile := filepath.Join(tempDir, "empty.ass")
|
|
if err := os.WriteFile(emptyFile, []byte{}, 0644); err != nil {
|
|
t.Fatalf("Failed to create empty test file: %v", err)
|
|
}
|
|
|
|
assFile, err := Parse(emptyFile)
|
|
if err != nil {
|
|
t.Fatalf("Failed to parse empty file: %v", err)
|
|
}
|
|
|
|
if len(assFile.Events) != 0 {
|
|
t.Errorf("Empty file should have 0 events, got %d", len(assFile.Events))
|
|
}
|
|
|
|
// Test file missing required sections
|
|
malformedContent := `[Script Info]
|
|
Title: Missing Sections Test
|
|
`
|
|
malformedFile := filepath.Join(tempDir, "malformed.ass")
|
|
if err := os.WriteFile(malformedFile, []byte(malformedContent), 0644); err != nil {
|
|
t.Fatalf("Failed to create malformed file: %v", err)
|
|
}
|
|
|
|
assFile, err = Parse(malformedFile)
|
|
if err != nil {
|
|
t.Fatalf("Failed to parse malformed file: %v", err)
|
|
}
|
|
|
|
if assFile.ScriptInfo["Title"] != "Missing Sections Test" {
|
|
t.Errorf("Should correctly parse the title")
|
|
}
|
|
if len(assFile.Events) != 0 {
|
|
t.Errorf("File missing Events section should have 0 events")
|
|
}
|
|
}
|
|
|
|
func TestParse_FileError(t *testing.T) {
|
|
// Test non-existent file
|
|
_, err := Parse("/nonexistent/file.ass")
|
|
if err == nil {
|
|
t.Error("Parsing non-existent file should return an error")
|
|
}
|
|
}
|