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

Add tests and fix some issues #14

Merged
merged 5 commits into from
Nov 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*~
.DS_Store
tags
vader.vim/
15 changes: 10 additions & 5 deletions autoload/pydocstring.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ let s:save_cpo = &cpo
set cpo&vim

" Path to docstring template.
if exists('g:pydocstring_templates_dir')
let s:tmpldir = g:pydocstring_templates_dir
else
let s:tmpldir = expand('<sfile>:p:h:h') . '/template/pydocstring/'
if !exists('g:pydocstring_templates_dir')
let g:pydocstring_templates_dir = expand('<sfile>:p:h:h') . '/template/pydocstring/'
endif

" Use comment.txt when cursor is not on def|class keyword.
if !exists('g:pydocstring_enable_comment')
let g:pydocstring_enable_comment = 1
Expand All @@ -31,7 +30,13 @@ let s:regexs = {
\ }

function! s:readtmpl(type)
let path = expand(s:tmpldir . a:type . '.txt')
let tmpldir = g:pydocstring_templates_dir
" Append the back slash if needed.
if g:pydocstring_templates_dir !~ '/$'
let tmpldir = tmpldir . '/'
endif

let path = expand(tmpldir . a:type . '.txt')
if !filereadable(path)
throw 'Template ' . path . ' is not exists.'
endif
Expand Down
151 changes: 151 additions & 0 deletions test/basic.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# vim:set et sw=4 ts=4 tw=79:

# Test def keyword
#-------------------------------------------------------------------------------
Given python (def foo):
def foo():
pass

Execute:
Pydocstring

Expect python:
def foo():
"""foo"""
pass

Given python (def foo with 1 param):
def foo(arg):
pass

Execute:
Pydocstring

Expect python:
def foo(arg):
"""foo

:param arg:
"""
pass

Given python (def foo with 2 params):
def foo(arg1, arg2):
pass

Execute:
Pydocstring

Expect python:
def foo(arg1, arg2):
"""foo

:param arg1:
:param arg2:
"""
pass

Given python (def foo with variadic params):
def foo(*arg):
pass

Execute:
Pydocstring
Expect python:
def foo(*arg):
"""foo

:param *arg:
"""
pass


# Test class keyword
#-------------------------------------------------------------------------------


Given python (class Foo):
class Foo(object):
def foo(self):
pass

def arg1(self, arg1):
pass

Execute:
Pydocstring

Expect python:
class Foo(object):
"""Foo"""
def foo(self):
pass

def arg1(self, arg1):
pass


Given python (class Foo):
class Foo(object):
def foo(self):
pass

def arg1(self, arg1):
pass

Do (Put cursor to def foo(self)):
j

Then Execute (Execute Pydocstring on def foo(self)):
Pydocstring

Expect python:
class Foo(object):
def foo(self):
"""foo"""
pass

def arg1(self, arg1):
pass


Given python (class Foo):
class Foo(object):
def foo(self):
pass

def arg1(self, arg1):
pass

Do (Put cursor to def arg1(self, arg1)):
4j

Then Execute (Execute Pydocstring on def arg1(self, arg1)):
Pydocstring

Expect python:
class Foo(object):
def foo(self):
pass

def arg1(self, arg1):
"""arg1

:param arg1:
"""
pass

# Test aync/await keyword
#-------------------------------------------------------------------------------
Given python (async def foo):
async def foo():
pass

Execute:
Pydocstring

Expect python:
async def foo():
"""foo"""
pass

56 changes: 56 additions & 0 deletions test/custom-template.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# vim:set et sw=4 ts=4 tw=79:
Execute (Setup template dir):
Save g:pydocstring_templates_dir
let g:pydocstring_templates_dir = './test-template/'

Given python (def foo):
def foo():
pass

Execute:
Pydocstring


Expect python:
def foo():
"""foo is """
pass


Given python (def foo with 1 params):
def foo(arg1):
pass

Execute:
Pydocstring

Expect python:
def foo(arg1):
"""foo

arg1:
arg1 is
"""
pass


Given python (def foo with 2 params):
def foo(arg1, arg2):
pass

Execute:
Pydocstring

Expect python:
def foo(arg1, arg2):
"""foo

arg1:
arg1 is
arg2:
arg2 is
"""
pass

Execute (Clear pydocstring_templates_dir):
Restore
16 changes: 16 additions & 0 deletions test/minimal_vimrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
" Minimal vimrc to run tests
" --------------------------

set nocompatible
set nu
filetype off
" Clear all rtp
set rtp=$VIMRUNTIME


" Add vader.vim to rtp
set rtp+=./vader.vim

" Add pydocstring folder into rtp
set rtp+=../
filetype plugin indent on
13 changes: 13 additions & 0 deletions test/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"watch": "../",
"ignore": [
".git",
"node_modules/**/node_modules",
"vader.vim"
],
"verbose": true,
"ext": "vader vim",
"execMap": {
"vader": "./run-single-test-file.sh"
}
}
46 changes: 46 additions & 0 deletions test/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Test cases for vim-pydocstring
==========================================

Prerequisite
------------

- [vader.vim](https://github.com/junegunn/vader.vim)

Before running test, clone vader into this directory with:

```
git clone https://github.com/junegunn/vader.vim
```


Run
---

```
./run.sh
```

Note that the command need to be executed under `test` folder.

Use TDD during development (optional)
-------------------------------------

You need [nodemon](https://github.com/remy/nodemon) to watch for files
change and re-run the test cases in given file. Here I already provide
nodemon configuration (see `nodemon.json`.)

Run (again, this need to be executed under `test` folder)

```
nodemon <test-file>.vader
```

Now, if you modify the `vader` or `vim` files in this project, nodemon will
re-run all the test cases (via `run-single-test-file.sh`).


### Know issue

Vim warns `Input is not from a terminal` when we run tests with
`nodemon`. Neovim doesn't. That why in the scripts, we prefer to use
`nvim` if it is found on path.
19 changes: 19 additions & 0 deletions test/run-single-test-file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

###############################################################################
# Execute a single test file #
###############################################################################

if [ $# -eq 0 ]; then
echo "Please supply an input *.vader file"
exit
fi

VIM_EXE="vim"

if hash nvim 2>/dev/null ; then
VIM_EXE="nvim"
fi

# Open vim with readonly mode just to execute all *.vader tests.
$VIM_EXE -Nu minimal_vimrc -R "+Vader $1"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need ! run Vim and exec Vader command.

-$VIM_EXE -Nu minimal_vimrc -R "+Vader $1"
+$VIM_EXE -Nu minimal_vimrc -R "+Vader! $1"

16 changes: 16 additions & 0 deletions test/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

###############################################################################
# Execute all test case #
###############################################################################

VIM_EXE="vim"

# If nvim is available in PATH, then we prefer to use nvim
# since it works better with nodemon
if hash nvim 2>/dev/null ; then
VIM_EXE="nvim"
fi

# Open vim with readonly mode just to execute all *.vader tests.
$VIM_EXE -Nu minimal_vimrc -R '+Vader *.vader'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-$VIM_EXE -Nu minimal_vimrc -R '+Vader *.vader' 
+$VIM_EXE -Nu minimal_vimrc -R '+Vader! *.vader'

1 change: 1 addition & 0 deletions test/test-template/comment.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#
4 changes: 4 additions & 0 deletions test/test-template/multi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""{{_header_}}

{{_arg_}}:{{_lf_}}{{_indent_}} {{_arg_}} is
"""
1 change: 1 addition & 0 deletions test/test-template/oneline.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""{{_header_}} is """