blob: ee0eaaa31e9646ebfe6d9a28b53289b6b0c119b9 [file] [log] [blame]
Philipp Stephani13dbe272017-01-17 17:30:55 +00001;;; 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
Chandler Carruth2946cd72019-01-19 08:50:56 +00007;; 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
Philipp Stephani13dbe272017-01-17 17:30:55 +000010
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'>
Philipp Stephanice2f6b42017-01-20 09:37:50 +000035<replacement offset='4' length='0'> </replacement>
36<replacement offset='10' length='0'> </replacement>
Philipp Stephani13dbe272017-01-17 17:30:55 +000037</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"
Philipp Stephanice2f6b42017-01-20 09:37:50 +000061 ;; Beginning of buffer, no byte-order mark.
62 "-offset" "0"
Philipp Stephani13dbe272017-01-17 17:30:55 +000063 ;; We have two lines with 2×2 bytes for the umlauts,
Philipp Stephanice2f6b42017-01-20 09:37:50 +000064 ;; 1 byte for the line ending, and 3 bytes for the
Philipp Stephani13dbe272017-01-17 17:30:55 +000065 ;; other ASCII characters each.
Philipp Stephanice2f6b42017-01-20 09:37:50 +000066 "-length" "16"
67 ;; Length of a single line (without line ending).
68 "-cursor" "7")))))))
Philipp Stephani13dbe272017-01-17 17:30:55 +000069
70(ert-deftest clang-format-buffer--process-encoding ()
71 "Tests that text is sent to the clang-format process in the
72right encoding."
73 (cl-letf* ((hexdump (executable-find "hexdump"))
74 (original-call-process-region
75 (symbol-function 'call-process-region))
76 (call-process-inputs nil)
77 ;; We redirect the input to hexdump so that we have guaranteed
78 ;; ASCII output.
79 ((symbol-function 'call-process-region)
80 (lambda (&rest args)
81 (pcase-exhaustive args
82 (`(,start ,end ,_program ,_delete (,stdout ,_stderr)
83 ,_display . ,_args)
84 (with-current-buffer stdout
85 (insert "<?xml version='1.0'?>
86<replacements xml:space='preserve' incomplete_format='false'>
87</replacements>
88"))
89 (let ((stdin (current-buffer)))
90 (with-temp-buffer
91 (prog1
92 (let ((stdout (current-buffer)))
93 (with-current-buffer stdin
94 (funcall original-call-process-region
95 start end hexdump nil stdout nil
96 "-v" "-e" "/1 \"%02x \"")))
97 (push (buffer-string) call-process-inputs)))))))))
98 (skip-unless hexdump)
99 (with-temp-buffer
100 (let ((buffer-file-name "foo.cpp")
101 (buffer-file-coding-system 'utf-8-with-signature-dos)
102 (default-process-coding-system 'latin-1-unix))
103 (insert "ä\n")
104 (clang-format-buffer))
105 (should (equal (buffer-string) "ä\n"))
106 (should (eobp)))
Philipp Stephanice2f6b42017-01-20 09:37:50 +0000107 (should (equal call-process-inputs '("c3 a4 0a ")))))
108
109(ert-deftest clang-format-buffer--end-to-end ()
110 "End-to-end test for ‘clang-format-buffer’.
111Actually calls the clang-format binary."
112 (skip-unless (file-executable-p clang-format-executable))
113 (with-temp-buffer
114 (let ((buffer-file-name "foo.cpp")
115 (buffer-file-coding-system 'utf-8-with-signature-dos)
116 (default-process-coding-system 'latin-1-unix))
117 (insert "ä =ö;\nü= ß;\n")
118 (goto-char (point-min))
119 (end-of-line)
120 (clang-format-buffer))
121 (should (equal (buffer-string) "ä = ö;\nü = ß;\n"))
122 (should (eolp))
123 (should (equal (buffer-substring (point) (point-max))
124 "\nü = ß;\n"))))
Philipp Stephani13dbe272017-01-17 17:30:55 +0000125
126;;; clang-format-test.el ends here