]> code.delx.au - gnu-emacs-elpa/blob - packages/gnome-c-style/gnome-c-tests.el
Merge commit 'd827bb511203a64da3ae5cc6910b87b7c99d233b'
[gnu-emacs-elpa] / packages / gnome-c-style / gnome-c-tests.el
1 ;;; gnome-c-tests.el --- tests for gnome-c-style -*- lexical-binding: t; -*-
2 ;; Copyright (C) 2016 Free Software Foundation, Inc.
3
4 ;; Author: Daiki Ueno <ueno@gnu.org>
5 ;; Keywords: GNOME, C, coding style
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Code:
23
24 (require 'gnome-c-align)
25 (require 'gnome-c-snippet)
26
27 (defconst gnome-c-test-program-1 "\
28 GGpgCtx *g_gpg_ctx_new (GError **error);
29
30 typedef void (*GGpgProgressCallback) (gpointer user_data,
31 const gchar *what,
32 gint type,
33 gint current,
34 gint total);
35
36 void g_gpg_ctx_set_progress_callback (GGpgCtx *ctx,
37 GGpgProgressCallback callback,
38 gpointer user_data,
39 GDestroyNotify destroy_data);
40 void g_gpg_ctx_add_signer (GGpgCtx *ctx, GGpgKey *key);
41 guint g_gpg_ctx_get_n_signers (GGpgCtx *ctx);
42 GGpgKey *g_gpg_ctx_get_signer (GGpgCtx *ctx, guint index);
43 void g_gpg_ctx_clear_signers (GGpgCtx *ctx);
44 ")
45
46 (defconst gnome-c-test-program-1-aligned "\
47 GGpgCtx *g_gpg_ctx_new (GError **error);
48
49 typedef void (*GGpgProgressCallback) (gpointer user_data,
50 const gchar *what,
51 gint type,
52 gint current,
53 gint total);
54
55 void g_gpg_ctx_set_progress_callback (GGpgCtx *ctx,
56 GGpgProgressCallback callback,
57 gpointer user_data,
58 GDestroyNotify destroy_data);
59 void g_gpg_ctx_add_signer (GGpgCtx *ctx,
60 GGpgKey *key);
61 guint g_gpg_ctx_get_n_signers (GGpgCtx *ctx);
62 GGpgKey *g_gpg_ctx_get_signer (GGpgCtx *ctx,
63 guint index);
64 void g_gpg_ctx_clear_signers (GGpgCtx *ctx);
65 ")
66
67 (defconst gnome-c-test-program-2 "\
68 GDK_AVAILABLE_IN_3_16
69 const gchar ** gtk_widget_list_action_prefixes (GtkWidget *widget);
70 ")
71
72 (defconst gnome-c-test-program-3 "\
73 /* overridable methods */
74 void (*set_property) (GObject *object,
75 guint property_id,
76 const GValue *value,
77 GParamSpec *pspec);
78 void (*get_property) (GObject *object,
79 guint property_id,
80 GValue *value,
81 GParamSpec *pspec);
82 ")
83
84 (defconst gnome-c-test-program-4 "\
85 FOO_AVAILABLE_IN_ALL
86 int foo (struct foo ***a, int b, ...) G_GNUC_CONST;
87 ")
88
89 (defconst gnome-c-test-program-4-aligned "\
90 FOO_AVAILABLE_IN_ALL
91 int foo (struct foo ***a,
92 int b,
93 ...) G_GNUC_CONST;
94 ")
95
96 (defconst gnome-c-test-program-5 "\
97 int * bar (const char * const * * a, int b);
98 ")
99
100 (defconst gnome-c-test-program-5-aligned "\
101 int *bar (const char * const **a,
102 int b);
103 ")
104
105 (defconst gnome-c-test-program-6 "\
106 int foo (char **a, int b);
107 type_1234567890 bar (char a, int b);
108 int identifier_1234567890 (double a, double b);
109 ")
110
111 (defconst gnome-c-test-program-6-aligned-1 "\
112 int foo
113 (char **a,
114 int b);
115 type_1234567890 bar
116 (char a,
117 int b);
118 int identifier_1234567890
119 (double a,
120 double b);
121 ")
122
123 (defconst gnome-c-test-program-6-aligned-2 "\
124 int foo (char **a,
125 int b);
126 type_1234567890 bar (char a,
127 int b);
128 int identifier_1234567890
129 (double a,
130 double b);
131 ")
132
133 (defconst gnome-c-test-program-7 "\
134 G_DECLARE_FINAL_TYPE (GGpgEngineInfo, g_gpg_engine_info, G_GPG, ENGINE_INFO,
135 GObject)
136 ")
137
138 (ert-deftest gnome-c-test-align--guess-optimal-columns ()
139 "Tests the `gnome-c-align--guess-optimal-columns'."
140 (with-temp-buffer
141 (insert gnome-c-test-program-1)
142 (c-mode)
143 (let* (gnome-c-align-max-column
144 (columns
145 (gnome-c-align--guess-optimal-columns (point-min) (point-max))))
146 (should (= (cdr (assq 'identifier-start-column columns)) 9))
147 (should (= (cdr (assq 'arglist-start-column columns)) 41))
148 (should (= (cdr (assq 'arglist-identifier-start-column columns)) 64)))))
149
150 (ert-deftest gnome-c-test-align-region ()
151 "Tests the `gnome-c-align-decls-region'."
152 (with-temp-buffer
153 (insert gnome-c-test-program-1)
154 (c-mode)
155 (let (gnome-c-align-max-column)
156 (gnome-c-align-guess-optimal-columns (point-min) (point-max))
157 (gnome-c-align-decls-region (point-min) (point-max)))
158 (should (equal (buffer-string) gnome-c-test-program-1-aligned))))
159
160 (ert-deftest gnome-c-test-align-region-2 ()
161 "Tests the `gnome-c-align-decls-region'."
162 (with-temp-buffer
163 (insert gnome-c-test-program-4)
164 (c-mode)
165 (let (gnome-c-align-max-column)
166 (gnome-c-align-guess-optimal-columns (point-min) (point-max))
167 (gnome-c-align-decls-region (point-min) (point-max)))
168 (should (equal (buffer-string) gnome-c-test-program-4-aligned))))
169
170 (ert-deftest gnome-c-test-align-region-3 ()
171 "Tests the `gnome-c-align-decls-region'."
172 (with-temp-buffer
173 (insert gnome-c-test-program-5)
174 (c-mode)
175 (let (gnome-c-align-max-column)
176 (gnome-c-align-guess-optimal-columns (point-min) (point-max))
177 (gnome-c-align-decls-region (point-min) (point-max)))
178 (should (equal (buffer-string) gnome-c-test-program-5-aligned))))
179
180 (ert-deftest gnome-c-test-align-region-4 ()
181 "Tests the `gnome-c-align-decls-region', with max columns set."
182 (with-temp-buffer
183 (insert gnome-c-test-program-6)
184 (c-mode)
185 (let ((gnome-c-align-max-column 20))
186 (gnome-c-align-guess-optimal-columns (point-min) (point-max))
187 (gnome-c-align-decls-region (point-min) (point-max)))
188 (should (equal (buffer-string) gnome-c-test-program-6-aligned-1))))
189
190 (ert-deftest gnome-c-test-align-region-5 ()
191 "Tests the `gnome-c-align-decls-region', with max columns set."
192 (with-temp-buffer
193 (insert gnome-c-test-program-6)
194 (c-mode)
195 (let ((gnome-c-align-max-column 30))
196 (gnome-c-align-guess-optimal-columns (point-min) (point-max))
197 (gnome-c-align-decls-region (point-min) (point-max)))
198 (should (equal (buffer-string) gnome-c-test-program-6-aligned-2))))
199
200 (ert-deftest gnome-c-test-align-guess-columns-1 ()
201 "Tests the `gnome-c-align-guess-columns'."
202 (with-temp-buffer
203 (insert gnome-c-test-program-2)
204 (c-mode)
205 (let (gnome-c-align-max-column)
206 (gnome-c-align-guess-columns (point-min) (point-max)))
207 (should (= gnome-c-align-identifier-start-column 24))
208 (should (= gnome-c-align-arglist-start-column 56))
209 (should (= gnome-c-align-arglist-identifier-start-column 80))))
210
211 (ert-deftest gnome-c-test-align-guess-columns-2 ()
212 "Tests the `gnome-c-align-guess-columns'."
213 (with-temp-buffer
214 (insert gnome-c-test-program-3)
215 (c-mode)
216 (let (gnome-c-align-max-column)
217 (gnome-c-align-guess-columns (point-min) (point-max)))
218 (should (= gnome-c-align-identifier-start-column 13))
219 (should (= gnome-c-align-arglist-start-column 40))
220 (should (= gnome-c-align-arglist-identifier-start-column 57))))
221
222 (ert-deftest gnome-c-test-snippet-guess-name-from-declaration ()
223 "Tests the `gnome-c-snippet--guess-name-from-declaration'."
224 (with-temp-buffer
225 (insert gnome-c-test-program-7)
226 (c-mode)
227 (setq buffer-file-name "gpgme-glib.h")
228 (let ((package (gnome-c-snippet--guess-name-from-declaration 'package))
229 (class (gnome-c-snippet--guess-name-from-declaration 'class))
230 (parent-package
231 (gnome-c-snippet--guess-name-from-declaration 'parent-package))
232 (parent-class
233 (gnome-c-snippet--guess-name-from-declaration 'parent-class)))
234 (should (equal package '("G" "Gpg")))
235 (should (equal class '("Engine" "Info")))
236 (should (equal parent-package '("G")))
237 (should (equal parent-class '("Object"))))))
238
239 (ert-deftest gnome-c-test-snippet-guess-name-from-declaration-2 ()
240 "Tests the `gnome-c-snippet--guess-name-from-declaration'."
241 (let (buffer)
242 (unwind-protect
243 (progn
244 (setq buffer (generate-new-buffer "header"))
245 (with-current-buffer buffer
246 (insert gnome-c-test-program-7)
247 (c-mode)
248 (setq buffer-file-name "gpgme-glib.h"))
249 (with-temp-buffer
250 (c-mode)
251 (setq buffer-file-name "gpgme-glib.c")
252 (let ((package
253 (gnome-c-snippet--guess-name-from-declaration 'package))
254 (class
255 (gnome-c-snippet--guess-name-from-declaration 'class))
256 (parent-package
257 (gnome-c-snippet--guess-name-from-declaration
258 'parent-package))
259 (parent-class
260 (gnome-c-snippet--guess-name-from-declaration
261 'parent-class)))
262 (should (equal package '("G" "Gpg")))
263 (should (equal class '("Engine" "Info")))
264 (should (equal parent-package '("G")))
265 (should (equal parent-class '("Object"))))))
266 (kill-buffer buffer))))
267
268 (ert-deftest gnome-c-test-snippet-guess-name-from-file-name ()
269 "Tests the `gnome-c-snippet--guess-name-from-file-name'"
270 (with-temp-buffer
271 (c-mode)
272 (setq buffer-file-name "g-gpg-engine-info.c")
273 (let ((package
274 (gnome-c-snippet--guess-name-from-file-name 'package))
275 (class
276 (gnome-c-snippet--guess-name-from-file-name 'class))
277 (parent-package
278 (gnome-c-snippet--guess-name-from-file-name 'parent-package))
279 (parent-class
280 (gnome-c-snippet--guess-name-from-file-name 'parent-class)))
281 (should (equal package '("G")))
282 (should (equal class '("Gpg" "Engine" "Info")))
283 (should (equal parent-package nil))
284 (should (equal parent-class nil)))))