summaryrefslogtreecommitdiff
path: root/test/lisp/cedet/srecode-utest-getset.el
blob: 1c6578038c06c8087089e930e15a3301b7d7d357 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
;;; srecode/test-getset.el --- Test the getset inserter. -*- lexical-binding:t -*-

;; Copyright (C) 2008, 2009, 2011, 2019-2021 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Unit tests for the getset inserter application.

(require 'srecode/semantic)

;;; Code:
(defvar srecode-utest-getset-pre-fill
  "// Test Class for getset tests in c++.

class myClass {
public:
  myClass() { };
  ~myClass() { };
  /** miscFunction
   */
  int miscFunction(int);

private:
  int fStartingField;

};

"
  "The pre-fill class for the getset tests.")


;;; Master Harness
;;
(defvar srecode-utest-getset-testfile
  (expand-file-name
   (concat (make-temp-name "srecode-utest-getset-") ".cpp")
   temporary-file-directory)
  "File used to do testing.")

(defvar srecode-insert-getset-fully-automatic-flag) ; Silence byte-compiler.
(ert-deftest srecode-utest-getset-output ()
  "Test various template insertion options."
  :tags '(:expensive-test)
  (save-excursion
    (let ((testbuff (find-file-noselect srecode-utest-getset-testfile))
	  (srecode-insert-getset-fully-automatic-flag t))

      (set-buffer testbuff)
      (semantic-mode 1)
      (srecode-load-tables-for-mode major-mode)
      (srecode-load-tables-for-mode major-mode 'getset)

      (should (srecode-table))
      ;;(error "No template table found for mode %s" major-mode))

      (condition-case nil
	  (erase-buffer)
	(error nil))

      (insert srecode-utest-getset-pre-fill)
      (goto-char (point-min))

      ;; Test PRE FILL
      (should-not
       (srecode-utest-getset-tagcheck '("public"
				        "myClass"
				        "myClass"
				        "miscFunction"
				        "private"
				        "fStartingField")))
      (should-not
       (srecode-utest-getset-jumptotag "fStartingField"))

      ;; Startup with fully automatic selection.
      (srecode-insert-getset)

      ;; * Post get-set "StartingField"
      (should-not
       (srecode-utest-getset-tagcheck '("public"
				        "myClass"
				        "myClass"
				        "getStartingField"
				        "setStartingField"
				        "miscFunction"
				        "private"
				        "fStartingField")))

      ;; Now try convenience args.
      (goto-char (point-min))
      (should-not
       (srecode-utest-getset-jumptotag "fStartingField"))
      (end-of-line)
      (insert "\n")

      (srecode-insert-getset nil "AutoInsertField")

      ;; * Post get-set "AutoInsertField"
      (should-not
       (srecode-utest-getset-tagcheck '("public"
				        "myClass"
				        "myClass"
				        "getStartingField"
				        "setStartingField"
				        "getAutoInsertField"
				        "setAutoInsertField"
				        "miscFunction"
				        "private"
				        "fStartingField"
				        "fAutoInsertField")))

      ;; Make sure all the comments are in the right place.
      (should-not
       (srecode-utest-getset-jumptotag "miscFunction"))

      (let ((pos (point)))
	(forward-comment -1)
	(re-search-forward "miscFunction" pos))

      ))
  (when (file-exists-p srecode-utest-getset-testfile)
    (delete-file srecode-utest-getset-testfile))
  )

(defun srecode-utest-getset-tagcheck (expected-members)
  "Make sure that the tags in myClass have EXPECTED-MEMBERS."
  (semantic-fetch-tags)
  (let* ((mc (semantic-find-tags-by-name "myClass" (current-buffer)))
	 (mem (semantic-tag-type-members (car mc)))
         (fail nil))
    (catch 'fail-early
      (while (and mem expected-members)
        (when (not (string= (semantic-tag-name (car mem))
			    (car expected-members)))
	  (switch-to-buffer (current-buffer))
	  (setq fail (format "Did not find %s in %s" (car expected-members)
                             (buffer-file-name)))
          (throw 'fail-early nil))
        (setq mem (cdr mem)
	      expected-members (cdr expected-members)))
      (when expected-members
        (switch-to-buffer (current-buffer))
        (setq fail (format "Did not find all expected tags in class: %s" (buffer-file-name)))
        (throw 'fail-early t))
      (when mem
        (switch-to-buffer (current-buffer))
        (setq fail (format "Found extra tags in class: %s" (buffer-file-name)))))

    (when fail (message "%s" (buffer-string)))
    fail))

(defun srecode-utest-getset-jumptotag (tagname)
  "Jump to the tag named TAGNAME."
  (semantic-fetch-tags)
  (let ((fail nil)
        (tag (semantic-deep-find-tags-by-name tagname (current-buffer))))
    (if tag
	(semantic-go-to-tag (car tag))
      (setq fail (format "Failed to jump to tag %s" tagname)))
    fail))

(provide 'cedet/srecode/test-getset)
;;; srecode/test-getset.el ends here