;; test-buffer.el --- Unit tests for phpinspect.el -*- lexical-binding: t; -*- ;; Copyright (C) 2021 Free Software Foundation, Inc. ;; Author: Hugo Thunnissen ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; ;;; Code: (require 'ert) (require 'phpinspect-parser) (require 'phpinspect-buffer) (ert-deftest phpinspect-buffer-region-lookups () (let* ((parsed) (class)) (with-temp-buffer (insert-file-contents (concat phpinspect-test-php-file-directory "/NamespacedClass.php")) (setq phpinspect-current-buffer (phpinspect-make-buffer :buffer (current-buffer))) (setq parsed (phpinspect-buffer-parse phpinspect-current-buffer)) (let* ((class (seq-find #'phpinspect-class-p (seq-find #'phpinspect-namespace-p parsed))) (classname (car (cddadr class)))) (let ((tokens (phpinspect-buffer-tokens-enclosing-point phpinspect-current-buffer 617))) (should (eq classname (phpinspect-meta-token (car tokens)))) (should (phpinspect-declaration-p (phpinspect-meta-token (cadr tokens)))) (should (eq class (phpinspect-meta-token (caddr tokens))))))))) (ert-deftest phpinspect-parse-buffer-no-current () "Confirm that the parser is still functional with `phpinspect-current-buffer' unset." (let*((buffer) (parsed)) (with-temp-buffer (should-not phpinspect-current-buffer) (insert-file-contents (concat phpinspect-test-php-file-directory "/NamespacedClass.php")) (setq parsed (phpinspect-parse-current-buffer))) (should (cdr parsed)))) (cl-defstruct (phpinspect-document (:constructor phpinspect-make-document)) (buffer (get-buffer-create (generate-new-buffer-name "**phpinspect-document** shadow buffer") t) :type buffer :documentation "A hidden buffer with a reference version of the document.")) (cl-defmethod phpinspect-document-apply-edit ((document phpinspect-document) start end delta contents) (with-current-buffer (phpinspect-document-buffer document) (goto-char start) (delete-region (point) (- end delta)) (insert contents))) (cl-defmethod phpinspect-document-set-contents ((document phpinspect-document) (contents string)) (with-current-buffer (phpinspect-document-buffer document) (erase-buffer) (insert contents))) (defmacro phpinspect-document-setq-local (document &rest assignments) (declare (indent 1)) `(with-current-buffer (phpinspect-document-buffer ,document) (setq-local ,@assignments))) (defmacro phpinspect-with-document-buffer (document &rest body) (declare (indent 1)) `(with-current-buffer (phpinspect-document-buffer ,document) ,@body)) (cl-defmethod phpinspect-document-contents ((document phpinspect-document)) (with-current-buffer (phpinspect-document-buffer document) (buffer-string))) (ert-deftest phpinspect-buffer-parse-incrementally () (let* ((document (phpinspect-make-document)) (buffer (phpinspect-make-buffer :buffer (phpinspect-document-buffer document))) (parsed)) ;; TODO: write tests for more complicated cases (multiple edits, etc.) (phpinspect-document-set-contents document "bip->bop(bar($bim), $bom) } } }") (phpinspect-document-setq-local document phpinspect-current-buffer buffer) (phpinspect-with-document-buffer document (setq buffer-undo-list nil) (add-hook 'after-change-functions #'phpinspect-after-change-function)) (setq parsed (phpinspect-buffer-parse buffer 'no-interrupt)) ;; Delete lines before class (phpinspect-with-document-buffer document (goto-char 40) (kill-line) (kill-line) (kill-line)) (setq parsed-after (phpinspect-buffer-parse buffer 'no-interrupt)) (should (equal parsed parsed-after)) ;; Delete namespace declaration (phpinspect-with-document-buffer document (goto-char 9) (kill-line)) (setq parsed-after (phpinspect-buffer-parse buffer 'no-interrupt)) (setq current-tree (phpinspect-with-document-buffer document (goto-char (point-min)) (phpinspect-parse-buffer-until-point (current-buffer) (point-max)))) (should (equal current-tree parsed-after)) ;;Bring back the namespace declaration (phpinspect-with-document-buffer document (undo-start) (undo-more 1)) (setq parsed-after (phpinspect-buffer-parse buffer 'no-interrupt)) (should (equal parsed parsed-after))))