1 ;;; gnome-c-tests.el --- tests for gnome-c-style -*- lexical-binding: t; -*-
2 ;; Copyright (C) 2016 Free Software Foundation, Inc.
4 ;; Author: Daiki Ueno <ueno@gnu.org>
5 ;; Keywords: GNOME, C, coding style
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 (require 'gnome-c-align)
25 (require 'gnome-c-snippet)
27 (defconst gnome-c-test-program-1 "\
28 GGpgCtx *g_gpg_ctx_new (GError **error);
30 typedef void (*GGpgProgressCallback) (gpointer user_data,
36 void g_gpg_ctx_set_progress_callback (GGpgCtx *ctx,
37 GGpgProgressCallback callback,
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);
46 (defconst gnome-c-test-program-1-aligned "\
47 GGpgCtx *g_gpg_ctx_new (GError **error);
49 typedef void (*GGpgProgressCallback) (gpointer user_data,
55 void g_gpg_ctx_set_progress_callback (GGpgCtx *ctx,
56 GGpgProgressCallback callback,
58 GDestroyNotify destroy_data);
59 void g_gpg_ctx_add_signer (GGpgCtx *ctx,
61 guint g_gpg_ctx_get_n_signers (GGpgCtx *ctx);
62 GGpgKey *g_gpg_ctx_get_signer (GGpgCtx *ctx,
64 void g_gpg_ctx_clear_signers (GGpgCtx *ctx);
67 (defconst gnome-c-test-program-2 "\
69 const gchar ** gtk_widget_list_action_prefixes (GtkWidget *widget);
72 (defconst gnome-c-test-program-3 "\
73 /* overridable methods */
74 void (*set_property) (GObject *object,
78 void (*get_property) (GObject *object,
84 (defconst gnome-c-test-program-4 "\
86 int foo (struct foo ***a, int b, ...) G_GNUC_CONST;
89 (defconst gnome-c-test-program-4-aligned "\
91 int foo (struct foo ***a,
96 (defconst gnome-c-test-program-5 "\
97 int * bar (const char * const * * a, int b);
100 (defconst gnome-c-test-program-5-aligned "\
101 int *bar (const char * const **a,
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);
111 (defconst gnome-c-test-program-6-aligned-1 "\
118 int identifier_1234567890
123 (defconst gnome-c-test-program-6-aligned-2 "\
126 type_1234567890 bar (char a,
128 int identifier_1234567890
133 (defconst gnome-c-test-program-7 "\
134 G_DECLARE_FINAL_TYPE (GGpgEngineInfo, g_gpg_engine_info, G_GPG, ENGINE_INFO,
138 (ert-deftest gnome-c-test-align--guess-optimal-columns ()
139 "Tests the `gnome-c-align--guess-optimal-columns'."
141 (insert gnome-c-test-program-1)
143 (let* (gnome-c-align-max-column
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)))))
150 (ert-deftest gnome-c-test-align-region ()
151 "Tests the `gnome-c-align-decls-region'."
153 (insert gnome-c-test-program-1)
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))))
160 (ert-deftest gnome-c-test-align-region-2 ()
161 "Tests the `gnome-c-align-decls-region'."
163 (insert gnome-c-test-program-4)
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))))
170 (ert-deftest gnome-c-test-align-region-3 ()
171 "Tests the `gnome-c-align-decls-region'."
173 (insert gnome-c-test-program-5)
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))))
180 (ert-deftest gnome-c-test-align-region-4 ()
181 "Tests the `gnome-c-align-decls-region', with max columns set."
183 (insert gnome-c-test-program-6)
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))))
190 (ert-deftest gnome-c-test-align-region-5 ()
191 "Tests the `gnome-c-align-decls-region', with max columns set."
193 (insert gnome-c-test-program-6)
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))))
200 (ert-deftest gnome-c-test-align-guess-columns-1 ()
201 "Tests the `gnome-c-align-guess-columns'."
203 (insert gnome-c-test-program-2)
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))))
211 (ert-deftest gnome-c-test-align-guess-columns-2 ()
212 "Tests the `gnome-c-align-guess-columns'."
214 (insert gnome-c-test-program-3)
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))))
222 (ert-deftest gnome-c-test-snippet-guess-name-from-declaration ()
223 "Tests the `gnome-c-snippet--guess-name-from-declaration'."
225 (insert gnome-c-test-program-7)
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))
231 (gnome-c-snippet--guess-name-from-declaration 'parent-package))
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"))))))
239 (ert-deftest gnome-c-test-snippet-guess-name-from-declaration-2 ()
240 "Tests the `gnome-c-snippet--guess-name-from-declaration'."
244 (setq buffer (generate-new-buffer "header"))
245 (with-current-buffer buffer
246 (insert gnome-c-test-program-7)
248 (setq buffer-file-name "gpgme-glib.h"))
251 (setq buffer-file-name "gpgme-glib.c")
253 (gnome-c-snippet--guess-name-from-declaration 'package))
255 (gnome-c-snippet--guess-name-from-declaration 'class))
257 (gnome-c-snippet--guess-name-from-declaration
260 (gnome-c-snippet--guess-name-from-declaration
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))))
268 (ert-deftest gnome-c-test-snippet-guess-name-from-file-name ()
269 "Tests the `gnome-c-snippet--guess-name-from-file-name'"
272 (setq buffer-file-name "g-gpg-engine-info.c")
274 (gnome-c-snippet--guess-name-from-file-name 'package))
276 (gnome-c-snippet--guess-name-from-file-name 'class))
278 (gnome-c-snippet--guess-name-from-file-name 'parent-package))
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)))))