1 ;;; flycheck-buttercup.el --- Flycheck: Extensions to Buttercup -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2017 Flycheck contributors
4 ;; Copyright (C) 2016 Sebastian Wiesner and Flycheck contributors
6 ;; Author: Sebastian Wiesner <swiesner@lunaryorn.com>
7 ;; Maintainer: Clément Pit-Claudel <clement.pitclaudel@live.com>
8 ;; fmdkdd <fmdkdd@gmail.com>
9 ;; Keywords: lisp, tools
11 ;; This file is not part of GNU Emacs.
13 ;; This program is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
28 ;; Extensions to Buttercup to write BDD tests for Flycheck.
30 ;; Buttercup is a BDD testing framework for Emacs, see URL
31 ;; `https://github.com/jorgenschaefer/emacs-buttercup/'. Flycheck uses
32 ;; Buttercup extensively for new tests.
34 ;; This library provides extensions to Buttercup to write Specs for Flycheck.
38 ;; (expect 'foo :to-be-local) - Is `foo' a local variable in the current buffer?
49 (defun flycheck-buttercup-format-error-list (errors)
50 "Format ERRORS into a human-readable string."
51 (mapconcat (lambda (e) (flycheck-error-format e 'with-file-name))
57 (buttercup-define-matcher :to-be-empty-string (s)
58 (let ((s (funcall s)))
60 (cons t (format "Expected %S not be an empty string" s))
61 (cons nil (format "Expected %S to be an empty string" s)))))
63 (buttercup-define-matcher :to-match-with-group (re s index match)
64 (let* ((re (funcall re))
66 (index (funcall index))
67 (match (funcall match))
68 (matches? (string-match re s))
69 (result (and matches? (match-string index s))))
70 (if (and matches? (equal result match))
71 (cons t (format "Expected %S not to match %S with %S in group %s"
74 (cons nil (format "Expected %S to match %S with %S in group %s, %s"
77 (format "but got %S" result)
78 "but did not match"))))))
81 ;;; Emacs feature matchers
83 (buttercup-define-matcher :to-be-live (buffer)
84 (let ((buffer (get-buffer (funcall buffer))))
85 (if (buffer-live-p buffer)
86 (cons t (format "Expected %S not to be a live buffer, but it is"
88 (cons nil (format "Expected %S to be a live buffer, but it is not"
91 (buttercup-define-matcher :to-be-visible (buffer)
92 (let ((buffer (get-buffer (funcall buffer))))
94 ((and buffer (get-buffer-window buffer))
95 (cons t (format "Expected %S not to be a visible buffer, but it is"
97 ((not (bufferp buffer))
99 (format "Expected %S to be a visible buffer, but it is not a buffer"
103 (format "Expected %S to be a visible buffer, but it is not visible"
106 (buttercup-define-matcher :to-be-local (symbol)
107 (let ((symbol (funcall symbol)))
108 (if (local-variable-p symbol)
109 (cons t (format "Expected %S not to be a local variable, but it is"
111 (cons nil (format "Expected %S to be a local variable, but it is not"
114 (buttercup-define-matcher :to-contain-match (buffer re)
115 (let ((buffer (funcall buffer))
117 (if (not (get-buffer buffer))
118 (cons nil (format "Expected %S to contain a match of %s, \
119 but is not a buffer" buffer re))
120 (with-current-buffer buffer
122 (goto-char (point-min))
123 (if (re-search-forward re nil 'noerror)
124 (cons t (format "Expected %S to contain a match \
125 for %s, but it did not" buffer re))
126 (cons nil (format "Expected %S not to contain a match for \
127 %s but it did not." buffer re))))))))
130 ;;; Flycheck matchers
132 (buttercup-define-matcher :to-be-equal-flycheck-errors (a b)
133 (let* ((a (funcall a))
135 (a-formatted (flycheck-buttercup-format-error-list a))
136 (b-formatted (flycheck-buttercup-format-error-list b)))
138 (cons t (format "Expected
141 %s" a-formatted b-formatted))
142 (cons nil (format "Expected
145 %s" a-formatted b-formatted)))))
147 (provide 'flycheck-buttercup)
149 ;; Disable byte compilation for this library, to prevent package.el choking on a
150 ;; missing `buttercup' library. See
151 ;; https://github.com/flycheck/flycheck/issues/860
154 ;; no-byte-compile: t
157 ;;; flycheck-buttercup.el ends here