Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
12 KiB

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