Skip to content

Commit

Permalink
automatically test all rules for ag too
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktasia committed Mar 1, 2016
1 parent 57bba57 commit 3af4322
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
26 changes: 22 additions & 4 deletions dumb-jump.el
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,22 @@
:group 'dumb-jump)

(defcustom dumb-jump-find-rules
'((:type "function" :language "elisp" :regex "\\\(defun\\s+JJJ\\j\\s*"
'((:type "function" :language "elisp" :regex "\\\(defun\\s+JJJ\\j"
;; \\j usage see `dumb-jump-ag-word-boundary`
:tests ("(defun test (blah)" "(defun test\n"))

(:type "variable" :language "elisp"
:regex "\\\(defvar\\b\\s*JJJ\\b\\s?" :tests ("(defvar test " "(defvar test\n"))
:regex "\\\(defvar\\b\\s*JJJ\\j" :tests ("(defvar test " "(defvar test\n"))

(:type "variable" :language "elisp"
:regex "\\\(defcustom\\b\\s*JJJ\\b\\s?" :tests ("(defcustom test " "(defcustom test\n"))
:regex "\\\(defcustom\\b\\s*JJJ\\j" :tests ("(defcustom test " "(defcustom test\n"))

(:type "variable" :language "elisp"
:regex "\\\(setq\\b\\s*JJJ\\b\\s*" :tests ("(setq test 123)"))
:regex "\\\(setq\\b\\s*JJJ\\j" :tests ("(setq test 123)"))
(:type "variable" :language "elisp"
:regex "\\\(JJJ\\s+" :tests ("(let ((test 123)))"))

;; variable in method signature
(:type "variable" :language "elisp"
:regex "\\(defun\\s*.+\\\(?\\s*JJJ\\b\\s*\\\)?"
:tests ("(defun blah (test)" "(defun blah (test blah)" "(defun (blah test)"))
Expand Down Expand Up @@ -265,6 +266,23 @@ Optionally pass t to see a list of all failed rules"
))))
failures))

