summaryrefslogtreecommitdiff
path: root/tests/test-theme-theme-persistence.el.disabled
blob: e0b2f9e3c988a679a32766f71489b7ec340717a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
;;; test-theme-theme-persistence.el --- Tests theme persistence mechanism -*- lexical-binding: t; -*-

;;; Commentary:
;; Unit tests for the persistence of the chosen theme

;;; 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-theme-theme-persistence)
;;; test-theme-theme-persistence.el ends here.