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)
26 (defconst gnome-c-test-program-1 "\
27 GGpgCtx *g_gpg_ctx_new (GError **error);
29 typedef void (*GGpgProgressCallback) (gpointer user_data,
35 void g_gpg_ctx_set_progress_callback (GGpgCtx *ctx,
36 GGpgProgressCallback callback,
38 GDestroyNotify destroy_data);
39 void g_gpg_ctx_add_signer (GGpgCtx *ctx, GGpgKey *key);
40 guint g_gpg_ctx_get_n_signers (GGpgCtx *ctx);
41 GGpgKey *g_gpg_ctx_get_signer (GGpgCtx *ctx, guint index);
42 void g_gpg_ctx_clear_signers (GGpgCtx *ctx);
45 (defconst gnome-c-test-program-1-aligned "\
46 GGpgCtx *g_gpg_ctx_new (GError **error);
48 typedef void (*GGpgProgressCallback) (gpointer user_data,
54 void g_gpg_ctx_set_progress_callback (GGpgCtx *ctx,
55 GGpgProgressCallback callback,
57 GDestroyNotify destroy_data);
58 void g_gpg_ctx_add_signer (GGpgCtx *ctx,
60 guint g_gpg_ctx_get_n_signers (GGpgCtx *ctx);
61 GGpgKey *g_gpg_ctx_get_signer (GGpgCtx *ctx,
63 void g_gpg_ctx_clear_signers (GGpgCtx *ctx);
66 (defconst gnome-c-test-program-2 "\
68 const gchar ** gtk_widget_list_action_prefixes (GtkWidget *widget);
71 (defconst gnome-c-test-program-3 "\
72 /* overridable methods */
73 void (*set_property) (GObject *object,
77 void (*get_property) (GObject *object,
83 (defconst gnome-c-test-program-4 "\
85 int foo (struct foo ***a, int b, ...) G_GNUC_CONST;
88 (defconst gnome-c-test-program-4-aligned "\
90 int foo (struct foo ***a,
95 (defconst gnome-c-test-program-5 "\
96 int * bar (const char * const * * a, int b);
99 (defconst gnome-c-test-program-5-aligned "\
100 int *bar (const char * const **a,
104 (defconst gnome-c-test-program-6 "\
105 int foo (char **a, int b);
106 type_1234567890 bar (char a, int b);
107 int identifier_1234567890 (double a, double b);
110 (defconst gnome-c-test-program-6-aligned-1 "\
117 int identifier_1234567890
122 (defconst gnome-c-test-program-6-aligned-2 "\
125 type_1234567890 bar (char a,
127 int identifier_1234567890
132 (ert-deftest gnome-c-test-align--guess-optimal-columns ()
133 "Tests the `gnome-c-align--guess-optimal-columns'."
135 (insert gnome-c-test-program-1)
137 (let* (gnome-c-align-max-column
139 (gnome-c-align--guess-optimal-columns (point-min) (point-max))))
140 (should (= (cdr (assq 'identifier-start-column columns)) 9))
141 (should (= (cdr (assq 'arglist-start-column columns)) 41))
142 (should (= (cdr (assq 'arglist-identifier-start-column columns)) 64)))))
144 (ert-deftest gnome-c-test-align-region ()
145 "Tests the `gnome-c-align-decls-region'."
147 (insert gnome-c-test-program-1)
149 (let (gnome-c-align-max-column)
150 (gnome-c-align-guess-optimal-columns (point-min) (point-max))
151 (gnome-c-align-decls-region (point-min) (point-max)))
152 (should (equal (buffer-string) gnome-c-test-program-1-aligned))))
154 (ert-deftest gnome-c-test-align-region-2 ()
155 "Tests the `gnome-c-align-decls-region'."
157 (insert gnome-c-test-program-4)
159 (let (gnome-c-align-max-column)
160 (gnome-c-align-guess-optimal-columns (point-min) (point-max))
161 (gnome-c-align-decls-region (point-min) (point-max)))
162 (should (equal (buffer-string) gnome-c-test-program-4-aligned))))
164 (ert-deftest gnome-c-test-align-region-3 ()
165 "Tests the `gnome-c-align-decls-region'."
167 (insert gnome-c-test-program-5)
169 (let (gnome-c-align-max-column)
170 (gnome-c-align-guess-optimal-columns (point-min) (point-max))
171 (gnome-c-align-decls-region (point-min) (point-max)))
172 (should (equal (buffer-string) gnome-c-test-program-5-aligned))))
174 (ert-deftest gnome-c-test-align-region-4 ()
175 "Tests the `gnome-c-align-decls-region', with max columns set."
177 (insert gnome-c-test-program-6)
179 (let ((gnome-c-align-max-column 20))
180 (gnome-c-align-guess-optimal-columns (point-min) (point-max))
181 (gnome-c-align-decls-region (point-min) (point-max)))
182 (should (equal (buffer-string) gnome-c-test-program-6-aligned-1))))
184 (ert-deftest gnome-c-test-align-region-5 ()
185 "Tests the `gnome-c-align-decls-region', with max columns set."
187 (insert gnome-c-test-program-6)
189 (let ((gnome-c-align-max-column 30))
190 (gnome-c-align-guess-optimal-columns (point-min) (point-max))
191 (gnome-c-align-decls-region (point-min) (point-max)))
192 (should (equal (buffer-string) gnome-c-test-program-6-aligned-2))))
194 (ert-deftest gnome-c-test-align-guess-columns-1 ()
195 "Tests the `gnome-c-align-guess-columns'."
197 (insert gnome-c-test-program-2)
199 (let (gnome-c-align-max-column)
200 (gnome-c-align-guess-columns (point-min) (point-max)))
201 (should (= gnome-c-align-identifier-start-column 24))
202 (should (= gnome-c-align-arglist-start-column 56))
203 (should (= gnome-c-align-arglist-identifier-start-column 80))))
205 (ert-deftest gnome-c-test-align-guess-columns-2 ()
206 "Tests the `gnome-c-align-guess-columns'."
208 (insert gnome-c-test-program-3)
210 (let (gnome-c-align-max-column)
211 (gnome-c-align-guess-columns (point-min) (point-max)))
212 (should (= gnome-c-align-identifier-start-column 13))
213 (should (= gnome-c-align-arglist-start-column 40))
214 (should (= gnome-c-align-arglist-identifier-start-column 57))))