test-ob-lilypond.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. ;;; test-ob-lilypond.el --- tests for ob-lilypond.el
  2. ;; Copyright (c) 2010-2014, 2019 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-gen-png ()
  121. (should (boundp 'org-babel-lilypond-gen-png)))
  122. (ert-deftest ob-lilypond/ly-gen-svg ()
  123. (should (boundp 'org-babel-lilypond-gen-svg)))
  124. (ert-deftest ob-lilypond/ly-gen-html ()
  125. (should (boundp 'org-babel-lilypond-gen-html)))
  126. (ert-deftest ob-lilypond/ly-gen-pdf ()
  127. (should (boundp 'org-babel-lilypond-gen-pdf)))
  128. (ert-deftest ob-lilypond/use-eps ()
  129. (should (boundp 'org-babel-lilypond-use-eps)))
  130. (ert-deftest ob-lilypond/ly-arrange-mode ()
  131. (should (boundp 'org-babel-lilypond-arrange-mode)))
  132. ;; (ert-deftest ob-lilypond/org-babel-default-header-args:lilypond ()
  133. ;; (should (equal '((:tangle . "yes")
  134. ;; (:noweb . "yes")
  135. ;; (:results . "silent")
  136. ;; (:comments . "yes"))
  137. ;; org-babel-default-header-args:lilypond)))
  138. ;;TODO finish...
  139. (ert-deftest ob-lilypond/org-babel-expand-body:lilypond ()
  140. (should (equal "This is a test"
  141. (org-babel-expand-body:lilypond "This is a test" ()))))
  142. ;;TODO (ert-deftest org-babel-lilypond-test-org-babel-execute:lilypond ())
  143. (ert-deftest ob-lilypond/ly-check-for-compile-error ()
  144. (set-buffer (get-buffer-create "*lilypond*"))
  145. (erase-buffer)
  146. (should (not (org-babel-lilypond-check-for-compile-error nil t)))
  147. (insert-file-contents (concat org-babel-lilypond-here
  148. "../examples/ob-lilypond-test.error")
  149. nil nil nil t)
  150. (goto-char (point-min))
  151. (should (org-babel-lilypond-check-for-compile-error nil t))
  152. (kill-buffer "*lilypond*"))
  153. (ert-deftest ob-lilypond/ly-process-compile-error ()
  154. (find-file-other-window (concat
  155. org-babel-lilypond-here
  156. "../examples/ob-lilypond-broken.org"))
  157. (set-buffer (get-buffer-create "*lilypond*"))
  158. (insert-file-contents (concat
  159. org-babel-lilypond-here
  160. "../examples/ob-lilypond-test.error")
  161. nil nil nil t)
  162. (goto-char (point-min))
  163. (search-forward "error:" nil t)
  164. (should-error
  165. (org-babel-lilypond-process-compile-error (concat
  166. org-babel-lilypond-here
  167. "../examples/ob-lilypond-broken.ly"))
  168. :type 'error)
  169. (set-buffer "ob-lilypond-broken.org")
  170. (should (equal 238 (point)))
  171. (exchange-point-and-mark)
  172. (should (equal (+ 238 (length "line 25")) (point)))
  173. (kill-buffer "*lilypond*")
  174. (kill-buffer "ob-lilypond-broken.org"))
  175. (ert-deftest ob-lilypond/ly-mark-error-line ()
  176. (let ((file-name (concat
  177. org-babel-lilypond-here
  178. "../examples/ob-lilypond-broken.org"))
  179. (expected-point-min 198)
  180. (expected-point-max 205)
  181. (line "line 20"))
  182. (find-file-other-window file-name)
  183. (org-babel-lilypond-mark-error-line file-name line)
  184. (should (equal expected-point-min (point)))
  185. (exchange-point-and-mark)
  186. (should (= expected-point-max (point)))
  187. (kill-buffer (file-name-nondirectory file-name))))
  188. (ert-deftest ob-lilypond/ly-parse-line-num ()
  189. (with-temp-buffer
  190. (insert-file-contents (concat
  191. org-babel-lilypond-here
  192. "../examples/ob-lilypond-test.error")
  193. nil nil nil t)
  194. (goto-char (point-min))
  195. (search-forward "error:")
  196. (should (equal 25 (org-babel-lilypond-parse-line-num (current-buffer))))))
  197. (ert-deftest ob-lilypond/ly-parse-error-line ()
  198. (let ((org-babel-lilypond-file (concat
  199. org-babel-lilypond-here
  200. "../examples/ob-lilypond-broken.ly")))
  201. (should (equal "line 20"
  202. (org-babel-lilypond-parse-error-line org-babel-lilypond-file 20)))
  203. (should (not (org-babel-lilypond-parse-error-line org-babel-lilypond-file 0)))))
  204. (ert-deftest ob-lilypond/ly-attempt-to-open-pdf ()
  205. (let ((post-tangle org-babel-lilypond-display-pdf-post-tangle)
  206. (org-babel-lilypond-file (concat
  207. org-babel-lilypond-here
  208. "../examples/ob-lilypond-test.ly"))
  209. (pdf-file (concat
  210. org-babel-lilypond-here
  211. "../examples/ob-lilypond-test.pdf")))
  212. (setq org-babel-lilypond-display-pdf-post-tangle t)
  213. (when (not (file-exists-p pdf-file))
  214. (set-buffer (get-buffer-create (file-name-nondirectory pdf-file)))
  215. (write-file pdf-file))
  216. (should (equal
  217. (concat
  218. org-babel-lilypond-pdf-command " " pdf-file)
  219. (org-babel-lilypond-attempt-to-open-pdf org-babel-lilypond-file t)))
  220. (delete-file pdf-file)
  221. (kill-buffer (file-name-nondirectory pdf-file))
  222. (should (string-prefix-p "No pdf file generated"
  223. (org-babel-lilypond-attempt-to-open-pdf pdf-file)))
  224. (setq org-babel-lilypond-display-pdf-post-tangle post-tangle)))
  225. (ert-deftest ob-lilypond/ly-attempt-to-play-midi ()
  226. (let ((post-tangle org-babel-lilypond-play-midi-post-tangle)
  227. (org-babel-lilypond-file (concat
  228. org-babel-lilypond-here
  229. "../examples/ob-lilypond-test.ly"))
  230. (midi-file (concat
  231. org-babel-lilypond-here
  232. "../examples/ob-lilypond-test.midi")))
  233. (setq org-babel-lilypond-play-midi-post-tangle t)
  234. (when (not (file-exists-p midi-file))
  235. (set-buffer (get-buffer-create (file-name-nondirectory midi-file)))
  236. (write-file midi-file))
  237. (should (equal
  238. (concat
  239. org-babel-lilypond-midi-command " " midi-file)
  240. (org-babel-lilypond-attempt-to-play-midi org-babel-lilypond-file t)))
  241. (delete-file midi-file)
  242. (kill-buffer (file-name-nondirectory midi-file))
  243. (should (string-prefix-p
  244. "No midi file generated"
  245. (org-babel-lilypond-attempt-to-play-midi midi-file)))
  246. (setq org-babel-lilypond-play-midi-post-tangle post-tangle)))
  247. (ert-deftest ob-lilypond/ly-toggle-midi-play-toggles-flag ()
  248. (if org-babel-lilypond-play-midi-post-tangle
  249. (progn
  250. (org-babel-lilypond-toggle-midi-play)
  251. (should (not org-babel-lilypond-play-midi-post-tangle))
  252. (org-babel-lilypond-toggle-midi-play)
  253. (should org-babel-lilypond-play-midi-post-tangle))
  254. (org-babel-lilypond-toggle-midi-play)
  255. (should org-babel-lilypond-play-midi-post-tangle)
  256. (org-babel-lilypond-toggle-midi-play)
  257. (should (not org-babel-lilypond-play-midi-post-tangle))))
  258. (ert-deftest ob-lilypond/ly-toggle-pdf-display-toggles-flag ()
  259. (if org-babel-lilypond-display-pdf-post-tangle
  260. (progn
  261. (org-babel-lilypond-toggle-pdf-display)
  262. (should (not org-babel-lilypond-display-pdf-post-tangle))
  263. (org-babel-lilypond-toggle-pdf-display)
  264. (should org-babel-lilypond-display-pdf-post-tangle))
  265. (org-babel-lilypond-toggle-pdf-display)
  266. (should org-babel-lilypond-display-pdf-post-tangle)
  267. (org-babel-lilypond-toggle-pdf-display)
  268. (should (not org-babel-lilypond-display-pdf-post-tangle))))
  269. (ert-deftest ob-lilypond/ly-toggle-pdf-generation-toggles-flag ()
  270. (if org-babel-lilypond-gen-pdf
  271. (progn
  272. (org-babel-lilypond-toggle-pdf-generation)
  273. (should (not org-babel-lilypond-gen-pdf))
  274. (org-babel-lilypond-toggle-pdf-generation)
  275. (should org-babel-lilypond-gen-pdf))
  276. (org-babel-lilypond-toggle-pdf-generation)
  277. (should org-babel-lilypond-gen-pdf)
  278. (org-babel-lilypond-toggle-pdf-generation)
  279. (should (not org-babel-lilypond-gen-pdf))))
  280. (ert-deftest ob-lilypond/ly-toggle-arrange-mode ()
  281. (if org-babel-lilypond-arrange-mode
  282. (progn
  283. (org-babel-lilypond-toggle-arrange-mode)
  284. (should (not org-babel-lilypond-arrange-mode))
  285. (org-babel-lilypond-toggle-arrange-mode)
  286. (should org-babel-lilypond-arrange-mode))
  287. (org-babel-lilypond-toggle-arrange-mode)
  288. (should org-babel-lilypond-arrange-mode)
  289. (org-babel-lilypond-toggle-arrange-mode)
  290. (should (not org-babel-lilypond-arrange-mode))))
  291. (ert-deftest ob-lilypond/ly-toggle-png-generation-toggles-flag ()
  292. (if org-babel-lilypond-gen-png
  293. (progn
  294. (org-babel-lilypond-toggle-png-generation)
  295. (should (not org-babel-lilypond-gen-png))
  296. (org-babel-lilypond-toggle-png-generation)
  297. (should org-babel-lilypond-gen-png))
  298. (org-babel-lilypond-toggle-png-generation)
  299. (should org-babel-lilypond-gen-png)
  300. (org-babel-lilypond-toggle-png-generation)
  301. (should (not org-babel-lilypond-gen-png))))
  302. (ert-deftest ob-lilypond/ly-toggle-html-generation-toggles-flag ()
  303. (if org-babel-lilypond-gen-html
  304. (progn
  305. (org-babel-lilypond-toggle-html-generation)
  306. (should (not org-babel-lilypond-gen-html))
  307. (org-babel-lilypond-toggle-html-generation)
  308. (should org-babel-lilypond-gen-html))
  309. (org-babel-lilypond-toggle-html-generation)
  310. (should org-babel-lilypond-gen-html)
  311. (org-babel-lilypond-toggle-html-generation)
  312. (should (not org-babel-lilypond-gen-html))))
  313. (ert-deftest ob-lilypond/ly-switch-extension-with-extensions ()
  314. (should (equal "test-name.xyz"
  315. (org-babel-lilypond-switch-extension "test-name" ".xyz")))
  316. (should (equal "test-name.xyz"
  317. (org-babel-lilypond-switch-extension "test-name.abc" ".xyz")))
  318. (should (equal "test-name"
  319. (org-babel-lilypond-switch-extension "test-name.abc" ""))))
  320. (ert-deftest ob-lilypond/ly-switch-extension-with-paths ()
  321. (should (equal "/some/path/to/test-name.xyz"
  322. (org-babel-lilypond-switch-extension "/some/path/to/test-name" ".xyz"))))
  323. (ert-deftest ob-lilypond/ly-get-header-args ()
  324. (should (equal '((:tangle . "yes")
  325. (:noweb . "yes")
  326. (:results . "silent")
  327. (:cache . "yes")
  328. (:comments . "yes"))
  329. (org-babel-lilypond-set-header-args t)))
  330. (should (equal '((:results . "file")
  331. (:exports . "results"))
  332. (org-babel-lilypond-set-header-args nil))))
  333. (ert-deftest ob-lilypond/ly-set-header-args ()
  334. (org-babel-lilypond-set-header-args t)
  335. (should (equal '((:tangle . "yes")
  336. (:noweb . "yes")
  337. (:results . "silent")
  338. (:cache . "yes")
  339. (:comments . "yes"))
  340. org-babel-default-header-args:lilypond))
  341. (org-babel-lilypond-set-header-args nil)
  342. (should (equal '((:results . "file")
  343. (:exports . "results"))
  344. org-babel-default-header-args:lilypond)))
  345. (provide 'test-ob-lilypond)
  346. ;;; test-ob-lilypond.el ends here