Package list ido-ubiquitous / e49d378b-74dc-4847-aa0e-47e48d80ecba/main tests-with-flx-ido / test-ido-completing-read+-with-flx-ido.el

Tree @e49d378b-74dc-4847-aa0e-47e48d80ecba/main (Download .tar.gz)

test-ido-completing-read+-with-flx-ido.el @e49d378b-74dc-4847-aa0e-47e48d80ecba/mainraw · history · blame

;;; -*- lexical-binding: t -*-

;; This file contains tests specifically for ido-cr+ interoperating
;; with flx-ido. These tests were split out from the main test file,
;; which unfortunately means that they brought a lot of the
;; scaffolding from the main test file with them. This might get
;; cleaned up at a later date. Putting these in a separate file is
;; necessary so that the main test suite can be both with and without
;; flx-ido loaded.

(require 'ido)
(require 'flx-ido)
(require 'minibuf-eldef)
(require 'ido-completing-read+)
(require 'buttercup)
(require 'cl-lib)
(require 'with-simulated-input)

(defun collection-as-function (collection)
  "Return a function equivalent to COLLECTION.

The returned function will work equivalently to COLLECTION when
passed to `all-completions' and `try-completion'."
  (completion-table-dynamic (lambda (string) (all-completions string collection))))

(defun shadow-var (var &optional temp-value)
  "Shadow the value of VAR.

This will push the current value of VAR to VAR's
`shadowed-values' property, and then set it to TEMP-VALUE. To
reverse this process, call `unshadow-var' on VAR. Vars can
be shadowed recursively, and must be unshadowed once for each
shadowing in order to restore the original value. You can think
of shadowing as dynamic binding with `let', but with manual
control over when bindings start and end.

If VAR is a Custom variable (see `custom-variable-p'), it will be
set using `customize-set-variable', and if TEMP-VALUE is nil it
will be replaces with VAR's standard value.

 Other variables will be set with `set-default', and a TEMP-VALUE
 of nil will not be treated specially.

`shadow-var' only works on variables declared as special (i.e.
using `defvar' or similar). It will not work on lexically bound
  (unless (special-variable-p var)
    (error "Cannot shadow lexical var `%s'" var))
  (let* ((use-custom (custom-variable-p var))
         (setter (if use-custom 'customize-set-variable 'set-default))
         (temp-value (or temp-value
                         (and use-custom
                              (eval (car (get var 'standard-value)))))))
    ;; Push the current value on the stack
    (push (symbol-value var) (get var 'shadowed-values))
    (funcall setter var temp-value)))

(defun var-shadowed-p (var)
  "Return non-nil if VAR is shadowed by `shadow-var'."
  ;; We don't actually want to return that list if it's non-nil.
  (and (get var 'shadowed-values) t))

(defun unshadow-var (var)
  "Reverse the last call to `shadow-var' on VAR."
  (if (var-shadowed-p var)
      (let* ((use-custom (custom-variable-p var))
             (setter (if use-custom 'customize-set-variable 'set-default))
             (value (pop (get var 'shadowed-values))))
        (funcall setter var value))
    (error "Var is not shadowed: %s" var)))

(defun fully-unshadow-var (var)
  "Reverse *all* calls to `shadow-var' on VAR."
  (when (var-shadowed-p var)
    (let* ((use-custom (custom-variable-p var))
           (setter (if use-custom 'customize-set-variable 'set-default))
           (value (car (last (get var 'shadowed-values)))))
      (put var 'shadowed-values nil)
      (funcall setter var value))))

(defun fully-unshadow-all-vars (&optional vars)
  "Reverse *all* calls to `shadow-var' on VARS.

If VARS is nil, unshadow *all* variables."
  (if vars
      (mapc #'fully-unshadow-var vars)
    (mapatoms #'fully-unshadow-var))

(defmacro shadow-vars (varlist)
  "Shadow a list of vars with new values.

VARLIST describes the variables to be shadowed with the same
syntax as `let'.

See `shadow-var'."
  (declare (indent 0))
   with var = nil
   with value = nil
   for binding in varlist
   if (symbolp binding)
   do (setq var binding
            value nil)
   do (setq var (car binding)
            value (cadr binding))
   collect `(shadow-var ',var ,value) into exprs
   finally return `(progn ,@exprs)))

(defmacro unshadow-vars (vars)
  "Un-shadow a list of VARS.

This is a macro for consistency with `shadow-vars', but it will
also accept a quoted list for the sake of convenience."
  (declare (indent 0))
  (when (eq (car vars) 'quote)
    (setq vars (eval vars)))
  `(mapc #'unshadow-var ',vars))

(describe "Within the `ido-completing-read+' package"

  ;; Reset all of these variables to their standard values before each
  ;; test, saving the previous values for later restoration.
     ((ido-mode t)
      (ido-ubiquitous-mode t)
      (ido-cr+-debug-mode t)
      (minibuffer-electric-default-mode t)))

    ;; Suppress all messages during tests
    (spy-on 'message))

  ;; Restore the saved values after each test

  (describe "the `ido-completing-read+' function"

    (describe "with dynamic collections"
        (setq my-dynamic-collection
               (lambda (text)
                  ;; Sub-completions for "hello"
                  ((s-prefix-p "hello" text)
                   '("hello" "hello-world" "hello-everyone" "hello-universe"))
                  ;; Sub-completions for "goodbye"
                  ((s-prefix-p "goodbye" text)
                   '("goodbye" "goodbye-world" "goodbye-everyone" "goodbye-universe"))
                  ;; General completions
                   '("hello" "goodbye" "helicopter" "helium" "goodness" "goodwill")))))))
        (setq my-dynamic-collection nil))
        (setq ido-enable-flex-matching t
              ido-confirm-unique-completion nil)
        (spy-on 'ido-cr+-update-dynamic-collection

      (describe "with flx-ido-mode"
          (flx-ido-mode 1)

        (it "should allow selection of dynamically-added completions"
           (with-simulated-input "hello-w RET"
             (ido-completing-read+ "Say something: " my-dynamic-collection))
           :to-equal "hello-world")
          (expect 'ido-cr+-update-dynamic-collection

        (it "should allow ido flex-matching of dynamically-added completions"
           (with-simulated-input "hello-ld RET"
             (ido-completing-read+ "Say something: " my-dynamic-collection))
          (expect 'ido-cr+-update-dynamic-collection

        (it "should do a dynamic update when pressing TAB"
           (with-simulated-input "h TAB -ld RET"
             (ido-completing-read+ "Say something: " my-dynamic-collection))
          (expect 'ido-cr+-update-dynamic-collection

        (it "should do a dynamic update when idle"
                 (wsi-simulate-idle-time (1+ ido-cr+-dynamic-update-idle-time))
                 "-ld RET")
             (ido-completing-read+ "Say something: " my-dynamic-collection))
          (expect 'ido-cr+-update-dynamic-collection

        (it "should do a dynamic update when there is only one match remaining"
           (with-simulated-input "hell-ld RET"
             (ido-completing-read+ "Say something: " my-dynamic-collection))
          (expect 'ido-cr+-update-dynamic-collection

        (it "should not exit with a unique match if new matches are dynamically added"
           (with-simulated-input '("hell TAB -ld RET")
             (ido-completing-read+ "Say something: " my-dynamic-collection))
          (expect 'ido-cr+-update-dynamic-collection

        (it "should exit with a match that is still unique after dynamic updating"
           (with-simulated-input '("helic TAB")
             (ido-completing-read+ "Say something: " my-dynamic-collection))
          (expect 'ido-cr+-update-dynamic-collection

        (it "should respect `ido-restrict-to-matches' when doing dynamic updates"
          (let ((collection
                 (list "aaa-ddd-ggg" "aaa-eee-ggg" "aaa-fff-ggg"
                       "bbb-ddd-ggg" "bbb-eee-ggg" "bbb-fff-ggg"
                       "ccc-ddd-ggg" "ccc-eee-ggg" "ccc-fff-ggg"
                       "aaa-ddd-hhh" "aaa-eee-hhh" "aaa-fff-hhh"
                       "bbb-ddd-hhh" "bbb-eee-hhh" "bbb-fff-hhh"
                       "ccc-ddd-hhh" "ccc-eee-hhh" "ccc-fff-hhh"
                       "aaa-ddd-iii" "aaa-eee-iii" "aaa-fff-iii"
                       "bbb-ddd-iii" "bbb-eee-iii" "bbb-fff-iii"
                       "ccc-ddd-iii" "ccc-eee-iii" "ccc-fff-iii")))
            ;; Test the internal function
              (list (cons nil "bbb")
                    (cons nil "eee")))
             :to-equal '("bbb-eee-ggg" "bbb-eee-hhh" "bbb-eee-iii"))

            ;; First verify it without a dynamic collection
             (with-simulated-input "eee C-SPC bbb C-SPC ggg RET"
                "Pick: " collection nil t nil nil (car collection)))
             :to-equal "bbb-eee-ggg")
            ;; Now test the same with a dynamic collection
             (with-simulated-input "eee C-SPC bbb C-SPC ggg RET"
                "Pick: " (collection-as-function collection) nil t nil nil (car collection)))
             :to-equal "bbb-eee-ggg")))))))

;;; test-ido-completing-read+.el ends here