diff options
| author | Hans Jang <hsjang8848@gmail.com> | 2019-04-03 17:19:45 +1100 |
|---|---|---|
| committer | Bozhidar Batsov <bozhidar.batsov@gmail.com> | 2019-04-03 09:19:45 +0300 |
| commit | 5bd9db6f4b0a9e1c27136561b134a4d119552cdb (patch) | |
| tree | 9e3e6db7d76b249dbd187bd5e6d5caea020596a7 /test | |
| parent | 3beb626dae274333aa66d09d42992b5331d12e00 (diff) | |
Add new 'related-files-fn' option to use custom function to find test/impl/other files (#1394)
Diffstat (limited to 'test')
| -rw-r--r-- | test/projectile-test.el | 346 |
1 files changed, 226 insertions, 120 deletions
diff --git a/test/projectile-test.el b/test/projectile-test.el index 2d92670..e127ea9 100644 --- a/test/projectile-test.el +++ b/test/projectile-test.el @@ -65,9 +65,29 @@ You'd normally combine this with `projectile-test-with-sandbox'." files) ,@body)) +(defmacro projectile-test-with-files-using-custom-project (files project-options &rest body) + "Evaluate BODY with the custom project having PROJECT-OPTIONS with FILES." + (declare (indent 2) (debug (sexp sexp &rest form))) + `(let ((projectile-indexing-method 'native) + (projectile-projects-cache (make-hash-table :test 'equal)) + (projectile-projects-cache-time (make-hash-table :test 'equal)) + (projectile-enable-caching t)) + ,@(mapcar (lambda (file) + (let* ((path (concat "project/" file)) + (dir (file-name-directory path))) + (if (string-suffix-p "/" file) + `(make-directory ,path t) + `(progn + (make-directory ,dir t) + (with-temp-file ,path))))) + files) + (projectile-register-project-type 'sample-project '("somefile") ,@project-options) + (spy-on 'projectile-project-type :and-return-value 'sample-project) + (spy-on 'projectile-project-root :and-return-value (file-truename (expand-file-name "project/"))) + ,@body)) + (defun projectile-test-tmp-file-path () - "Return a filename suitable to save data to in the -test temp directory" + "Return a filename suitable to save data to in the test temp directory." (concat projectile-test-path "/tmp/temporary-file-" (format "%d" (random)) ".eld")) @@ -723,74 +743,125 @@ test temp directory" (describe "projectile-get-other-files" (it "returns files with same names but different extensions" - (let ((projectile-other-file-alist '(;; handle C/C++ extensions - ("cpp" . ("h" "hpp" "ipp")) - ("ipp" . ("h" "hpp" "cpp")) - ("hpp" . ("h" "ipp" "cpp")) - ("cxx" . ("hxx" "ixx")) - ("ixx" . ("cxx" "hxx")) - ("hxx" . ("ixx" "cxx")) - ("c" . ("h")) - ("m" . ("h")) - ("mm" . ("h")) - ("h" . ("c" "cpp" "ipp" "hpp" "m" "mm")) - ("cc" . ("hh")) - ("hh" . ("cc")) - - ;; vertex shader and fragment shader extensions in glsl - ("vert" . ("frag")) - ("frag" . ("vert")) - - ;; handle files with no extension - (nil . ("lock" "gpg")) - ("lock" . ("")) - ("gpg" . ("")) - - ;; handle files with nested extensions - ("service.js" . ("service.spec.js")) - ("js" . ("js")))) - (source-tree '("src/test1.c" - "src/test2.c" - "src/test+copying.m" - "src/test1.cpp" - "src/test2.cpp" - "src/Makefile" - "src/test.vert" - "src/test.frag" - "src/same_name.c" - "src/some_module/same_name.c" - "include1/same_name.h" - "include1/test1.h" - "include1/test1.h~" - "include1/test2.h" - "include1/test+copying.h" - "include1/test1.hpp" - "include2/some_module/same_name.h" - "include2/test1.h" - "include2/test2.h" - "include2/test2.hpp" - - "src/test1.service.js" - "src/test2.service.spec.js" - "include1/test1.service.spec.js" - "include2/test1.service.spec.js" - "include1/test2.js" - "include2/test2.js"))) - - (expect (projectile-get-other-files "src/test1.c" source-tree) :to-equal '("include1/test1.h" "include2/test1.h")) - (expect (projectile-get-other-files "src/test1.cpp" source-tree) :to-equal '("include1/test1.h" "include2/test1.h" "include1/test1.hpp")) - (expect (projectile-get-other-files "test2.c" source-tree) :to-equal '("include1/test2.h" "include2/test2.h")) - (expect (projectile-get-other-files "test2.cpp" source-tree) :to-equal '("include1/test2.h" "include2/test2.h" "include2/test2.hpp")) - (expect (projectile-get-other-files "test1.h" source-tree) :to-equal '("src/test1.c" "src/test1.cpp" "include1/test1.hpp")) - (expect (projectile-get-other-files "test2.h" source-tree) :to-equal '("src/test2.c" "src/test2.cpp" "include2/test2.hpp")) - (expect (projectile-get-other-files "include1/test1.h" source-tree t) :to-equal '("src/test1.c" "src/test1.cpp" "include1/test1.hpp")) - (expect (projectile-get-other-files "Makefile.lock" source-tree) :to-equal '("src/Makefile")) - (expect (projectile-get-other-files "include2/some_module/same_name.h" source-tree) :to-equal '("src/some_module/same_name.c" "src/same_name.c")) - ;; nested extensions - (expect (projectile-get-other-files "src/test1.service.js" source-tree) :to-equal '("include1/test1.service.spec.js" "include2/test1.service.spec.js")) - ;; fallback to outer extensions if no rule for nested extension defined - (expect (projectile-get-other-files "src/test2.service.spec.js" source-tree) :to-equal '("include1/test2.js" "include2/test2.js")) - (expect (projectile-get-other-files "src/test+copying.m" source-tree) :to-equal '("include1/test+copying.h"))))) + (projectile-test-with-sandbox + (projectile-test-with-files-using-custom-project + ("src/test1.c" + "src/test2.c" + "src/test+copying.m" + "src/test1.cpp" + "src/test2.cpp" + "src/Makefile" + "src/test.vert" + "src/test.frag" + "src/same_name.c" + "src/some_module/same_name.c" + "include1/same_name.h" + "include1/test1.h" + "include1/test1.h~" + "include1/test2.h" + "include1/test+copying.h" + "include1/test1.hpp" + "include2/some_module/same_name.h" + "include2/test1.h" + "include2/test2.h" + "include2/test2.hpp" + "src/test1.service.js" + "src/test2.service.spec.js" + "include1/test1.service.spec.js" + "include2/test1.service.spec.js" + "include1/test2.js" + "include2/test2.js") + () + (let ((projectile-other-file-alist '(;; handle C/C++ extensions + ("cpp" . ("h" "hpp" "ipp")) + ("ipp" . ("h" "hpp" "cpp")) + ("hpp" . ("h" "ipp" "cpp")) + ("cxx" . ("hxx" "ixx")) + ("ixx" . ("cxx" "hxx")) + ("hxx" . ("ixx" "cxx")) + ("c" . ("h")) + ("m" . ("h")) + ("mm" . ("h")) + ("h" . ("c" "cpp" "ipp" "hpp" "m" "mm")) + ("cc" . ("hh")) + ("hh" . ("cc")) + + ;; vertex shader and fragment shader extensions in glsl + ("vert" . ("frag")) + ("frag" . ("vert")) + + ;; handle files with no extension + (nil . ("lock" "gpg")) + ("lock" . ("")) + ("gpg" . ("")) + + ;; handle files with nested extensions + ("service.js" . ("service.spec.js")) + ("js" . ("js"))))) + (expect (projectile-get-other-files "src/test1.c") :to-equal '("include1/test1.h" "include2/test1.h")) + (expect (projectile-get-other-files "src/test1.cpp") :to-equal '("include1/test1.h" "include2/test1.h" "include1/test1.hpp")) + (expect (projectile-get-other-files "test2.c") :to-equal '("include1/test2.h" "include2/test2.h")) + (expect (projectile-get-other-files "test2.cpp") :to-equal '("include1/test2.h" "include2/test2.h" "include2/test2.hpp")) + (expect (projectile-get-other-files "test1.h") :to-equal '("src/test1.c" "src/test1.cpp" "include1/test1.hpp")) + (expect (projectile-get-other-files "test2.h") :to-equal '("src/test2.c" "src/test2.cpp" "include2/test2.hpp")) + (expect (projectile-get-other-files "include1/test1.h" t) :to-equal '("src/test1.c" "src/test1.cpp" "include1/test1.hpp")) + (expect (projectile-get-other-files "Makefile.lock") :to-equal '("src/Makefile")) + (expect (projectile-get-other-files "include2/some_module/same_name.h") :to-equal '("src/some_module/same_name.c" "src/same_name.c")) + ;; nested extensions + (expect (projectile-get-other-files "src/test1.service.js") :to-equal '("include1/test1.service.spec.js" "include2/test1.service.spec.js")) + ;; fallback to outer extensions if no rule for nested extension defined + (expect (projectile-get-other-files "src/test2.service.spec.js") :to-equal '("include1/test2.js" "include2/test2.js")) + (expect (projectile-get-other-files "src/test+copying.m") :to-equal '("include1/test+copying.h")))))) + + (it "returns files based on the paths returned by :related-files-fn option" + (projectile-test-with-sandbox + (projectile-test-with-files-using-custom-project + ("src/test1.cpp" + "src/test1.def" + "src/test2.def" + "src/test2.cpp" + "src/test2.h" + "src/test3.cpp" + "src/test3.h") + (:related-files-fn (lambda (file) + (cond ((equal file "src/test1.def") '(:other "src/test1.cpp")) + ((equal file "src/test2.def") '(:other ("src/test2.cpp" "src/test2.h" "src/test4.h"))) + ((equal file "src/test3.cpp") '(:other nil))))) + (expect (projectile-get-other-files "src/test1.def") :to-equal '("src/test1.cpp")) + (expect (projectile-get-other-files "src/test2.def") :to-equal '("src/test2.cpp" "src/test2.h")) + ;; Make sure extension based mechanism is still working + (expect (projectile-get-other-files "src/test2.cpp") :to-equal '("src/test2.h")) + ;; Make sure that related-files-fn option has priority over existing mechanism + (expect (projectile-get-other-files "src/test3.cpp") :to-equal nil)))) + + (it "returns files based on the predicate returned by :related-files-fn option" + (projectile-test-with-sandbox + (projectile-test-with-files-using-custom-project + ("src/test1.cpp" + "src/test1.def" + "src/test2.def" + "src/test2.cpp" + "src/test2.h" + "src/test3.cpp" + "src/test3.h") + (:related-files-fn + (lambda (file) + (cond ((equal file "src/test1.def") + (list :other (lambda (other-file) + (equal other-file "src/test1.cpp")))) + ((equal file "src/test2.def") + (list :other (lambda (other-file) + (or (equal other-file "src/test2.cpp") + (equal other-file "src/test2.h"))))) + ((equal file "src/test3.cpp") + (list :other (lambda (other-file) nil)))))) + + (expect (projectile-get-other-files "src/test1.def") :to-equal '("src/test1.cpp")) + (expect (projectile-get-other-files "src/test2.def") :to-equal '("src/test2.cpp" "src/test2.h")) + ;; Make sure extension based mechanism is still working + (expect (projectile-get-other-files "src/test2.cpp") :to-equal '("src/test2.h")) + ;; Make sure that related-files-fn option has priority over existing mechanism + (expect (projectile-get-other-files "src/test3.cpp") :to-equal nil))))) (describe "projectile-compilation-dir" (it "returns the compilation directory for a project" @@ -866,60 +937,95 @@ test temp directory" (expect (projectile-dirname-matching-count "src/weed/sea.c" "src/food/sea.c") :to-equal 0) (expect (projectile-dirname-matching-count "test/demo-test.el" "demo.el") :to-equal 0))) -(describe "projectile-find-matching-test" +(describe "projectile--find-matching-test" (it "finds matching test or file" (projectile-test-with-sandbox - (projectile-test-with-files - ("project/app/models/weed/" - "project/app/models/food/" - "project/spec/models/weed/" - "project/spec/models/food/" - "project/app/models/weed/sea.rb" - "project/app/models/food/sea.rb" - "project/spec/models/weed/sea_spec.rb" - "project/spec/models/food/sea_spec.rb") - (let ((projectile-indexing-method 'native)) - (spy-on 'projectile-project-type :and-return-value 'rails-rspec) - (spy-on 'projectile-project-root :and-return-value (file-truename (expand-file-name "project/"))) - (expect (projectile-find-matching-test "app/models/food/sea.rb") :to-equal "spec/models/food/sea_spec.rb") - (expect (projectile-find-matching-file "spec/models/food/sea_spec.rb") :to-equal "app/models/food/sea.rb"))))) - (it "finds matching test or file in a custom project" + (projectile-test-with-files-using-custom-project + ("app/models/weed/sea.rb" + "app/models/food/sea.rb" + "spec/models/weed/sea_spec.rb" + "spec/models/food/sea_spec.rb") + (:test-suffix "_spec") + (expect (projectile--find-matching-test "app/models/food/sea.rb") :to-equal '("spec/models/food/sea_spec.rb")) + (expect (projectile--find-matching-file "spec/models/food/sea_spec.rb") :to-equal '("app/models/food/sea.rb"))))) + + (it "finds matching test or file with dirs" (projectile-test-with-sandbox - (projectile-test-with-files - ("project/src/foo/" - "project/src/bar/" - "project/test/foo/" - "project/test/bar/" - "project/src/foo/foo.service.js" - "project/src/bar/bar.service.js" - "project/test/foo/foo.service.spec.js" - "project/test/bar/bar.service.spec.js") - (let ((projectile-indexing-method 'native)) - (projectile-register-project-type 'npm-project '("somefile") :test-suffix ".spec") - (spy-on 'projectile-project-type :and-return-value 'npm-project) - (spy-on 'projectile-project-root :and-return-value (file-truename (expand-file-name "project/"))) - (expect (projectile-find-matching-test "src/foo/foo.service.js") :to-equal "test/foo/foo.service.spec.js") - (expect (projectile-find-matching-file "test/bar/bar.service.spec.js") :to-equal "src/bar/bar.service.js"))))) - (it "finds matching test or file in a custom project with dirs" + (projectile-test-with-files-using-custom-project + ("source/foo/foo.service.js" + "source/bar/bar.service.js" + "spec/foo/foo.service.spec.js" + "spec/bar/bar.service.spec.js") + (:test-suffix ".spec" :test-dir "spec/" :src-dir "source/") + (expect (projectile--find-matching-test "source/foo/foo.service.js") :to-equal '("spec/foo/foo.service.spec.js")) + (expect (projectile--find-matching-file "spec/bar/bar.service.spec.js") :to-equal '("source/bar/bar.service.js"))))) + + (it "finds matching test or file based on the paths returned by :related-files-fn option" + (defun -my/related-files(file) + (if (string-match (rx (group (or "src" "test")) (group "/" (1+ anything) ".cpp")) file) + (if (equal (match-string 1 file ) "test") + (list :impl (concat "src" (match-string 2 file))) + (list :test (concat "test" (match-string 2 file)))))) (projectile-test-with-sandbox - (projectile-test-with-files - ("project/source/foo/" - "project/source/bar/" - "project/spec/foo/" - "project/spec/bar/" - "project/source/foo/foo.service.js" - "project/source/bar/bar.service.js" - "project/spec/foo/foo.service.spec.js" - "project/spec/bar/bar.service.spec.js") - (let ((projectile-indexing-method 'native)) - (projectile-register-project-type 'npm-project '("somefile") - :test-suffix ".spec" - :test-dir "spec/" - :src-dir "source/") - (spy-on 'projectile-project-type :and-return-value 'npm-project) - (spy-on 'projectile-project-root :and-return-value (file-truename (expand-file-name "project/"))) - (expect (projectile-find-matching-test "source/foo/foo.service.js") :to-equal "spec/foo/foo.service.spec.js") - (expect (projectile-find-matching-file "spec/bar/bar.service.spec.js") :to-equal "source/bar/bar.service.js")))))) + (projectile-test-with-files-using-custom-project + ("src/Foo.cpp" + "src/Bar.cpp" + "src/Baz.py" + "test/Bar.cpp" + "test/Foo.cpp" + "other/Test_Baz.py") + (:related-files-fn #'-my/related-files :test-prefix "Test_") + (expect (projectile-test-file-p "test/Foo.cpp") :to-equal t) + (expect (projectile-test-file-p "src/Foo.cpp") :to-equal nil) + (expect (projectile--find-matching-test "src/Foo.cpp") :to-equal '("test/Foo.cpp")) + (expect (projectile--find-matching-test "src/Foo2.cpp") :to-equal nil) + (expect (projectile--find-matching-file "test/Foo.cpp") :to-equal '("src/Foo.cpp")) + (expect (projectile--find-matching-file "test/Foo2.cpp") :to-equal nil) + ;; Make sure that existing mechanism(:test-prefix) still works + (expect (projectile-test-file-p "other/Test_Baz.py") :to-equal t) + (expect (projectile-test-file-p "other/Baz.py") :to-equal nil) + (expect (projectile--find-matching-file "other/Test_Baz.py") :to-equal '("src/Baz.py")) + (expect (projectile--find-matching-test "src/Baz.py") :to-equal '("other/Test_Baz.py"))))) + + (it "finds matching test or file by the predicate returned by :related-files-fn option" + (defun -my/related-files(file) + (cond ((equal file "src/Foo.cpp") + (list :test (lambda (other-file) + (equal other-file "test/Foo.cpp")))) + ((equal file "test/Foo.cpp") + (list :impl (lambda (other-file) + (equal other-file "src/Foo.cpp")))))) + (projectile-test-with-sandbox + (projectile-test-with-files-using-custom-project + ("src/Foo.cpp" + "src/Bar.cpp" + "test/Bar.cpp" + "test/Foo.cpp") + (:related-files-fn #'-my/related-files) + (expect (projectile-test-file-p "test/Foo.cpp") :to-equal t) + (expect (projectile-test-file-p "src/Foo.cpp") :to-equal nil) + (expect (projectile--find-matching-test "src/Foo.cpp") :to-equal '("test/Foo.cpp")) + (expect (projectile--find-matching-test "src/Foo.cpp") :to-equal '("test/Foo.cpp")) + (expect (projectile--find-matching-file "test/Foo.cpp") :to-equal '("src/Foo.cpp")))))) + +(describe "projectile--get-related-files" + (it "returns related files for the given file" + (defun -my/related-files(file) + (cond ((equal file "src/Foo.c") + (list :test "src/TestFoo.c" :doc "doc/Foo.txt")) + ((equal file "src/TestFoo.c") + (list :impl (lambda (other-file) + (equal other-file "src/Foo.c")))))) + (projectile-test-with-sandbox + (projectile-test-with-files-using-custom-project + ("src/Foo.c" + "src/TestFoo.c" + "doc/Foo.txt") + (:related-files-fn #'-my/related-files) + (expect (projectile--get-related-file-kinds "src/Foo.c") :to-equal '(:test :doc)) + (expect (projectile--get-related-file-kinds "src/TestFoo.c") :to-equal '(:impl)) + (expect (projectile--get-related-files "src/TestFoo.c" :impl) :to-equal '("src/Foo.c")) + (expect (projectile--get-related-files "src/Foo.c" :doc) :to-equal '("doc/Foo.txt")))))) (describe "projectile-get-all-sub-projects" (it "excludes out-of-project submodules" |
