Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pystache_custom
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pystache_custom
Commits
a8e390cb
Commit
a8e390cb
authored
Apr 08, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
All unit tests except for spec tests now work in both Python 2/3.
parent
f9bda3a5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
70 additions
and
52 deletions
+70
-52
pystache/loader.py
+14
-8
pystache/renderer.py
+3
-2
pystache/tests/common.py
+25
-0
pystache/tests/test_loader.py
+14
-27
pystache/tests/test_renderer.py
+14
-15
No files found.
pystache/loader.py
View file @
a8e390cb
...
...
@@ -13,14 +13,20 @@ from pystache import defaults
from
pystache.locator
import
Locator
def
_default_to_unicode
(
s
,
encoding
=
None
):
"""
Raises a TypeError exception if the given string is already unicod
e.
# We make a function so that the current defaults take effect.
# TODO: revisit whether this is necessary.
# TODO: change assertNotEquals to assertNotEqual everywher
e.
"""
if
encoding
is
None
:
encoding
=
defaults
.
STRING_ENCODING
return
unicode
(
s
,
encoding
,
defaults
.
DECODE_ERRORS
)
def
_make_to_unicode
():
def
to_unicode
(
s
,
encoding
=
None
):
"""
Raises a TypeError exception if the given string is already unicode.
"""
if
encoding
is
None
:
encoding
=
defaults
.
STRING_ENCODING
return
unicode
(
s
,
encoding
,
defaults
.
DECODE_ERRORS
)
return
to_unicode
class
Loader
(
object
):
...
...
@@ -68,7 +74,7 @@ class Loader(object):
search_dirs
=
defaults
.
SEARCH_DIRS
if
to_unicode
is
None
:
to_unicode
=
_
default_to_unicode
to_unicode
=
_
make_to_unicode
()
self
.
extension
=
extension
self
.
file_encoding
=
file_encoding
...
...
pystache/renderer.py
View file @
a8e390cb
...
...
@@ -40,8 +40,9 @@ class Renderer(object):
>>> partials = {'partial': 'Hello, {{thing}}!'}
>>> renderer = Renderer(partials=partials)
>>> renderer.render('{{>partial}}', {'thing': 'world'})
u'Hello, world!'
>>> # We apply print to make the test work in Python 3 after 2to3.
>>> print renderer.render('{{>partial}}', {'thing': 'world'})
Hello, world!
"""
...
...
pystache/tests/common.py
View file @
a8e390cb
...
...
@@ -88,3 +88,28 @@ class AssertIsMixin:
# http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone
def
assertIs
(
self
,
first
,
second
):
self
.
assertTrue
(
first
is
second
,
msg
=
"
%
s is not
%
s"
%
(
repr
(
first
),
repr
(
second
)))
class
SetupDefaults
(
object
):
"""
Mix this class in to a unittest.TestCase for standard defaults.
This class allows for consistent test results across Python 2/3.
"""
def
setup_defaults
(
self
):
self
.
original_decode_errors
=
defaults
.
DECODE_ERRORS
self
.
original_file_encoding
=
defaults
.
FILE_ENCODING
self
.
original_string_encoding
=
defaults
.
STRING_ENCODING
defaults
.
DECODE_ERRORS
=
'strict'
defaults
.
FILE_ENCODING
=
'ascii'
defaults
.
STRING_ENCODING
=
'ascii'
def
teardown_defaults
(
self
):
defaults
.
DECODE_ERRORS
=
self
.
original_decode_errors
defaults
.
FILE_ENCODING
=
self
.
original_file_encoding
defaults
.
STRING_ENCODING
=
self
.
original_string_encoding
pystache/tests/test_loader.py
View file @
a8e390cb
...
...
@@ -9,25 +9,18 @@ import os
import
sys
import
unittest
from
pystache.tests.common
import
AssertStringMixin
,
DATA_DIR
from
pystache.tests.common
import
AssertStringMixin
,
DATA_DIR
,
SetupDefaults
from
pystache
import
defaults
from
pystache.loader
import
Loader
class
LoaderTests
(
unittest
.
TestCase
,
AssertStringMixin
):
class
LoaderTests
(
unittest
.
TestCase
,
AssertStringMixin
,
SetupDefaults
):
# Switching to standard encodings allows for consistent test
# results across Python 2/3.
def
setUp
(
self
):
self
.
original_string_encoding
=
defaults
.
STRING_ENCODING
self
.
original_file_encoding
=
defaults
.
FILE_ENCODING
defaults
.
STRING_ENCODING
=
'ascii'
defaults
.
FILE_ENCODING
=
'ascii'
self
.
setup_defaults
()
def
tearDown
(
self
):
defaults
.
STRING_ENCODING
=
self
.
original_string_encoding
defaults
.
FILE_ENCODING
=
self
.
original_file_encoding
self
.
teardown_defaults
()
def
test_init__extension
(
self
):
loader
=
Loader
(
extension
=
'foo'
)
...
...
@@ -63,25 +56,19 @@ class LoaderTests(unittest.TestCase, AssertStringMixin):
decode_errors
=
defaults
.
DECODE_ERRORS
string_encoding
=
defaults
.
STRING_ENCODING
nonascii
=
'abcdé'
nonascii
=
u'abcdé'
.
encode
(
'utf-8'
)
try
:
defaults
.
DECODE_ERRORS
=
'strict'
defaults
.
STRING_ENCODING
=
'ascii'
loader
=
Loader
()
self
.
assertRaises
(
UnicodeDecodeError
,
loader
.
to_unicode
,
nonascii
)
loader
=
Loader
()
self
.
assertRaises
(
UnicodeDecodeError
,
loader
.
to_unicode
,
nonascii
)
defaults
.
DECODE_ERRORS
=
'ignore'
loader
=
Loader
()
self
.
assertString
(
loader
.
to_unicode
(
nonascii
),
u'abcd'
)
defaults
.
DECODE_ERRORS
=
'ignore'
loader
=
Loader
()
self
.
assertString
(
loader
.
to_unicode
(
nonascii
),
u'abcd'
)
defaults
.
STRING_ENCODING
=
'utf-8'
loader
=
Loader
()
self
.
assertString
(
loader
.
to_unicode
(
nonascii
),
u'abcdé'
)
defaults
.
STRING_ENCODING
=
'utf-8'
loader
=
Loader
()
self
.
assertString
(
loader
.
to_unicode
(
nonascii
),
u'abcdé'
)
finally
:
defaults
.
DECODE_ERRORS
=
decode_errors
defaults
.
STRING_ENCODING
=
string_encoding
def
_get_path
(
self
,
filename
):
return
os
.
path
.
join
(
DATA_DIR
,
filename
)
...
...
@@ -126,10 +113,10 @@ class LoaderTests(unittest.TestCase, AssertStringMixin):
Test unicode(): encoding attribute.
"""
# TODO: rename reader to loader everywhere in this module.
reader
=
Loader
()
non_ascii
=
u'abcdé'
.
encode
(
'utf-8'
)
self
.
assertRaises
(
UnicodeDecodeError
,
reader
.
unicode
,
non_ascii
)
def
to_unicode
(
s
,
encoding
=
None
):
...
...
pystache/tests/test_renderer.py
View file @
a8e390cb
...
...
@@ -15,8 +15,7 @@ from pystache import Renderer
from
pystache
import
TemplateSpec
from
pystache.loader
import
Loader
from
pystache.tests.common
import
get_data_path
from
pystache.tests.common
import
AssertStringMixin
from
pystache.tests.common
import
get_data_path
,
AssertStringMixin
from
pystache.tests.data.views
import
SayHello
...
...
@@ -179,7 +178,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test that the string_encoding attribute is respected.
"""
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
b
=
u"é"
.
encode
(
'utf-8'
)
renderer
.
string_encoding
=
"ascii"
...
...
@@ -193,7 +192,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test that the decode_errors attribute is respected.
"""
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
renderer
.
string_encoding
=
"ascii"
b
=
u"déf"
.
encode
(
'utf-8'
)
...
...
@@ -211,7 +210,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test that _make_loader() returns a Loader.
"""
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
loader
=
renderer
.
_make_loader
()
self
.
assertEqual
(
type
(
loader
),
Loader
)
...
...
@@ -223,7 +222,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
"""
unicode_
=
lambda
x
:
x
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
renderer
.
file_encoding
=
'enc'
renderer
.
file_extension
=
'ext'
renderer
.
unicode
=
unicode_
...
...
@@ -241,22 +240,22 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Check that render() returns a string of type unicode.
"""
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
rendered
=
renderer
.
render
(
'foo'
)
self
.
assertEqual
(
type
(
rendered
),
unicode
)
def
test_render__unicode
(
self
):
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
actual
=
renderer
.
render
(
u'foo'
)
self
.
assertEqual
(
actual
,
u'foo'
)
def
test_render__str
(
self
):
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
actual
=
renderer
.
render
(
'foo'
)
self
.
assertEqual
(
actual
,
'foo'
)
def
test_render__non_ascii_character
(
self
):
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
actual
=
renderer
.
render
(
u'Poincaré'
)
self
.
assertEqual
(
actual
,
u'Poincaré'
)
...
...
@@ -265,7 +264,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test render(): passing a context.
"""
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
self
.
assertEqual
(
renderer
.
render
(
'Hi {{person}}'
,
{
'person'
:
'Mom'
}),
'Hi Mom'
)
def
test_render__context_and_kwargs
(
self
):
...
...
@@ -273,7 +272,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test render(): passing a context and **kwargs.
"""
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
template
=
'Hi {{person1}} and {{person2}}'
self
.
assertEqual
(
renderer
.
render
(
template
,
{
'person1'
:
'Mom'
},
person2
=
'Dad'
),
'Hi Mom and Dad'
)
...
...
@@ -282,7 +281,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test render(): passing **kwargs and no context.
"""
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
self
.
assertEqual
(
renderer
.
render
(
'Hi {{person}}'
,
person
=
'Mom'
),
'Hi Mom'
)
def
test_render__context_and_kwargs__precedence
(
self
):
...
...
@@ -290,7 +289,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test render(): **kwargs takes precedence over context.
"""
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
self
.
assertEqual
(
renderer
.
render
(
'Hi {{person}}'
,
{
'person'
:
'Mom'
},
person
=
'Dad'
),
'Hi Dad'
)
def
test_render__kwargs_does_not_modify_context
(
self
):
...
...
@@ -299,7 +298,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
"""
context
=
{}
renderer
=
R
enderer
()
renderer
=
self
.
_r
enderer
()
renderer
.
render
(
'Hi {{person}}'
,
context
=
context
,
foo
=
"bar"
)
self
.
assertEqual
(
context
,
{})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment