Add tests and package lint corrections

This commit is contained in:
Andros Fenollosa 2023-06-04 16:15:29 +02:00
parent cca72bb402
commit 8daa17bfd0

View File

@ -1,4 +1,4 @@
;;; format-region.el --- Transform region in different formats: camelCase, kebab-case or lisp-case, PascalCase and snake_case. -*- lexical-binding: t; ;;; format-region.el --- Transform region. -*- lexical-binding: t;
;; ;;
;; Copyright © 2023 Andros Fenollosa ;; Copyright © 2023 Andros Fenollosa
;; Authors: Andros Fenollosa <andros@fenollosa.email> ;; Authors: Andros Fenollosa <andros@fenollosa.email>
@ -11,8 +11,7 @@
;;; Code: ;;; Code:
(defun format-region-to-format (sentence separator is-first-word-capitalized is-all-words-capitalized)
(defun to-format (sentence separator is-first-word-capitalized is-all-words-capitalized)
"Convert SENTENCE to format. "Convert SENTENCE to format.
SEPARATOR is the character that will separate the words. SEPARATOR is the character that will separate the words.
IS-FIRST-WORD-CAPITALIZED is a boolean that indicates if the first word should be capitalized. IS-FIRST-WORD-CAPITALIZED is a boolean that indicates if the first word should be capitalized.
@ -26,17 +25,17 @@ IS-ALL-WORDS-CAPITALIZED is a boolean that indicates if all words should be capi
sentence-with-first-word-capitalized)) sentence-with-first-word-capitalized))
(defmacro curried-format (separator is-first-word-capitalized is-all-words-capitalized) (defmacro format-region-curried (separator is-first-word-capitalized is-all-words-capitalized)
"Curry the function to-format with SEPARATOR, IS-FIRST-WORD-CAPITALIZED and IS-ALL-WORDS-CAPITALIZED." "Curry the function to-format with SEPARATOR, IS-FIRST-WORD-CAPITALIZED and IS-ALL-WORDS-CAPITALIZED."
`(lambda (sentence) (to-format sentence ,separator ,is-first-word-capitalized ,is-all-words-capitalized))) `(lambda (sentence) (format-region-to-format sentence ,separator ,is-first-word-capitalized ,is-all-words-capitalized)))
;; Define functions ;; Define functions
(setq to-camel-case (curried-format nil nil t)) ; camelCase (setq format-region-to-format-camel-case (format-region-curried nil nil t)) ; camelCase
(setq to-kebab-case (curried-format "-" nil nil)) ; kebab-case or lisp-case (setq format-region-to-format-kebab-case (format-region-curried "-" nil nil)) ; kebab-case or lisp-case
(setq to-pascal-case (curried-format nil t t)) ; PascalCase (setq format-region-to-format-pascal-case (format-region-curried nil t t)) ; PascalCase
(setq to-snake-case (curried-format "_" nil nil)) ; snake_case (setq format-region-to-format-snake-case (format-region-curried "_" nil nil)) ; snake_case
(defun format-region (fn-format) (defun format-region-selected (fn-format)
"Format the selected region with FN-FORMAT." "Format the selected region with FN-FORMAT."
(interactive) (interactive)
(let ((text (buffer-substring-no-properties (region-beginning) (region-end)))) (let ((text (buffer-substring-no-properties (region-beginning) (region-end))))
@ -45,29 +44,43 @@ IS-ALL-WORDS-CAPITALIZED is a boolean that indicates if all words should be capi
;; Interactive functions ;; Interactive functions
(defun format-to-camel-case-region () (defun format-region-to-camel-case ()
"Convert the selected text to camelCase." "Convert the selected text to camelCase."
(interactive) (interactive)
(format-region to-camel-case)) (format-region-selected format-region-to-format-camel-case))
(defun format-to-kebab-case-region () (defun format-region-to-kebab-case ()
"Convert the selected text to kebab-case or lisp-case." "Convert the selected text to kebab-case or lisp-case."
(interactive) (interactive)
(format-region to-kebab-case)) (format-region-selected format-region-to-format-kebab-case))
(defun format-to-lisp-case-region () (defun format-region-to-lisp-case ()
"Convert the selected text to kebab-case or lisp-case." "Convert the selected text to kebab-case or lisp-case."
(interactive) (interactive)
(format-region to-kebab-case)) (format-region-selected format-region-to-format-kebab-case))
(defun format-to-pascal-case-region () (defun format-region-to-pascal-case ()
"Convert the selected text to PascalCase." "Convert the selected text to PascalCase."
(interactive) (interactive)
(format-region to-pascal-case)) (format-region-selected format-region-to-format-pascal-case))
(defun format-to-snake-case-region () (defun format-region-to-snake-case ()
"Convert the selected text to snake_case." "Convert the selected text to snake_case."
(interactive) (interactive)
(format-region to-snake-case)) (format-region-selected format-region-to-format-snake-case))
;; Tests
(defun format-region-debug ()
"Check if all functions work correctly. Print OK if so."
(let (
(is-camel-case-ok (string= (funcall format-region-to-format-camel-case "hello world") "helloWorld"))
(is-kebab-case-ok (string= (funcall format-region-to-format-kebab-case "hello world") "hello-world"))
(is-pascal-case-ok (string= (funcall format-region-to-format-pascal-case "hello world") "HelloWorld"))
(is-snake-case-ok (string= (funcall format-region-to-format-snake-case "hello world") "hello_world")))
(if (and is-camel-case-ok is-kebab-case-ok is-pascal-case-ok is-snake-case-ok)
(message "OK")
(message "ERROR"))))
(provide 'format-region)
;;; format-region.el ends here ;;; format-region.el ends here