Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concise step definitions #132

Closed
wants to merge 8 commits into from
10 changes: 10 additions & 0 deletions ecukes-load.el
Expand Up @@ -8,6 +8,16 @@
(require 'ecukes-project)
(require 'ecukes-core)

(defvar ecukes-concise-keywords-alist '()
"Keywords which could be a part of step definition body and
which while parsing replaces by appropriate regexps.")

(defun ecukes-define-keyword (name pattern)
"Define new keyword. Add keyword with NAME and PATTERN to the
`ecukes-concise-keywords-alist'."
(let ((keyword-list (list name pattern)))
(add-to-list 'ecukes-concise-keywords-alist keyword-list)))

(defun ecukes-load ()
"Load support and step definitions."
(unless (f-dir? (ecukes-project-features-path))
Expand Down
15 changes: 13 additions & 2 deletions ecukes-parse.el
Expand Up @@ -41,7 +41,6 @@
"^\\s-*|.+|"
"Regexp matching table.")


(defun ecukes-parse-feature (feature-file)
"Parse FEATURE-FILE."
(with-temp-buffer
Expand Down Expand Up @@ -206,9 +205,21 @@
((ecukes-parse-table-step-p)
(setq arg (ecukes-parse-table-step))
(setq type 'table))
(t (setq type 'regular)))
(t (setq type 'regular)
(setq body (ecukes-parse-body-concise-keywords body))))
(make-ecukes-step :name name :head head :body body :type type :arg arg)))

(defun ecukes-parse-body-concise-keywords (body)
"Replace keywords in a BODY by regexps. Keywords and regexps
defined in the `ecukes-concise-keywords-alist'."
(setq case-fold-search nil)
(--each ecukes-concise-keywords-alist
(let ((keyword (car it))
(regexp (cadr it)))
(while (s-match keyword body)
(setq body (s-replace keyword regexp body)))))
body)

(defun ecukes-parse-table-step-p ()
"Check if step is a table step or not."
(save-excursion
Expand Down
33 changes: 33 additions & 0 deletions test/ecukes-parse-regular-step-test.el
Expand Up @@ -44,3 +44,36 @@
(should (eq type 'regular))
(should (eq type 'regular))
(should (equal name "But observe outcomes")))))

(ert-deftest parse-regular-step-body-with-keywords ()
"Should parse concise body."
(ecukes-define-keyword "NAME" "\"\\(.+\\)\"")
(ecukes-define-keyword "TEXT" "\"\\(.+\\)\"")
(ecukes-define-keyword "POS" "\\([0-9]+\\)")
(ecukes-define-keyword "MODE" "\\(.+\\)")
(with-parse-step
"body-with-keywords"
(lambda (name head body type arg)
(should (eq type 'regular))
(should (equal name "And input TEXT text another NAME and POS and MODE"))
(should (equal body (format "input \"\\(.+\\)\" text another \"\\(.+\\)\" and \\([0-9]+\\) and \\(.+\\)"))))))

(ert-deftest parse-regular-step-concise-body-several-same-keywords ()
"Should parse concise body."
(ecukes-define-keyword "TEXT" "\"\\(.+\\)\"")
(with-parse-step
"body-several-same-type-keywords"
(lambda (name head body type arg)
(should (eq type 'regular))
(should (equal name "And input TEXT text another TEXT and TEXT"))
(should (equal body (format "input \"\\(.+\\)\" text another \"\\(.+\\)\" and \"\\(.+\\)\""))))))

(ert-deftest parse-regular-step-concise-body-contents ()
"Should parse concise body."
(ecukes-define-keyword " CONTENTS" "\\(?: \"\\(.*\\)\"\\|:\\)")
(with-parse-step
"body-contents"
(lambda (name head body type arg)
(should (eq type 'regular))
(should (equal name "And input CONTENTS and CONTENTS and CONTENTS with action"))
(should (equal body "input\\(?: \"\\(.*\\)\"\\|:\\) and\\(?: \"\\(.*\\)\"\\|:\\) and\\(?: \"\\(.*\\)\"\\|:\\) with action")))))
2 changes: 2 additions & 0 deletions test/fixtures/step/body-contents.feature
@@ -0,0 +1,2 @@
Scenario: Some scenario
And input CONTENTS and CONTENTS and CONTENTS with action
2 changes: 2 additions & 0 deletions test/fixtures/step/body-several-same-type-keywords.feature
@@ -0,0 +1,2 @@
Scenario: Some scenario
And input TEXT text another TEXT and TEXT
2 changes: 2 additions & 0 deletions test/fixtures/step/body-with-keywords.feature
@@ -0,0 +1,2 @@
Scenario: Some scenario
And input TEXT text another NAME and POS and MODE