config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

flycheck-buttercup.el (5371B)


      1 ;;; flycheck-buttercup.el --- Flycheck: Extensions to Buttercup -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2017 Flycheck contributors
      4 ;; Copyright (C) 2016 Sebastian Wiesner and Flycheck contributors
      5 
      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
     10 
     11 ;; This file is not part of GNU Emacs.
     12 
     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.
     17 
     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.
     22 
     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/>.
     25 
     26 ;;; Commentary:
     27 
     28 ;; Extensions to Buttercup to write BDD tests for Flycheck.
     29 ;;
     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.
     33 ;;
     34 ;; This library provides extensions to Buttercup to write Specs for Flycheck.
     35 ;;
     36 ;; * Custom matchers
     37 ;;
     38 ;; (expect 'foo :to-be-local) - Is `foo' a local variable in the current buffer?
     39 
     40 ;;; Code:
     41 
     42 (require 'buttercup)
     43 (require 'flycheck)
     44 (require 'seq)
     45 
     46 
     47 ;;; Buttercup helpers
     48 
     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))
     52              errors "\n"))
     53 
     54 
     55 ;;; Data matchers
     56 
     57 (buttercup-define-matcher :to-be-empty-string (s)
     58   (let ((s (funcall s)))
     59     (if (equal 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)))))
     62 
     63 (buttercup-define-matcher :to-match-with-group (re s index match)
     64   (let* ((re (funcall re))
     65          (s (funcall s))
     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"
     72                         re s match index))
     73 
     74       (cons nil (format "Expected %S to match %S with %S in group %s, %s"
     75                         re s match index
     76                         (if matches?
     77                             (format "but got %S" result)
     78                           "but did not match"))))))
     79 
     80 
     81 ;;; Emacs feature matchers
     82 
     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"
     87                         buffer))
     88       (cons nil (format "Expected %S to be a live buffer, but it is not"
     89                         buffer)))))
     90 
     91 (buttercup-define-matcher :to-be-visible (buffer)
     92   (let ((buffer (get-buffer (funcall buffer))))
     93     (cond
     94      ((and buffer (get-buffer-window buffer))
     95       (cons t (format "Expected %S not to be a visible buffer, but it is"
     96                       buffer)))
     97      ((not (bufferp buffer))
     98       (cons nil
     99             (format "Expected %S to be a visible buffer, but it is not a buffer"
    100                     buffer)))
    101      (t (cons
    102          nil
    103          (format "Expected %S to be a visible buffer, but it is not visible"
    104                  buffer))))))
    105 
    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"
    110                         symbol))
    111       (cons nil (format "Expected %S to be a local variable, but it is not"
    112                         symbol)))))
    113 
    114 (buttercup-define-matcher :to-contain-match (buffer re)
    115   (let ((buffer (funcall buffer))
    116         (re (funcall re)))
    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
    121         (save-excursion
    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))))))))
    128 
    129 
    130 ;;; Flycheck matchers
    131 
    132 (buttercup-define-matcher :to-be-equal-flycheck-errors (a b)
    133   (let* ((a (funcall a))
    134          (b (funcall b))
    135          (a-formatted (flycheck-buttercup-format-error-list a))
    136          (b-formatted (flycheck-buttercup-format-error-list b)))
    137     (if (equal a b)
    138         (cons t (format "Expected
    139 %s
    140 not to be equal to
    141 %s" a-formatted b-formatted))
    142       (cons nil (format "Expected
    143 %s
    144 to be equal to
    145 %s" a-formatted b-formatted)))))
    146 
    147 (provide 'flycheck-buttercup)
    148 
    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
    152 
    153 ;; Local Variables:
    154 ;; no-byte-compile: t
    155 ;; End:
    156 
    157 ;;; flycheck-buttercup.el ends here