test-ob-lilypond.el 13 KB

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