Skip to content

Commit

Permalink
add tests, minor corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
tpapp committed Apr 10, 2024
1 parent 5f2e774 commit 9bcca66
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
43 changes: 43 additions & 0 deletions julia-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,49 @@ hello world
(string-to-syntax "\\")
(syntax-after 13)))))

;;; testing julia-latexsub-or-indent

(cl-defun julia-test-latexsub-or-indent (from &key (position (1+ (length from))) (greedy t))
"Utility function to test `julia-latexsub-or-indent'.
This is how it works:
1. FROM is inserted in a buffer.
2. The point is moved to POSITION.
3. `julia-latexsub-or-indent' is called on the buffer.
If `julia-latexsub-selector' is called, it selects the first replacement, which is also placed in SELECTION (otherwise it is NIL).
Return a cons of the
1. buffer contents
2. the replacement of SELECTION when not nil.
The latter can be used to construct test comparisons."
(let* ((selection)
(julia-latexsub-selector
(lambda (replacements)
(setf selection (car replacements))
selection))
(julia-latexsub-greedy greedy))
(cons (with-temp-buffer
(insert from)
(goto-char position)
(julia-latexsub-or-indent t)
(buffer-string))
(gethash selection julia-mode-latexsubs))))

(ert-deftest julia--test-latexsub-or-indent ()
(should (equal (julia-test-latexsub-or-indent "\\circ") '("")))
(let ((result (julia-test-latexsub-or-indent "\\circXX" :position 5)))
(should (equal (car result) (concat (cdr result) "cXX"))))
(let ((result (julia-test-latexsub-or-indent "\\circ" :greedy nil)))
(should (equal (car result) (cdr result))))
(should (equal (julia-test-latexsub-or-indent "\\alpha") '("α"))))

;;;
;;; run all tests
;;;
Expand Down
22 changes: 11 additions & 11 deletions julia-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -951,16 +951,16 @@ When multiple options match, ask the user to clarify via `julia-latexsub-selecto
(let ((partial (buffer-substring-no-properties beg (point))))
(when-let (replacements (gethash partial julia-mode--latexsubs-partials))
(let* ((complete-match (member partial replacements))
(replacement (cond
;; complete match w/ greedy
((and complete-match julia-latexsub-greedy) partial)
;; multiple replacements, ask user
((cdr replacements) (funcall julia-latexsub-selector replacements))
;; single replacement, pick that
(t (car replacements)))))
(cons beg replacement))))))

(defun julia-latexsub-or-indent (arg)
(latex (cond
;; complete match w/ greedy
((and complete-match julia-latexsub-greedy) partial)
;; multiple replacements, ask user
((cdr replacements) (funcall julia-latexsub-selector replacements))
;; single replacement, pick that
(t (car replacements)))))
(cons beg latex))))))

(defun julia-latexsub-or-indent (arg)
"Either indent according to Julia mode conventions or perform a LaTeX-like symbol substution.
When multiple options match, ask the user to clarify via `julia-latexsub-selector', unless there is a complete match and `julia-latexsub-greedy' is `t'.
Expand All @@ -969,7 +969,7 @@ Presently, this is not the default. Enable with eg
(define-key julia-mode-map (kbd \"TAB\") 'julia-latexsub-or-indent)
in your `julia-mode-hook'."
eg in your `julia-mode-hook'."
(interactive "*i")
(if-let (replacement (julia-mode--latexsub-before-point))
(progn
Expand Down

0 comments on commit 9bcca66

Please sign in to comment.