package srt import ( "os" "path/filepath" "testing" ) func TestParse(t *testing.T) { // Create a temporary test file content := `1 00:00:01,000 --> 00:00:04,000 This is the first line. 2 00:00:05,000 --> 00:00:08,000 This is the second line. 3 00:00:09,500 --> 00:00:12,800 This is the third line with a line break. ` tempDir := t.TempDir() testFile := filepath.Join(tempDir, "test.srt") if err := os.WriteFile(testFile, []byte(content), 0644); err != nil { t.Fatalf("Failed to create test file: %v", err) } // Test parsing entries, err := Parse(testFile) if err != nil { t.Fatalf("Parse failed: %v", err) } // Verify results if len(entries) != 3 { t.Errorf("Expected 3 entries, got %d", len(entries)) } // Check first entry if entries[0].Number != 1 { t.Errorf("First entry number: expected 1, got %d", entries[0].Number) } if entries[0].StartTime.Hours != 0 || entries[0].StartTime.Minutes != 0 || entries[0].StartTime.Seconds != 1 || entries[0].StartTime.Milliseconds != 0 { t.Errorf("First entry start time: expected 00:00:01,000, got %+v", entries[0].StartTime) } if entries[0].EndTime.Hours != 0 || entries[0].EndTime.Minutes != 0 || entries[0].EndTime.Seconds != 4 || entries[0].EndTime.Milliseconds != 0 { t.Errorf("First entry end time: expected 00:00:04,000, got %+v", entries[0].EndTime) } if entries[0].Content != "This is the first line." { t.Errorf("First entry content: expected 'This is the first line.', got '%s'", entries[0].Content) } // Check third entry if entries[2].Number != 3 { t.Errorf("Third entry number: expected 3, got %d", entries[2].Number) } expectedContent := "This is the third line\nwith a line break." if entries[2].Content != expectedContent { t.Errorf("Third entry content: expected '%s', got '%s'", expectedContent, entries[2].Content) } } func TestParse_EdgeCases(t *testing.T) { // Test with empty file tempDir := t.TempDir() emptyFile := filepath.Join(tempDir, "empty.srt") if err := os.WriteFile(emptyFile, []byte(""), 0644); err != nil { t.Fatalf("Failed to create empty file: %v", err) } entries, err := Parse(emptyFile) if err != nil { t.Fatalf("Parse failed with empty file: %v", err) } if len(entries) != 0 { t.Errorf("Expected 0 entries for empty file, got %d", len(entries)) } // Test with malformed timestamp malformedContent := `1 00:00:01,000 --> 00:00:04,000 First entry. 2 bad timestamp format Second entry. ` malformedFile := filepath.Join(tempDir, "malformed.srt") if err := os.WriteFile(malformedFile, []byte(malformedContent), 0644); err != nil { t.Fatalf("Failed to create malformed file: %v", err) } entries, err = Parse(malformedFile) if err != nil { t.Fatalf("Parse failed with malformed file: %v", err) } // Should still parse the first entry correctly if len(entries) != 1 { t.Errorf("Expected 1 entry for malformed file, got %d", len(entries)) } // Test with missing numbers missingNumContent := `00:00:01,000 --> 00:00:04,000 First entry without number. 2 00:00:05,000 --> 00:00:08,000 Second entry with number. ` missingNumFile := filepath.Join(tempDir, "missing_num.srt") if err := os.WriteFile(missingNumFile, []byte(missingNumContent), 0644); err != nil { t.Fatalf("Failed to create missing num file: %v", err) } entries, err = Parse(missingNumFile) if err != nil { t.Fatalf("Parse failed with missing num file: %v", err) } // Parsing behavior may vary, but it should not crash // In this case, it will typically parse just the second entry // Test with extra empty lines extraLineContent := `1 00:00:01,000 --> 00:00:04,000 First entry with extra spaces. 2 00:00:05,000 --> 00:00:08,000 Second entry with extra spaces. ` extraLineFile := filepath.Join(tempDir, "extra_lines.srt") if err := os.WriteFile(extraLineFile, []byte(extraLineContent), 0644); err != nil { t.Fatalf("Failed to create extra lines file: %v", err) } entries, err = Parse(extraLineFile) if err != nil { t.Fatalf("Parse failed with extra lines file: %v", err) } if len(entries) != 2 { t.Errorf("Expected 2 entries for extra lines file, got %d", len(entries)) } // Check content was trimmed correctly if entries[0].Content != "First entry with extra spaces." { t.Errorf("Expected trimmed content, got '%s'", entries[0].Content) } } func TestParse_FileError(t *testing.T) { // Test with non-existent file _, err := Parse("/nonexistent/file.srt") if err == nil { t.Error("Expected error when parsing non-existent file, got nil") } }