1
+ package tools
2
+
3
+ import org .scalatest .funsuite .AnyFunSuite
4
+ import org .scalatest .matchers .should .Matchers
5
+
6
+ import java .io .PrintWriter
7
+ import java .nio .file .Files
8
+
9
+ class SrtParserSpec extends AnyFunSuite with Matchers {
10
+
11
+ test(" parse valid srt file" ) {
12
+ val parser = new SrtParser (" src/main/resources/EN_challenges.srt" )
13
+ val blocks = parser.runSync()
14
+
15
+ blocks.nonEmpty shouldBe true
16
+ blocks.head.lines.nonEmpty shouldBe true
17
+ }
18
+
19
+ test(" convert subtitle block to formatted output" ) {
20
+ val block = SubtitleBlock (
21
+ start = 1000 ,
22
+ end = 4000 ,
23
+ lines = Seq (" First line" , " Second line" )
24
+ )
25
+ val formatted = block.formatOutBlock(1 )
26
+
27
+ formatted should include(" 00:00:01,000 --> 00:00:04,000" )
28
+ formatted should include(" First line" )
29
+ formatted should include(" Second line" )
30
+ }
31
+
32
+ test(" parse subtitle block with multiple lines" ) {
33
+ val block = SubtitleBlock (
34
+ start = 0 ,
35
+ end = 2000 ,
36
+ lines = Seq (" Line 1" , " Line 2" , " Line 3" )
37
+ )
38
+ block.allLines shouldBe " Line 1 Line 2 Line 3"
39
+ }
40
+
41
+ test(" format time correctly" ) {
42
+ val block = SubtitleBlock (
43
+ start = 3661000 , // 1 hour, 1 minute, 1 second
44
+ end = 3662000 , // 1 hour, 1 minute, 2 seconds
45
+ lines = Seq (" Test" )
46
+ )
47
+ val formatted = block.formatOutBlock(1 )
48
+
49
+ formatted should include(" 01:01:01,000 --> 01:01:02,000" )
50
+ }
51
+
52
+
53
+ test(" handle empty subtitle file" ) {
54
+ val tempFile = Files .createTempFile(" empty" , " .srt" )
55
+ tempFile.toFile.deleteOnExit()
56
+ val parser = new SrtParser (tempFile.toString)
57
+ val blocks = parser.runSync()
58
+
59
+ blocks shouldBe empty
60
+ }
61
+
62
+ test(" handle invalid srt file format" ) {
63
+ val tempFile = Files .createTempFile(" invalid" , " .srt" )
64
+ tempFile.toFile.deleteOnExit()
65
+ val writer = new PrintWriter (tempFile.toFile)
66
+ writer.write(
67
+ """
68
+ |Invalid Format
69
+ |No timestamps here
70
+ |Just random text
71
+ |Without proper structure
72
+ |---> wrong separator
73
+ """ .stripMargin)
74
+ writer.close()
75
+
76
+ val parser = new SrtParser (tempFile.toString)
77
+
78
+ assertThrows[java.time.format.DateTimeParseException ] {
79
+ parser.runSync()
80
+ }
81
+ }
82
+ }
0 commit comments