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 ;; Beginning of buffer, no byte-order mark. 62 "-offset" "0" 63 ;; We have two lines with 2×2 bytes for the umlauts, 64 ;; 1 byte for the line ending, and 3 bytes for the 65 ;; other ASCII characters each. 66 "-length" "16" 67 ;; Length of a single line (without line ending). 68 "-cursor" "7"))))))) 69 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))) 107 (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")))) 125 126;;; clang-format-test.el ends here 127