test-ob-lilypond.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. ;;; test-ob-lilypond.el --- tests for ob-lilypond.el
  2. ;; Copyright (c) 2010-2012 Martyn Jago
  3. ;; Authors: Martyn Jago
  4. ;; Released under the GNU General Public License version 3
  5. ;; see: http://www.gnu.org/licenses/gpl-3.0.html
  6. ;;; Code:
  7. (unless (featurep 'ob-lilypond)
  8. (signal 'missing-test-dependency "Support for Lilypond code blocks"))
  9. (save-excursion
  10. (set-buffer (get-buffer-create "test-ob-lilypond.el"))
  11. (setq ly-here
  12. (file-name-directory
  13. (or load-file-name (buffer-file-name)))))
  14. (ert-deftest ob-lilypond/assert ()
  15. (should t))
  16. (ert-deftest ob-lilypond/feature-provision ()
  17. (should (featurep 'ob-lilypond)))
  18. (ert-deftest ob-lilypond/check-lilypond-alias ()
  19. (should (fboundp 'lilypond-mode)))
  20. (ert-deftest ob-lilypond/org-babel-tangle-lang-exts ()
  21. (let ((found nil)
  22. (list org-babel-tangle-lang-exts))
  23. (while list
  24. (when (equal (car list) '("LilyPond" . "ly"))
  25. (setq found t))
  26. (setq list (cdr list)))
  27. (should found)))
  28. (ert-deftest ob-lilypond/org-babel-prep-session:lilypond ()
  29. (should-error (org-babel-prep-session:lilypond nil nil))
  30. :type 'error)
  31. (ert-deftest ob-lilypond/ly-compile-lilyfile ()
  32. (should (equal
  33. `(,(ly-determine-ly-path) ;program
  34. nil ;infile
  35. "*lilypond*" ;buffer
  36. t ;display
  37. ,(if ly-gen-png "--png" "") ;&rest...
  38. ,(if ly-gen-html "--html" "")
  39. ,(if ly-gen-pdf "--pdf" "")
  40. ,(if ly-use-eps "-dbackend=eps" "")
  41. ,(if ly-gen-svg "-dbackend=svg" "")
  42. "--output=test-file"
  43. "test-file.ly")
  44. (ly-compile-lilyfile "test-file.ly" t))))
  45. (ert-deftest ob-lilypond/ly-compile-post-tangle ()
  46. (should (boundp 'ly-compile-post-tangle)))
  47. (ert-deftest ob-lilypond/ly-display-pdf-post-tangle ()
  48. (should (boundp 'ly-display-pdf-post-tangle)))
  49. (ert-deftest ob-lilypond/ly-play-midi-post-tangle ()
  50. (should (boundp 'ly-play-midi-post-tangle)))
  51. (ert-deftest ob-lilypond/ly-OSX-ly-path ()
  52. (should (boundp 'ly-OSX-ly-path))
  53. (should (stringp ly-OSX-ly-path)))
  54. (ert-deftest ob-lilypond/ly-OSX-pdf-path ()
  55. (should (boundp 'ly-OSX-pdf-path))
  56. (should (stringp ly-OSX-pdf-path)))
  57. (ert-deftest ob-lilypond/ly-OSX-midi-path ()
  58. (should (boundp 'ly-OSX-midi-path))
  59. (should (stringp ly-OSX-midi-path)))
  60. (ert-deftest ob-lilypond/ly-nix-ly-path ()
  61. (should (boundp 'ly-nix-ly-path))
  62. (should (stringp ly-nix-ly-path)))
  63. (ert-deftest ob-lilypond/ly-nix-pdf-path ()
  64. (should (boundp 'ly-nix-pdf-path))
  65. (should (stringp ly-nix-pdf-path)))
  66. (ert-deftest ob-lilypond/ly-nix-midi-path ()
  67. (should (boundp 'ly-nix-midi-path))
  68. (should (stringp ly-nix-midi-path)))
  69. (ert-deftest ob-lilypond/ly-win32-ly-path ()
  70. (should (boundp 'ly-win32-ly-path))
  71. (should (stringp ly-win32-ly-path)))
  72. (ert-deftest ob-lilypond/ly-win32-pdf-path ()
  73. (should (boundp 'ly-win32-pdf-path))
  74. (should (stringp ly-win32-pdf-path)))
  75. (ert-deftest ob-lilypond/ly-win32-midi-path ()
  76. (should (boundp 'ly-win32-midi-path))
  77. (should (stringp ly-win32-midi-path)))
  78. (ert-deftest ob-lilypond/ly-gen-png ()
  79. (should (boundp 'ly-gen-png)))
  80. (ert-deftest ob-lilypond/ly-gen-svg ()
  81. (should (boundp 'ly-gen-svg)))
  82. (ert-deftest ob-lilypond/ly-gen-html ()
  83. (should (boundp 'ly-gen-html)))
  84. (ert-deftest ob-lilypond/ly-gen-html ()
  85. (should (boundp 'ly-gen-pdf)))
  86. (ert-deftest ob-lilypond/use-eps ()
  87. (should (boundp 'ly-use-eps)))
  88. (ert-deftest ob-lilypond/ly-arrange-mode ()
  89. (should (boundp 'ly-arrange-mode)))
  90. ;; (ert-deftest ob-lilypond/org-babel-default-header-args:lilypond ()
  91. ;; (should (equal '((:tangle . "yes")
  92. ;; (:noweb . "yes")
  93. ;; (:results . "silent")
  94. ;; (:comments . "yes"))
  95. ;; org-babel-default-header-args:lilypond)))
  96. ;;TODO finish...
  97. (ert-deftest ob-lilypond/org-babel-expand-body:lilypond ()
  98. (should (equal "This is a test"
  99. (org-babel-expand-body:lilypond "This is a test" ()))))
  100. ;;TODO (ert-deftest ly-test-org-babel-execute:lilypond ())
  101. (ert-deftest ob-lilypond/ly-check-for-compile-error ()
  102. (set-buffer (get-buffer-create "*lilypond*"))
  103. (erase-buffer)
  104. (should (not (ly-check-for-compile-error nil t)))
  105. (insert-file-contents (concat ly-here
  106. "../examples/ob-lilypond-test.error")
  107. nil nil nil t)
  108. (goto-char (point-min))
  109. (should (ly-check-for-compile-error nil t))
  110. (kill-buffer "*lilypond*"))
  111. (ert-deftest ob-lilypond/ly-process-compile-error ()
  112. (find-file-other-window (concat
  113. ly-here
  114. "../examples/ob-lilypond-broken.org"))
  115. (set-buffer (get-buffer-create "*lilypond*"))
  116. (insert-file-contents (concat
  117. ly-here
  118. "../examples/ob-lilypond-test.error")
  119. nil nil nil t)
  120. (goto-char (point-min))
  121. (search-forward "error:" nil t)
  122. (should-error
  123. (ly-process-compile-error (concat
  124. ly-here
  125. "../examples/ob-lilypond-broken.ly"))
  126. :type 'error)
  127. (set-buffer "ob-lilypond-broken.org")
  128. (should (equal 238 (point)))
  129. (exchange-point-and-mark)
  130. (should (equal (+ 238 (length "line 25")) (point)))
  131. (kill-buffer "*lilypond*")
  132. (kill-buffer "ob-lilypond-broken.org"))
  133. (ert-deftest ob-lilypond/ly-mark-error-line ()
  134. (let ((file-name (concat
  135. ly-here
  136. "../examples/ob-lilypond-broken.org"))
  137. (expected-point-min 198)
  138. (expected-point-max 205)
  139. (line "line 20"))
  140. (find-file-other-window file-name)
  141. (ly-mark-error-line file-name line)
  142. (should (equal expected-point-min (point)))
  143. (exchange-point-and-mark)
  144. (should (= expected-point-max (point)))
  145. (kill-buffer (file-name-nondirectory file-name))))
  146. (ert-deftest ob-lilypond/ly-parse-line-num ()
  147. (with-temp-buffer
  148. (insert-file-contents (concat
  149. ly-here
  150. "../examples/ob-lilypond-test.error")
  151. nil nil nil t)
  152. (goto-char (point-min))
  153. (search-forward "error:")
  154. (should (equal 25 (ly-parse-line-num (current-buffer))))))
  155. (ert-deftest ob-lilypond/ly-parse-error-line ()
  156. (let ((ly-file (concat
  157. ly-here
  158. "../examples/ob-lilypond-broken.ly")))
  159. (should (equal "line 20"
  160. (ly-parse-error-line ly-file 20)))
  161. (should (not (ly-parse-error-line ly-file 0)))))
  162. (ert-deftest ob-lilypond/ly-attempt-to-open-pdf ()
  163. (let ((post-tangle ly-display-pdf-post-tangle)
  164. (ly-file (concat
  165. ly-here
  166. "../examples/ob-lilypond-test.ly"))
  167. (pdf-file (concat
  168. ly-here
  169. "../examples/ob-lilypond-test.pdf")))
  170. (setq ly-display-pdf-post-tangle t)
  171. (when (not (file-exists-p pdf-file))
  172. (set-buffer (get-buffer-create (file-name-nondirectory pdf-file)))
  173. (write-file pdf-file))
  174. (should (equal
  175. (concat
  176. (ly-determine-pdf-path) " " pdf-file)
  177. (ly-attempt-to-open-pdf ly-file t)))
  178. (delete-file pdf-file)
  179. (kill-buffer (file-name-nondirectory pdf-file))
  180. (should (equal
  181. "No pdf file generated so can't display!"
  182. (ly-attempt-to-open-pdf pdf-file)))
  183. (setq ly-display-pdf-post-tangle post-tangle)))
  184. (ert-deftest ob-lilypond/ly-attempt-to-play-midi ()
  185. (let ((post-tangle ly-play-midi-post-tangle)
  186. (ly-file (concat
  187. ly-here
  188. "../examples/ob-lilypond-test.ly"))
  189. (midi-file (concat
  190. ly-here
  191. "../examples/ob-lilypond-test.midi")))
  192. (setq ly-play-midi-post-tangle t)
  193. (when (not (file-exists-p midi-file))
  194. (set-buffer (get-buffer-create (file-name-nondirectory midi-file)))
  195. (write-file midi-file))
  196. (should (equal
  197. (concat
  198. (ly-determine-midi-path) " " midi-file)
  199. (ly-attempt-to-play-midi ly-file t)))
  200. (delete-file midi-file)
  201. (kill-buffer (file-name-nondirectory midi-file))
  202. (should (equal
  203. "No midi file generated so can't play!"
  204. (ly-attempt-to-play-midi midi-file)))
  205. (setq ly-play-midi-post-tangle post-tangle)))
  206. (ert-deftest ob-lilypond/ly-determine-ly-path ()
  207. (should (equal ly-OSX-ly-path
  208. (ly-determine-ly-path "darwin")))
  209. (should (equal ly-win32-ly-path
  210. (ly-determine-ly-path "win32")))
  211. (should (equal ly-nix-ly-path
  212. (ly-determine-ly-path "nix"))))
  213. (ert-deftest ob-lilypond/ly-determine-pdf-path ()
  214. (should (equal ly-OSX-pdf-path
  215. (ly-determine-pdf-path "darwin")))
  216. (should (equal ly-win32-pdf-path
  217. (ly-determine-pdf-path "win32")))
  218. (should (equal ly-nix-pdf-path
  219. (ly-determine-pdf-path "nix"))))
  220. (ert-deftest ob-lilypond/ly-determine-midi-path ()
  221. (should (equal ly-OSX-midi-path
  222. (ly-determine-midi-path "darwin")))
  223. (should (equal ly-win32-midi-path
  224. (ly-determine-midi-path "win32")))
  225. (should (equal ly-nix-midi-path
  226. (ly-determine-midi-path "nix"))))
  227. (ert-deftest ob-lilypond/ly-toggle-midi-play-toggles-flag ()
  228. (if ly-play-midi-post-tangle
  229. (progn
  230. (ly-toggle-midi-play)
  231. (should (not ly-play-midi-post-tangle))
  232. (ly-toggle-midi-play)
  233. (should ly-play-midi-post-tangle))
  234. (ly-toggle-midi-play)
  235. (should ly-play-midi-post-tangle)
  236. (ly-toggle-midi-play)
  237. (should (not ly-play-midi-post-tangle))))
  238. (ert-deftest ob-lilypond/ly-toggle-pdf-display-toggles-flag ()
  239. (if ly-display-pdf-post-tangle
  240. (progn
  241. (ly-toggle-pdf-display)
  242. (should (not ly-display-pdf-post-tangle))
  243. (ly-toggle-pdf-display)
  244. (should ly-display-pdf-post-tangle))
  245. (ly-toggle-pdf-display)
  246. (should ly-display-pdf-post-tangle)
  247. (ly-toggle-pdf-display)
  248. (should (not ly-display-pdf-post-tangle))))
  249. (ert-deftest ob-lilypond/ly-toggle-pdf-generation-toggles-flag ()
  250. (if ly-gen-pdf
  251. (progn
  252. (ly-toggle-pdf-generation)
  253. (should (not ly-gen-pdf))
  254. (ly-toggle-pdf-generation)
  255. (should ly-gen-pdf))
  256. (ly-toggle-pdf-generation)
  257. (should ly-gen-pdf)
  258. (ly-toggle-pdf-generation)
  259. (should (not ly-gen-pdf))))
  260. (ert-deftest ob-lilypond/ly-toggle-arrange-mode ()
  261. (if ly-arrange-mode
  262. (progn
  263. (ly-toggle-arrange-mode)
  264. (should (not ly-arrange-mode))
  265. (ly-toggle-arrange-mode)
  266. (should ly-arrange-mode))
  267. (ly-toggle-arrange-mode)
  268. (should ly-arrange-mode)
  269. (ly-toggle-arrange-mode)
  270. (should (not ly-arrange-mode))))
  271. (ert-deftest ob-lilypond/ly-toggle-png-generation-toggles-flag ()
  272. (if ly-gen-png
  273. (progn
  274. (ly-toggle-png-generation)
  275. (should (not ly-gen-png))
  276. (ly-toggle-png-generation)
  277. (should ly-gen-png))
  278. (ly-toggle-png-generation)
  279. (should ly-gen-png)
  280. (ly-toggle-png-generation)
  281. (should (not ly-gen-png))))
  282. (ert-deftest ob-lilypond/ly-toggle-html-generation-toggles-flag ()
  283. (if ly-gen-html
  284. (progn
  285. (ly-toggle-html-generation)
  286. (should (not ly-gen-html))
  287. (ly-toggle-html-generation)
  288. (should ly-gen-html))
  289. (ly-toggle-html-generation)
  290. (should ly-gen-html)
  291. (ly-toggle-html-generation)
  292. (should (not ly-gen-html))))
  293. (ert-deftest ob-lilypond/ly-switch-extension-with-extensions ()
  294. (should (equal "test-name.xyz"
  295. (ly-switch-extension "test-name" ".xyz")))
  296. (should (equal "test-name.xyz"
  297. (ly-switch-extension "test-name.abc" ".xyz")))
  298. (should (equal "test-name"
  299. (ly-switch-extension "test-name.abc" ""))))
  300. (ert-deftest ob-lilypond/ly-switch-extension-with-paths ()
  301. (should (equal "/some/path/to/test-name.xyz"
  302. (ly-switch-extension "/some/path/to/test-name" ".xyz"))))
  303. (ert-deftest ob-lilypond/ly-get-header-args ()
  304. (should (equal '((:tangle . "yes")
  305. (:noweb . "yes")
  306. (:results . "silent")
  307. (:cache . "yes")
  308. (:comments . "yes"))
  309. (ly-set-header-args t)))
  310. (should (equal '((:results . "file")
  311. (:exports . "results"))
  312. (ly-set-header-args nil))))
  313. (ert-deftest ob-lilypond/ly-set-header-args ()
  314. (ly-set-header-args t)
  315. (should (equal '((:tangle . "yes")
  316. (:noweb . "yes")
  317. (:results . "silent")
  318. (:cache . "yes")
  319. (:comments . "yes"))
  320. org-babel-default-header-args:lilypond))
  321. (ly-set-header-args nil)
  322. (should (equal '((:results . "file")
  323. (:exports . "results"))
  324. org-babel-default-header-args:lilypond)))
  325. (provide 'test-ob-lilypond)
  326. ;;; test-ob-lilypond.el ends here