summaryrefslogtreecommitdiff
path: root/lisp/ldg-test.el
blob: 478c62d85cd05707861653ea283a37d2ec45f8a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(defcustom ledger-source-directory "~/src/ledger"
  "Directory where the Ledger sources are located."
  :type 'directory
  :group 'ledger)

(defcustom ledger-test-binary "~/Products/ledger/debug/ledger"
  "Directory where the Ledger sources are located."
  :type 'file
  :group 'ledger)

(defun ledger-test-org-narrow-to-entry ()
  (outline-back-to-heading)
  (narrow-to-region (point) (progn (outline-next-heading) (point)))
  (goto-char (point-min)))

(defun ledger-test-create ()
  (interactive)
  (let ((uuid (org-entry-get (point) "ID")))
    (when (string-match "\\`\\([^-]+\\)-" uuid)
      (let ((prefix (match-string 1 uuid))
            input output)
        (save-restriction
          (ledger-test-org-narrow-to-entry)
          (goto-char (point-min))
          (while (re-search-forward "#\\+begin_src ledger" nil t)
            (goto-char (match-end 0))
            (forward-line 1)
            (let ((beg (point)))
              (re-search-forward "#\\+end_src")
              (setq input
                    (concat (or input "")
                            (buffer-substring beg (match-beginning 0))))))
          (goto-char (point-min))
          (while (re-search-forward ":OUTPUT:" nil t)
            (goto-char (match-end 0))
            (forward-line 1)
            (let ((beg (point)))
              (re-search-forward ":END:")
              (setq output
                    (concat (or output "")
                            (buffer-substring beg (match-beginning 0)))))))
        (find-file-other-window
         (expand-file-name (concat prefix ".test")
                           (expand-file-name "test/regress"
                                             ledger-source-directory)))
        (ledger-mode)
        (if input
            (insert input)
          (insert "2012-03-17 Payee\n")
          (insert "    Expenses:Food                $20\n")
          (insert "    Assets:Cash\n"))
        (insert "\ntest reg\n")
        (if output
            (insert output))
        (insert "end test\n")))))

(defun ledger-test-run ()
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward "^test \\(.+?\\)\\( ->.*\\)?$" nil t)
      (let ((command (expand-file-name ledger-test-binary))
            (args (format "--args-only --columns=80 --no-color -f \"%s\" %s"
                          buffer-file-name (match-string 1))))
        (setq args (replace-regexp-in-string "\\$sourcepath"
                                             ledger-source-directory args))
        (kill-new args)
        (message "Testing: ledger %s" args)
        (let ((prev-directory default-directory))
          (cd ledger-source-directory)
          (unwind-protect
              (async-shell-command (format "\"%s\" %s" command args))
            (cd prev-directory)))))))

(provide 'ldg-test)