1 | ;;; clang-format-test.el --- unit tests for clang-format.el -*- lexical-binding: t; -*- |
2 | |
3 | ;; Copyright (C) 2017 Google Inc. |
4 | |
5 | ;; Author: Philipp Stephani <phst@google.com> |
6 | |
7 | ;; Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
8 | ;; See https://llvm.org/LICENSE.txt for license information. |
9 | ;; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
10 | |
11 | ;;; Commentary: |
12 | |
13 | ;; Unit tests for clang-format.el. |
14 | |
15 | ;;; Code: |
16 | |
17 | (require 'clang-format) |
18 | |
19 | (require 'cl-lib) |
20 | (require 'ert) |
21 | (require 'pcase) |
22 | |
23 | (ert-deftest clang-format-buffer--buffer-encoding () |
24 | "Tests that encoded text is handled properly." |
25 | (cl-letf* ((call-process-args nil) |
26 | ((symbol-function 'call-process-region) |
27 | (lambda (&rest args) |
28 | (push args call-process-args) |
29 | (pcase-exhaustive args |
30 | (`(,_start ,_end ,_program ,_delete (,stdout ,_stderr) |
31 | ,_display . ,_args) |
32 | (with-current-buffer stdout |
33 | (insert "<?xml version='1.0'?> |
34 | <replacements xml:space='preserve' incomplete_format='false'> |
35 | <replacement offset='4' length='0'> </replacement> |
36 | <replacement offset='10' length='0'> </replacement> |
37 | </replacements> |
38 | ")) |
39 | 0))))) |
40 | (with-temp-buffer |
41 | (let ((buffer-file-name "foo.cpp") |
42 | (buffer-file-coding-system 'utf-8-with-signature-dos) |
43 | (default-process-coding-system 'latin-1-unix)) |
44 | (insert "ä =ö;\nü= ß;\n") |
45 | (goto-char (point-min)) |
46 | (end-of-line) |
47 | (clang-format-buffer)) |
48 | (should (equal (buffer-string) "ä = ö;\nü = ß;\n")) |
49 | (should (eolp)) |
50 | (should (equal (buffer-substring (point) (point-max)) |
51 | "\nü = ß;\n"))) |
52 | (should-not (cdr call-process-args)) |
53 | (pcase-exhaustive call-process-args |
54 | (`((,start ,end ,_program ,delete (,_stdout ,_stderr) ,display . ,args)) |
55 | (should-not start) |
56 | (should-not end) |
57 | (should-not delete) |
58 | (should-not display) |
59 | (should (equal args |
60 | '("-output-replacements-xml" "-assume-filename" "foo.cpp" |
61 | "-style" "file" |
62 | ;; Beginning of buffer, no byte-order mark. |
63 | "-offset" "0" |
64 | ;; We have two lines with 2×2 bytes for the umlauts, |
65 | ;; 1 byte for the line ending, and 3 bytes for the |
66 | ;; other ASCII characters each. |
67 | "-length" "16" |
68 | ;; Length of a single line (without line ending). |
69 | "-cursor" "7"))))))) |
70 | |
71 | (ert-deftest clang-format-buffer--process-encoding () |
72 | "Tests that text is sent to the clang-format process in the |
73 | right encoding." |
74 | (cl-letf* ((hexdump (executable-find "hexdump")) |
75 | (original-call-process-region |
76 | (symbol-function 'call-process-region)) |
77 | (call-process-inputs nil) |
78 | ;; We redirect the input to hexdump so that we have guaranteed |
79 | ;; ASCII output. |
80 | ((symbol-function 'call-process-region) |
81 | (lambda (&rest args) |
82 | (pcase-exhaustive args |
83 | (`(,start ,end ,_program ,_delete (,stdout ,_stderr) |
84 | ,_display . ,_args) |
85 | (with-current-buffer stdout |
86 | (insert "<?xml version='1.0'?> |
87 | <replacements xml:space='preserve' incomplete_format='false'> |
88 | </replacements> |
89 | ")) |
90 | (let ((stdin (current-buffer))) |
91 | (with-temp-buffer |
92 | (prog1 |
93 | (let ((stdout (current-buffer))) |
94 | (with-current-buffer stdin |
95 | (funcall original-call-process-region |
96 | start end hexdump nil stdout nil |
97 | "-v" "-e" "/1 \"%02x \""))) |
98 | (push (buffer-string) call-process-inputs))))))))) |
99 | (skip-unless hexdump) |
100 | (with-temp-buffer |
101 | (let ((buffer-file-name "foo.cpp") |
102 | (buffer-file-coding-system 'utf-8-with-signature-dos) |
103 | (default-process-coding-system 'latin-1-unix)) |
104 | (insert "ä\n") |
105 | (clang-format-buffer)) |
106 | (should (equal (buffer-string) "ä\n")) |
107 | (should (eobp))) |
108 | (should (equal call-process-inputs '("c3 a4 0a "))))) |
109 | |
110 | (ert-deftest clang-format-buffer--end-to-end () |
111 | "End-to-end test for ‘clang-format-buffer’. |
112 | Actually calls the clang-format binary." |
113 | (skip-unless (file-executable-p clang-format-executable)) |
114 | (with-temp-buffer |
115 | (let ((buffer-file-name "foo.cpp") |
116 | (buffer-file-coding-system 'utf-8-with-signature-dos) |
117 | (default-process-coding-system 'latin-1-unix)) |
118 | (insert "ä =ö;\nü= ß;\n") |
119 | (goto-char (point-min)) |
120 | (end-of-line) |
121 | (clang-format-buffer)) |
122 | (should (equal (buffer-string) "ä = ö;\nü = ß;\n")) |
123 | (should (eolp)) |
124 | (should (equal (buffer-substring (point) (point-max)) |
125 | "\nü = ß;\n")))) |
126 | |
127 | ;;; clang-format-test.el ends here |
128 | |