From 2794941e8c323744a6cb0741226bc912a0f12422 Mon Sep 17 00:00:00 2001 From: Ethan Dye Date: Sun, 22 Sep 2024 14:17:24 -0600 Subject: [PATCH] Fix tests Signed-off-by: Ethan Dye --- Sources/macSubtitleOCR/macSubtitleOCR.swift | 2 +- .../Resources/expectedOutput/testMKV.json | 794 ++++++++++++++++++ .../macSubtitleOCRTests.swift | 17 +- 3 files changed, 805 insertions(+), 8 deletions(-) create mode 100644 Tests/macSubtitleOCRTests/Resources/expectedOutput/testMKV.json diff --git a/Sources/macSubtitleOCR/macSubtitleOCR.swift b/Sources/macSubtitleOCR/macSubtitleOCR.swift index 9fa07ab..cb778f7 100644 --- a/Sources/macSubtitleOCR/macSubtitleOCR.swift +++ b/Sources/macSubtitleOCR/macSubtitleOCR.swift @@ -110,7 +110,7 @@ struct macSubtitleOCR: ParsableCommand { try manager.moveItem( at: URL(fileURLWithPath: intermediateFiles[trackNumber]!), to: URL(fileURLWithPath: inFile).deletingLastPathComponent().appendingPathComponent(fileName)) - } else { + } else if inFile.hasSuffix(".mkv") { try manager.removeItem(at: URL(fileURLWithPath: intermediateFiles[trackNumber]!)) } } diff --git a/Tests/macSubtitleOCRTests/Resources/expectedOutput/testMKV.json b/Tests/macSubtitleOCRTests/Resources/expectedOutput/testMKV.json new file mode 100644 index 0000000..58305b2 --- /dev/null +++ b/Tests/macSubtitleOCRTests/Resources/expectedOutput/testMKV.json @@ -0,0 +1,794 @@ +[ + { + "image" : 1, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "This blade has a dark past.", + "width" : 680, + "x" : 619, + "y" : 0 + } + ], + "text" : "This blade has a dark past." + }, + { + "image" : 2, + "lines" : [ + { + "confidence" : 1, + "height" : 41, + "text" : "It has shed much innocent blood.", + "width" : 828, + "x" : 544, + "y" : 0 + } + ], + "text" : "It has shed much innocent blood." + }, + { + "image" : 3, + "lines" : [ + { + "confidence" : 1, + "height" : 59, + "text" : "You're a fool for traveling alone,", + "width" : 789, + "x" : 566, + "y" : 0 + }, + { + "confidence" : 1, + "height" : 53, + "text" : "so completely unprepared.", + "width" : 672, + "x" : 619, + "y" : 64 + } + ], + "text" : "You're a fool for traveling alone,\nso completely unprepared." + }, + { + "image" : 4, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "You're lucky your blood's still flowing.", + "width" : 918, + "x" : 502, + "y" : 0 + } + ], + "text" : "You're lucky your blood's still flowing." + }, + { + "image" : 5, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "Thank you.", + "width" : 281, + "x" : 817, + "y" : 0 + } + ], + "text" : "Thank you." + }, + { + "image" : 6, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "So...", + "width" : 114, + "x" : 901, + "y" : 0 + } + ], + "text" : "So..." + }, + { + "image" : 7, + "lines" : [ + { + "confidence" : 1, + "height" : 59, + "text" : "What brings you to", + "width" : 474, + "x" : 722, + "y" : 0 + }, + { + "confidence" : 1, + "height" : 53, + "text" : "the land of the gatekeepers?", + "width" : 717, + "x" : 600, + "y" : 64 + } + ], + "text" : "What brings you to\nthe land of the gatekeepers?" + }, + { + "image" : 8, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "I'm searching for someone.", + "width" : 680, + "x" : 619, + "y" : 0 + } + ], + "text" : "I'm searching for someone." + }, + { + "image" : 9, + "lines" : [ + { + "confidence" : 1, + "height" : 54, + "text" : "Someone very dear?", + "width" : 519, + "x" : 700, + "y" : 0 + }, + { + "confidence" : 1, + "height" : 54, + "text" : "A kindred spirit?", + "width" : 410, + "x" : 753, + "y" : 63 + } + ], + "text" : "Someone very dear?\nA kindred spirit?" + }, + { + "image" : 10, + "lines" : [ + { + "confidence" : 1, + "height" : 60, + "text" : "A dragon.", + "width" : 249, + "x" : 835, + "y" : -1 + } + ], + "text" : "A dragon." + }, + { + "image" : 11, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "A dangerous quest for a lone hunter.", + "width" : 912, + "x" : 505, + "y" : 0 + } + ], + "text" : "A dangerous quest for a lone hunter." + }, + { + "image" : 12, + "lines" : [ + { + "confidence" : 1, + "height" : 47, + "text" : "I've been alone for", + "width" : 468, + "x" : 725, + "y" : 0 + }, + { + "confidence" : 1, + "height" : 53, + "text" : "as long as I can remember.", + "width" : 683, + "x" : 616, + "y" : 64 + } + ], + "text" : "I've been alone for\nas long as I can remember." + }, + { + "image" : 13, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "We're almost done. Shhh...", + "width" : 683, + "x" : 616, + "y" : 0 + } + ], + "text" : "We're almost done. Shhh..." + }, + { + "image" : 14, + "lines" : [ + { + "confidence" : 1, + "height" : 56, + "text" : "Hey, sit still.", + "width" : 299, + "x" : 811, + "y" : -2 + } + ], + "text" : "Hey, sit still." + }, + { + "image" : 15, + "lines" : [ + { + "confidence" : 1, + "height" : 53, + "text" : "Good night, Scales.", + "width" : 488, + "x" : 717, + "y" : 0 + } + ], + "text" : "Good night, Scales." + }, + { + "image" : 16, + "lines" : [ + { + "confidence" : 1, + "height" : 50, + "text" : "Get him, Scales! Come on!", + "width" : 680, + "x" : 619, + "y" : 0 + } + ], + "text" : "Get him, Scales! Come on!" + }, + { + "image" : 17, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "Scales?", + "width" : 203, + "x" : 856, + "y" : 0 + } + ], + "text" : "Scales?" + }, + { + "image" : 18, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "Yeah! Come on!", + "width" : 410, + "x" : 756, + "y" : 0 + } + ], + "text" : "Yeah! Come on!" + }, + { + "image" : 19, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "Scales!", + "width" : 186, + "x" : 867, + "y" : 0 + } + ], + "text" : "Scales!" + }, + { + "image" : 20, + "lines" : [ + { + "confidence" : 1, + "height" : 42, + "text" : "I have failed.", + "width" : 326, + "x" : 795, + "y" : 0 + } + ], + "text" : "I have failed." + }, + { + "image" : 21, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "You've only failed to see...", + "width" : 653, + "x" : 633, + "y" : 0 + } + ], + "text" : "You've only failed to see..." + }, + { + "image" : 22, + "lines" : [ + { + "confidence" : 1, + "height" : 53, + "text" : "These are dragon lands, Sintel.", + "width" : 781, + "x" : 569, + "y" : 0 + } + ], + "text" : "These are dragon lands, Sintel." + }, + { + "image" : 23, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "You are closer than you know.", + "width" : 753, + "x" : 583, + "y" : 0 + } + ], + "text" : "You are closer than you know." + }, + { + "image" : 24, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "Scales!", + "width" : 186, + "x" : 867, + "y" : 0 + } + ], + "text" : "Scales!" + }, + { + "image" : 25, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "Scales?", + "width" : 203, + "x" : 856, + "y" : 0 + } + ], + "text" : "Scales?" + }, + { + "image" : 26, + "lines" : [ + { + "confidence" : 1, + "height" : 45, + "text" : "Scales...", + "width" : 212, + "x" : 853, + "y" : -1 + } + ], + "text" : "Scales..." + }, + { + "image" : 1, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "This blade has a dark past.", + "width" : 680, + "x" : 619, + "y" : 0 + } + ], + "text" : "This blade has a dark past." + }, + { + "image" : 2, + "lines" : [ + { + "confidence" : 1, + "height" : 41, + "text" : "It has shed much innocent blood.", + "width" : 828, + "x" : 544, + "y" : 0 + } + ], + "text" : "It has shed much innocent blood." + }, + { + "image" : 3, + "lines" : [ + { + "confidence" : 1, + "height" : 59, + "text" : "You're a fool for traveling alone,", + "width" : 789, + "x" : 566, + "y" : 0 + }, + { + "confidence" : 1, + "height" : 53, + "text" : "so completely unprepared.", + "width" : 672, + "x" : 619, + "y" : 64 + } + ], + "text" : "You're a fool for traveling alone,\nso completely unprepared." + }, + { + "image" : 4, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "You're lucky your blood's still flowing.", + "width" : 918, + "x" : 502, + "y" : 0 + } + ], + "text" : "You're lucky your blood's still flowing." + }, + { + "image" : 5, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "Thank you.", + "width" : 281, + "x" : 817, + "y" : 0 + } + ], + "text" : "Thank you." + }, + { + "image" : 6, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "So...", + "width" : 114, + "x" : 901, + "y" : 0 + } + ], + "text" : "So..." + }, + { + "image" : 7, + "lines" : [ + { + "confidence" : 1, + "height" : 59, + "text" : "What brings you to", + "width" : 474, + "x" : 722, + "y" : 0 + }, + { + "confidence" : 1, + "height" : 53, + "text" : "the land of the gatekeepers?", + "width" : 717, + "x" : 600, + "y" : 64 + } + ], + "text" : "What brings you to\nthe land of the gatekeepers?" + }, + { + "image" : 8, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "I'm searching for someone.", + "width" : 680, + "x" : 619, + "y" : 0 + } + ], + "text" : "I'm searching for someone." + }, + { + "image" : 9, + "lines" : [ + { + "confidence" : 1, + "height" : 54, + "text" : "Someone very dear?", + "width" : 519, + "x" : 700, + "y" : 0 + }, + { + "confidence" : 1, + "height" : 54, + "text" : "A kindred spirit?", + "width" : 410, + "x" : 753, + "y" : 63 + } + ], + "text" : "Someone very dear?\nA kindred spirit?" + }, + { + "image" : 10, + "lines" : [ + { + "confidence" : 1, + "height" : 60, + "text" : "A dragon.", + "width" : 249, + "x" : 835, + "y" : -1 + } + ], + "text" : "A dragon." + }, + { + "image" : 11, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "A dangerous quest for a lone hunter.", + "width" : 912, + "x" : 505, + "y" : 0 + } + ], + "text" : "A dangerous quest for a lone hunter." + }, + { + "image" : 12, + "lines" : [ + { + "confidence" : 1, + "height" : 47, + "text" : "I've been alone for", + "width" : 468, + "x" : 725, + "y" : 0 + }, + { + "confidence" : 1, + "height" : 53, + "text" : "as long as I can remember.", + "width" : 683, + "x" : 616, + "y" : 64 + } + ], + "text" : "I've been alone for\nas long as I can remember." + }, + { + "image" : 13, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "We're almost done. Shhh...", + "width" : 683, + "x" : 616, + "y" : 0 + } + ], + "text" : "We're almost done. Shhh..." + }, + { + "image" : 14, + "lines" : [ + { + "confidence" : 1, + "height" : 56, + "text" : "Hey, sit still.", + "width" : 299, + "x" : 811, + "y" : -2 + } + ], + "text" : "Hey, sit still." + }, + { + "image" : 15, + "lines" : [ + { + "confidence" : 1, + "height" : 53, + "text" : "Good night, Scales.", + "width" : 488, + "x" : 717, + "y" : 0 + } + ], + "text" : "Good night, Scales." + }, + { + "image" : 16, + "lines" : [ + { + "confidence" : 1, + "height" : 50, + "text" : "Get him, Scales! Come on!", + "width" : 680, + "x" : 619, + "y" : 0 + } + ], + "text" : "Get him, Scales! Come on!" + }, + { + "image" : 17, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "Scales?", + "width" : 203, + "x" : 856, + "y" : 0 + } + ], + "text" : "Scales?" + }, + { + "image" : 18, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "Yeah! Come on!", + "width" : 410, + "x" : 756, + "y" : 0 + } + ], + "text" : "Yeah! Come on!" + }, + { + "image" : 19, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "Scales!", + "width" : 186, + "x" : 867, + "y" : 0 + } + ], + "text" : "Scales!" + }, + { + "image" : 20, + "lines" : [ + { + "confidence" : 1, + "height" : 42, + "text" : "I have failed.", + "width" : 326, + "x" : 795, + "y" : 0 + } + ], + "text" : "I have failed." + }, + { + "image" : 21, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "You've only failed to see...", + "width" : 653, + "x" : 633, + "y" : 0 + } + ], + "text" : "You've only failed to see..." + }, + { + "image" : 22, + "lines" : [ + { + "confidence" : 1, + "height" : 53, + "text" : "These are dragon lands, Sintel.", + "width" : 781, + "x" : 569, + "y" : 0 + } + ], + "text" : "These are dragon lands, Sintel." + }, + { + "image" : 23, + "lines" : [ + { + "confidence" : 1, + "height" : 52, + "text" : "You are closer than you know.", + "width" : 753, + "x" : 583, + "y" : 0 + } + ], + "text" : "You are closer than you know." + }, + { + "image" : 24, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "Scales!", + "width" : 186, + "x" : 867, + "y" : 0 + } + ], + "text" : "Scales!" + }, + { + "image" : 25, + "lines" : [ + { + "confidence" : 1, + "height" : 43, + "text" : "Scales?", + "width" : 203, + "x" : 856, + "y" : 0 + } + ], + "text" : "Scales?" + }, + { + "image" : 26, + "lines" : [ + { + "confidence" : 1, + "height" : 45, + "text" : "Scales...", + "width" : 212, + "x" : 853, + "y" : -1 + } + ], + "text" : "Scales..." + } +] \ No newline at end of file diff --git a/Tests/macSubtitleOCRTests/macSubtitleOCRTests.swift b/Tests/macSubtitleOCRTests/macSubtitleOCRTests.swift index 50ecda6..5f7180c 100644 --- a/Tests/macSubtitleOCRTests/macSubtitleOCRTests.swift +++ b/Tests/macSubtitleOCRTests/macSubtitleOCRTests.swift @@ -15,11 +15,11 @@ import Testing @Test func pgsMKV() throws { // Setup files let manager = FileManager.default - let srtPath = (manager.temporaryDirectory.path + "/test.srt") + let srtPath = (manager.temporaryDirectory.path + "/srt") let jsonPath = (manager.temporaryDirectory.path + "/test.json") let mkvPath = Bundle.module.url(forResource: "test.mkv", withExtension: nil)!.absoluteString.replacing("file://", with: "") let goodSRTPath = Bundle.module.url(forResource: "test.srt", withExtension: nil)!.absoluteString.replacing("file://", with: "") - let goodJSONPath = Bundle.module.url(forResource: "test.json", withExtension: nil)!.absoluteString.replacing("file://", with: "") + let goodJSONPath = Bundle.module.url(forResource: "testMKV.json", withExtension: nil)!.absoluteString.replacing("file://", with: "") // Run tests let options = [mkvPath, srtPath, "--json", jsonPath, "--language-correction"] @@ -28,21 +28,24 @@ import Testing // Compare output let srtExpectedOutput = try String(contentsOfFile: goodSRTPath, encoding: .utf8) - let srtActualOutput = try String(contentsOfFile: srtPath, encoding: .utf8) + let srt0ActualOutput = try String(contentsOfFile: srtPath + "/track_0.srt", encoding: .utf8) + let srt1ActualOutput = try String(contentsOfFile: srtPath + "/track_1.srt", encoding: .utf8) let jsonExpectedOutput = try String(contentsOfFile: goodJSONPath, encoding: .utf8) let jsonActualOutput = try String(contentsOfFile: jsonPath, encoding: .utf8) - let srtMatch = similarityPercentage(srtExpectedOutput, srtActualOutput) + let srt0Match = similarityPercentage(srtExpectedOutput, srt0ActualOutput) + let srt1Match = similarityPercentage(srtExpectedOutput, srt1ActualOutput) let jsonMatch = similarityPercentage(jsonExpectedOutput, jsonActualOutput) - #expect(srtMatch >= 90.0) + #expect(srt0Match >= 90.0) + #expect(srt1Match >= 90.0) #expect(jsonMatch >= 90.0) } @Test func pgsSUP() throws { // Setup files let manager = FileManager.default - let srtPath = (manager.temporaryDirectory.path + "/test.srt") + let srtPath = (manager.temporaryDirectory.path + "/srt") let jsonPath = (manager.temporaryDirectory.path + "/test.json") let supPath = Bundle.module.url(forResource: "test.sup", withExtension: nil)!.absoluteString.replacing("file://", with: "") let goodSRTPath = Bundle.module.url(forResource: "test.srt", withExtension: nil)!.absoluteString.replacing("file://", with: "") @@ -55,7 +58,7 @@ import Testing // Compare output let srtExpectedOutput = try String(contentsOfFile: goodSRTPath, encoding: .utf8) - let srtActualOutput = try String(contentsOfFile: srtPath, encoding: .utf8) + let srtActualOutput = try String(contentsOfFile: srtPath + "/track_0.srt", encoding: .utf8) let jsonExpectedOutput = try String(contentsOfFile: goodJSONPath, encoding: .utf8) let jsonActualOutput = try String(contentsOfFile: jsonPath, encoding: .utf8)