[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/subed 1c52f1b 361/389: Add tests for splitting subtitles a
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/subed 1c52f1b 361/389: Add tests for splitting subtitles and handle more cases |
Date: |
Fri, 3 Dec 2021 11:00:58 -0500 (EST) |
branch: elpa/subed
commit 1c52f1b4b85013bd979a695950bd8bb6199acb30
Author: Sacha Chua <sacha@sachachua.com>
Commit: Sacha Chua <sacha@sachachua.com>
Add tests for splitting subtitles and handle more cases
* tests/test-subed-common.el: Add lexical binding. Add tests for
splitting subtitles.
* subed/subed-common.el (subed-split-subtitle): Handle spaces,
beginning of line, and empty subtitles.
---
subed/subed-common.el | 16 +++-
tests/test-subed-common.el | 213 +++++++++++++++++++++++++++++++++++++++++----
2 files changed, 209 insertions(+), 20 deletions(-)
diff --git a/subed/subed-common.el b/subed/subed-common.el
index 8713cd4..a3ea291 100644
--- a/subed/subed-common.el
+++ b/subed/subed-common.el
@@ -621,6 +621,16 @@ following manner:
(subed-regenerate-ids-soon))
(point))
+(defun subed-set-subtitle-text (text &optional sub-id)
+ "Set subtitle text to TEXT.
+
+If SUB-ID is not given, set the text of the current subtitle.
+
+Return the new subtitle text."
+ (delete-region (subed-jump-to-subtitle-text sub-id)
+ (or (subed-jump-to-subtitle-end sub-id) (point)))
+ (insert text))
+
(defun subed-split-subtitle (&optional offset)
"Split current subtitle at point.
@@ -660,7 +670,7 @@ position of the point."
(subed-jump-to-subtitle-text))
(let* ((orig-start (subed-subtitle-msecs-start))
(orig-end (subed-subtitle-msecs-stop))
- (text-fraction (/ (* 1.0 (- (point) text-beg)) (- text-end
text-beg)))
+ (text-fraction (if (= text-beg text-end) 1 (/ (* 1.0 (- (point)
text-beg)) (- text-end text-beg))))
(time-fraction (floor (* text-fraction (- orig-end orig-start))))
(split-timestamp
(cond
@@ -675,8 +685,8 @@ position of the point."
(new-text (string-trim (buffer-substring (point) text-end)))
(new-start-timestamp (+ split-timestamp subed-subtitle-spacing)))
(subed-set-subtitle-time-stop split-timestamp)
- (delete-region (point) text-end)
- (subed-append-subtitle nil new-start-timestamp orig-end new-text))
+ (subed-set-subtitle-text (string-trim (buffer-substring-no-properties
text-beg (point))))
+ (subed-append-subtitle nil new-start-timestamp orig-end (string-trim
new-text)))
(subed-regenerate-ids-soon)
(point)))
diff --git a/tests/test-subed-common.el b/tests/test-subed-common.el
index a5d392b..d6266eb 100644
--- a/tests/test-subed-common.el
+++ b/tests/test-subed-common.el
@@ -1,4 +1,4 @@
-;; -*- eval: (buttercup-minor-mode) -*-
+;; -*- lexical-binding: t; eval: (buttercup-minor-mode) -*-
(add-to-list 'load-path "./subed")
(require 'subed)
(require 'subed-srt)
@@ -505,14 +505,14 @@ Baz.
"Foo.\n"))
(expect (spy-calls-args-for 'foo 0) :to-equal `(,(+ 60000 2000 123)))
(expect (spy-calls-count 'foo) :to-equal 1)))
- (let ((subed-mpv-playback-position (+ 60000 5000 456)))
- (expect (subed-copy-player-pos-to-stop-time) :to-be
subed-mpv-playback-position)
- (expect (buffer-string) :to-equal (concat "1\n"
- "00:01:02,123 -->
00:01:05,456\n"
- "Foo.\n"))
- (expect (spy-calls-args-for 'foo 0) :to-equal `(,(+ 60000 2000 123)))
- (expect (spy-calls-count 'foo) :to-equal 2)))
- (remove-hook 'subed-subtitle-time-adjusted-hook 'foo))
+ (let ((subed-mpv-playback-position (+ 60000 5000 456)))
+ (expect (subed-copy-player-pos-to-stop-time) :to-be
subed-mpv-playback-position)
+ (expect (buffer-string) :to-equal (concat "1\n"
+ "00:01:02,123 -->
00:01:05,456\n"
+ "Foo.\n"))
+ (expect (spy-calls-args-for 'foo 0) :to-equal `(,(+ 60000 2000 123)))
+ (expect (spy-calls-count 'foo) :to-equal 2)))
+ (remove-hook 'subed-subtitle-time-adjusted-hook 'foo))
)
(describe "Moving"
@@ -910,9 +910,9 @@ Baz.
(spy-on 'subed-enable-replay-adjusted-subtitle :and-call-through)
(spy-on 'subed-disable-replay-adjusted-subtitle :and-call-through)
(spy-on 'subed-adjust-subtitle-time-start :and-call-fake
- (lambda (msecs &optional a b) (expect
(subed-replay-adjusted-subtitle-p) :to-be nil)))
+ (lambda (msecs &optional a b) (expect
(subed-replay-adjusted-subtitle-p) :to-be nil)))
(spy-on 'subed-adjust-subtitle-stop :and-call-fake
- (lambda (msecs &optional a b) (expect
(subed-replay-adjusted-subtitle-p) :to-be nil)))
+ (lambda (msecs &optional a b) (expect
(subed-replay-adjusted-subtitle-p) :to-be nil)))
(subed-move-subtitle-forward 100)
(expect 'subed-disable-replay-adjusted-subtitle
:to-have-been-called-times 1)
(expect 'subed-enable-replay-adjusted-subtitle
:to-have-been-called-times 1)
@@ -926,9 +926,9 @@ Baz.
(spy-on 'subed-enable-replay-adjusted-subtitle :and-call-through)
(spy-on 'subed-disable-replay-adjusted-subtitle :and-call-through)
(spy-on 'subed-adjust-subtitle-time-start :and-call-fake
- (lambda (msecs &optional a b) (expect
(subed-replay-adjusted-subtitle-p) :to-be nil)))
+ (lambda (msecs &optional a b) (expect
(subed-replay-adjusted-subtitle-p) :to-be nil)))
(spy-on 'subed-adjust-subtitle-stop :and-call-fake
- (lambda (msecs &optional a b) (expect
(subed-replay-adjusted-subtitle-p) :to-be nil)))
+ (lambda (msecs &optional a b) (expect
(subed-replay-adjusted-subtitle-p) :to-be nil)))
(subed-move-subtitle-forward 100)
(expect 'subed-disable-replay-adjusted-subtitle
:to-have-been-called-times 1)
(expect 'subed-enable-replay-adjusted-subtitle
:to-have-been-called-times 0)
@@ -2117,10 +2117,10 @@ Baz.
(it "schedules re-enabling of point-to-player syncing."
(subed-disable-sync-point-to-player-temporarily)
(expect 'run-at-time :to-have-been-called-with
- subed-point-sync-delay-after-motion nil
- '(closure (t) nil
- (setq subed--point-sync-delay-after-motion-timer nil)
- (subed-enable-sync-point-to-player :quiet))))
+ subed-point-sync-delay-after-motion nil
+ '(closure (t) nil
+ (setq subed--point-sync-delay-after-motion-timer nil)
+ (subed-enable-sync-point-to-player :quiet))))
(it "cancels previously scheduled re-enabling of point-to-player syncing."
(subed-disable-sync-point-to-player-temporarily)
(expect 'cancel-timer :not :to-have-been-called-with "mock timer")
@@ -2132,3 +2132,182 @@ Baz.
(expect 'cancel-timer :to-have-been-called-times 2))
)
)
+
+(describe "Splitting subtitles"
+ (describe "when there are multiple lines"
+ :var ((text "1
+00:01:23,000 --> 00:02:34,567
+This is a subtitle
+that has two lines.
+
+")
+ (subed-subtitle-spacing 100))
+ (it "properly splits text when called at the beginning of the subtitle."
+ (with-temp-srt-buffer
+ (insert text)
+ (re-search-backward "This is a subtitle")
+ (goto-char (match-beginning 0))
+ (save-excursion (subed-split-subtitle 100))
+ (expect (buffer-string) :to-equal "1
+00:01:23,000 --> 00:01:23,100
+
+
+0
+00:01:23,200 --> 00:02:34,567
+This is a subtitle
+that has two lines.
+")))
+ (it "properly splits text when called in the middle of the subtitle."
+ (with-temp-srt-buffer
+ (insert text)
+ (re-search-backward "This is a")
+ (goto-char (match-end 0))
+ (subed-split-subtitle 100)
+ (expect (subed-subtitle-text 1) :to-equal "This is a")
+ (subed-regenerate-ids)
+ (expect (subed-subtitle-text 2) :to-equal "subtitle\nthat has two
lines.")))
+ (it "properly splits text when called at the end of a line in the middle
of the subtitle"
+ (with-temp-srt-buffer
+ (insert text)
+ (re-search-backward "This is a subtitle")
+ (goto-char (match-end 0))
+ (subed-split-subtitle 100)
+ (expect (subed-subtitle-text 1) :to-equal "This is a subtitle")
+ (subed-regenerate-ids)
+ (expect (subed-subtitle-text 2) :to-equal "that has two lines.")))
+ (it "properly splits text when called at the beginning of a line in the
middle of the subtitle."
+ (with-temp-srt-buffer
+ (insert text)
+ (re-search-backward "that has two lines")
+ (goto-char (match-beginning 0))
+ (subed-split-subtitle 100)
+ (expect (subed-subtitle-text 1) :to-equal "This is a subtitle")
+ (subed-regenerate-ids)
+ (expect (subed-subtitle-text 2) :to-equal "that has two lines.")))
+ (it "properly splits text when called at the end of the subtitle."
+ (with-temp-srt-buffer
+ (insert text)
+ (subed-jump-to-subtitle-end 1)
+ (subed-split-subtitle 100)
+ (expect (subed-subtitle-text 1) :to-equal "This is a subtitle\nthat
has two lines.")
+ (subed-regenerate-ids)
+ (expect (subed-subtitle-text 2) :to-equal "")))
+ (it "properly splits text when called before whitespace at the end of the
subtitle."
+ (with-temp-srt-buffer
+ (insert text)
+ (subed-jump-to-subtitle-end 1)
+ (save-excursion (insert " "))
+ (subed-split-subtitle 100)
+ (expect (subed-subtitle-text 1) :to-equal "This is a subtitle\nthat
has two lines.")
+ (subed-regenerate-ids)
+ (expect (subed-subtitle-text 2) :to-equal ""))))
+ (describe "when playing the video in MPV"
+ (it "splits at point in the middle of the subtitle."
+ (with-temp-srt-buffer
+ (insert mock-srt-data)
+ (re-search-backward "Foo\\.")
+ (end-of-line)
+ (save-excursion (insert " Some text here."))
+ (setq-local subed-mpv-playback-position 61600)
+ (setq-local subed-subtitle-spacing 100)
+ (subed-split-subtitle)
+ (prin1 (buffer-string))
+ (expect (subed-subtitle-msecs-start) :to-equal 61700)
+ (expect (subed-subtitle-msecs-stop) :to-equal 65123)
+ (expect (subed-subtitle-text) :to-equal "Some text here.")
+ (subed-backward-subtitle-time-start)
+ (expect (subed-subtitle-msecs-stop) :to-equal 61600)
+ (expect (subed-subtitle-text) :to-equal "Foo.")))
+ (it "splits at the end even if there are spaces."
+ (with-temp-srt-buffer
+ (insert mock-srt-data)
+ (re-search-backward "Foo\\.")
+ (subed-jump-to-subtitle-end)
+ (insert " ")
+ (setq-local subed-mpv-playback-position 61600)
+ (setq-local subed-subtitle-spacing 100)
+ (subed-split-subtitle)
+ (expect (subed-subtitle-text) :to-equal "")
+ (expect (subed-subtitle-msecs-start) :to-equal 61700)
+ (expect (subed-subtitle-msecs-stop) :to-equal 65123)
+ (subed-backward-subtitle-time-start)
+ (expect (subed-subtitle-text) :to-equal "Foo.")
+ (expect (subed-subtitle-msecs-stop) :to-equal 61600)))
+ (it "splits at the beginning."
+ (with-temp-srt-buffer
+ (save-excursion (insert mock-srt-data))
+ (subed-jump-to-subtitle-text)
+ (setq-local subed-mpv-playback-position 61600)
+ (setq-local subed-subtitle-spacing 100)
+ (subed-split-subtitle)
+ (expect (subed-subtitle-text) :to-equal "Foo.")
+ (expect (subed-subtitle-msecs-start) :to-equal 61700)
+ (expect (subed-subtitle-msecs-stop) :to-equal 65123)
+ (subed-backward-subtitle-time-start)
+ (expect (subed-subtitle-text) :to-equal "")
+ (expect (subed-subtitle-msecs-stop) :to-equal 61600))))
+ (describe "when a positive offset is specified"
+ (it "splits from the starting time."
+ (with-temp-srt-buffer
+ (insert mock-srt-data)
+ (re-search-backward "Foo\\.")
+ (end-of-line)
+ (save-excursion (insert " Some text here."))
+ (setq-local subed-subtitle-spacing 100)
+ (subed-split-subtitle 300)
+ (expect (subed-subtitle-msecs-start) :to-equal 61400)
+ (expect (subed-subtitle-msecs-stop) :to-equal 65123)
+ (expect (subed-subtitle-text) :to-equal "Some text here.")
+ (subed-backward-subtitle-time-start)
+ (expect (subed-subtitle-msecs-stop) :to-equal 61300)
+ (expect (subed-subtitle-text) :to-equal "Foo.")))
+ (it "uses the offset instead of the playing position."
+ (with-temp-srt-buffer
+ (insert mock-srt-data)
+ (re-search-backward "Foo\\.")
+ (setq-local subed-mpv-playback-position 61600)
+ (setq-local subed-subtitle-spacing 100)
+ (subed-split-subtitle 300)
+ (expect (subed-subtitle-msecs-start) :to-equal 61400)
+ (expect (subed-subtitle-msecs-stop) :to-equal 65123))))
+ (describe "when a negative offset is specified"
+ (it "splits from the ending time."
+ (with-temp-srt-buffer
+ (insert mock-srt-data)
+ (re-search-backward "Foo\\.")
+ (end-of-line)
+ (save-excursion (insert " Some text here."))
+ (setq-local subed-subtitle-spacing 100)
+ (subed-split-subtitle -300)
+ (expect (subed-subtitle-msecs-start) :to-equal 64923)
+ (expect (subed-subtitle-msecs-stop) :to-equal 65123)
+ (expect (subed-subtitle-text) :to-equal "Some text here.")
+ (subed-backward-subtitle-time-start)
+ (expect (subed-subtitle-msecs-stop) :to-equal 64823)
+ (expect (subed-subtitle-text) :to-equal "Foo.")))
+ (it "uses the offset instead of the playing position."
+ (with-temp-srt-buffer
+ (insert mock-srt-data)
+ (re-search-backward "Foo\\.")
+ (setq-local subed-subtitle-spacing 100)
+ (setq-local subed-mpv-playback-position 61600)
+ (subed-split-subtitle -300)
+ (expect (subed-subtitle-msecs-start) :to-equal 64923)
+ (expect (subed-subtitle-msecs-stop) :to-equal 65123)
+ (subed-backward-subtitle-time-start)
+ (expect (subed-subtitle-msecs-stop) :to-equal 64823))))
+ (describe "when nothing is specified"
+ (it "splits proportional to the location."
+ (with-temp-srt-buffer
+ (insert mock-srt-data)
+ (re-search-backward "Foo\\.")
+ (end-of-line)
+ (save-excursion (insert " Bar"))
+ (setq-local subed-subtitle-spacing 100)
+ (subed-split-subtitle)
+ (expect (subed-subtitle-msecs-start) :to-equal 63161)
+ (expect (subed-subtitle-msecs-stop) :to-equal 65123)
+ (expect (subed-subtitle-text) :to-equal "Bar")
+ (subed-backward-subtitle-time-start)
+ (expect (subed-subtitle-msecs-stop) :to-equal 63061)
+ (expect (subed-subtitle-text) :to-equal "Foo.")))))
- [nongnu] elpa/subed 3245922 282/389: Focus correct subtitle, (continued)
- [nongnu] elpa/subed 3245922 282/389: Focus correct subtitle, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed d076bac 285/389: Make enforced subtitle boundaries optional, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed b5667c4 286/389: Add tests for subed-enforce-time-boundaries, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed dde5dfd 287/389: Simplify subed-mpv--client-buffer, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed b1c5b88 354/389: Some adjustments to subed-split-subtitle, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 5f1ad41 355/389: "mpv" instead of "MPV" (see `man mpv`, https://mpv.io etc), ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed a83ee74 357/389: Remove trailing space, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed da4dac9 358/389: Two spaces after sentence to make `make test` pass, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 49ddccc 359/389: Move subed--init-alist to subed.el, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 8263b33 360/389: subed-split-subtitle: Use offset or text fraction, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 1c52f1b 361/389: Add tests for splitting subtitles and handle more cases,
ELPA Syncer <=
- [nongnu] elpa/subed e065632 362/389: Fix previous commit for subed-set-subtitle-text, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed ec7b222 368/389: Enable CPS showing by default and improve CPS toggling functions, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed f28ad22 379/389: Make subed-mpv-jump-to-current-subtitle interactive, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed c76ba50 387/389: Prompt for playback speed factor, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 9d0aa0f 388/389: Make make-subtitle a generic function, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed d0dfa1a 389/389: Preliminary support for .ass files (Advanced SubStation Alpha), ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed f30780e 249/389: Default keybinding: C-M-i -> subed-insert-subtitle-adjacent, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed f8793fb 330/389: Move motion hooks from subed-config.el to subed-common.el, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 31556c6 372/389: Add functions for bold and italic and change keybindings, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed ba22919 289/389: Add subed-mpv-jump-to-current-subtitle, ELPA Syncer, 2021/12/03