summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /tests
new repository
Diffstat (limited to 'tests')
-rw-r--r--tests/test-join-line-or-region.el52
-rw-r--r--tests/test-theme-persistence.el135
2 files changed, 187 insertions, 0 deletions
diff --git a/tests/test-join-line-or-region.el b/tests/test-join-line-or-region.el
new file mode 100644
index 00000000..f949a62c
--- /dev/null
+++ b/tests/test-join-line-or-region.el
@@ -0,0 +1,52 @@
+;;; test-join-line-or-region.el --- Test Join Line/Region -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; Tests for the join-line-or-region function
+
+;;; Code:x
+
+;; (require 'custom-functions)
+
+(require 'ert)
+
+(defun cj/join-line-or-region (beg end)
+ "Apply 'join-line' over the region (identified by BEG and END).
+If the region's not active, join line with previous line above."
+ (interactive "r")
+ ;; when in region
+ (if mark-active
+ (message "mark is active")
+ (let ((beg (region-beginning))
+ (end (copy-marker (region-end))))
+ (goto-char beg)
+ ;; apply join lines until point => end
+ (while (< (point) end)
+ (join-line 1))
+ (goto-char end)
+ (newline)))
+ ;; outside of region; just join line
+ (join-line)(newline))
+
+
+(ert-deftest test-cj/join-line-or-region ()
+ (let ((given "Line1\nLine2\nLine3\n")
+ (expected "Line1Line2Line3")
+ (beg nil)
+ (end nil))
+ (with-temp-buffer
+ (insert given)
+ ;; select the whole buffer as region
+ (setq beg (copy-marker (goto-char (point-min))))
+ (setq end (copy-marker (goto-char (point-max))))
+
+ ;; Call the function being tested
+ (cj/join-line-or-region beg end)
+
+ ;; Perform assertions to check the expected result
+ (should (equal (buffer-substring-no-properties (point-min)
+ (point-max))
+ expected)))))
+
+
+(provide 'test-join-line-or-region)
+;;; test-join-line-or-region.el ends here.
diff --git a/tests/test-theme-persistence.el b/tests/test-theme-persistence.el
new file mode 100644
index 00000000..3d5b4f8e
--- /dev/null
+++ b/tests/test-theme-persistence.el
@@ -0,0 +1,135 @@
+;;; test-ui-theme.el --- Tests the ui-theme module -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; All the unit tests for the ui-theme.el module
+
+;;; Code:
+
+(add-to-list 'load-path (concat user-emacs-directory "modules"))
+(require 'ui-theme)
+
+;; ------------------------ Constants / Setup / Teardown -----------------------
+
+(defvar cj/original-theme-name nil)
+(defvar cj/original-newline-setting nil)
+
+(defun cj/test-setup ()
+ "Required settings and save state before each test."
+
+ ;; save the current theme for restoration
+ (setq cj/original-theme-name (symbol-name (car custom-enabled-themes)))
+ (setq cj/original-newline-setting mode-require-final-newline)
+
+ ;; unload all themes before starting test
+ (mapcar #'disable-theme custom-enabled-themes)
+
+ ;; no EOF newlines
+ (custom-set-variables
+ '(require-final-newline nil))
+ (setq mode-require-final-newline nil))
+
+(defun cj/test-teardown ()
+ "Restore the state before each test."
+ ;; restore newline setting
+ (setq require-final-newline cj/original-newline-setting)
+
+ ;; if there wasn't an original theme, remove all themes
+ (if (string= cj/original-theme-name "nil")
+ (mapcar #'disable-theme custom-enabled-themes)
+ ;; otherwise, restore it
+ (load-theme (intern cj/original-theme-name))))
+
+;; ----------------------------------- Tests -----------------------------------
+
+(ert-deftest test-write-file-contents ()
+ "Normal Case: Uses function to write a string, reads it back, and compares."
+ (cj/test-setup)
+ (let ((teststring "testing123")
+ (testfilename "test-write-file-contents.txt"))
+ ;; call the function
+ (should (equal (cj/write-file-contents teststring testfilename)
+ 't))
+ ;; Read the file and check it's contents
+ (should (equal (with-temp-buffer(insert-file-contents testfilename)
+ (buffer-string))
+ teststring))
+ ;; clean up test file
+ (delete-file testfilename))
+ (cj/test-teardown))
+
+(ert-deftest test-write-file-not-writable ()
+ "Test writing to a non-writable file."
+ (cl-flet ((file-writeable-p (file) nil))
+ (let* ((non-writable-file (make-temp-file "test-non-writable"))
+ (should (equal (cj/write-file-contents "cowabunga" non-writable-file) 'nil)))
+ (delete-file non-writable-file))))
+
+(ert-deftest test-read-file-contents ()
+ "Normal Case: Writes string to file and reads contents using function."
+ (cj/test-setup)
+ (let ((teststring "testing123")
+ (testfilename "test-read-file-contents.txt"))
+ ;; write the file
+ (with-temp-buffer
+ (insert teststring)
+ (write-file testfilename))
+ ;; call the function
+ (should (equal (cj/read-file-contents testfilename)
+ teststring))
+ ;; clean up test file
+ (delete-file testfilename))
+ (cj/test-teardown))
+
+(ert-deftest test-read-file-nonexistent ()
+ "Test reading from a non-existent file returns nil."
+ (cj/test-setup)
+ (let* ((filename (concat (number-to-string (random 99999999)) "nonexistent-file.txt"))
+ (result (cj/read-file-contents filename)))
+ (should (equal result nil)))
+ (cj/test-teardown))
+
+(ert-deftest test-get-active-theme ()
+ (cj/test-setup)
+ "Normal Case: Sets theme, gets theme-name, and compares."
+ (let ((expected "wombat"))
+ (load-theme (intern expected))
+ (should (string= (cj/get-active-theme-name) expected))
+ (cj/test-teardown)))
+
+(ert-deftest test-get-active-theme ()
+ (cj/test-setup)
+ "Normal Case: Sets theme, gets theme-name, and compares."
+ (let ((expected "nil"))
+ (mapcar #'disable-theme custom-enabled-themes)
+ (should (equal (cj/get-active-theme-name) expected))
+ (cj/test-teardown)))
+
+(ert-deftest test-save-theme-to-file ()
+ "Normal case: sets theme, saves it, reads from file, and compares."
+ (cj/test-setup)
+ (let ((expected "wombat"))
+ (load-theme (intern expected))
+ (cj/save-theme-to-file)
+ (should (equal (cj/read-file-contents theme-file) expected))
+ (cj/test-teardown)))
+
+(ert-deftest test-load-theme-from-file ()
+ "Normal case: saves new theme to file, loads it from file, and compares."
+ (cj/test-setup)
+ (let ((expected "wombat")) ;; the ui theme that test-setup uses.
+ (cj/write-file-contents expected theme-file)
+ (cj/load-theme-from-file)
+ (should (equal expected (cj/get-active-theme-name))))
+ (cj/test-teardown))
+
+(ert-deftest test-load-nil-theme ()
+ "Corner case: saves 'nil as theme name to file, loads it, and compares to not having a theme."
+ (cj/test-setup)
+ (let ((expected "nil")) ;; the ui theme that test-setup uses.
+ (cj/write-file-contents expected theme-file)
+ (cj/load-theme-from-file)
+ (should (equal expected (cj/get-active-theme-name))))
+ (cj/test-teardown))
+
+(provide 'test-ui-theme)
+;;; test-ui-theme.el ends here.