test-ob-lilypond.el 13 KB

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