test-ob-lilypond.el 15 KB

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