Permalink
Browse files

Initial cleanup of run/print.

  • Loading branch information...
1 parent 2db1b98 commit e33ed74c2c32439303cec19de0e9efe3ca777551 @rejeep rejeep committed Nov 17, 2012
View
@@ -0,0 +1,17 @@
+;;; ecukes-helpers.el --- Misc helpers
+
+(defun ecukes-feature-steps (features)
+ "Return all steps in all FEATURES."
+ (let* ((scenarios
+ (-flatten (--map (ecukes-feature-scenarios it) features)))
+ (backgrounds
+ (-reject 'null (--map (ecukes-feature-background it) features)))
+ (scenario-steps
+ (--map (ecukes-scenario-steps it) scenarios))
+ (background-steps
+ (--map (ecukes-background-steps it) backgrounds)))
+ (-flatten (-concat background-steps scenario-steps))))
+
+(provide 'ecukes-helpers)
+
+;;; ecukes-helpers.el ends here
View
@@ -7,6 +7,7 @@
(add-to-list 'load-path ecukes-path)
(require 's)
+(require 'dash)
(require 'ansi)
(require 'ecukes-project)
(require 'ecukes-template)
View
@@ -1,113 +1,182 @@
;;; ecukes-print.el --- Print various stuff on screen
+(require 'ecukes-stats)
+(require 'ecukes-template)
+
(defvar ecukes-print-offset 0
"Current indentation offset (number of spaces).")
-
-(defun ecukes-print-undefined-steps (steps)
- "Print STEPS as undefined."
- (ecukes-print-undefined-steps-title)
- (mapc 'ecukes-print-undefined-step steps))
-
-(defun ecukes-print-undefined-step (step)
- "Print STEP as undefined."
- (let ((name (ecukes-step-name step)))
- (string-match ecukes-parse-step-re name)
- (let* ((fn (match-string 1 name))
- (regex (ecukes-print-undefined-step-regex step))
- (args (ecukes-print-undefined-step-args step))
- (contents
- (ecukes-template-get
- 'step
- `(("regex" . ,regex)
- ("args" . ,args)
- ("fn" . ,fn)))))
- (ecukes-print-message (ansi-yellow contents))
- (ecukes-print-newline))))
-
-(defun ecukes-print-undefined-step-args (step)
- "Return STEP arguments as a list."
- (let* ((query (ecukes-steps-query step))
- (quotes
- (loop for sub on (cdr (split-string query "\""))
- by (function cddr)
- collect (car sub)))
- (num-quotes (length quotes)))
- (if (or (equal (ecukes-step-type step) 'table)
- (equal (ecukes-step-type step) 'py-string))
- (setq num-quotes (1+ num-quotes)))
- (mapconcat 'identity (make-list num-quotes "arg") " ")))
-
-(defun ecukes-print-undefined-step-regex (step)
- "Return STEP regex."
- (let* ((query (ecukes-steps-query step))
- (slashes "\\\\\\\\\\\\\\\\")
- (regex "\"[^\"]+\"")
- (rep (format "\\\\\\\\\"%s(.+%s)\\\\\\\\\"" slashes slashes)))
- (replace-regexp-in-string regex rep query)))
-
-(defun ecukes-print-undefined-steps-title ()
- "Print missing steps title."
+(defun ecukes-print-missing-steps (steps)
+ "Print missing steps"
+ (ecukes-print-missing-steps-header)
+ (let ((step-bodies))
+ (-each
+ steps
+ (lambda (step)
+ (let ((step-body (ecukes-print-step-body step))
+ (step-string (ecukes-print-step-string step)))
+ (unless (--any? (equal step-body it) step-bodies)
+ (add-to-list 'step-bodies step-body)
+ (ecukes-print-message "%s\n" step-string)))))))
+
+(defun ecukes-print-missing-steps-header ()
+ "Print missing steps header."
(ecukes-print-message
- (ansi-yellow
- "Some steps does not have a matching definition. Please implement the following step definitions:\n")))
+ (ansi-yellow "Some steps does not have a matching definition. Please implement the following step definitions:\n")))
+
+(defun ecukes-print-step-string (step)
+ "Return missing step string."
+ (let ((head (ecukes-step-head step))
+ (body (ecukes-print-step-body step))
+ (args (ecukes-print-step-args step)))
+ (ecukes-template-get
+ 'missing-step
+ `(("head" . ,head)
+ ("body" . ,body)
+ ("args" . ,args)))))
+
+(defun ecukes-print-step-args (step)
+ "Return args from STEP."
+ (let* ((result)
+ (arg (ecukes-step-arg step))
+ (args (ecukes-step-args step))
+ (type (ecukes-step-type step))
+ (args-count
+ (+
+ (length args)
+ (if (or
+ (equal type 'table)
+ (equal type 'py-string)) 1 0))))
+ (if (= args-count 1)
+ "arg"
+ (progn
+ (-dotimes
+ args-count
+ (lambda (n)
+ (add-to-list 'result (format "arg-%d" (1+ n)) t)))
+ (s-join " " result)))))
+
+(defun ecukes-print-step-body (step)
+ "Return body from STEP."
+ (let* ((body (ecukes-step-body step))
+ (args (ecukes-step-args step))
+ (result body))
+ (when args
+ (-each
+ args
+ (lambda (arg)
+ (setq result (s-replace (s-concat "\"" arg "\"") "\\\"\\\\([^\\\"]+\\\\)\\\"" result)))))
+ result))
(defun ecukes-print-intro (intro)
"Print INTRO."
- (setq ecukes-print-offset 0)
- (let ((header (ecukes-intro-header intro)))
- (ecukes-print-message "Feature: %s" header))
- (setq ecukes-print-offset 2)
- (dolist (description (ecukes-intro-description intro))
- (ecukes-print-message description))
- (ecukes-print-newline))
-
-(defun ecukes-print-newline ()
- "Print newline."
- (ecukes-print-message " "))
-
-(defun ecukes-print-message (string &rest objects)
- "Print message taking offset into account."
- (let* ((ascii-space 32)
- (spaces (make-string ecukes-print-offset ascii-space)))
- (apply 'message (cons (concat spaces string) objects))))
+ (let ((ecukes-print-offset 0)
+ (header (ecukes-intro-header intro)))
+ (ecukes-print-message "Feature: %s" header)
+ (let ((ecukes-print-offset 2)
+ (description (ecukes-intro-description intro)))
+ (--each description (ecukes-print-message it)))
+ (ecukes-print-newline)))
(defun ecukes-print-background-header ()
"Print background header."
- (ecukes-print-message "Background:"))
-
-(defun ecukes-print-step-success (step)
- "Print STEP as success."
- (ecukes-print-step step 'ansi-green))
-
-(defun ecukes-print-step-failure (step)
- "Print STEP as failure. Also print the error."
- (ecukes-print-step step 'ansi-red)
- (let ((err (ecukes-step-err step)))
- (ecukes-print-error err)))
-
-(defun ecukes-print-step-pending (step)
- "Print STEP as pending."
- (ecukes-print-step step 'ansi-yellow))
-
-(defun ecukes-print-error (err)
- "Print error messag ERR."
- (setq ecukes-print-offset (+ ecukes-print-offset 4))
- (ecukes-print-message (ansi-red err))
- (setq ecukes-print-offset (- ecukes-print-offset 4)))
-
-(defun ecukes-print-step (step color)
- "Print STEP in COLOR."
- (setq ecukes-print-offset (+ ecukes-print-offset 2))
- (let ((name (ecukes-step-name step)))
- (ecukes-print-message (funcall color name)))
- (setq ecukes-print-offset (- ecukes-print-offset 2)))
+ (let ((ecukes-print-offset 2))
+ (ecukes-print-message "Background:")))
(defun ecukes-print-scenario-header (scenario)
"Print SCENARIO header."
- (let ((name (ecukes-scenario-name scenario)))
+ (let ((name (ecukes-scenario-name scenario))
+ (ecukes-print-offset 2))
(ecukes-print-message "Scenario: %s" name)))
+(defun ecukes-print-step (step status)
+ "Print STEP in correct STATUS color."
+ (let ((name (ecukes-step-name step))
+ (err (ecukes-step-err step)))
+ (let ((ecukes-print-offset 4))
+ (ecukes-print-message
+ (ecukes-print-status name status)))
+ (if (eq (ecukes-step-type step) 'table)
+ (ecukes-print-table step status)
+ (if (eq (ecukes-step-type step) 'py-string)
+ (ecukes-print-py-string step status)))
+ (when (eq status 'failure)
+ (let ((ecukes-print-offset 6))
+ (-each
+ (s-lines (or err "Unknown error..."))
+ (lambda (line)
+ (ecukes-print-message
+ (ansi-red line))))))))
+
+(defun ecukes-print-table (step status)
+ "Print STEP table."
+ (let* ((table (ecukes-step-arg step))
+ (rows (length table))
+ (cols (length (car table)))
+ (widths))
+ (-dotimes
+ cols
+ (lambda (col)
+ (let ((width 0))
+ (-dotimes
+ rows
+ (lambda (row)
+ (setq width (max width (length (nth col (nth row table)))))))
+ (push width widths))))
+ (setq widths (reverse widths))
+ (-dotimes
+ rows
+ (lambda (row)
+ (let ((col-strings))
+ (-dotimes
+ cols
+ (lambda (col)
+ (let* ((orig (nth col (nth row table)))
+ (pad (- (nth col widths) (length orig)))
+ (col-string (s-concat (ecukes-print-status orig status) (s-repeat pad " "))))
+ (add-to-list 'col-strings col-string t 'eq))))
+ (let ((ecukes-print-offset 6))
+ (ecukes-print-message (s-concat "| " (s-join " | " col-strings) " |"))))))))
+
+(defun ecukes-print-py-string (step status)
+ "Print STEP py-string."
+ (let* ((arg (ecukes-step-arg step))
+ (lines (s-lines arg)))
+ (let ((ecukes-print-offset 6))
+ (ecukes-print-message
+ (ecukes-print-status "\"\"\"" status))
+ (-each
+ lines
+ (lambda (line)
+ (ecukes-print-message
+ (ecukes-print-status line status))))
+ (ecukes-print-message
+ (ecukes-print-status "\"\"\"" status)))))
+
+(defun ecukes-print-status (string status)
+ "Return STRING in correct color depending on STATUS."
+ (let ((color
+ (cond ((eq status 'success)
+ 'ansi-green)
+ ((eq status 'failure)
+ 'ansi-red)
+ ((eq status 'skipped)
+ 'ansi-cyan))))
+ (funcall color string)))
+
+(defun ecukes-print-stats-summary ()
+ "Print stats summary."
+ (ecukes-print-message (ecukes-stats-summary)))
+
+(defun ecukes-print-newline ()
+ "Print newline."
+ (ecukes-print-message " "))
+
+(defun ecukes-print-message (format-string &rest args)
+ "Print MESSAGE."
+ (let ((message (apply 'format (cons format-string args)))
+ (offset (s-repeat ecukes-print-offset " ")))
+ (message (s-concat offset message))))
(provide 'ecukes-print)
Oops, something went wrong.

0 comments on commit e33ed74

Please sign in to comment.