package ass import ( "os" "path/filepath" "strings" "testing" ) func TestFormat(t *testing.T) { // Create a test ASS file with non-standard formatting content := `[Script Info] ScriptType:v4.00+ Title: Format Test 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 [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,Default,,0,0,0,,This is the second subtitle line. ` tempDir := t.TempDir() testFile := filepath.Join(tempDir, "format_test.ass") if err := os.WriteFile(testFile, []byte(content), 0644); err != nil { t.Fatalf("Failed to create test file: %v", err) } // Test format err := Format(testFile) if err != nil { t.Fatalf("Format failed: %v", err) } // Read the formatted file formattedContent, err := os.ReadFile(testFile) if err != nil { t.Fatalf("Failed to read formatted file: %v", err) } contentStr := string(formattedContent) // Check for consistency and proper spacing if !strings.Contains(contentStr, "Title: Format Test") { t.Errorf("Title should be properly formatted, got: %s", contentStr) } // Check style section formatting if !strings.Contains(contentStr, "Format: Name, Fontname, Fontsize") { t.Errorf("Style format should be properly spaced, got: %s", contentStr) } // Check event section formatting if !strings.Contains(contentStr, "Dialogue: 0,") { t.Errorf("Dialogue should be properly formatted, got: %s", contentStr) } // Parse formatted file to ensure it's valid assFile, err := Parse(testFile) if err != nil { t.Fatalf("Failed to parse formatted file: %v", err) } // Verify basic structure remains intact if assFile.ScriptInfo["Title"] != "Format Test" { t.Errorf("Title mismatch after formatting: expected 'Format Test', got '%s'", assFile.ScriptInfo["Title"]) } if len(assFile.Events) != 2 { t.Errorf("Expected 2 events after formatting, got %d", len(assFile.Events)) } } func TestFormat_NonExistentFile(t *testing.T) { err := Format("/nonexistent/file.ass") if err == nil { t.Error("Formatting non-existent file should return an error") } } func TestFormat_InvalidWritable(t *testing.T) { // Create a directory instead of a file tempDir := t.TempDir() dirAsFile := filepath.Join(tempDir, "dir_as_file") if err := os.Mkdir(dirAsFile, 0755); err != nil { t.Fatalf("Failed to create test directory: %v", err) } // Try to format a directory err := Format(dirAsFile) if err == nil { t.Error("Formatting a directory should return an error") } }