]> code.delx.au - gnu-emacs-elpa/blob - packages/tiny/tiny-test.el
multishell - merge 1.0.6 changes.
[gnu-emacs-elpa] / packages / tiny / tiny-test.el
1 ;;; tiny-test.el --- Tests for Tiny
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Oleh Krehel
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 (when (require 'undercover nil t)
23 (undercover "tiny.el"))
24
25 (require 'tiny nil t)
26
27 (defun with-text-value (txt fn &rest args)
28 "Return the result of (apply 'FN ARGS), in a temp buffer with TXT,
29 with point at the end of TXT."
30 (with-temp-buffer
31 (insert txt)
32 (apply fn args)))
33
34 (ert-deftest tiny-mapconcat-parse ()
35 (should (equal (with-text-value "m10" #'tiny-mapconcat-parse)
36 '(nil nil "10" nil nil)))
37 (should (equal (with-text-value "m5%x" #'tiny-mapconcat-parse)
38 '(nil nil "5" nil "%x")))
39 (should (equal (with-text-value "m5 10" #'tiny-mapconcat-parse)
40 '("5" " " "10" nil nil)))
41 (should (equal (with-text-value "m5,10" #'tiny-mapconcat-parse)
42 '("5" "," "10" nil nil)))
43 (should (equal (with-text-value "m5 10*xx" #'tiny-mapconcat-parse)
44 '("5" " " "10" "(* x x)" nil)))
45 (should (equal (with-text-value "m5 10*xx%x" #'tiny-mapconcat-parse)
46 '("5" " " "10" "(* x x)" "%x")))
47 (should (equal (with-text-value "m5 10*xx|0x%x" #'tiny-mapconcat-parse)
48 '("5" " " "10" "(* x x)" "0x%x")))
49 (should (equal (with-text-value "m25+x?a%c" #'tiny-mapconcat-parse)
50 '(nil nil "25" "(+ x 97)" "%c")))
51 (should (equal (with-text-value "m25+x?A%c" #'tiny-mapconcat-parse)
52 '(nil nil "25" "(+ x 65)" "%c")))
53 (should (equal (with-text-value "m97,122stringx" #'tiny-mapconcat-parse)
54 '("97" "," "122" "(string x)" nil)))
55 (should (equal (with-text-value "m97,122stringxx" #'tiny-mapconcat-parse)
56 '("97" "," "122" "(string x x)" nil)))
57 (should (equal (with-text-value "m97,120stringxupcasex" #'tiny-mapconcat-parse)
58 '("97" "," "120" "(string x (upcase x))" nil)))
59 (should (equal (with-text-value "m97,120stringxupcasex)x" #'tiny-mapconcat-parse)
60 '("97" "," "120" "(string x (upcase x) x)" nil)))
61 (should (equal (with-text-value "m\\n;; 10|%(+ x x) and %(* x x) and %s" #'tiny-mapconcat-parse)
62 '(nil "\\n;; " "10" nil "%(+ x x) and %(* x x) and %s")))
63 (should (equal (with-text-value "m10|%0.2f" #'tiny-mapconcat-parse)
64 '(nil nil "10" nil "%0.2f"))))
65
66 (ert-deftest tiny-extract-sexps ()
67 (should (equal (tiny-extract-sexps "expr1 %(+ x x), nothing %% char %c, hex %x, and expr2 %(* x x), float %0.2f and sym %s")
68 '("expr1 %s, nothing %% char %c, hex %x, and expr2 %s, float %0.2f and sym %s"
69 "(+ x x)" nil nil "(* x x)" nil nil)))
70 (should (equal (tiny-extract-sexps "m1\n5| (%c(+ x ?a -1)) %0.1f(* x 0.2)")
71 '("m1
72 5| (%c) %0.1f" "(+ x ?a -1)" "(* x 0.2)"))))
73
74 (ert-deftest tiny-mapconcat ()
75 (should (equal (with-text-value "m10" (lambda()(eval (read (tiny-mapconcat)))))
76 "0 1 2 3 4 5 6 7 8 9 10"))
77 (should (equal (with-text-value "mm10" (lambda()(eval (read (tiny-mapconcat)))))
78 "012345678910"))
79 (should (equal (with-text-value "m5 10" (lambda()(eval (read (tiny-mapconcat)))))
80 "5 6 7 8 9 10"))
81 (should (equal (with-text-value "m5 10*xx" (lambda()(eval (read (tiny-mapconcat)))))
82 "25 36 49 64 81 100"))
83 (should (equal (with-text-value "m5 10*xx%x" (lambda()(eval (read (tiny-mapconcat)))))
84 "19 24 31 40 51 64"))
85 (should (equal (with-text-value "m5 10*xx|0x%x" (lambda()(eval (read (tiny-mapconcat)))))
86 "0x19 0x24 0x31 0x40 0x51 0x64"))
87 (should (equal (with-text-value "m25+x?a%c" (lambda()(eval (read (tiny-mapconcat)))))
88 "a b c d e f g h i j k l m n o p q r s t u v w x y z"))
89 (should (equal (with-text-value "m25+x?A%c" (lambda()(eval (read (tiny-mapconcat)))))
90 "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"))
91 (should (equal (with-text-value "m97,122(string x)" (lambda()(eval (read (tiny-mapconcat)))))
92 "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"))
93 (should (equal (with-text-value "m97,122stringxx" (lambda()(eval (read (tiny-mapconcat)))))
94 "aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,ll,mm,nn,oo,pp,qq,rr,ss,tt,uu,vv,ww,xx,yy,zz"))
95 (should (equal (with-text-value "m97,120stringxupcasex" (lambda()(eval (read (tiny-mapconcat)))))
96 "aA,bB,cC,dD,eE,fF,gG,hH,iI,jJ,kK,lL,mM,nN,oO,pP,qQ,rR,sS,tT,uU,vV,wW,xX"))
97 (should (equal (with-text-value "m97,120stringxupcasex)x" (lambda()(eval (read (tiny-mapconcat)))))
98 "aAa,bBb,cCc,dDd,eEe,fFf,gGg,hHh,iIi,jJj,kKk,lLl,mMm,nNn,oOo,pPp,qQq,rRr,sSs,tTt,uUu,vVv,wWw,xXx"))
99 (should (equal (with-text-value "m10|%(+ x x) and %(* x x) and %s" (lambda()(eval (read (tiny-mapconcat)))))
100 "0 and 0 and 0 2 and 1 and 1 4 and 4 and 2 6 and 9 and 3 8 and 16 and 4 10 and 25 and 5 12 and 36 and 6 14 and 49 and 7 16 and 64 and 8 18 and 81 and 9 20 and 100 and 10"))
101 (should (equal (with-text-value "m10*2+3x" (lambda()(eval (read (tiny-mapconcat)))))
102 "6 8 10 12 14 16 18 20 22 24 26"))
103 (should (equal (with-text-value "m10expx" (lambda()(eval (read (tiny-mapconcat)))))
104 "1.0 2.718281828459045 7.38905609893065 20.085536923187668 54.598150033144236 148.4131591025766 403.4287934927351 1096.6331584284585 2980.9579870417283 8103.083927575384 22026.465794806718"))
105 (should (equal (with-text-value "m5 20expx%014.2f" (lambda()(eval (read (tiny-mapconcat)))))
106 "00000000148.41 00000000403.43 00000001096.63 00000002980.96 00000008103.08 00000022026.47 00000059874.14 00000162754.79 00000442413.39 00001202604.28 00003269017.37 00008886110.52 00024154952.75 00065659969.14 00178482300.96 00485165195.41"))
107 (should (equal (with-text-value "m, 7|0x%02x" (lambda()(eval (read (tiny-mapconcat)))))
108 "0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07"))
109 (should (equal (with-text-value "m1\\n14|*** TODO http://emacsrocks.com/e%02d.html" (lambda()(eval (read (tiny-mapconcat)))))
110 "*** TODO http://emacsrocks.com/e01.html
111 *** TODO http://emacsrocks.com/e02.html
112 *** TODO http://emacsrocks.com/e03.html
113 *** TODO http://emacsrocks.com/e04.html
114 *** TODO http://emacsrocks.com/e05.html
115 *** TODO http://emacsrocks.com/e06.html
116 *** TODO http://emacsrocks.com/e07.html
117 *** TODO http://emacsrocks.com/e08.html
118 *** TODO http://emacsrocks.com/e09.html
119 *** TODO http://emacsrocks.com/e10.html
120 *** TODO http://emacsrocks.com/e11.html
121 *** TODO http://emacsrocks.com/e12.html
122 *** TODO http://emacsrocks.com/e13.html
123 *** TODO http://emacsrocks.com/e14.html"))
124 (should (equal (with-text-value "m1\\n10|convert img%s.jpg -monochrome -resize 50%% -rotate 180 img%s_mono.pdf" (lambda()(eval (read (tiny-mapconcat)))))
125 "convert img1.jpg -monochrome -resize 50% -rotate 180 img1_mono.pdf
126 convert img2.jpg -monochrome -resize 50% -rotate 180 img2_mono.pdf
127 convert img3.jpg -monochrome -resize 50% -rotate 180 img3_mono.pdf
128 convert img4.jpg -monochrome -resize 50% -rotate 180 img4_mono.pdf
129 convert img5.jpg -monochrome -resize 50% -rotate 180 img5_mono.pdf
130 convert img6.jpg -monochrome -resize 50% -rotate 180 img6_mono.pdf
131 convert img7.jpg -monochrome -resize 50% -rotate 180 img7_mono.pdf
132 convert img8.jpg -monochrome -resize 50% -rotate 180 img8_mono.pdf
133 convert img9.jpg -monochrome -resize 50% -rotate 180 img9_mono.pdf
134 convert img10.jpg -monochrome -resize 50% -rotate 180 img10_mono.pdf"))
135 (should (equal (with-text-value "m\\n;; 16list*xxx)*xx%s:%s:%s" (lambda()(eval (read (tiny-mapconcat)))))
136 "0:0:0
137 ;; 1:1:1
138 ;; 8:4:2
139 ;; 27:9:3
140 ;; 64:16:4
141 ;; 125:25:5
142 ;; 216:36:6
143 ;; 343:49:7
144 ;; 512:64:8
145 ;; 729:81:9
146 ;; 1000:100:10
147 ;; 1331:121:11
148 ;; 1728:144:12
149 ;; 2197:169:13
150 ;; 2744:196:14
151 ;; 3375:225:15
152 ;; 4096:256:16"))
153 (should (equal (with-text-value "m\\n8|**** TODO Learning from Data Week %(+ x 2)\\nSCHEDULED: <%(date \"Oct 7\" (* x 7))> DEADLINE: <%(date \"Oct 14\" (* x 7))>"
154 (lambda()(eval (read (tiny-mapconcat)))))
155 "**** TODO Learning from Data Week 2
156 SCHEDULED: <2015-10-07 Wed> DEADLINE: <2015-10-14 Wed>
157 **** TODO Learning from Data Week 3
158 SCHEDULED: <2015-10-14 Wed> DEADLINE: <2015-10-21 Wed>
159 **** TODO Learning from Data Week 4
160 SCHEDULED: <2015-10-21 Wed> DEADLINE: <2015-10-28 Wed>
161 **** TODO Learning from Data Week 5
162 SCHEDULED: <2015-10-28 Wed> DEADLINE: <2015-11-04 Wed>
163 **** TODO Learning from Data Week 6
164 SCHEDULED: <2015-11-04 Wed> DEADLINE: <2015-11-11 Wed>
165 **** TODO Learning from Data Week 7
166 SCHEDULED: <2015-11-11 Wed> DEADLINE: <2015-11-18 Wed>
167 **** TODO Learning from Data Week 8
168 SCHEDULED: <2015-11-18 Wed> DEADLINE: <2015-11-25 Wed>
169 **** TODO Learning from Data Week 9
170 SCHEDULED: <2015-11-25 Wed> DEADLINE: <2015-12-02 Wed>
171 **** TODO Learning from Data Week 10
172 SCHEDULED: <2015-12-02 Wed> DEADLINE: <2015-12-09 Wed>"))
173 (should (string= (with-text-value "m\\n4|**** TODO Classical Mechanics Week %(+ x 5)\\nSCHEDULED: <%(date \"Oct 15\" (* x 7))> DEADLINE: <%(date \"Oct 23\" (* x 7))>"
174 (lambda()(eval (read (tiny-mapconcat)))))
175 "**** TODO Classical Mechanics Week 5
176 SCHEDULED: <2015-10-15 Thu> DEADLINE: <2015-10-23 Fri>
177 **** TODO Classical Mechanics Week 6
178 SCHEDULED: <2015-10-22 Thu> DEADLINE: <2015-10-30 Fri>
179 **** TODO Classical Mechanics Week 7
180 SCHEDULED: <2015-10-29 Thu> DEADLINE: <2015-11-06 Fri>
181 **** TODO Classical Mechanics Week 8
182 SCHEDULED: <2015-11-05 Thu> DEADLINE: <2015-11-13 Fri>
183 **** TODO Classical Mechanics Week 9
184 SCHEDULED: <2015-11-12 Thu> DEADLINE: <2015-11-20 Fri>"))
185 (should (string= (with-text-value "m7|%(expt 2 x)"
186 (lambda()(eval (read (tiny-mapconcat)))))
187 "1 2 4 8 16 32 64 128"))
188 (should (string= (with-text-value "m\\n25+?ax|(\"%c\")"
189 (lambda()(eval (read (tiny-mapconcat)))))
190 "(\"a\")\n(\"b\")\n(\"c\")\n(\"d\")\n(\"e\")\n(\"f\")\n(\"g\")\n(\"h\")\n(\"i\")\n(\"j\")\n(\"k\")\n(\"l\")\n(\"m\")\n(\"n\")\n(\"o\")\n(\"p\")\n(\"q\")\n(\"r\")\n(\"s\")\n(\"t\")\n(\"u\")\n(\"v\")\n(\"w\")\n(\"x\")\n(\"y\")\n(\"z\")")))
191
192 (ert-deftest tiny-replace-this-sexp ()
193 (should (equal (with-text-value "(mapcar (lambda (x) (* x x)) '(1 2 3))"
194 (lambda()(goto-char 16)(tiny-replace-this-sexp)(buffer-string)))
195 "(1 4 9)"))
196 (should (equal (with-text-value "(mapcar (lambda (x) (* x x)) '(1 2 3))"
197 (lambda()(goto-char 2)(tiny-replace-this-sexp)(buffer-string)))
198 "(1 4 9)")))
199
200 (ert-deftest tiny-tokenize ()
201 (should (equal (tiny-tokenize "stringxx") "(string x x)"))
202 (should (equal (tiny-tokenize "*2+xxx") "(* 2 (+ x x x))"))
203 (should (equal (tiny-tokenize "*2+xxx") "(* 2 (+ x x x))"))
204 (should (equal (tiny-tokenize "*2+xx)x") "(* 2 (+ x x) x)"))
205 (should (equal (tiny-tokenize "string x") "(string x)"))
206 (should (equal (tiny-tokenize "(string x)") "(string x)"))
207 (should (equal (tiny-tokenize "string x)") "(string x)"))
208 (should (equal (tiny-tokenize "stringxupcasex)x") "(string x (upcase x) x)"))
209 (should (equal (tiny-tokenize "(stringxupcasex)x") "(string x (upcase x) x)"))
210 (should (equal (tiny-tokenize "(string xupcasex)x") "(string x (upcase x) x)"))
211 (should (equal (tiny-tokenize "(string x upcasex)x") "(string x (upcase x) x)"))
212 (should (equal (tiny-tokenize "(string x upcase x) x") "(string x (upcase x) x)"))
213 (should (equal (tiny-tokenize "(string x (upcase x) x") "(string x (upcase x) x)"))
214 (should (equal (tiny-tokenize "(string x (upcase x) x)") "(string x (upcase x) x)")))
215
216 (provide 'tiny-test)