1 ;;; clang-format-test.el --- unit tests for clang-format.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2017 Google Inc.
5 ;; Author: Philipp Stephani <phst@google.com>
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
13 ;; Unit tests for clang-format.el. Not run by lit, run as:
14 ;; emacs -Q -batch -l clang/tools/clang-format/clang-format.el -l clang/tools/clang-format/clang-format-test.el -f ert-run-tests-batch-and-exit
18 (require 'clang-format
)
24 (ert-deftest clang-format-buffer--buffer-encoding
()
25 "Tests that encoded text is handled properly."
26 (cl-letf* ((call-process-args nil
)
27 ((symbol-function 'call-process-region
)
29 (push args call-process-args
)
30 (pcase-exhaustive args
31 (`(,_start
,_end
,_program
,_delete
(,stdout
,_stderr
)
33 (with-current-buffer stdout
34 (insert "<?xml version='1.0'?>
35 <replacements xml:space='preserve' incomplete_format='false'>
36 <replacement offset='4' length='0'> </replacement>
37 <replacement offset='10' length='0'> </replacement>
42 (let ((buffer-file-name "foo.cpp")
43 (buffer-file-coding-system 'utf-8-with-signature-dos
)
44 (default-process-coding-system 'latin-1-unix
))
45 (insert "ä =ö;\nü= ß;\n")
46 (goto-char (point-min))
48 (clang-format-buffer))
49 (should (equal (buffer-string) "ä = ö;\nü = ß;\n"))
51 (should (equal (buffer-substring (point) (point-max))
53 (should-not (cdr call-process-args
))
54 (pcase-exhaustive call-process-args
55 (`((,start
,end
,_program
,delete
(,_stdout
,_stderr
) ,display .
,args
))
61 '("-output-replacements-xml" "-assume-filename" "foo.cpp"
62 "-fallback-style" "none"
63 ;; Beginning of buffer, no byte-order mark.
65 ;; We have two lines with 2×2 bytes for the umlauts,
66 ;; 1 byte for the line ending, and 3 bytes for the
67 ;; other ASCII characters each.
69 ;; Length of a single line (without line ending).
72 (ert-deftest clang-format-buffer--process-encoding
()
73 "Tests that text is sent to the clang-format process in the
75 (cl-letf* ((hexdump (executable-find "hexdump"))
76 (original-call-process-region
77 (symbol-function 'call-process-region
))
78 (call-process-inputs nil
)
79 ;; We redirect the input to hexdump so that we have guaranteed
81 ((symbol-function 'call-process-region
)
83 (pcase-exhaustive args
84 (`(,start
,end
,_program
,_delete
(,stdout
,_stderr
)
86 (with-current-buffer stdout
87 (insert "<?xml version='1.0'?>
88 <replacements xml:space='preserve' incomplete_format='false'>
91 (let ((stdin (current-buffer)))
94 (let ((stdout (current-buffer)))
95 (with-current-buffer stdin
96 (funcall original-call-process-region
97 start end hexdump nil stdout nil
98 "-v" "-e" "/1 \"%02x \"")))
99 (push (buffer-string) call-process-inputs
)))))))))
100 (skip-unless hexdump
)
102 (let ((buffer-file-name "foo.cpp")
103 (buffer-file-coding-system 'utf-8-with-signature-dos
)
104 (default-process-coding-system 'latin-1-unix
))
106 (clang-format-buffer))
107 (should (equal (buffer-string) "ä\n"))
109 (should (equal call-process-inputs
'("c3 a4 0a ")))))
111 (ert-deftest clang-format-buffer--end-to-end
()
112 "End-to-end test for ‘clang-format-buffer’.
113 Actually calls the clang-format binary."
114 (skip-unless (file-executable-p clang-format-executable
))
116 (let ((buffer-file-name "foo.cpp")
117 (buffer-file-coding-system 'utf-8-with-signature-dos
)
118 (default-process-coding-system 'latin-1-unix
))
119 (insert "ä =ö;\nü= ß;\n")
120 (goto-char (point-min))
122 (clang-format-buffer))
123 (should (equal (buffer-string) "ä = ö;\nü = ß;\n"))
125 (should (equal (buffer-substring (point) (point-max))
128 ;;; clang-format-test.el ends here