]> code.delx.au - gnu-emacs-elpa/blob - tiny-test.el
tiny-test.el: Update
[gnu-emacs-elpa] / tiny-test.el
1 (when (require 'undercover nil t)
2 (undercover "tiny.el"))
3
4 (require 'tiny nil t)
5
6 (defun with-text-value (txt fn &rest args)
7 "Return the result of (apply 'FN ARGS), in a temp buffer with TXT,
8 with point at the end of TXT."
9 (with-temp-buffer
10 (insert txt)
11 (apply fn args)))
12
13 (ert-deftest tiny-mapconcat-parse ()
14 (should (equal (with-text-value "m10" #'tiny-mapconcat-parse)
15 '(nil nil "10" nil nil)))
16 (should (equal (with-text-value "m5%x" #'tiny-mapconcat-parse)
17 '(nil nil "5" nil "%x")))
18 (should (equal (with-text-value "m5 10" #'tiny-mapconcat-parse)
19 '("5" " " "10" nil nil)))
20 (should (equal (with-text-value "m5,10" #'tiny-mapconcat-parse)
21 '("5" "," "10" nil nil)))
22 (should (equal (with-text-value "m5 10*xx" #'tiny-mapconcat-parse)
23 '("5" " " "10" "(* x x)" nil)))
24 (should (equal (with-text-value "m5 10*xx%x" #'tiny-mapconcat-parse)
25 '("5" " " "10" "(* x x)" "%x")))
26 (should (equal (with-text-value "m5 10*xx|0x%x" #'tiny-mapconcat-parse)
27 '("5" " " "10" "(* x x)" "0x%x")))
28 (should (equal (with-text-value "m25+x?a%c" #'tiny-mapconcat-parse)
29 '(nil nil "25" "(+ x 97)" "%c")))
30 (should (equal (with-text-value "m25+x?A%c" #'tiny-mapconcat-parse)
31 '(nil nil "25" "(+ x 65)" "%c")))
32 (should (equal (with-text-value "m97,122stringx" #'tiny-mapconcat-parse)
33 '("97" "," "122" "(string x)" nil)))
34 (should (equal (with-text-value "m97,122stringxx" #'tiny-mapconcat-parse)
35 '("97" "," "122" "(string x x)" nil)))
36 (should (equal (with-text-value "m97,120stringxupcasex" #'tiny-mapconcat-parse)
37 '("97" "," "120" "(string x (upcase x))" nil)))
38 (should (equal (with-text-value "m97,120stringxupcasex)x" #'tiny-mapconcat-parse)
39 '("97" "," "120" "(string x (upcase x) x)" nil)))
40 (should (equal (with-text-value "m\\n;; 10|%(+ x x) and %(* x x) and %s" #'tiny-mapconcat-parse)
41 '(nil "\\n;; " "10" nil "%(+ x x) and %(* x x) and %s")))
42 (should (equal (with-text-value "m10|%0.2f" #'tiny-mapconcat-parse)
43 '(nil nil "10" nil "%0.2f"))))
44
45 (ert-deftest tiny-extract-sexps ()
46 (should (equal (tiny-extract-sexps "expr1 %(+ x x), nothing %% char %c, hex %x, and expr2 %(* x x), float %0.2f and sym %s")
47 '("expr1 %s, nothing %% char %c, hex %x, and expr2 %s, float %0.2f and sym %s"
48 "(+ x x)" nil nil "(* x x)" nil nil)))
49 (should (equal (tiny-extract-sexps "m1\n5| (%c(+ x ?a -1)) %0.1f(* x 0.2)")
50 '("m1
51 5| (%c) %0.1f" "(+ x ?a -1)" "(* x 0.2)"))))
52
53 (ert-deftest tiny-mapconcat ()
54 (should (equal (with-text-value "m10" (lambda()(eval (read (tiny-mapconcat)))))
55 "0 1 2 3 4 5 6 7 8 9 10"))
56 (should (equal (with-text-value "m5 10" (lambda()(eval (read (tiny-mapconcat)))))
57 "5 6 7 8 9 10"))
58 (should (equal (with-text-value "m5 10*xx" (lambda()(eval (read (tiny-mapconcat)))))
59 "25 36 49 64 81 100"))
60 (should (equal (with-text-value "m5 10*xx%x" (lambda()(eval (read (tiny-mapconcat)))))
61 "19 24 31 40 51 64"))
62 (should (equal (with-text-value "m5 10*xx|0x%x" (lambda()(eval (read (tiny-mapconcat)))))
63 "0x19 0x24 0x31 0x40 0x51 0x64"))
64 (should (equal (with-text-value "m25+x?a%c" (lambda()(eval (read (tiny-mapconcat)))))
65 "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"))
66 (should (equal (with-text-value "m25+x?A%c" (lambda()(eval (read (tiny-mapconcat)))))
67 "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"))
68 (should (equal (with-text-value "m97,122(string x)" (lambda()(eval (read (tiny-mapconcat)))))
69 "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"))
70 (should (equal (with-text-value "m97,122stringxx" (lambda()(eval (read (tiny-mapconcat)))))
71 "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"))
72 (should (equal (with-text-value "m97,120stringxupcasex" (lambda()(eval (read (tiny-mapconcat)))))
73 "aA,bB,cC,dD,eE,fF,gG,hH,iI,jJ,kK,lL,mM,nN,oO,pP,qQ,rR,sS,tT,uU,vV,wW,xX"))
74 (should (equal (with-text-value "m97,120stringxupcasex)x" (lambda()(eval (read (tiny-mapconcat)))))
75 "aAa,bBb,cCc,dDd,eEe,fFf,gGg,hHh,iIi,jJj,kKk,lLl,mMm,nNn,oOo,pPp,qQq,rRr,sSs,tTt,uUu,vVv,wWw,xXx"))
76 (should (equal (with-text-value "m10|%(+ x x) and %(* x x) and %s" (lambda()(eval (read (tiny-mapconcat)))))
77 "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"))
78 (should (equal (with-text-value "m10*2+3x" (lambda()(eval (read (tiny-mapconcat)))))
79 "6 8 10 12 14 16 18 20 22 24 26"))
80 (should (equal (with-text-value "m10expx" (lambda()(eval (read (tiny-mapconcat)))))
81 "1.0 2.718281828459045 7.38905609893065 20.085536923187668 54.598150033144236 148.4131591025766 403.4287934927351 1096.6331584284585 2980.9579870417283 8103.083927575384 22026.465794806718"))
82 (should (equal (with-text-value "m5 20expx%014.2f" (lambda()(eval (read (tiny-mapconcat)))))
83 "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"))
84 (should (equal (with-text-value "m, 7|0x%02x" (lambda()(eval (read (tiny-mapconcat)))))
85 "0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07"))
86 (should (equal (with-text-value "m1\\n14|*** TODO http://emacsrocks.com/e%02d.html" (lambda()(eval (read (tiny-mapconcat)))))
87 "*** TODO http://emacsrocks.com/e01.html
88 *** TODO http://emacsrocks.com/e02.html
89 *** TODO http://emacsrocks.com/e03.html
90 *** TODO http://emacsrocks.com/e04.html
91 *** TODO http://emacsrocks.com/e05.html
92 *** TODO http://emacsrocks.com/e06.html
93 *** TODO http://emacsrocks.com/e07.html
94 *** TODO http://emacsrocks.com/e08.html
95 *** TODO http://emacsrocks.com/e09.html
96 *** TODO http://emacsrocks.com/e10.html
97 *** TODO http://emacsrocks.com/e11.html
98 *** TODO http://emacsrocks.com/e12.html
99 *** TODO http://emacsrocks.com/e13.html
100 *** TODO http://emacsrocks.com/e14.html"))
101 (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)))))
102 "convert img1.jpg -monochrome -resize 50% -rotate 180 img1_mono.pdf
103 convert img2.jpg -monochrome -resize 50% -rotate 180 img2_mono.pdf
104 convert img3.jpg -monochrome -resize 50% -rotate 180 img3_mono.pdf
105 convert img4.jpg -monochrome -resize 50% -rotate 180 img4_mono.pdf
106 convert img5.jpg -monochrome -resize 50% -rotate 180 img5_mono.pdf
107 convert img6.jpg -monochrome -resize 50% -rotate 180 img6_mono.pdf
108 convert img7.jpg -monochrome -resize 50% -rotate 180 img7_mono.pdf
109 convert img8.jpg -monochrome -resize 50% -rotate 180 img8_mono.pdf
110 convert img9.jpg -monochrome -resize 50% -rotate 180 img9_mono.pdf
111 convert img10.jpg -monochrome -resize 50% -rotate 180 img10_mono.pdf"))
112 (should (equal (with-text-value "m\\n;; 16list*xxx)*xx%s:%s:%s" (lambda()(eval (read (tiny-mapconcat)))))
113 "0:0:0
114 ;; 1:1:1
115 ;; 8:4:2
116 ;; 27:9:3
117 ;; 64:16:4
118 ;; 125:25:5
119 ;; 216:36:6
120 ;; 343:49:7
121 ;; 512:64:8
122 ;; 729:81:9
123 ;; 1000:100:10
124 ;; 1331:121:11
125 ;; 1728:144:12
126 ;; 2197:169:13
127 ;; 2744:196:14
128 ;; 3375:225:15
129 ;; 4096:256:16"))
130 (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))>"
131 (lambda()(eval (read (tiny-mapconcat)))))
132 "**** TODO Learning from Data Week 2
133 SCHEDULED: <2015-10-07 Wed> DEADLINE: <2015-10-14 Wed>
134 **** TODO Learning from Data Week 3
135 SCHEDULED: <2015-10-14 Wed> DEADLINE: <2015-10-21 Wed>
136 **** TODO Learning from Data Week 4
137 SCHEDULED: <2015-10-21 Wed> DEADLINE: <2015-10-28 Wed>
138 **** TODO Learning from Data Week 5
139 SCHEDULED: <2015-10-28 Wed> DEADLINE: <2015-11-04 Wed>
140 **** TODO Learning from Data Week 6
141 SCHEDULED: <2015-11-04 Wed> DEADLINE: <2015-11-11 Wed>
142 **** TODO Learning from Data Week 7
143 SCHEDULED: <2015-11-11 Wed> DEADLINE: <2015-11-18 Wed>
144 **** TODO Learning from Data Week 8
145 SCHEDULED: <2015-11-18 Wed> DEADLINE: <2015-11-25 Wed>
146 **** TODO Learning from Data Week 9
147 SCHEDULED: <2015-11-25 Wed> DEADLINE: <2015-12-02 Wed>
148 **** TODO Learning from Data Week 10
149 SCHEDULED: <2015-12-02 Wed> DEADLINE: <2015-12-09 Wed>"))
150 (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))>"
151 (lambda()(eval (read (tiny-mapconcat)))))
152 "**** TODO Classical Mechanics Week 5
153 SCHEDULED: <2015-10-15 Thu> DEADLINE: <2015-10-23 Fri>
154 **** TODO Classical Mechanics Week 6
155 SCHEDULED: <2015-10-22 Thu> DEADLINE: <2015-10-30 Fri>
156 **** TODO Classical Mechanics Week 7
157 SCHEDULED: <2015-10-29 Thu> DEADLINE: <2015-11-06 Fri>
158 **** TODO Classical Mechanics Week 8
159 SCHEDULED: <2015-11-05 Thu> DEADLINE: <2015-11-13 Fri>
160 **** TODO Classical Mechanics Week 9
161 SCHEDULED: <2015-11-12 Thu> DEADLINE: <2015-11-20 Fri>"))
162 (should (string= (with-text-value "m7|%(expt 2 x)"
163 (lambda()(eval (read (tiny-mapconcat)))))
164 "1 2 4 8 16 32 64 128"))
165 (should (string= (with-text-value "m\\n25+?ax|(\"%c\")"
166 (lambda()(eval (read (tiny-mapconcat)))))
167 "(\"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\")")))
168
169 (ert-deftest tiny-replace-this-sexp ()
170 (should (equal (with-text-value "(mapcar (lambda (x) (* x x)) '(1 2 3))"
171 (lambda()(goto-char 16)(tiny-replace-this-sexp)(buffer-string)))
172 "(1 4 9)"))
173 (should (equal (with-text-value "(mapcar (lambda (x) (* x x)) '(1 2 3))"
174 (lambda()(goto-char 2)(tiny-replace-this-sexp)(buffer-string)))
175 "(1 4 9)")))
176
177 (ert-deftest tiny-tokenize ()
178 (should (equal (tiny-tokenize "stringxx") "(string x x)"))
179 (should (equal (tiny-tokenize "*2+xxx") "(* 2 (+ x x x))"))
180 (should (equal (tiny-tokenize "*2+xxx") "(* 2 (+ x x x))"))
181 (should (equal (tiny-tokenize "*2+xx)x") "(* 2 (+ x x) x)"))
182 (should (equal (tiny-tokenize "string x") "(string x)"))
183 (should (equal (tiny-tokenize "(string x)") "(string x)"))
184 (should (equal (tiny-tokenize "string x)") "(string x)"))
185 (should (equal (tiny-tokenize "stringxupcasex)x") "(string x (upcase x) x)"))
186 (should (equal (tiny-tokenize "(stringxupcasex)x") "(string x (upcase x) x)"))
187 (should (equal (tiny-tokenize "(string xupcasex)x") "(string x (upcase x) x)"))
188 (should (equal (tiny-tokenize "(string x upcasex)x") "(string x (upcase x) x)"))
189 (should (equal (tiny-tokenize "(string x upcase x) x") "(string x (upcase x) x)"))
190 (should (equal (tiny-tokenize "(string x (upcase x) x") "(string x (upcase x) x)"))
191 (should (equal (tiny-tokenize "(string x (upcase x) x)") "(string x (upcase x) x)")))
192
193 (provide 'tiny-test)