Change README.md
[navi2ch.git] / test / test.el
blob19bc7bbc3f172b6d50fdefeed43845ad18de93ac
1 ;; run with `emacs --batch --script test.el'
3 ;;; load ert
4 (when load-file-name
5 (add-to-list 'load-path
6 (expand-file-name "ert"
7 (file-name-directory load-file-name))))
8 (require 'ert-batch)
10 ;;; load navi2ch
12 (setq load-path
13 (cons (expand-file-name ".."
14 (file-name-directory load-file-name))
15 load-path))
16 (require 'navi2ch-util)
17 (require 'navi2ch-http-date)
18 (require 'navi2ch-net)
19 (require 'navi2ch-thumbnail)
21 ;;; define tests
23 (ert-deftest replace-html-tag ()
24 (should (string=
25 (navi2ch-replace-html-tag
26 " <a href=\"../test/read.cgi/***/***/***\" target=\"_blank\">&gt;&gt;139</a> <br> foo")
27 " >>139 \n foo")))
29 (ert-deftest http-date-test-1 ()
30 (let ((expected "Sun, 06 Nov 1994 08:49:37 GMT"))
31 (should (string= expected (navi2ch-http-date-encode
32 (navi2ch-http-date-decode
33 "Sun, 06 Nov 1994 08:49:37 GMT"))))))
34 (ert-deftest http-date-test-2 ()
35 (let ((expected "Sun, 06 Nov 1994 08:49:37 GMT"))
36 (should (string= expected (navi2ch-http-date-encode
37 (navi2ch-http-date-decode
38 "Sunday, 06-Nov-94 08:49:37 GMT"))))))
39 (ert-deftest http-date-test-3 ()
40 (let ((expected "Sun, 06 Nov 1994 08:49:37 GMT"))
41 (should (string= expected (navi2ch-http-date-encode
42 (navi2ch-http-date-decode
43 "Sun Nov 6 08:49:37 1994"))))))
45 (ert-deftest tanpan-check-1 ()
46 (should (navi2ch-net-is-tanpan-thread-p
47 "短パンマン ★<><>2010/09/15 01:09:42 ID:TanpanM<>いろいろあるさ @bg r.so ver 2008/02/19<br>ERROR = 5656 <br>(e_mes = []) <br> <>名古屋はエ〜エ〜で ♪
48 ")))
50 (ert-deftest thumbnail-save-good-work ()
51 (let* ((workdir (make-temp-file "workdir." t))
52 (fileFoo (expand-file-name "foo" workdir))
53 (fileBar (expand-file-name "bar" workdir)))
54 (unwind-protect
55 (progn
56 (with-temp-file fileFoo (insert "foo"))
57 ;; copy する
58 (navi2ch-thumbnail-save-content fileFoo fileBar)
59 (should
60 (string= (with-temp-buffer (insert-file-contents fileBar) (buffer-string))
61 "foo"))
62 (with-temp-file fileBar (insert "bar"))
63 ;; overwrite
64 (navi2ch-thumbnail-save-content fileFoo fileBar t)
65 (should
66 (string= (with-temp-buffer (insert-file-contents fileBar) (buffer-string))
67 "foo")))
68 (delete-directory workdir t))))
70 (ert-deftest thumbnail-save-interactive-check ()
71 (let ((si:read-file-name (symbol-function 'read-file-name))
72 intform)
73 (unwind-protect
74 (progn
75 (fset 'read-file-name (lambda (a b c)
76 (expand-file-name (or c "hoge") b)))
77 (lexical-let ((evalform (cadr (interactive-form 'navi2ch-thumbnail-save-content))))
78 (fset 'intform (lambda () (eval evalform))))
79 (with-temp-buffer
80 ;; prop `navi2ch-link' から読みだす。読みだせないとエラー。
81 (should-error (intform))
82 (save-excursion (insert (propertize "foo" 'navi2ch-link "foobar")))
83 (let ((res (intform)))
84 (should (equal res (list "foobar"
85 (expand-file-name "hoge"
86 navi2ch-thumbnail-save-content-dir)
87 0))))
88 (put-text-property (point-min) (point-max) 'file-name "barbaz")
89 (let ((res (intform)))
90 (should (equal res (list "foobar"
91 (expand-file-name "barbaz"
92 navi2ch-thumbnail-save-content-dir)
93 0))))))
94 (fset 'read-file-name si:read-file-name))))
96 (ert-deftest thumbnail-save-file-invalid ()
97 (let* ((workdir (make-temp-file "workdir." t))
98 (fileFoo (expand-file-name "foo" workdir))
99 (fileBar (expand-file-name "bar" workdir)))
100 (unwind-protect
101 (progn
102 ;; ファイルがない
103 (should-error
104 (navi2ch-thumbnail-save-content fileFoo fileBar))
105 ;; ファイルが書きこみできない
106 (with-temp-file fileFoo (insert "foo"))
107 (with-temp-file fileBar (insert "bar"))
108 (chmod fileBar 256)
109 (should-error
110 (navi2ch-thumbnail-save-content fileFoo fileBar)))
111 (delete-directory workdir t))))
113 (ert-deftest thumbnail-save-invalid-argument ()
114 ;; 無効な引数
115 (let* ((workdir (make-temp-file "workdir." t))
116 (fileFoo (expand-file-name "foo" workdir)))
117 (unwind-protect
118 (progn
119 (should-error
120 (navi2ch-thumbnail-save-content nil nil))
121 (should-error
122 (navi2ch-thumbnail-save-content nil fileFoo))
123 (should-error
124 (navi2ch-thumbnail-save-content fileFoo nil)))
125 (delete-directory workdir t))))
127 (ert-deftest thumbnail-save-same-file ()
128 ;; 同じファイル
129 (let* ((workdir (make-temp-file "workdir." t))
130 (file (expand-file-name "foo" workdir)))
131 (unwind-protect
132 (progn
133 (with-temp-file file (insert "foo"))
134 (should-error
135 (navi2ch-thumbnail-save-content file file t))
136 (should-error
137 (navi2ch-thumbnail-save-content file file nil)))
138 (delete-directory workdir t))))
140 (ert-deftest bm-sort-by-number ()
141 (should (string=
142 "1\n2\n 12\n1000\n"
143 (with-temp-buffer
144 (insert "1\n1000\n2\n 12\n")
145 (navi2ch-bm-sort-by-number)
146 (buffer-substring-no-properties (point-min) (point-max))))))
148 ;;; run the tests
150 (ert-run-tests-batch)