Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions jsonian-tests.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; jsonian-tests.el --- Tests for jsonian.el -*- lexical-bindings: t; -*-
;;; jsonian-tests.el --- Tests for jsonian.el -*- lexical-binding: t; -*-

;; Copyright (C) 2022 Ian Wahbe

Expand Down Expand Up @@ -212,7 +212,7 @@ We test that all lines are unchanged"
(should (string= (buffer-string) file-contents)))))
(with-file-and-point "path1.json" (point-min)
(let ((jsonian-indentation 4))
(dotimes (l (count-lines (point-min) (point-max)))
(dotimes (_ (count-lines (point-min) (point-max)))
(jsonian-indent-line)
(forward-line))
(should (string= "{
Expand Down Expand Up @@ -798,5 +798,39 @@ If START and END are provided, they are set as point and mark."
\"small\": true
}`" 2 '(17 . 23)))

(ert-deftest jsonian-edit-string-return ()
"Test `jsonian-edit-string' moves to a new buffer and then returns correctly."
(with-temp-buffer
(let ((json-string "{\"key\": \"some complex value\\nwith new lines\"}")
(b (current-buffer)))
(jsonian-mode)
(insert json-string)
(goto-char 10) ;; Move the point into the "complex" string
(jsonian-edit-string)
(should (string= "some complex value\nwith new lines" (buffer-string)))
(should (not (equal b (current-buffer))))
(delete-region (point-min) (point-max))
(insert "the new value,\tstill with special characters")
(jsonian-edit-mode-return)
(should (equal b (current-buffer)))
(should (string= (buffer-string) "{\"key\": \"the new value,\\tstill with special characters\"}")))))

(ert-deftest jsonian-edit-string-cancel ()
"Test `jsonian-edit-string' moves to a new buffer and then cancels correctly."
(with-temp-buffer
(let ((json-string "{\"key\": \"some complex value\\nwith new lines\"}")
(b (current-buffer)))
(jsonian-mode)
(insert json-string)
(goto-char 10) ;; Move the point into the "complex" string
(jsonian-edit-string)
(should (string= "some complex value\nwith new lines" (buffer-string)))
(should (not (equal b (current-buffer))))
(delete-region (point-min) (point-max))
(insert "This value should not show up in the original buffer")
(jsonian-edit-mode-cancel)
(should (equal b (current-buffer)))
(should (string= (buffer-string) json-string)))))

(provide 'jsonian-tests)
;;; jsonian-tests.el ends here
7 changes: 6 additions & 1 deletion jsonian.el
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,12 @@ This means replacing '\\n' with '\n' and '\\t' with '\t'."
(if kill-window
(kill-buffer-and-window)
(kill-current-buffer))
(select-window (get-buffer-window back-buffer))
;; Go back to the display window, if it exists.
;;
;; It should exist as long as Emacs is running with UI.
(if-let (w (get-buffer-window back-buffer))
(select-window w)
(switch-to-buffer back-buffer))
(read-only-mode -1)))

(define-minor-mode jsonian--edit-mode
Expand Down