blob: 0e1f4dde406b89cb9da2ef385180e6fcdd34ed6d [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
7;; This file is distributed under the University of Illinois Open Source
8;; License. See LICENSE.TXT for details.
9
10;;; Commentary:
11
12;; Unit tests for clang-format.el.
13
14;;; Code:
15
16(require 'clang-format)
17
18(require 'cl-lib)
19(require 'ert)
20(require 'pcase)
21
22(ert-deftest clang-format-buffer--buffer-encoding ()
23 "Tests that encoded text is handled properly."
24 (cl-letf* ((call-process-args nil)
25 ((symbol-function 'call-process-region)
26 (lambda (&rest args)
27 (push args call-process-args)
28 (pcase-exhaustive args
29 (`(,_start ,_end ,_program ,_delete (,stdout ,_stderr)
30 ,_display . ,_args)
31 (with-current-buffer stdout
32 (insert "<?xml version='1.0'?>
33<replacements xml:space='preserve' incomplete_format='false'>
Philipp Stephanice2f6b42017-01-20 09:37:50 +000034<replacement offset='4' length='0'> </replacement>
35<replacement offset='10' length='0'> </replacement>
Philipp Stephani13dbe272017-01-17 17:30:55 +000036</replacements>
37"))
38 0)))))
39 (with-temp-buffer
40 (let ((buffer-file-name "foo.cpp")
41 (buffer-file-coding-system 'utf-8-with-signature-dos)
42 (default-process-coding-system 'latin-1-unix))
43 (insert "ä =ö;\nü= ß;\n")
44 (goto-char (point-min))
45 (end-of-line)
46 (clang-format-buffer))
47 (should (equal (buffer-string) "ä = ö;\nü = ß;\n"))
48 (should (eolp))
49 (should (equal (buffer-substring (point) (point-max))
50 "\nü = ß;\n")))
51 (should-not (cdr call-process-args))
52 (pcase-exhaustive call-process-args
53 (`((,start ,end ,_program ,delete (,_stdout ,_stderr) ,display . ,args))
54 (should-not start)
55 (should-not end)
56 (should-not delete)
57 (should-not display)
58 (should (equal args
59 '("-output-replacements-xml" "-assume-filename" "foo.cpp"
60 "-style" "file"
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