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
8782cc27
Commit
8782cc27
authored
Jan 24, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
The view.Locator class now supports view.template_encoding.
parent
af2b04dc
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
72 additions
and
33 deletions
+72
-33
pystache/view.py
+11
-5
tests/data/non_ascii.mustache
+0
-0
tests/data/sample_view.mustache
+2
-2
tests/data/views.py
+4
-0
tests/test_reader.py
+3
-3
tests/test_renderer.py
+2
-2
tests/test_view.py
+50
-21
No files found.
pystache/view.py
View file @
8782cc27
...
@@ -9,6 +9,7 @@ import os.path
...
@@ -9,6 +9,7 @@ import os.path
from
.context
import
Context
from
.context
import
Context
from
.locator
import
Locator
as
TemplateLocator
from
.locator
import
Locator
as
TemplateLocator
from
.reader
import
Reader
from
.renderer
import
Renderer
from
.renderer
import
Renderer
...
@@ -137,7 +138,14 @@ class Locator(object):
...
@@ -137,7 +138,14 @@ class Locator(object):
"""
"""
def
__init__
(
self
,
reader
,
search_dirs
,
template_locator
):
# TODO: unit test this.
def
__init__
(
self
,
search_dirs
,
template_locator
=
None
,
reader
=
None
):
if
reader
is
None
:
reader
=
Reader
()
if
template_locator
is
None
:
template_locator
=
TemplateLocator
()
self
.
reader
=
reader
self
.
reader
=
reader
self
.
search_dirs
=
search_dirs
self
.
search_dirs
=
search_dirs
self
.
template_locator
=
template_locator
self
.
template_locator
=
template_locator
...
@@ -180,10 +188,8 @@ class Locator(object):
...
@@ -180,10 +188,8 @@ class Locator(object):
"""
"""
if
view
.
template
is
not
None
:
if
view
.
template
is
not
None
:
# TODO: unit test rendering with a non-unicode value for this attribute.
return
self
.
reader
.
unicode
(
view
.
template
,
view
.
template_encoding
)
return
view
.
template
path
=
self
.
get_template_path
(
view
)
path
=
self
.
get_template_path
(
view
)
# TODO: add support for encoding.
return
self
.
reader
.
read
(
path
,
view
.
template_encoding
)
return
self
.
reader
.
read
(
path
)
tests/data/nonascii.mustache
→
tests/data/non
_
ascii.mustache
View file @
8782cc27
File moved
tests/data/sample_view.mustache
View file @
8782cc27
Sample view...
ascii: abc
\ No newline at end of file
\ No newline at end of file
tests/data/views.py
View file @
8782cc27
...
@@ -10,3 +10,7 @@ class SayHello(object):
...
@@ -10,3 +10,7 @@ class SayHello(object):
class
SampleView
(
View
):
class
SampleView
(
View
):
pass
pass
class
NonAscii
(
View
):
pass
tests/test_reader.py
View file @
8782cc27
...
@@ -100,7 +100,7 @@ class ReaderTestCase(unittest.TestCase):
...
@@ -100,7 +100,7 @@ class ReaderTestCase(unittest.TestCase):
"""
"""
reader
=
Reader
()
reader
=
Reader
()
path
=
self
.
_get_path
(
'nonascii.mustache'
)
path
=
self
.
_get_path
(
'non
_
ascii.mustache'
)
self
.
assertRaises
(
UnicodeDecodeError
,
reader
.
read
,
path
)
self
.
assertRaises
(
UnicodeDecodeError
,
reader
.
read
,
path
)
reader
.
encoding
=
'utf-8'
reader
.
encoding
=
'utf-8'
...
@@ -112,7 +112,7 @@ class ReaderTestCase(unittest.TestCase):
...
@@ -112,7 +112,7 @@ class ReaderTestCase(unittest.TestCase):
"""
"""
reader
=
Reader
()
reader
=
Reader
()
path
=
self
.
_get_path
(
'nonascii.mustache'
)
path
=
self
.
_get_path
(
'non
_
ascii.mustache'
)
self
.
assertRaises
(
UnicodeDecodeError
,
reader
.
read
,
path
)
self
.
assertRaises
(
UnicodeDecodeError
,
reader
.
read
,
path
)
self
.
assertEquals
(
reader
.
read
(
path
,
encoding
=
'utf-8'
),
u'non-ascii: é'
)
self
.
assertEquals
(
reader
.
read
(
path
,
encoding
=
'utf-8'
),
u'non-ascii: é'
)
...
@@ -123,7 +123,7 @@ class ReaderTestCase(unittest.TestCase):
...
@@ -123,7 +123,7 @@ class ReaderTestCase(unittest.TestCase):
"""
"""
reader
=
Reader
()
reader
=
Reader
()
path
=
self
.
_get_path
(
'nonascii.mustache'
)
path
=
self
.
_get_path
(
'non
_
ascii.mustache'
)
self
.
assertRaises
(
UnicodeDecodeError
,
reader
.
read
,
path
)
self
.
assertRaises
(
UnicodeDecodeError
,
reader
.
read
,
path
)
reader
.
decode_errors
=
'replace'
reader
.
decode_errors
=
'replace'
...
...
tests/test_renderer.py
View file @
8782cc27
...
@@ -200,7 +200,7 @@ class RendererTestCase(unittest.TestCase):
...
@@ -200,7 +200,7 @@ class RendererTestCase(unittest.TestCase):
self
.
assertEquals
(
type
(
actual
),
unicode
)
self
.
assertEquals
(
type
(
actual
),
unicode
)
def
test_read__file_encoding
(
self
):
def
test_read__file_encoding
(
self
):
filename
=
'nonascii.mustache'
filename
=
'non
_
ascii.mustache'
renderer
=
Renderer
()
renderer
=
Renderer
()
renderer
.
file_encoding
=
'ascii'
renderer
.
file_encoding
=
'ascii'
...
@@ -211,7 +211,7 @@ class RendererTestCase(unittest.TestCase):
...
@@ -211,7 +211,7 @@ class RendererTestCase(unittest.TestCase):
self
.
assertEquals
(
actual
,
u'non-ascii: é'
)
self
.
assertEquals
(
actual
,
u'non-ascii: é'
)
def
test_read__decode_errors
(
self
):
def
test_read__decode_errors
(
self
):
filename
=
'nonascii.mustache'
filename
=
'non
_
ascii.mustache'
renderer
=
Renderer
()
renderer
=
Renderer
()
self
.
assertRaises
(
UnicodeDecodeError
,
self
.
_read
,
renderer
,
filename
)
self
.
assertRaises
(
UnicodeDecodeError
,
self
.
_read
,
renderer
,
filename
)
...
...
tests/test_view.py
View file @
8782cc27
# coding: utf-8
"""
Unit tests of view.py.
"""
import
os.path
import
os.path
import
unittest
import
unittest
...
@@ -5,12 +12,13 @@ from examples.simple import Simple
...
@@ -5,12 +12,13 @@ from examples.simple import Simple
from
examples.complex_view
import
ComplexView
from
examples.complex_view
import
ComplexView
from
examples.lambdas
import
Lambdas
from
examples.lambdas
import
Lambdas
from
examples.inverted
import
Inverted
,
InvertedLists
from
examples.inverted
import
Inverted
,
InvertedLists
from
pystache.
locator
import
Locator
as
TemplateLocato
r
from
pystache.
reader
import
Reade
r
from
pystache.view
import
View
from
pystache.view
import
View
from
pystache.view
import
Locator
as
ViewLocator
from
pystache.view
import
Locator
as
ViewLocator
from
.common
import
AssertIsMixin
from
.common
import
AssertIsMixin
from
.common
import
DATA_DIR
from
.common
import
DATA_DIR
from
.data.views
import
SampleView
from
.data.views
import
SampleView
from
.data.views
import
NonAscii
class
Thing
(
object
):
class
Thing
(
object
):
...
@@ -177,19 +185,13 @@ class ViewTestCase(unittest.TestCase):
...
@@ -177,19 +185,13 @@ class ViewTestCase(unittest.TestCase):
class
LocatorTests
(
unittest
.
TestCase
,
AssertIsMixin
):
class
LocatorTests
(
unittest
.
TestCase
,
AssertIsMixin
):
def
_make_locator
(
self
):
def
_make_locator
(
self
):
class
MockReader
(
object
):
locator
=
ViewLocator
(
search_dirs
=
[
DATA_DIR
])
def
read
(
self
,
path
):
return
"read:
%
s"
%
repr
(
path
)
reader
=
MockReader
()
template_locator
=
TemplateLocator
()
locator
=
ViewLocator
(
reader
=
reader
,
search_dirs
=
[
DATA_DIR
],
template_locator
=
template_locator
)
return
locator
return
locator
# TODO: fully test constructor.
# TODO: fully test constructor.
def
test_init__reader
(
self
):
def
test_init__reader
(
self
):
reader
=
"reader"
# in practice, this is a reader instance.
reader
=
"reader"
# in practice, this is a reader instance.
locator
=
ViewLocator
(
reader
,
search_dirs
=
None
,
template_locator
=
None
)
locator
=
ViewLocator
(
search_dirs
=
None
,
template_locator
=
None
,
reader
=
reader
)
self
.
assertIs
(
locator
.
reader
,
reader
)
self
.
assertIs
(
locator
.
reader
,
reader
)
...
@@ -273,26 +275,53 @@ class LocatorTests(unittest.TestCase, AssertIsMixin):
...
@@ -273,26 +275,53 @@ class LocatorTests(unittest.TestCase, AssertIsMixin):
self
.
assertEquals
(
actual
,
expected
)
self
.
assertEquals
(
actual
,
expected
)
def
test_get_template__template_attribute_set
(
self
):
def
_assert_get_template
(
self
,
view
,
expected
):
locator
=
self
.
_make_locator
()
actual
=
locator
.
get_template
(
view
)
self
.
assertEquals
(
type
(
actual
),
unicode
)
self
.
assertEquals
(
actual
,
expected
)
def
test_get_template
(
self
):
"""
"""
Test get_template()
with view.template set to a non-None value
.
Test get_template()
: default behavior (no attributes set)
.
"""
"""
locator
=
self
.
_make_locator
()
view
=
SampleView
()
view
=
View
()
self
.
_assert_get_template
(
view
,
u"ascii: abc"
)
def
test_get_template__template
(
self
):
"""
Test get_template(): template attribute.
"""
view
=
SampleView
()
view
.
template
=
'foo'
view
.
template
=
'foo'
self
.
assertEquals
(
locator
.
get_template
(
view
)
,
'foo'
)
self
.
_assert_get_template
(
view
,
'foo'
)
def
test_get_template__template_
attribute_not_set
(
self
):
def
test_get_template__template_
_template_encoding
(
self
):
"""
"""
Test get_template()
with view.template set to Non
e.
Test get_template()
: template attribute with template encoding attribut
e.
"""
"""
locator
=
self
.
_make_locator
()
view
=
SampleView
()
locator
.
get_template_path
=
lambda
view
:
"path"
view
.
template
=
u'é'
.
encode
(
'utf-8'
)
self
.
assertRaises
(
UnicodeDecodeError
,
self
.
_assert_get_template
,
view
,
'foo'
)
view
.
template_encoding
=
'utf-8'
self
.
_assert_get_template
(
view
,
u'é'
)
def
test_get_template__template_encoding
(
self
):
"""
Test get_template(): template_encoding attribute.
"""
view
=
NonAscii
()
view
=
View
()
self
.
assertRaises
(
UnicodeDecodeError
,
self
.
_assert_get_template
,
view
,
'foo'
)
view
.
template
=
None
self
.
assertEquals
(
locator
.
get_template
(
view
),
"read: 'path'"
)
view
.
template_encoding
=
'utf-8'
self
.
_assert_get_template
(
view
,
u"non-ascii: é"
)
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