From 823d5b7b96b5e4c42bb2cf12d9a7ed1ad2aa3137 Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Tue, 15 Sep 2015 14:14:11 -0400 Subject: [PATCH 1/9] Make :Pytest capture doctests as wel --- ftplugin/python/pytest.vim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ftplugin/python/pytest.vim b/ftplugin/python/pytest.vim index a66b6a2..5960e41 100755 --- a/ftplugin/python/pytest.vim +++ b/ftplugin/python/pytest.vim @@ -344,7 +344,7 @@ endfunction function! s:RunInSplitWindow(path) - let cmd = "py.test --tb=short " . a:path + let cmd = "py.test --doctest-modules --tb=short " . a:path if exists("g:ConqueTerm_Loaded") call conque_term#open(cmd, ['split', 'resize 20'], 0) else @@ -558,9 +558,9 @@ function! s:RunPyTest(path, ...) let g:pytest_last_session = "" if (len(parametrized) && parametrized != "0") - let cmd = "py.test -k " . parametrized . " --tb=short " . a:path + let cmd = "py.test --doctest-modules -k " . parametrized . " --tb=short " . a:path else - let cmd = "py.test --tb=short " . a:path + let cmd = "py.test --doctest-modules --tb=short " . a:path endif let out = system(cmd) @@ -990,7 +990,7 @@ endfunction function! s:Pdb(path, ...) - let pdb_command = "py.test " . a:1 . " " . a:path + let pdb_command = "py.test --doctest-modules " . a:1 . " " . a:path if exists("g:ConqueTerm_Loaded") call conque_term#open(pdb_command, ['split', 'resize 20'], 0) else From 51006d64d20a4214a365a418acef6bb19628278c Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Wed, 16 Sep 2015 10:51:50 -0400 Subject: [PATCH 2/9] add doctest testing functionality for functions --- ftplugin/python/pytest.vim | 53 +++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/ftplugin/python/pytest.vim b/ftplugin/python/pytest.vim index 5960e41..7227012 100755 --- a/ftplugin/python/pytest.vim +++ b/ftplugin/python/pytest.vim @@ -343,8 +343,13 @@ function! s:ProjectPath() endfunction -function! s:RunInSplitWindow(path) - let cmd = "py.test --doctest-modules --tb=short " . a:path +function! s:RunInSplitWindow(path, doctest) + let cmd = "py.test" + if (a:doctest == 'True') + let cmd = cmd . ' --doctest-modules' + endif + let cmd = cmd . " --tb=short " . a:path + if exists("g:ConqueTerm_Loaded") call conque_term#open(cmd, ['split', 'resize 20'], 0) else @@ -548,7 +553,7 @@ function! s:ResetAll() endfunction! -function! s:RunPyTest(path, ...) +function! s:RunPyTest(path, doctest ...) if (a:0 > 0) let parametrized = a:1 else @@ -557,10 +562,14 @@ function! s:RunPyTest(path, ...) let g:pytest_last_session = "" + let cmd = 'py.test' + if (a:doctest == 'True') + let cmd = cmd . ' --doctest-modules' + if (len(parametrized) && parametrized != "0") - let cmd = "py.test --doctest-modules -k " . parametrized . " --tb=short " . a:path + let cmd = cmd . " -k " . parametrized . " --tb=short " . a:path else - let cmd = "py.test --doctest-modules --tb=short " . a:path + let cmd = cmd . " --tb=short " . a:path endif let out = system(cmd) @@ -873,10 +882,14 @@ function! s:HasPythonDecorator(line) endfunction -function! s:ThisFunction(verbose, ...) +function! s:ThisFunction(verbose, doctest, ...) let save_cursor = getpos('.') call s:ClearAll() let c_name = s:NameOfCurrentFunction() + if (a:doctest == 'True') + let c_name = substitute(substitute(@%, '.py', '.', ''), '/', '.', 'g') . c_name + endif + let is_parametrized = s:HasPythonDecorator(line('.')) let abspath = s:CurrentPath() if (strlen(c_name) == 1) @@ -904,9 +917,9 @@ function! s:ThisFunction(verbose, ...) endif if (a:verbose == 1) - call s:RunInSplitWindow(path) + call s:RunInSplitWindow(path, a:doctest) else - call s:RunPyTest(path, c_name) + call s:RunPyTest(path, c_name, a:doctest) endif endfunction @@ -1047,8 +1060,12 @@ function! s:Proxy(action, ...) let pdb = 'False' let looponfail = 0 let delgado = [] + let doctest = 'False' if (a:0 > 0) + if ((a:1 == 'doctest') || ((a:0 > 1) && (a:2 == 'doctest'))) + let doctest = 'True' + endif if (a:1 == 'verbose') let verbose = 1 elseif (a:1 == '--pdb') @@ -1065,37 +1082,37 @@ function! s:Proxy(action, ...) if (a:action == "class") if looponfail == 1 call s:LoopOnFail(a:action) - call s:ThisClass(verbose, pdb, delgado) + call s:ThisClass(verbose, doctest, pdb, delgado) else - call s:ThisClass(verbose, pdb, delgado) + call s:ThisClass(verbose, doctest, pdb, delgado) endif elseif (a:action == "method") if looponfail == 1 call s:LoopOnFail(a:action) - call s:ThisMethod(verbose, pdb, delgado) + call s:ThisMethod(verbose, doctest, pdb, delgado) else - call s:ThisMethod(verbose, pdb, delgado) + call s:ThisMethod(verbose, doctest, pdb, delgado) endif elseif (a:action == "function") if looponfail == 1 call s:LoopOnFail(a:action) - call s:ThisFunction(verbose, pdb, delgado) + call s:ThisFunction(verbose, doctest, pdb, delgado) else - call s:ThisFunction(verbose, pdb, delgado) + call s:ThisFunction(verbose, doctest, pdb, delgado) endif elseif (a:action == "file") if looponfail == 1 call s:LoopOnFail(a:action) - call s:ThisFile(verbose, pdb, delgado) + call s:ThisFile(verbose, doctest, pdb, delgado) else - call s:ThisFile(verbose, pdb, delgado) + call s:ThisFile(verbose, doctest, pdb, delgado) endif elseif (a:action == "project" ) if looponfail ==1 call s:LoopOnFail(a:action) - call s:ThisProject(verbose, pdb, delgado) + call s:ThisProject(verbose, doctest, pdb, delgado) else - call s:ThisProject(verbose, pdb,delgado) + call s:ThisProject(verbose, doctest, pdb,delgado) endif elseif (a:action == "projecttestwd") let projecttests = s:ProjectPath() From 26e2c6c4ae607181b367d8e41534bfb52f911dc3 Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Wed, 16 Sep 2015 10:55:49 -0400 Subject: [PATCH 3/9] add support for files --- ftplugin/python/pytest.vim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ftplugin/python/pytest.vim b/ftplugin/python/pytest.vim index 7227012..ac41191 100755 --- a/ftplugin/python/pytest.vim +++ b/ftplugin/python/pytest.vim @@ -924,7 +924,7 @@ function! s:ThisFunction(verbose, doctest, ...) endfunction -function! s:ThisClass(verbose, ...) +function! s:ThisClass(verbose, doctest, ...) let save_cursor = getpos('.') call s:ClearAll() let c_name = s:NameOfCurrentClass() @@ -956,7 +956,7 @@ function! s:ThisClass(verbose, ...) endfunction -function! s:ThisFile(verbose, ...) +function! s:ThisFile(verbose, doctest, ...) call s:ClearAll() let message = "py.test ==> Running tests for entire file" call s:Echo(message, 1) @@ -972,9 +972,9 @@ function! s:ThisFile(verbose, ...) endif if (a:verbose == 1) - call s:RunInSplitWindow(abspath) + call s:RunInSplitWindow(abspath, a:doctest) else - call s:RunPyTest(abspath) + call s:RunPyTest(abspath, a:doctest) endif endfunction From f364cdac7550e8d1553dae68b3cbfee9f8f3e264 Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Wed, 16 Sep 2015 10:57:08 -0400 Subject: [PATCH 4/9] Add (I think?) support for projects --- ftplugin/python/pytest.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ftplugin/python/pytest.vim b/ftplugin/python/pytest.vim index ac41191..0b0ba4b 100755 --- a/ftplugin/python/pytest.vim +++ b/ftplugin/python/pytest.vim @@ -978,7 +978,7 @@ function! s:ThisFile(verbose, doctest, ...) endif endfunction -function! s:ThisProject(verbose, ...) +function! s:ThisProject(verbose, doctest, ...) call s:ClearAll() let message = "py.test ==> Running tests for entire project" call s:Echo(message, 1) @@ -995,9 +995,9 @@ function! s:ThisProject(verbose, ...) endif if (a:verbose == 1) - call s:RunInSplitWindow(abspath) + call s:RunInSplitWindow(abspath, a:doctest) else - call s:RunPyTest(abspath) + call s:RunPyTest(abspath, a:doctest) endif endfunction From 0fbc5fe7a675a03c2adec1a32155d71fc9ca26b1 Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Wed, 16 Sep 2015 11:01:22 -0400 Subject: [PATCH 5/9] Whoops, typo --- ftplugin/python/pytest.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ftplugin/python/pytest.vim b/ftplugin/python/pytest.vim index 0b0ba4b..fd70b01 100755 --- a/ftplugin/python/pytest.vim +++ b/ftplugin/python/pytest.vim @@ -553,7 +553,7 @@ function! s:ResetAll() endfunction! -function! s:RunPyTest(path, doctest ...) +function! s:RunPyTest(path, doctest, ...) if (a:0 > 0) let parametrized = a:1 else From e4f365532fa6fb14fbc659b38b05273f34a077eb Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Wed, 16 Sep 2015 11:12:50 -0400 Subject: [PATCH 6/9] Abstract the python path logic, get class doctests working --- ftplugin/python/pytest.vim | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ftplugin/python/pytest.vim b/ftplugin/python/pytest.vim index fd70b01..3c65867 100755 --- a/ftplugin/python/pytest.vim +++ b/ftplugin/python/pytest.vim @@ -300,6 +300,15 @@ function! s:NameOfCurrentClass() endfunction +function! s:PythonPathToModule() + let path = @% + let path = substitute(path, '.__init__.py', '.', 'g') + let path = substitute(path, '.py', '.', 'g') + let path = substitute(path, '/', '.', 'g') + return path +endfunction + + function! s:NameOfCurrentMethod() normal! $ let find_object = s:FindPythonObject('method') @@ -887,7 +896,7 @@ function! s:ThisFunction(verbose, doctest, ...) call s:ClearAll() let c_name = s:NameOfCurrentFunction() if (a:doctest == 'True') - let c_name = substitute(substitute(@%, '.py', '.', ''), '/', '.', 'g') . c_name + let c_name = s:PythonPathToModule() . c_name endif let is_parametrized = s:HasPythonDecorator(line('.')) @@ -928,6 +937,9 @@ function! s:ThisClass(verbose, doctest, ...) let save_cursor = getpos('.') call s:ClearAll() let c_name = s:NameOfCurrentClass() + if (a:doctest == 'True') + let c_name = s:PythonPathToModule() . c_name + endif let abspath = s:CurrentPath() if (strlen(c_name) == 1) call setpos('.', save_cursor) @@ -949,9 +961,9 @@ function! s:ThisClass(verbose, doctest, ...) endif if (a:verbose == 1) - call s:RunInSplitWindow(path) + call s:RunInSplitWindow(path, a:doctest) else - call s:RunPyTest(path) + call s:RunPyTest(path, a:doctest) endif endfunction From 7c099df2e303bd6f75c5f7010385e06a99b56a54 Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Wed, 16 Sep 2015 11:29:02 -0400 Subject: [PATCH 7/9] add support for methods --- ftplugin/python/pytest.vim | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ftplugin/python/pytest.vim b/ftplugin/python/pytest.vim index 3c65867..d76dcf5 100755 --- a/ftplugin/python/pytest.vim +++ b/ftplugin/python/pytest.vim @@ -815,14 +815,20 @@ function! s:GreenBar() endfunction -function! s:ThisMethod(verbose, ...) +function! s:ThisMethod(verbose, doctest, ...) let save_cursor = getpos('.') call s:ClearAll() + let sep = '::' let m_name = s:NameOfCurrentMethod() let is_parametrized = s:HasPythonDecorator(line('.')) let c_name = s:NameOfCurrentClass() + if (a:doctest == 'True') + let c_name = s:PythonPathToModule() . c_name + let sep = '.' + endif let abspath = s:CurrentPath() + if (strlen(m_name) == 1) call setpos('.', save_cursor) call s:Echo("Unable to find a matching method for testing") @@ -842,7 +848,7 @@ function! s:ThisMethod(verbose, ...) let parametrized_flag = m_name let message = "py.test ==> Running test for parametrized method " . m_name else - let path = abspath . "::" . c_name . "::" . m_name + let path = abspath . "::" . c_name . sep . m_name let parametrized_flag = "0" let message = "py.test ==> Running test for method " . m_name endif @@ -857,9 +863,9 @@ function! s:ThisMethod(verbose, ...) return endif if (a:verbose == 1) - call s:RunInSplitWindow(path) + call s:RunInSplitWindow(path, a:doctest) else - call s:RunPyTest(path, parametrized_flag) + call s:RunPyTest(path, a:doctest, parametrized_flag) endif endfunction From 2e32228241e7afe2efbf116109f72640e549c8f0 Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Wed, 16 Sep 2015 12:27:12 -0400 Subject: [PATCH 8/9] add missing endif --- ftplugin/python/pytest.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/ftplugin/python/pytest.vim b/ftplugin/python/pytest.vim index d76dcf5..d0ad6cc 100755 --- a/ftplugin/python/pytest.vim +++ b/ftplugin/python/pytest.vim @@ -574,6 +574,7 @@ function! s:RunPyTest(path, doctest, ...) let cmd = 'py.test' if (a:doctest == 'True') let cmd = cmd . ' --doctest-modules' + endif if (len(parametrized) && parametrized != "0") let cmd = cmd . " -k " . parametrized . " --tb=short " . a:path From 464297a39b6f660278adf8a30f1b0857979d2a66 Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Wed, 16 Sep 2015 15:20:26 -0400 Subject: [PATCH 9/9] fix the parameter order for s:RunPyTest --- ftplugin/python/pytest.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ftplugin/python/pytest.vim b/ftplugin/python/pytest.vim index d0ad6cc..f7b1abe 100755 --- a/ftplugin/python/pytest.vim +++ b/ftplugin/python/pytest.vim @@ -935,7 +935,7 @@ function! s:ThisFunction(verbose, doctest, ...) if (a:verbose == 1) call s:RunInSplitWindow(path, a:doctest) else - call s:RunPyTest(path, c_name, a:doctest) + call s:RunPyTest(path, a:doctest, c_name) endif endfunction