started to add unit tests
This commit is contained in:
parent
cdaa0ba957
commit
5340d806c9
9
Makefile
9
Makefile
@ -4,8 +4,13 @@ filename=steadymark-`python -c 'import steadymark;print steadymark.version'`.tar
|
|||||||
|
|
||||||
export PYTHONPATH:= ${PWD}
|
export PYTHONPATH:= ${PWD}
|
||||||
|
|
||||||
test: clean
|
test: clean unit functional
|
||||||
@echo "Running code examples from README.md as tests"
|
|
||||||
|
unit:
|
||||||
|
@echo "Running unit tests"
|
||||||
|
@nosetests --verbosity=2 -s tests/unit
|
||||||
|
|
||||||
|
functional:
|
||||||
@python steadymark/__init__.py
|
@python steadymark/__init__.py
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
distribute>=0.6.24
|
distribute==0.6.27
|
||||||
misaka>=1.0.2
|
ipdb==0.7
|
||||||
sure
|
ipython==0.13
|
||||||
|
misaka==1.0.2
|
||||||
|
nose==1.1.2
|
||||||
|
sure==1.0.5
|
||||||
|
wsgiref==0.1.2
|
||||||
|
@ -31,6 +31,7 @@ import sys
|
|||||||
import traceback
|
import traceback
|
||||||
import codecs
|
import codecs
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
from misaka import (
|
from misaka import (
|
||||||
BaseRenderer,
|
BaseRenderer,
|
||||||
@ -138,6 +139,7 @@ class READMETestRunner(BaseRenderer):
|
|||||||
return formatted_tb.replace(
|
return formatted_tb.replace(
|
||||||
'@STEADYMARK@', title)
|
'@STEADYMARK@', title)
|
||||||
|
|
||||||
|
|
||||||
class Runner(object):
|
class Runner(object):
|
||||||
def __init__(self, filename=None, text=u''):
|
def __init__(self, filename=None, text=u''):
|
||||||
renderer = READMETestRunner()
|
renderer = READMETestRunner()
|
||||||
@ -161,6 +163,55 @@ class Runner(object):
|
|||||||
return self.md.render(self.text) and renderer or renderer
|
return self.md.render(self.text) and renderer or renderer
|
||||||
|
|
||||||
|
|
||||||
|
class SteadyMark(BaseRenderer):
|
||||||
|
def preprocess(self, text):
|
||||||
|
self._tests = [{}]
|
||||||
|
return unicode(text)
|
||||||
|
|
||||||
|
def block_code(self, code, language):
|
||||||
|
if language != 'python':
|
||||||
|
return
|
||||||
|
|
||||||
|
item = self._tests[-1]
|
||||||
|
item[u'code'] = unicode(code).strip()
|
||||||
|
if 'title' not in item:
|
||||||
|
item[u'title'] = u'Test #{0}'.format(len(self._tests))
|
||||||
|
self._tests.append({})
|
||||||
|
|
||||||
|
def header(self, title, level):
|
||||||
|
t = unicode(title)
|
||||||
|
t = re.sub(ur'^[# ]*(.*)', '\g<1>', t)
|
||||||
|
t = re.sub(ur'`([^`]*)`', '\033[1;33m\g<1>\033[0m', t)
|
||||||
|
self._tests.append({
|
||||||
|
u'title': t,
|
||||||
|
})
|
||||||
|
|
||||||
|
def postprocess(self, full_document):
|
||||||
|
tests = []
|
||||||
|
|
||||||
|
for test in filter(lambda x: 'code' in x, self._tests):
|
||||||
|
raw_code = str(test['code'].encode('utf-8'))
|
||||||
|
title = str(test['title'].encode('utf-8'))
|
||||||
|
tests.append(MarkdownTest(title, raw_code))
|
||||||
|
|
||||||
|
self.tests = tests
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def inspect(cls, markdown):
|
||||||
|
renderer = cls()
|
||||||
|
extensions = EXT_FENCED_CODE | EXT_NO_INTRA_EMPHASIS
|
||||||
|
md = Markdown(renderer, extensions=extensions)
|
||||||
|
md.render(markdown)
|
||||||
|
return renderer
|
||||||
|
|
||||||
|
|
||||||
|
class MarkdownTest(object):
|
||||||
|
def __init__(self, title, raw_code):
|
||||||
|
self.title = title
|
||||||
|
self.raw_code = raw_code
|
||||||
|
self.code = compile(raw_code, "@STEADYMARK@", "exec")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
|
25
tests/__init__.py
Normal file
25
tests/__init__.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# <steadymark - markdown-based test runner for python>
|
||||||
|
# Copyright (C) <2012> Gabriel Falcão <gabriel@nacaolivre.org>
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person
|
||||||
|
# obtaining a copy of this software and associated documentation
|
||||||
|
# files (the "Software"), to deal in the Software without
|
||||||
|
# restriction, including without limitation the rights to use,
|
||||||
|
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the
|
||||||
|
# Software is furnished to do so, subject to the following
|
||||||
|
# conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
# OTHER DEALINGS IN THE SOFTWARE.
|
25
tests/unit/__init__.py
Normal file
25
tests/unit/__init__.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# <steadymark - markdown-based test runner for python>
|
||||||
|
# Copyright (C) <2012> Gabriel Falcão <gabriel@nacaolivre.org>
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person
|
||||||
|
# obtaining a copy of this software and associated documentation
|
||||||
|
# files (the "Software"), to deal in the Software without
|
||||||
|
# restriction, including without limitation the rights to use,
|
||||||
|
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the
|
||||||
|
# Software is furnished to do so, subject to the following
|
||||||
|
# conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
# OTHER DEALINGS IN THE SOFTWARE.
|
72
tests/unit/test_parser.py
Normal file
72
tests/unit/test_parser.py
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# <steadymark - markdown-based test runner for python>
|
||||||
|
# Copyright (C) <2012> Gabriel Falcão <gabriel@nacaolivre.org>
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person
|
||||||
|
# obtaining a copy of this software and associated documentation
|
||||||
|
# files (the "Software"), to deal in the Software without
|
||||||
|
# restriction, including without limitation the rights to use,
|
||||||
|
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the
|
||||||
|
# Software is furnished to do so, subject to the following
|
||||||
|
# conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
# OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
import sure
|
||||||
|
from steadymark import SteadyMark
|
||||||
|
|
||||||
|
|
||||||
|
def test_find_python_code_with_titles():
|
||||||
|
(u"SteadyMark should find python code and use the "
|
||||||
|
"previous header as title")
|
||||||
|
|
||||||
|
md = u"""# test 1
|
||||||
|
a paragraph
|
||||||
|
|
||||||
|
```python
|
||||||
|
assert False, 'boom!'
|
||||||
|
```
|
||||||
|
|
||||||
|
# not a test
|
||||||
|
|
||||||
|
foobar
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
ruby no!
|
||||||
|
```
|
||||||
|
|
||||||
|
# another part
|
||||||
|
|
||||||
|
## test 2
|
||||||
|
|
||||||
|
a paragraph
|
||||||
|
|
||||||
|
```python
|
||||||
|
assert False, 'uh yeah'
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
|
||||||
|
sm = SteadyMark.inspect(md)
|
||||||
|
|
||||||
|
sm.tests.should.have.length_of(2)
|
||||||
|
|
||||||
|
test1, test2 = sm.tests
|
||||||
|
|
||||||
|
test1.title.should.equal("test 1")
|
||||||
|
test1.raw_code.should.equal("assert False, 'boom!'")
|
||||||
|
eval.when.called_with(test1.code).should.throw(AssertionError, "boom!")
|
||||||
|
|
||||||
|
test2.title.should.equal("test 2")
|
||||||
|
test2.raw_code.should.equal("assert False, 'uh yeah'")
|
||||||
|
eval.when.called_with(test2.code).should.throw(AssertionError, "uh yeah")
|
Loading…
Reference in New Issue
Block a user