New testcases. Add docstring.
[elisp/wanderlust.git] / tests / test-elmo-util.el
1 (require 'lunit)
2 (require 'elmo-util)
3
4
5 (luna-define-class test-elmo-util (lunit-test-case))
6
7 ;; setup & teardown
8 (defvar test-elmo-temoporary-file)
9
10 (luna-define-method lunit-test-case-setup ((case test-elmo-util))
11   (setq print-length 1
12         print-level 1)
13   (setq case-fold-search nil)
14   (setq test-elmo-temoporary-file
15         (make-temp-file temporary-file-directory)))
16
17 (luna-define-method lunit-test-case-teardown ((case test-elmo-util))
18   (setq print-length nil
19         print-level nil)
20   (when (file-exists-p test-elmo-temoporary-file)
21     (delete-file test-elmo-temoporary-file)))
22
23
24 (luna-define-method test-elmo-replace-string-as-filename-1 ((case test-elmo-util))
25   "Revert checking replace and recover."
26   (lunit-assert
27    (let ((str "/foo//./../bar/"))
28      (string= str
29               (elmo-recover-string-from-filename
30                (elmo-replace-string-as-filename str))))))
31
32
33 ;; object save & load
34 (luna-define-method test-elmo-object-save-1 ((case test-elmo-util))
35   "Check `print-length' let bindings."
36   (let ((list '(1 2 3 4 5 6 7 8 9 10 11 12))
37         (print-length 1))
38     (elmo-object-save test-elmo-temoporary-file list)
39     (lunit-assert
40      (equal list
41             (elmo-object-load test-elmo-temoporary-file)))))
42
43 (luna-define-method test-elmo-object-save-2 ((case test-elmo-util))
44   "Check `print-level' let bindings."
45   (let ((list '(1 (2 :foo (nil . :bar))))
46         (print-level 1))
47     (elmo-object-save test-elmo-temoporary-file list)
48     (lunit-assert
49      (equal list
50             (elmo-object-load test-elmo-temoporary-file)))))
51
52 (luna-define-method test-elmo-save-string-1 ((case test-elmo-util))
53   )
54
55 ;; list functions
56 (luna-define-method test-elmo-uniq-list-1 ((case test-elmo-util))
57   (lunit-assert
58    (eq nil (elmo-uniq-list nil)))
59   (lunit-assert
60    (equal '(1) (elmo-uniq-list '(1))))
61   (lunit-assert
62    (equal '(1) (elmo-uniq-list '(1 1))))
63   (lunit-assert
64    (equal '(1) (elmo-uniq-list '(1 1 1)))))
65
66 (luna-define-method test-elmo-uniq-list-2 ((case test-elmo-util))
67   (lunit-assert
68    (equal '(1 2 3 4 5 6 nil)
69           (elmo-uniq-list '(1 2 3 4 1 5 6 nil nil 1 1 2))))
70   (lunit-assert
71    (equal '("foo") (elmo-uniq-list '("foo" "foo")))))
72
73 (luna-define-method test-elmo-uniq-list-3 ((case test-elmo-util))
74   "Check using DELETE-FUNCTION"
75   (lunit-assert
76    (equal '("foo" "foo") (elmo-uniq-list '("foo" "foo") #'delq)))
77   (lunit-assert
78    (equal '(:foo) (elmo-uniq-list '(:foo :foo) #'delq))))
79
80 (luna-define-method test-elmo-list-insert-1 ((case test-elmo-util))
81   (lunit-assert
82    (equal '(1 2 3 4 5)
83           (elmo-list-insert '(1 2 3 5) 4 3)))
84   (lunit-assert
85    (equal '(1 2 3 5 9)
86           (elmo-list-insert '(1 2 3 5) 9 :notfound)))
87 ;;; memq vs. member
88 ;;;   (equal '(1 2 "3" 5 4)
89 ;;;       (elmo-list-insert '(1 2 "3" 5) 4 "3"))
90    )
91
92 (luna-define-method test-elmo-list-insert-2 ((case test-elmo-util))
93   "Check not copied"
94   (let* ((list1 '(1 2 3 4 5))
95          (list2 list1))
96     (elmo-list-insert list1 4 3)
97     (lunit-assert
98      (eq list1 list2))))
99 ;;; memq vs. member
100 ;;;   (equal '(1 2 "3" 5 4)
101 ;;;       (elmo-list-insert '(1 2 "3" 5) 4 "3"))
102
103 (luna-define-method test-elmo-delete-char-1 ((case test-elmo-util))
104   (lunit-assert
105    (string= "f" (elmo-delete-char ?o "foo")))
106   (lunit-assert
107    (string= "f\nf" (elmo-delete-char ?o "foo\nfoo")))
108   (lunit-assert
109    (string= "" (elmo-delete-char ?o  "oo")))
110   (lunit-assert
111    (string= "" (elmo-delete-char ?o  ""))))
112
113 (luna-define-method test-elmo-concat-path-1 ((case test-elmo-util))
114   (lunit-assert
115    (string=
116     "/home/foo"
117     (elmo-concat-path "/home" "foo")))
118   (lunit-assert
119    (string=
120     (elmo-concat-path "/home/" "foo")
121     (elmo-concat-path "/home//" "foo"))))
122
123
124 (luna-define-method test-elmo-remove-passwd-1 ((case test-elmo-util))
125   "Check shred password."
126   (let* ((password "cGFzc3dk")
127          (elmo-passwd-alist (list (cons "key" password))))
128     (elmo-remove-passwd "key")
129     (lunit-assert
130      (string= "\0\0\0\0\0\0\0\0" password))))
131
132 (luna-define-method test-elmo-remove-passwd-2 ((case test-elmo-util))
133   "Check remove target pair only.  Not rassoc."
134   (let ((password "cGFzc3dk")
135         (elmo-passwd-alist '(("foo" . "key")
136                              ("key" . "ok")
137                              ("bar" . "baz"))))
138     (elmo-remove-passwd "key")
139     (lunit-assert
140      (equal '(("foo" . "key")
141               ("bar" . "baz"))
142             elmo-passwd-alist))))
143
144 (luna-define-method test-elmo-remove-passwd-3 ((case test-elmo-util))
145   "Multiple same key."
146   (let ((password "cGFzc3dk")
147         (elmo-passwd-alist '(("foo" . "key")
148                              ("key" . "ok")
149                              ("key" . "ok2")
150                              ("bar" . "baz"))))
151     (elmo-remove-passwd "key")
152     (lunit-assert
153      (equal '(("foo" . "key")
154               ("bar" . "baz"))
155             elmo-passwd-alist))))
156
157 (luna-define-method test-elmo-passwd-alist-clear-1 ((case test-elmo-util))
158   "Check shred ALL password."
159   (let* ((password1 "cGFzc3dk")
160          (password2 (copy-sequence password1))
161          (elmo-passwd-alist (list (cons "key1" password1)
162                                   (cons "key2" password2))))
163     (elmo-passwd-alist-clear)
164     (lunit-assert
165      (string= "\0\0\0\0\0\0\0\0" password1))
166     (lunit-assert
167      (string= "\0\0\0\0\0\0\0\0" password2))))