Skip to content

Commit

Permalink
optional :not list of things that should not match
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktasia committed Mar 2, 2016
1 parent 3af4322 commit ee45d61
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
4 changes: 1 addition & 3 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

1)
;; TODO: fix off my one error with defvar use special \\j
;; TODO: have method to test ag regex rules too
;; TODO: When there's multiple options use the compilations view for selecting the file
;; TODO: built-in :tests for contexts like rules
;; TODO: split up context type searches if only one or both apply?

2)
;; TODO: support es6 javascript - (add to list of easy PRs)
;; TODO: rules should have (optional?) tests that fail :fails

3)
;; TODO: make dumb-jump-test-rules run on boot? or interactive
Expand Down
35 changes: 22 additions & 13 deletions dumb-jump.el
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@

(defcustom dumb-jump-ag-word-boundary
"(?![\\w-])"
"`\\b` acts differently in ag vs grep. When this matters use `\\j` instead. grep will do `\\b` and ag this value"
"`\\b` thinks `-` is a word boundary. When this matters use `\\j` instead and ag will use this value."
:group 'dumb-jump)

(defcustom dumb-jump-grep-word-boundary
"(?:$|[^\\w-])"
"`\\b` thinks `-` is a word boundary. When this matters use `\\j` instead and grep will use this value."
:group 'dumb-jump)

(defcustom dumb-jump-force-grep
Expand Down Expand Up @@ -98,7 +103,7 @@
(defcustom dumb-jump-find-rules
'((:type "function" :language "elisp" :regex "\\\(defun\\s+JJJ\\j"
;; \\j usage see `dumb-jump-ag-word-boundary`
:tests ("(defun test (blah)" "(defun test\n"))
:tests ("(defun test (blah)" "(defun test\n") :not ("(defun test-asdf (blah)" "(defun test-blah\n"))

(:type "variable" :language "elisp"
:regex "\\\(defvar\\b\\s*JJJ\\j" :tests ("(defvar test " "(defvar test\n"))
Expand Down Expand Up @@ -249,36 +254,40 @@ immediately to the right of a symbol then it's probably a function call"
(setq cur-pos (1- cur-pos)))))
(1+ cur-pos)))

(defun dumb-jump-test-rules ()
(defun dumb-jump-test-rules (&optional run-not-tests)
"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'"))
(fail-tmpl "grep %s FAILURE '%s' not in response '%s' | CMD: '%s' | regex: '%s'"))
(-each dumb-jump-find-rules
(lambda (rule)
(-each (plist-get rule :tests)
(-each (plist-get rule (if run-not-tests :not :tests))
(lambda (test)
(let* ((cmd (concat " echo '" test "' | grep -En -e '"
(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))))
(when (or
(and (not run-not-tests) (not (s-contains? test resp)))
(and run-not-tests (> (length resp) 0)))
(add-to-list 'failures (format fail-tmpl (if run-not-tests "not" "") test resp cmd (plist-get rule :regex)))))
))))
failures))

(defun dumb-jump-test-ag-rules ()
(defun dumb-jump-test-ag-rules (&optional run-not-tests)
"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'"))
(fail-tmpl "ag FAILURE '%s' not in response '%s' | CMD: '%s' | rule: '%s'"))
(-each dumb-jump-find-rules
(lambda (rule)
(-each (plist-get rule :tests)
(-each (plist-get rule (if run-not-tests :not :tests))
(lambda (test)
(let* ((cmd (concat " echo '" test "' | ag --nocolor --nogroup \""
(dumb-jump-populate-regex (plist-get rule :regex) "test" nil) "\""))
(dumb-jump-populate-regex (plist-get rule :regex) "test" t) "\""))
(resp (shell-command-to-string cmd)))
(when (not (s-contains? test resp))
(when (or
(and (not run-not-tests) (not (s-contains? test resp)))
(and run-not-tests (> (length resp) 0)))
(add-to-list 'failures (format fail-tmpl test resp cmd rule))))
))))
failures))
Expand Down Expand Up @@ -645,7 +654,7 @@ denoter file/dir is found or uses dumb-jump-default-profile"

(defun dumb-jump-populate-regex (it look-for use-ag)
"Populate IT regex template with LOOK-FOR"
(s-replace "\\j" (if use-ag dumb-jump-ag-word-boundary "\\b")
(s-replace "\\j" (if use-ag dumb-jump-ag-word-boundary dumb-jump-grep-word-boundary)
(s-replace "JJJ" (regexp-quote look-for) it)))

(defun dumb-jump-populate-regexes (look-for regexes use-ag)
Expand Down
18 changes: 13 additions & 5 deletions test/dumb-jump-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

(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' -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*\\)?' ."))
(expected "LANG=C grep -REn --include \\*.el --include \\*.el.gz -e '\\(defun\\s+tester(?:$|[^\\w-])' -e '\\(defvar\\b\\s*tester(?:$|[^\\w-])' -e '\\(defcustom\\b\\s*tester(?:$|[^\\w-])' -e '\\(setq\\b\\s*tester(?:$|[^\\w-])' -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 ()
Expand All @@ -60,24 +60,24 @@
(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' .")
(zexpected "LANG=C zgrep -REn -e '\\(defun\\s+tester\\b' ."))
(expected "LANG=C grep -REn -e '\\(defun\\s+tester(?:$|[^\\w-])' .")
(zexpected "LANG=C zgrep -REn -e '\\(defun\\s+tester(?:$|[^\\w-])' ."))
(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' ."))
(expected "LANG=C grep -REn -e '\\(defun\\s+tester(?:$|[^\\w-])' ."))
;; 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' -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*\\)?' ."))
(expected "LANG=C grep -REn -e '\\(defun\\s+tester(?:$|[^\\w-])' -e '\\(defvar\\b\\s*tester(?:$|[^\\w-])' -e '\\(defcustom\\b\\s*tester(?:$|[^\\w-])' -e '\\(setq\\b\\s*tester(?:$|[^\\w-])' -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 @@ -152,6 +152,14 @@
(let ((rule-failures (dumb-jump-test-ag-rules)))
(should (= (length rule-failures) 0))))

(ert-deftest dumb-jump-test-rules-not-test () ;; :not tests
(let ((rule-failures (dumb-jump-test-rules t)))
(should (= (length rule-failures) 0))))

(ert-deftest dumb-jump-test-ag-rules-not-test () ;; :not tests
(let ((rule-failures (dumb-jump-test-ag-rules t)))
(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))
Expand Down

0 comments on commit ee45d61

Please sign in to comment.