Skip to content
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.pyc
__pycache__
.cache
build
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ You should see the string `<html><body>Hello World!</body></html>` printed out.
After installing pyxl:

```sh
easy_install unittest2
python pyxl_tests.py
pip install pytest
pytest tests
```

## How it works
Expand Down
7 changes: 0 additions & 7 deletions tests/error_cases/if_1.py.txt

This file was deleted.

7 changes: 0 additions & 7 deletions tests/error_cases/if_2.py.txt

This file was deleted.

6 changes: 0 additions & 6 deletions tests/error_cases/if_3.py.txt

This file was deleted.

164 changes: 78 additions & 86 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -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(<div />.to_string(), '<div></div>')
self.assertEqual(<img src="blah" />.to_string(), '<img src="blah" />')
self.assertEqual(<div class="c"></div>.to_string(), '<div class="c"></div>')
self.assertEqual(<div><span></span></div>.to_string(), '<div><span></span></div>')
self.assertEqual(<frag><span /><span /></frag>.to_string(), '<span></span><span></span>')

def test_escaping(self):
self.assertEqual(<div class="&">&{'&'}</div>.to_string(), '<div class="&amp;">&&amp;</div>')
self.assertEqual(<div>{html.rawhtml('&')}</div>.to_string(), '<div>&</div>')

def test_comments(self):
pyxl = (
<div
class="blah" # attr comment
> # comment1
<!-- comment2 -->
text# comment3
# comment4
</div>)
self.assertEqual(pyxl.to_string(), '<div class="blah">text</div>')

def test_cond_comment(self):
s = 'blahblah'
self.assertEqual(
<cond_comment cond="lt IE 8"><div class=">">{s}</div></cond_comment>.to_string(),
'<!--[if lt IE 8]><div class="&gt;">blahblah</div><![endif]-->')
self.assertEqual(
<cond_comment cond="(lt IE 8) & (gt IE 5)"><div>{s}</div></cond_comment>.to_string(),
'<!--[if (lt IE 8) & (gt IE 5)]><div>blahblah</div><![endif]-->')

def test_decl(self):
self.assertEqual(
<script><![CDATA[<div><div>]]></script>.to_string(),
'<script><![CDATA[<div><div>]]></script>')

def test_form_error(self):
self.assertEqual(
<form_error name="foo" />.to_string(),
'<form:error name="foo" />')

def test_enum_attrs(self):
class x_foo(x_base):
__attrs__ = {
'value': ['a', 'b'],
}

def _to_list(self, l):
pass

self.assertEqual(<foo />.attr('value'), 'a')
self.assertEqual(<foo />.value, 'a')
self.assertEqual(<foo value="b" />.attr('value'), 'b')
self.assertEqual(<foo value="b" />.value, 'b')
with self.assertRaises(PyxlException):
<foo value="c" />

class x_bar(x_base):
__attrs__ = {
'value': ['a', None, 'b'],
}

def _to_list(self, l):
pass

with self.assertRaises(PyxlException):
<bar />.attr('value')

with self.assertRaises(PyxlException):
<bar />.value

class x_baz(x_base):
__attrs__ = {
'value': [None, 'a', 'b'],
}

def _to_list(self, l):
pass

self.assertEqual(<baz />.value, None)

if __name__ == '__main__':
unittest2.main()
def test_basics():
assert str(<div />) == '<div></div>'
assert str(<img src="blah" />) == '<img src="blah" />'
assert str(<div class="c"></div>) == '<div class="c"></div>'
assert str(<div><span></span></div>) == '<div><span></span></div>'
assert str(<frag><span /><span /></frag>) == '<span></span><span></span>'

def test_escaping():
assert str(<div class="&">&{'&'}</div>) == '<div class="&amp;">&&amp;</div>'
assert str(<div>{html.rawhtml('&')}</div>) == '<div>&</div>'

def test_comments():
pyxl = (
<div
class="blah" # attr comment
> # comment1
<!-- comment2 -->
text# comment3
# comment4
</div>)
assert str(pyxl) == '<div class="blah">text</div>'

def test_cond_comment():
s = 'blahblah'
assert (str(<cond_comment cond="lt IE 8"><div class=">">{s}</div></cond_comment>)
== '<!--[if lt IE 8]><div class="&gt;">blahblah</div><![endif]-->')
assert (str(<cond_comment cond="(lt IE 8) & (gt IE 5)"><div>{s}</div></cond_comment>)
== '<!--[if (lt IE 8) & (gt IE 5)]><div>blahblah</div><![endif]-->')

def test_decl():
assert (str(<script><![CDATA[<div><div>]]></script>)
== '<script><![CDATA[<div><div>]]></script>')

def test_form_error():
assert str(<form_error name="foo" />) == '<form:error name="foo" />'

def test_enum_attrs():
class x_foo(x_base):
__attrs__ = {
'value': ['a', 'b'],
}

def _to_list(self, l):
pass

assert (<foo />.attr('value')) == 'a'
assert (<foo />.value) == 'a'
assert (<foo value="b" />.attr('value')) == 'b'
assert (<foo value="b" />.value) == 'b'

with pytest.raises(PyxlException):
<foo value="c" />

class x_bar(x_base):
__attrs__ = {
'value': ['a', None, 'b'],
}

def _to_list(self, l):
pass

with pytest.raises(PyxlException):
<bar />.attr('value')

with pytest.raises(PyxlException):
<bar />.value

class x_baz(x_base):
__attrs__ = {
'value': [None, 'a', 'b'],
}

def _to_list(self, l):
pass

assert (<baz />.value) == None
44 changes: 26 additions & 18 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -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("""
<frag>
<if cond="{true}">foo</if>
this is incorrect!
<else>bar</else>
</frag>""")

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("""
<frag>
<if cond="{true}">foo</if>
<else>bar</else>
<else>baz</else>
</frag>""")

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("""
<frag>
<if cond="{true}">foo</if>
<else><else>bar</else></else>
</frag>""")
Loading