test-ob-lilypond.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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-pdf ()
  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 (string-prefix-p "No pdf file generated"
  240. (org-babel-lilypond-attempt-to-open-pdf pdf-file)))
  241. (setq org-babel-lilypond-display-pdf-post-tangle post-tangle)))
  242. (ert-deftest ob-lilypond/ly-attempt-to-play-midi ()
  243. (let ((post-tangle org-babel-lilypond-play-midi-post-tangle)
  244. (org-babel-lilypond-file (concat
  245. org-babel-lilypond-here
  246. "../examples/ob-lilypond-test.ly"))
  247. (midi-file (concat
  248. org-babel-lilypond-here
  249. "../examples/ob-lilypond-test.midi")))
  250. (setq org-babel-lilypond-play-midi-post-tangle t)
  251. (when (not (file-exists-p midi-file))
  252. (set-buffer (get-buffer-create (file-name-nondirectory midi-file)))
  253. (write-file midi-file))
  254. (should (equal
  255. (concat
  256. org-babel-lilypond-midi-command " " midi-file)
  257. (org-babel-lilypond-attempt-to-play-midi org-babel-lilypond-file t)))
  258. (delete-file midi-file)
  259. (kill-buffer (file-name-nondirectory midi-file))
  260. (should (string-prefix-p
  261. "No midi file generated"
  262. (org-babel-lilypond-attempt-to-play-midi midi-file)))
  263. (setq org-babel-lilypond-play-midi-post-tangle post-tangle)))
  264. (ert-deftest ob-lilypond/ly-toggle-midi-play-toggles-flag ()
  265. (if org-babel-lilypond-play-midi-post-tangle
  266. (progn
  267. (org-babel-lilypond-toggle-midi-play)
  268. (should (not org-babel-lilypond-play-midi-post-tangle))
  269. (org-babel-lilypond-toggle-midi-play)
  270. (should org-babel-lilypond-play-midi-post-tangle))
  271. (org-babel-lilypond-toggle-midi-play)
  272. (should org-babel-lilypond-play-midi-post-tangle)
  273. (org-babel-lilypond-toggle-midi-play)
  274. (should (not org-babel-lilypond-play-midi-post-tangle))))
  275. (ert-deftest ob-lilypond/ly-toggle-pdf-display-toggles-flag ()
  276. (if org-babel-lilypond-display-pdf-post-tangle
  277. (progn
  278. (org-babel-lilypond-toggle-pdf-display)
  279. (should (not org-babel-lilypond-display-pdf-post-tangle))
  280. (org-babel-lilypond-toggle-pdf-display)
  281. (should org-babel-lilypond-display-pdf-post-tangle))
  282. (org-babel-lilypond-toggle-pdf-display)
  283. (should org-babel-lilypond-display-pdf-post-tangle)
  284. (org-babel-lilypond-toggle-pdf-display)
  285. (should (not org-babel-lilypond-display-pdf-post-tangle))))
  286. (ert-deftest ob-lilypond/ly-toggle-pdf-generation-toggles-flag ()
  287. (if org-babel-lilypond-gen-pdf
  288. (progn
  289. (org-babel-lilypond-toggle-pdf-generation)
  290. (should (not org-babel-lilypond-gen-pdf))
  291. (org-babel-lilypond-toggle-pdf-generation)
  292. (should org-babel-lilypond-gen-pdf))
  293. (org-babel-lilypond-toggle-pdf-generation)
  294. (should org-babel-lilypond-gen-pdf)
  295. (org-babel-lilypond-toggle-pdf-generation)
  296. (should (not org-babel-lilypond-gen-pdf))))
  297. (ert-deftest ob-lilypond/ly-toggle-arrange-mode ()
  298. (if org-babel-lilypond-arrange-mode
  299. (progn
  300. (org-babel-lilypond-toggle-arrange-mode)
  301. (should (not org-babel-lilypond-arrange-mode))
  302. (org-babel-lilypond-toggle-arrange-mode)
  303. (should org-babel-lilypond-arrange-mode))
  304. (org-babel-lilypond-toggle-arrange-mode)
  305. (should org-babel-lilypond-arrange-mode)
  306. (org-babel-lilypond-toggle-arrange-mode)
  307. (should (not org-babel-lilypond-arrange-mode))))
  308. (ert-deftest ob-lilypond/ly-toggle-png-generation-toggles-flag ()
  309. (if org-babel-lilypond-gen-png
  310. (progn
  311. (org-babel-lilypond-toggle-png-generation)
  312. (should (not org-babel-lilypond-gen-png))
  313. (org-babel-lilypond-toggle-png-generation)
  314. (should org-babel-lilypond-gen-png))
  315. (org-babel-lilypond-toggle-png-generation)
  316. (should org-babel-lilypond-gen-png)
  317. (org-babel-lilypond-toggle-png-generation)
  318. (should (not org-babel-lilypond-gen-png))))
  319. (ert-deftest ob-lilypond/ly-toggle-html-generation-toggles-flag ()
  320. (if org-babel-lilypond-gen-html
  321. (progn
  322. (org-babel-lilypond-toggle-html-generation)
  323. (should (not org-babel-lilypond-gen-html))
  324. (org-babel-lilypond-toggle-html-generation)
  325. (should org-babel-lilypond-gen-html))
  326. (org-babel-lilypond-toggle-html-generation)
  327. (should org-babel-lilypond-gen-html)
  328. (org-babel-lilypond-toggle-html-generation)
  329. (should (not org-babel-lilypond-gen-html))))
  330. (ert-deftest ob-lilypond/ly-switch-extension-with-extensions ()
  331. (should (equal "test-name.xyz"
  332. (org-babel-lilypond-switch-extension "test-name" ".xyz")))
  333. (should (equal "test-name.xyz"
  334. (org-babel-lilypond-switch-extension "test-name.abc" ".xyz")))
  335. (should (equal "test-name"
  336. (org-babel-lilypond-switch-extension "test-name.abc" ""))))
  337. (ert-deftest ob-lilypond/ly-switch-extension-with-paths ()
  338. (should (equal "/some/path/to/test-name.xyz"
  339. (org-babel-lilypond-switch-extension "/some/path/to/test-name" ".xyz"))))
  340. (ert-deftest ob-lilypond/ly-get-header-args ()
  341. (should (equal '((:tangle . "yes")
  342. (:noweb . "yes")
  343. (:results . "silent")
  344. (:cache . "yes")
  345. (:comments . "yes"))
  346. (org-babel-lilypond-set-header-args t)))
  347. (should (equal '((:results . "file")
  348. (:exports . "results"))
  349. (org-babel-lilypond-set-header-args nil))))
  350. (ert-deftest ob-lilypond/ly-set-header-args ()
  351. (org-babel-lilypond-set-header-args t)
  352. (should (equal '((:tangle . "yes")
  353. (:noweb . "yes")
  354. (:results . "silent")
  355. (:cache . "yes")
  356. (:comments . "yes"))
  357. org-babel-default-header-args:lilypond))
  358. (org-babel-lilypond-set-header-args nil)
  359. (should (equal '((:results . "file")
  360. (:exports . "results"))
  361. org-babel-default-header-args:lilypond)))
  362. (provide 'test-ob-lilypond)
  363. ;;; test-ob-lilypond.el ends here