Change README.md
[navi2ch.git] / test / test-basic.el
blob912b8709eab86b65304dcb16a5ef6ae0e7ad4588
1 ;; test.el -*- coding: iso-2022-jp; lexical-binding: t; -*-
2 ;; run with `emacs --batch -q -l ert -l test.el --eval "(ert-run-tests-batch-and-exit)"
4 (require 'navi2ch-rfc3986)
5 (require 'navi2ch-util)
6 (require 'navi2ch-board-misc)
7 (require 'navi2ch-http-date)
8 (require 'navi2ch-net)
9 (require 'navi2ch-thumbnail)
10 (require 'navi2ch-multibbs)
12 ;;; define tests
14 (ert-deftest replace-html-tag ()
15 (should (string=
16 (navi2ch-replace-html-tag
17 " <a href=\"../test/read.cgi/***/***/***\" target=\"_blank\">&gt;&gt;139</a> <br> foo")
18 " >>139 \n foo")))
20 (ert-deftest http-date-test-1 ()
21 (let ((expected "Sun, 06 Nov 1994 08:49:37 GMT"))
22 (should (string= expected (navi2ch-http-date-encode
23 (navi2ch-http-date-decode
24 "Sun, 06 Nov 1994 08:49:37 GMT"))))))
25 (ert-deftest http-date-test-2 ()
26 (let ((expected "Sun, 06 Nov 1994 08:49:37 GMT"))
27 (should (string= expected (navi2ch-http-date-encode
28 (navi2ch-http-date-decode
29 "Sunday, 06-Nov-94 08:49:37 GMT"))))))
30 (ert-deftest http-date-test-3 ()
31 (let ((expected "Sun, 06 Nov 1994 08:49:37 GMT"))
32 (should (string= expected (navi2ch-http-date-encode
33 (navi2ch-http-date-decode
34 "Sun Nov 6 08:49:37 1994"))))))
36 (ert-deftest tanpan-check-1 ()
37 (should (navi2ch-net-is-tanpan-thread-p
38 "\e$BC;%Q%s%^%s\e(B \e$B!z\e(B<><>2010/09/15 01:09:42 ID:TanpanM<>\e$B$$$m$$$m$"$k$5\e(B \e$B!w\e(Bbg r.so ver 2008/02/19<br>ERROR = 5656 <br>(e_mes = []) <br> <>\e$BL>8E20$O%(!A%(!A$G\e(B \e$B"v\e(B
39 ")))
41 (ert-deftest thumbnail-save-good-work ()
42 (let* ((workdir (make-temp-file "workdir." t))
43 (fileFoo (expand-file-name "foo" workdir))
44 (fileBar (expand-file-name "bar" workdir)))
45 (unwind-protect
46 (progn
47 (with-temp-file fileFoo (insert "foo"))
48 ;; copy \e$B$9$k\e(B
49 (navi2ch-thumbnail-save-content fileFoo fileBar)
50 (should
51 (string= (with-temp-buffer (insert-file-contents fileBar) (buffer-string))
52 "foo"))
53 (with-temp-file fileBar (insert "bar"))
54 ;; overwrite
55 (navi2ch-thumbnail-save-content fileFoo fileBar t)
56 (should
57 (string= (with-temp-buffer (insert-file-contents fileBar) (buffer-string))
58 "foo")))
59 (delete-directory workdir t))))
61 (ert-deftest thumbnail-save-interactive-check ()
62 (cl-flet ((intform `(lambda () ,(cadr (interactive-form 'navi2ch-thumbnail-save-content))))
63 (org:expand-file-name (symbol-function 'expand-file-name)))
64 (cl-letf (((symbol-function 'read-file-name)
65 (lambda (prompt &optional dir default-filename)
66 (org:expand-file-name default-filename dir)))
67 ((symbol-function 'expand-file-name)
68 (lambda (name &optional default-directory)
69 (org:expand-file-name (or name "hoge") default-directory))))
71 (with-temp-buffer
72 ;; prop `navi2ch-link' \e$B$+$iFI$_$@$9!#FI$_$@$;$J$$$H%(%i!<!#\e(B
73 (should-error (intform))
74 (save-excursion (insert (propertize "foo" 'navi2ch-link "foobar")))
75 (let ((res (intform)))
76 (should (equal res (list "foobar"
77 (expand-file-name "hoge"
78 navi2ch-thumbnail-save-content-dir)
79 0))))
80 (put-text-property (point-min) (point-max) 'file-name "barbaz")
81 (let ((res (intform)))
82 (should (equal res (list "foobar"
83 (expand-file-name "barbaz"
84 navi2ch-thumbnail-save-content-dir)
85 0))))))))
87 (ert-deftest thumbnail-save-file-invalid ()
88 (let* ((workdir (make-temp-file "workdir." t))
89 (fileFoo (expand-file-name "foo" workdir))
90 (fileBar (expand-file-name "bar" workdir)))
91 (unwind-protect
92 (progn
93 ;; \e$B%U%!%$%k$,$J$$\e(B
94 (should-error
95 (navi2ch-thumbnail-save-content fileFoo fileBar))
96 ;; \e$B%U%!%$%k$,=q$-$3$_$G$-$J$$\e(B
97 (with-temp-file fileFoo (insert "foo"))
98 (with-temp-file fileBar (insert "bar"))
99 (chmod fileBar 256)
100 (should-error
101 (navi2ch-thumbnail-save-content fileFoo fileBar)))
102 (delete-directory workdir t))))
104 (ert-deftest thumbnail-save-invalid-argument ()
105 ;; \e$BL58z$J0z?t\e(B
106 (let* ((workdir (make-temp-file "workdir." t))
107 (fileFoo (expand-file-name "foo" workdir)))
108 (unwind-protect
109 (progn
110 (should-error
111 (navi2ch-thumbnail-save-content nil nil))
112 (should-error
113 (navi2ch-thumbnail-save-content nil fileFoo))
114 (should-error
115 (navi2ch-thumbnail-save-content fileFoo nil)))
116 (delete-directory workdir t))))
118 (ert-deftest thumbnail-save-same-file ()
119 ;; \e$BF1$8%U%!%$%k\e(B
120 (let* ((workdir (make-temp-file "workdir." t))
121 (file (expand-file-name "foo" workdir)))
122 (unwind-protect
123 (progn
124 (with-temp-file file (insert "foo"))
125 (should-error
126 (navi2ch-thumbnail-save-content file file t))
127 (should-error
128 (navi2ch-thumbnail-save-content file file nil)))
129 (delete-directory workdir t))))
131 (ert-deftest bm-sort-by-number ()
132 (should (string=
133 "1\n2\n 12\n1000\n"
134 (with-temp-buffer
135 (insert "1\n1000\n2\n 12\n")
136 (navi2ch-bm-sort-by-number)
137 (buffer-substring-no-properties (point-min) (point-max))))))
139 (ert-deftest 2ch-url-to-board ()
140 (let ((datas '(("http://greta.5ch.net/poverty/oyster/1697/1697461053.dat" .
141 ((uri . "http://greta.5ch.net/poverty/") (id . "poverty")))
142 ("http://pc.2ch.net/unix/kako/999/999166513.html" .
143 ((uri . "http://pc.2ch.net/unix/") (id . "unix"))))))
144 (dolist (x datas)
145 (should (equal (cdr x)
146 (navi2ch-2ch-url-to-board (car x)))))))
148 (ert-deftest 2ch-url-to-article ()
149 (let ((datas '(("http://greta.5ch.net/poverty/oyster/1697/1697461053.dat" .
150 ((kako . t) (artid . "1697461053")))
151 ("http://pc.2ch.net/unix/kako/999/999166513.html" .
152 ((kako . t) (artid . "999166513"))))))
153 (dolist (x datas)
154 (should (equal (cdr x)
155 (navi2ch-2ch-url-to-article (car x)))))))
157 (ert-deftest util-replace-html-tag ()
158 (let ((datas
159 (list (cons "<a href=\"//localhost/hoge.html\">abc</a>" "abc")
160 (cons "&amp;" "&")
161 (cons "&#x26;" "&")
162 (cons "&#38;" "&")
163 (cons "&#12354;" (char-to-string 12354)) ; hiragana A
164 (cons "&Alpha;" (char-to-string 913))
165 (cons "&alpha;" (char-to-string 945))
166 ; \e$B%;%_%3%m%s$G=*N;$7$F$$$J$$$N$GJQ49$7$J$$\e(B
167 ; \e$B%P%C%U%!$N%F%9%H$GO"7k$9$k$N$G:G8e$K$*$/\e(B
168 (cons "&alpha" "&alpha"))))
170 (dolist (pair datas)
171 (should (string= (navi2ch-replace-html-tag (car pair)) (cdr pair))))
172 (should (string=
173 (with-temp-buffer
174 (insert (mapconcat (lambda (pair) (car pair)) datas "; "))
175 (navi2ch-replace-html-tag-with-buffer)
176 (buffer-string))
177 (mapconcat (lambda (pair) (cdr pair)) datas "; ")))))
179 (ert-deftest thumbnail-image-escape-filename ()
180 (let ((datas
181 (list (cons "/dir1/con/file" "/dir1/%63on/file")
182 (cons "/dir1/con.dir/file" "/dir1/%63on.dir/file")
183 (cons "/dir1/con/?file" "/dir1/%63on/%3ffile")
184 (cons "/dir1/con/:file" "/dir1/%63on/%3afile")
185 (cons "/dir1/con/*file" "/dir1/%63on/%2afile")
186 (cons "/dir1/con/|file" "/dir1/%63on/%7cfile")
187 (cons "/dir1/dir2/lPt1.txt" "/dir1/dir2/%6cPt1.txt")
188 (cons "cdn-9.motorsport.com" "cdn-9.motorsport.com"))))
190 (dolist (pair datas)
191 (should (string= (navi2ch-thumbnail-image-escape-filename (car pair)) (cdr pair))))))