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