ledger-test.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ;;; ledger-test.el --- Helper code for use with the "ledger" command-line tool
  2. ;; Copyright (C) 2003-2016 John Wiegley (johnw AT gnu DOT org)
  3. ;; This file is not part of GNU Emacs.
  4. ;; This is free software; you can redistribute it and/or modify it under
  5. ;; the terms of the GNU General Public License as published by the Free
  6. ;; Software Foundation; either version 2, or (at your option) any later
  7. ;; version.
  8. ;;
  9. ;; This is distributed in the hope that it will be useful, but WITHOUT
  10. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. ;; for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  16. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. ;; MA 02110-1301 USA.
  18. ;;; Commentary:
  19. ;;; Code:
  20. (declare-function ledger-mode "ledger-mode") ; TODO: fix this cyclic dependency
  21. (declare-function org-narrow-to-subtree "org")
  22. (declare-function org-entry-get "org")
  23. (declare-function outline-back-to-heading "outline")
  24. (declare-function outline-next-heading "outline")
  25. (defgroup ledger-test nil
  26. "Definitions for the Ledger testing framework"
  27. :group 'ledger)
  28. (defcustom ledger-source-directory "~/ledger/"
  29. "Directory where the Ledger sources are located."
  30. :type 'directory
  31. :group 'ledger-test)
  32. (defcustom ledger-test-binary "/Products/ledger/debug/ledger"
  33. "Directory where the Ledger debug binary is located."
  34. :type 'file
  35. :group 'ledger-test)
  36. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  37. (defun ledger-create-test ()
  38. "Create a regression test."
  39. (interactive)
  40. (save-restriction
  41. (org-narrow-to-subtree)
  42. (save-excursion
  43. (let (text beg)
  44. (goto-char (point-min))
  45. (forward-line 1)
  46. (setq beg (point))
  47. (search-forward ":PROPERTIES:")
  48. (goto-char (line-beginning-position))
  49. (setq text (buffer-substring-no-properties beg (point)))
  50. (goto-char (point-min))
  51. (re-search-forward ":ID:\\s-+\\([^-]+\\)")
  52. (find-file-other-window
  53. (format "~/src/ledger/test/regress/%s.test" (match-string 1)))
  54. (sit-for 0)
  55. (insert text)
  56. (goto-char (point-min))
  57. (while (not (eobp))
  58. (goto-char (line-beginning-position))
  59. (delete-char 3)
  60. (forward-line 1))))))
  61. (defun ledger-test-org-narrow-to-entry ()
  62. (outline-back-to-heading)
  63. (narrow-to-region (point) (progn (outline-next-heading) (point)))
  64. (goto-char (point-min)))
  65. (defun ledger-test-create ()
  66. (interactive)
  67. (let ((uuid (org-entry-get (point) "ID")))
  68. (when (string-match "\\`\\([^-]+\\)-" uuid)
  69. (let ((prefix (match-string 1 uuid))
  70. input output)
  71. (save-restriction
  72. (ledger-test-org-narrow-to-entry)
  73. (goto-char (point-min))
  74. (while (re-search-forward "#\\+begin_src ledger" nil t)
  75. (goto-char (match-end 0))
  76. (forward-line 1)
  77. (let ((beg (point)))
  78. (re-search-forward "#\\+end_src")
  79. (setq input
  80. (concat (or input "")
  81. (buffer-substring beg (match-beginning 0))))))
  82. (goto-char (point-min))
  83. (while (re-search-forward ":OUTPUT:" nil t)
  84. (goto-char (match-end 0))
  85. (forward-line 1)
  86. (let ((beg (point)))
  87. (re-search-forward ":END:")
  88. (setq output
  89. (concat (or output "")
  90. (buffer-substring beg (match-beginning 0)))))))
  91. (find-file-other-window
  92. (expand-file-name (concat prefix ".test")
  93. (expand-file-name "test/regress"
  94. ledger-source-directory)))
  95. (ledger-mode)
  96. (if input
  97. (insert input)
  98. (insert "2012-03-17 Payee\n")
  99. (insert " Expenses:Food $20\n")
  100. (insert " Assets:Cash\n"))
  101. (insert "\ntest reg\n")
  102. (if output
  103. (insert output))
  104. (insert "end test\n")))))
  105. (defun ledger-test-run ()
  106. (interactive)
  107. (save-excursion
  108. (goto-char (point-min))
  109. (when (re-search-forward "^test \\(.+?\\)\\( ->.*\\)?$" nil t)
  110. (let ((command (expand-file-name ledger-test-binary))
  111. (args (format "--args-only --columns=80 --no-color -f \"%s\" %s"
  112. buffer-file-name (match-string 1))))
  113. (setq args (replace-regexp-in-string "\\$sourcepath"
  114. ledger-source-directory args))
  115. (kill-new args)
  116. (message "Testing: ledger %s" args)
  117. (let ((prev-directory default-directory))
  118. (cd ledger-source-directory)
  119. (unwind-protect
  120. (async-shell-command (format "\"%s\" %s" command args))
  121. (cd prev-directory)))))))
  122. (provide 'ledger-test)
  123. ;;; ledger-test.el ends here