diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca316c9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.pyc +__pycache__ +.cache +build diff --git a/README.md b/README.md index 187fc77..fdb97a3 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,8 @@ You should see the string `Hello World!` printed out. After installing pyxl: ```sh -easy_install unittest2 -python pyxl_tests.py +pip install pytest +pytest tests ``` ## How it works diff --git a/tests/error_cases/if_1.py.txt b/tests/error_cases/if_1.py.txt deleted file mode 100644 index a4260d4..0000000 --- a/tests/error_cases/if_1.py.txt +++ /dev/null @@ -1,7 +0,0 @@ -# coding: pyxl - -a = ( - foo - this is incorrect! - bar - ) diff --git a/tests/error_cases/if_2.py.txt b/tests/error_cases/if_2.py.txt deleted file mode 100644 index a7df87e..0000000 --- a/tests/error_cases/if_2.py.txt +++ /dev/null @@ -1,7 +0,0 @@ -# coding: pyxl - -a = ( - foo - bar - baz - ) diff --git a/tests/error_cases/if_3.py.txt b/tests/error_cases/if_3.py.txt deleted file mode 100644 index 470818c..0000000 --- a/tests/error_cases/if_3.py.txt +++ /dev/null @@ -1,6 +0,0 @@ -# coding: pyxl - -a = ( - foo - bar - ) diff --git a/tests/test_basic.py b/tests/test_basic.py index 0cae196..d1f3aab 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1,90 +1,82 @@ # coding: pyxl -import unittest2 +import pytest + from pyxl import html from pyxl.base import PyxlException, x_base -class PyxlTests(unittest2.TestCase): - - def test_basics(self): - self.assertEqual(
.to_string(), '
') - self.assertEqual(.to_string(), '') - self.assertEqual(
.to_string(), '
') - self.assertEqual(
.to_string(), '
') - self.assertEqual(.to_string(), '') - - def test_escaping(self): - self.assertEqual(
&{'&'}
.to_string(), '
&&
') - self.assertEqual(
{html.rawhtml('&')}
.to_string(), '
&
') - - def test_comments(self): - pyxl = ( -
# comment1 - - text# comment3 - # comment4 -
) - self.assertEqual(pyxl.to_string(), '
text
') - - def test_cond_comment(self): - s = 'blahblah' - self.assertEqual( -
{s}
.to_string(), - '') - self.assertEqual( -
{s}
.to_string(), - '') - - def test_decl(self): - self.assertEqual( - .to_string(), - '') - - def test_form_error(self): - self.assertEqual( - .to_string(), - '') - - def test_enum_attrs(self): - class x_foo(x_base): - __attrs__ = { - 'value': ['a', 'b'], - } - - def _to_list(self, l): - pass - - self.assertEqual(.attr('value'), 'a') - self.assertEqual(.value, 'a') - self.assertEqual(.attr('value'), 'b') - self.assertEqual(.value, 'b') - with self.assertRaises(PyxlException): - - - class x_bar(x_base): - __attrs__ = { - 'value': ['a', None, 'b'], - } - - def _to_list(self, l): - pass - - with self.assertRaises(PyxlException): - .attr('value') - - with self.assertRaises(PyxlException): - .value - - class x_baz(x_base): - __attrs__ = { - 'value': [None, 'a', 'b'], - } - - def _to_list(self, l): - pass - - self.assertEqual(.value, None) - -if __name__ == '__main__': - unittest2.main() +def test_basics(): + assert str(
) == '
' + assert str() == '' + assert str(
) == '
' + assert str(
) == '
' + assert str() == '' + +def test_escaping(): + assert str(
&{'&'}
) == '
&&
' + assert str(
{html.rawhtml('&')}
) == '
&
' + +def test_comments(): + pyxl = ( +
# comment1 + + text# comment3 + # comment4 +
) + assert str(pyxl) == '
text
' + +def test_cond_comment(): + s = 'blahblah' + assert (str(
{s}
) + == '') + assert (str(
{s}
) + == '') + +def test_decl(): + assert (str() + == '') + +def test_form_error(): + assert str() == '' + +def test_enum_attrs(): + class x_foo(x_base): + __attrs__ = { + 'value': ['a', 'b'], + } + + def _to_list(self, l): + pass + + assert (.attr('value')) == 'a' + assert (.value) == 'a' + assert (.attr('value')) == 'b' + assert (.value) == 'b' + + with pytest.raises(PyxlException): + + + class x_bar(x_base): + __attrs__ = { + 'value': ['a', None, 'b'], + } + + def _to_list(self, l): + pass + + with pytest.raises(PyxlException): + .attr('value') + + with pytest.raises(PyxlException): + .value + + class x_baz(x_base): + __attrs__ = { + 'value': [None, 'a', 'b'], + } + + def _to_list(self, l): + pass + + assert (.value) == None diff --git a/tests/test_errors.py b/tests/test_errors.py index cd18bd0..49a3500 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -1,23 +1,31 @@ +# coding: pyxl +import pytest + from pyxl.codec.register import pyxl_decode -from pyxl.codec.tokenizer import PyxlParseError from pyxl.codec.parser import ParseError -import os - -error_cases_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), - 'error_cases') +def test_malformed_if(): + with pytest.raises(ParseError): + pyxl_decode(""" + + foo + this is incorrect! + bar + """) -def _expect_failure(file_name): - path = os.path.join(error_cases_path, file_name) - try: - with open(path) as f: - print pyxl_decode(f.read()) - assert False, "successfully decoded file %r" % file_name - except (PyxlParseError, ParseError): - pass +def test_multiple_else(): + with pytest.raises(ParseError): + pyxl_decode(""" + + foo + bar + baz + """) -def test_error_cases(): - cases = os.listdir(error_cases_path) - for file_name in cases: - if file_name.endswith(".txt"): - yield (_expect_failure, file_name) +def test_nested_else(): + with pytest.raises(ParseError): + pyxl_decode(""" + + foo + bar + """) diff --git a/tests/test_rss.py b/tests/test_rss.py index 5a9729c..507c0e6 100644 --- a/tests/test_rss.py +++ b/tests/test_rss.py @@ -1,195 +1,190 @@ #coding: pyxl import datetime -from unittest2 import TestCase from pyxl import html from pyxl import rss -class RssTests(TestCase): - def test_decl(self): - decl = .to_string() - self.assertEqual(decl, u'') +def test_decl(): + assert (str() + == u'') - def test_rss(self): - r = .to_string() - self.assertEqual(r, u'') +def test_rss(): + assert str() == u'' - def test_channel(self): - c = ( +def test_channel(): + assert str( + + + + ) == u'' + +def test_channel_with_required_elements(): + channel = ( + + - + + A Title + https://www.dropbox.com + A detailed description + - ).to_string() - - self.assertEqual(c, u'') - - def test_channel_with_required_elements(self): - channel = ( - - - - - A Title - https://www.dropbox.com - A detailed description - - - - ) - - expected = ''' + + ) + + expected = ''' - - A Title - https://www.dropbox.com - A detailed description - + + A Title + https://www.dropbox.com + A detailed description + ''' - expected = u''.join(l.strip() for l in expected.splitlines()) - - self.assertEqual(channel.to_string(), expected) - - def test_channel_with_optional_elements(self): - channel = ( - - - - - A Title - https://www.dropbox.com - A detailed description - 60 - en-us - - - - ) - - expected = """ + expected = u''.join(l.strip() for l in expected.splitlines()) + + assert str(channel) == expected + +def test_channel_with_optional_elements(): + channel = ( + + + + + A Title + https://www.dropbox.com + A detailed description + 60 + en-us + + + + ) + + expected = """ - - A Title - https://www.dropbox.com - A detailed description - 60 - en-us - + + A Title + https://www.dropbox.com + A detailed description + 60 + en-us + """ - expected = u''.join(l.strip() for l in expected.splitlines()) - self.assertEqual(channel.to_string(), expected) - - def test_item_with_common_elements(self): - item = ( - - Item Title - - {html.rawhtml('')} - - https://www.dropbox.com/somewhere - - ) - - expected = """ + expected = u''.join(l.strip() for l in expected.splitlines()) + assert str(channel) == expected + +def test_item_with_common_elements(): + item = ( + + Item Title + + {html.rawhtml('')} + + https://www.dropbox.com/somewhere + + ) + + expected = """ - Item Title - - https://www.dropbox.com/somewhere +Item Title + +https://www.dropbox.com/somewhere """ - expected = u''.join(l.strip() for l in expected.splitlines()) - self.assertEqual(item.to_string(), expected) - - def test_guid(self): - self.assertEqual(foo.to_string(), u'foo') - self.assertEqual(foo.to_string(), - u'foo') - self.assertEqual(foo.to_string(), - u'foo') - - def test_date_elements(self): - dt = datetime.datetime(2013, 12, 17, 23, 54, 14) - self.assertEqual(.to_string(), - u'Tue, 17 Dec 2013 23:54:14 GMT') - self.assertEqual(.to_string(), - u'Tue, 17 Dec 2013 23:54:14 GMT') - - def test_rss_document(self): - dt = datetime.datetime(2013, 12, 17, 23, 54, 14) - dt2 = datetime.datetime(2013, 12, 18, 11, 54, 14) - doc = ( - - - - - A Title - https://www.dropbox.com - A detailed description - 60 - en-us - - - Item Title - - {html.rawhtml('')} - - https://www.dropbox.com/somewhere - - 123456789 - - - Another Item - - {html.rawhtml('')} - - https://www.dropbox.com/nowhere - - ABCDEFGHIJ - - - - - ) - - expected = """ + expected = u''.join(l.strip() for l in expected.splitlines()) + assert str(item) == expected + +def test_guid(): + assert str(foo) == u'foo' + assert (str(foo) + == u'foo') + assert (str(foo) + == u'foo') + +def test_date_elements(): + dt = datetime.datetime(2013, 12, 17, 23, 54, 14) + assert (str() + == u'Tue, 17 Dec 2013 23:54:14 GMT') + assert (str() + == u'Tue, 17 Dec 2013 23:54:14 GMT') + +def test_rss_document(): + dt = datetime.datetime(2013, 12, 17, 23, 54, 14) + dt2 = datetime.datetime(2013, 12, 18, 11, 54, 14) + doc = ( + + + + + A Title + https://www.dropbox.com + A detailed description + 60 + en-us + + + Item Title + + {html.rawhtml('')} + + https://www.dropbox.com/somewhere + + 123456789 + + + Another Item + + {html.rawhtml('')} + + https://www.dropbox.com/nowhere + + ABCDEFGHIJ + + + + + ) + + expected = """ - - A Title - https://www.dropbox.com - A detailed description - 60 - en-us - Tue, 17 Dec 2013 23:54:14 GMT - - Item Title - - https://www.dropbox.com/somewhere - Tue, 17 Dec 2013 23:54:14 GMT - 123456789 - - - Another Item - - https://www.dropbox.com/nowhere - Wed, 18 Dec 2013 11:54:14 GMT - ABCDEFGHIJ - - + + A Title + https://www.dropbox.com + A detailed description + 60 + en-us + Tue, 17 Dec 2013 23:54:14 GMT + + Item Title + + https://www.dropbox.com/somewhere + Tue, 17 Dec 2013 23:54:14 GMT + 123456789 + + + Another Item + + https://www.dropbox.com/nowhere + Wed, 18 Dec 2013 11:54:14 GMT + ABCDEFGHIJ + + """ - expected = ''.join(l.strip() for l in expected.splitlines()) + expected = ''.join(l.strip() for l in expected.splitlines()) - self.assertEqual(doc.to_string(), expected) + assert str(doc) == expected