test-ob-lilypond.el 16 KB

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