summaryrefslogtreecommitdiff
path: root/bog-tests.el
blob: e7ce2cc023e04748d66f744bc4b71132b5ed232d (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
(require 'ert)
(require 'org)
(require 'bog)


;;; Citekey functions

;; `bog-citekey-p'

(ert-deftest bog-citekey-p ()
  (should (bog-citekey-p "name2010word"))
  (should (bog-citekey-p "name1900word"))
  (should-not (bog-citekey-p "name201word")))

(ert-deftest bog-citekey-p-with-hyphen-in-name ()
  (should (bog-citekey-p "hyphen-ok2010word")))

(ert-deftest bog-citekey-p-with-other-text ()
  (should (bog-citekey-p "name2010word more text")))

(ert-deftest bog-citekey-only-p ()
  (should (bog-citekey-only-p "name2010word"))
  (should-not (bog-citekey-only-p "name201word"))
  (should (bog-citekey-only-p "hyphen-ok2010word"))
  (should-not (bog-citekey-only-p "name2010word more text")))

;; `bog-citekey-groups-with-delim'

(ert-deftest bog-citekey-groups-with-delim ()
  (let ((citekey "name2010word"))
    (should (equal (bog-citekey-groups-with-delim citekey)
                   "name 2010 word"))
    (should (equal (bog-citekey-groups-with-delim citekey ",")
                   "name,2010,word"))
    (should (equal (bog-citekey-groups-with-delim citekey nil '(1 3))
                   "name word"))))

;; `bog-citekey-at-point'

(ert-deftest bog-citekey-at-point ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert citekey)
      (goto-char (point-min))
      (should (equal (bog-citekey-at-point) citekey)))))

;; `bog-citekey-heading'

(ert-deftest bog-citekey-heading-current-level ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert (format "\n* top level\n\n** %s\n\nsome text\n"
                      citekey))
      (org-mode)
      (show-all)
      (should (equal (bog-citekey-heading) citekey)))))

(ert-deftest bog-citekey-heading-in-parent ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert (format "\n* top level\n\n** %s\n\n*** subheading\n\nsome text\n"
                      citekey))
      (org-mode)
      (show-all)
      (should (equal (bog-citekey-heading) citekey)))))

(ert-deftest bog-citekey-heading-on-heading ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert (format "\n* top level\n\n** %s\n\nsome text\n"
                      citekey))
      (org-mode)
      (show-all)
      (re-search-backward bog-citekey-format)
      (should (equal (bog-citekey-heading) citekey)))))

(ert-deftest bog-citekey-action-in-normal-text ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert (format "\n* top level\n\n** %s\n\nsome text\n"
                      citekey))
      (org-mode)
      (show-all)
      (flet ((funcall (action citekey) citekey))
        (should (equal (bog-citekey-action nil nil nil) citekey))))))

;; `bog-citekey-action'

(ert-deftest bog-citekey-action-on-heading ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert (format "\n* top level\n\n** %s\n\nsome text\n"
                      citekey))
      (org-mode)
      (show-all)
      (re-search-backward bog-citekey-format)
      (flet ((funcall (action citekey) citekey))
        (should (equal (bog-citekey-action nil nil nil) citekey))))))

(ert-deftest bog-citekey-action-on-in-text-citekey ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert (format "\n* top level\n\n** other2000key\n\nsome text and %s\n"
                      citekey))
      (org-mode)
      (show-all)
      (re-search-backward bog-citekey-format)
      (flet ((funcall (action citekey) citekey))
        (should (equal (bog-citekey-action nil nil nil) citekey))))))

(ert-deftest bog-citekey-action-no-citekey ()
  (with-temp-buffer
    (insert  "\n* top level\n\n** second\n\n")
    (org-mode)
    (show-all)
    (should-error (bog-citekey-action nil nil nil))))