Fetching contributors…
Cannot retrieve contributors at this time
106 lines (83 sloc) 3.11 KB
;;; ecukes-steps.el --- Functions to define and call step definitions
(eval-when-compile (require 'cl))
(require 'ansi)
(require 'ecukes-parse)
(defvar ecukes-steps-definitions nil
"All defined step definitions.")
(defalias 'Given 'ecukes-steps-define-or-call-step
"Put the system in a known state.")
(defalias 'When 'ecukes-steps-define-or-call-step
"Describe the key action.")
(defalias 'Then 'ecukes-steps-define-or-call-step
"Observe outcomes.")
(defalias 'And 'ecukes-steps-define-or-call-step
"Make Given/When/Then read more fluently.")
(defalias 'But 'ecukes-steps-define-or-call-step
"Make Given/When/Then read more fluently.")
(defun ecukes-steps-define-or-call-step (name &rest args)
"Define or call step.
When *defining* a step, argument takes the following form:
where STEP-REGEXP is a regular expression defining a step and
FUNCTION is the definition of the step. You can optionally
give a docstring DOC as the second argument.
When *calling* a step, argument takes the following form:
(let ((fn (car (last args)))
(doc (when (= (length args) 2) (car args))))
(if (functionp fn)
;; `buffer-file-name' is for the case evaluated interactively.
(ecukes-steps-define name fn doc
(or load-file-name buffer-file-name))
(ecukes-steps-call name args))))
(put 'ecukes-steps-define-or-call-step 'lisp-indent-function 'defun)
(put 'ecukes-steps-define-or-call-step 'doc-string-elt 2)
(defun ecukes-steps-define (regex fn &optional doc file)
"Define step."
(unless (-any?
(lambda (step-def)
(equal regex step-def)) ecukes-steps-definitions)
(make-ecukes-step-def :regex regex :fn fn :doc doc :file file))))
(defun ecukes-steps-call (name args)
"Call step"
(let* ((query (apply 'format (cons name args)))
(step-def (ecukes-steps-find query)))
(if step-def
(apply (ecukes-step-def-fn step-def)
(or args
(make-ecukes-step :body name))))
(error (ansi-red "Step not defined: `%s`" query)))))
(defun ecukes-steps-without-definition (steps)
"Return from STEPS those who have not been defined."
(lambda (step)
(ecukes-steps-find (ecukes-step-body step))) steps))
(defun ecukes-steps-find (name)
"Find step by name."
(lambda (step-def)
(s-matches? (ecukes-step-def-regex step-def) name))
(defun ecukes-steps-args (step)
"Return args from step BODY."
(let* ((body (ecukes-step-body step))
(step-def (ecukes-steps-find body)))
(if step-def
(cdr (s-match (ecukes-step-def-regex step-def) body))
(loop for sub on (cdr (split-string body "\""))
by (function cddr)
collect (car sub)))))
(provide 'ecukes-steps)
;;; ecukes-steps.el ends here