(defun dumb-jump-test-ag-rules ()
"Test all the rules and return count of those that fail
Optionally pass t to see a list of all failed rules"
(let ((failures '())
(fail-tmpl "FAILURE '%s' not in response '%s' | CMD: '%s' | rule: '%s'"))
(-each dumb-jump-find-rules
(lambda (rule)
(-each (plist-get rule :tests)
(lambda (test)
(let* ((cmd (concat " echo '" test "' | ag --nocolor --nogroup \""
(dumb-jump-populate-regex (plist-get rule :regex) "test" nil) "\""))
(resp (shell-command-to-string cmd)))
(when (not (s-contains? test resp))
(add-to-list 'failures (format fail-tmpl test resp cmd rule))))
))))
failures))

(defun dumb-jump-message (str &rest args)
"Log message to the *Messages* buffer if not using dumb-jump-quiet"
(when (not dumb-jump-quiet)
Expand Down
23 changes: 17 additions & 6 deletions test/dumb-jump-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,35 @@

(ert-deftest dumb-jump-generate-grep-command-no-ctx-test ()
(let ((regexes (dumb-jump-get-contextual-regexes "elisp" nil))
(expected "LANG=C grep -REn --include \\*.el --include \\*.el.gz -e '\\(defun\\s+tester\\b\\s*' -e '\\(defvar\\b\\s*tester\\b\\s?' -e '\\(defcustom\\b\\s*tester\\b\\s?' -e '\\(setq\\b\\s*tester\\b\\s*' -e '\\(tester\\s+' -e '\\(defun\\s*.+\\(?\\s*tester\\b\\s*\\)?' ."))
(expected "LANG=C grep -REn --include \\*.el --include \\*.el.gz -e '\\(defun\\s+tester\\b' -e '\\(defvar\\b\\s*tester\\b' -e '\\(defcustom\\b\\s*tester\\b' -e '\\(setq\\b\\s*tester\\b' -e '\\(tester\\s+' -e '\\(defun\\s*.+\\(?\\s*tester\\b\\s*\\)?' ."))
(should (string= expected (dumb-jump-generate-grep-command "tester" "blah.el" "." regexes "elisp" nil)))))

(ert-deftest dumb-jump-generate-ag-command-no-ctx-test ()
(let ((regexes (dumb-jump-get-contextual-regexes "elisp" nil))
(expected "ag --nocolor --nogroup \"\\(defun\\s+tester(?![\\w-])\\s*|\\(defvar\\b\\s*tester\\b\\s?|\\(defcustom\\b\\s*tester\\b\\s?|\\(setq\\b\\s*tester\\b\\s*|\\(tester\\s+|\\(defun\\s*.+\\(?\\s*tester\\b\\s*\\)?\" ."))
(expected "ag --nocolor --nogroup \"\\(defun\\s+tester(?![\\w-])|\\(defvar\\b\\s*tester(?![\\w-])|\\(defcustom\\b\\s*tester(?![\\w-])|\\(setq\\b\\s*tester(?![\\w-])|\\(tester\\s+|\\(defun\\s*.+\\(?\\s*tester\\b\\s*\\)?\" ."))
(should (string= expected (dumb-jump-generate-ag-command "tester" "blah.el" "." regexes "elisp" nil)))))

(ert-deftest dumb-jump-generate-grep-command-no-ctx-funcs-only-test ()
(let* ((dumb-jump-functions-only t)
(regexes (dumb-jump-get-contextual-regexes "elisp" nil))
(expected "LANG=C grep -REn -e '\\(defun\\s+tester\\b\\s*' .")
(zexpected "LANG=C zgrep -REn -e '\\(defun\\s+tester\\b\\s*' ."))
(expected "LANG=C grep -REn -e '\\(defun\\s+tester\\b' .")
(zexpected "LANG=C zgrep -REn -e '\\(defun\\s+tester\\b' ."))
(should (string= expected (dumb-jump-generate-grep-command "tester" "blah.el" "." regexes "" nil)))
(should (string= zexpected (dumb-jump-generate-grep-command "tester" "blah.el.gz" "." regexes "" nil)))))

(ert-deftest dumb-jump-generate-grep-command-with-ctx-test ()
(let* ((ctx-type (dumb-jump-get-ctx-type-by-language "elisp" '(:left "(" :right nil)))
(dumb-jump-ignore-context nil) ;; overriding the default
(regexes (dumb-jump-get-contextual-regexes "elisp" ctx-type))
(expected "LANG=C grep -REn -e '\\(defun\\s+tester\\b\\s*' ."))
(expected "LANG=C grep -REn -e '\\(defun\\s+tester\\b' ."))
;; the point context being passed should match a "function" type so only the one command
(should (string= expected (dumb-jump-generate-grep-command "tester" "blah.el" "." regexes "" nil)))))

(ert-deftest dumb-jump-generate-grep-command-with-ctx-but-ignored-test ()
(let* ((ctx-type (dumb-jump-get-ctx-type-by-language "elisp" '(:left "(" :right nil)))
(dumb-jump-ignore-context t)
(regexes (dumb-jump-get-contextual-regexes "elisp" ctx-type))
(expected "LANG=C grep -REn -e '\\(defun\\s+tester\\b\\s*' -e '\\(defvar\\b\\s*tester\\b\\s?' -e '\\(defcustom\\b\\s*tester\\b\\s?' -e '\\(setq\\b\\s*tester\\b\\s*' -e '\\(tester\\s+' -e '\\(defun\\s*.+\\(?\\s*tester\\b\\s*\\)?' ."))
(expected "LANG=C grep -REn -e '\\(defun\\s+tester\\b' -e '\\(defvar\\b\\s*tester\\b' -e '\\(defcustom\\b\\s*tester\\b' -e '\\(setq\\b\\s*tester\\b' -e '\\(tester\\s+' -e '\\(defun\\s*.+\\(?\\s*tester\\b\\s*\\)?' ."))

;; the point context being passed is ignored so ALL should return
(should (string= expected (dumb-jump-generate-grep-command "tester" "blah.el" "." regexes "" nil)))))
Expand Down Expand Up @@ -148,13 +148,24 @@
(let ((rule-failures (dumb-jump-test-rules)))
(should (= (length rule-failures) 0))))

(ert-deftest dumb-jump-test-ag-rules-test ()
(let ((rule-failures (dumb-jump-test-ag-rules)))
(should (= (length rule-failures) 0))))

(ert-deftest dumb-jump-test-rules-fail-test ()
(let* ((bad-rule '(:type "variable" :language "elisp" :regex "\\\(defvarJJJ\\b\\s*" :tests ("(defvar test ")))
(dumb-jump-find-rules (cons bad-rule dumb-jump-find-rules))
(rule-failures (dumb-jump-test-rules)))
;(message "%s" (prin1-to-string rule-failures))
(should (= (length rule-failures) 1))))

(ert-deftest dumb-jump-test-ag-rules-fail-test ()
(let* ((bad-rule '(:type "variable" :language "elisp" :regex "\\\(defvarJJJ\\b\\s*" :tests ("(defvar test ")))
(dumb-jump-find-rules (cons bad-rule dumb-jump-find-rules))
(rule-failures (dumb-jump-test-ag-rules)))
;(message "%s" (prin1-to-string rule-failures))
(should (= (length rule-failures) 1))))

(ert-deftest dumb-jump-match-test ()
(should (not (dumb-jump-re-match nil "asdf")))
(should (dumb-jump-re-match "^asdf$" "asdf"))
Expand Down

0 comments on commit 3af4322

Please sign in to comment.