started to add unit tests

This commit is contained in:
Gabriel Falcao 2012-08-30 13:54:16 -04:00
parent cdaa0ba957
commit 5340d806c9
6 changed files with 187 additions and 5 deletions

View File

@ -4,8 +4,13 @@ filename=steadymark-`python -c 'import steadymark;print steadymark.version'`.tar
export PYTHONPATH:= ${PWD}
test: clean
@echo "Running code examples from README.md as tests"
test: clean unit functional
unit:
@echo "Running unit tests"
@nosetests --verbosity=2 -s tests/unit
functional:
@python steadymark/__init__.py
clean:

View File

@ -1,3 +1,7 @@
distribute>=0.6.24
misaka>=1.0.2
sure
distribute==0.6.27
ipdb==0.7
ipython==0.13
misaka==1.0.2
nose==1.1.2
sure==1.0.5
wsgiref==0.1.2

View File

@ -31,6 +31,7 @@ import sys
import traceback
import codecs
from datetime import datetime
from collections import OrderedDict
from misaka import (
BaseRenderer,
@ -138,6 +139,7 @@ class READMETestRunner(BaseRenderer):
return formatted_tb.replace(
'@STEADYMARK@', title)
class Runner(object):
def __init__(self, filename=None, text=u''):
renderer = READMETestRunner()
@ -161,6 +163,55 @@ class Runner(object):
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():
from optparse import OptionParser

25
tests/__init__.py Normal file
View 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
View 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
View 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")