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
ce47e5e4
Commit
ce47e5e4
authored
Apr 04, 2012
by
Rodrigo Bernardo Pimentel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved dot notation tests to test_renderengine, and expanded them. Added helper class
parent
59771530
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
21 deletions
+82
-21
tests/common.py
+17
-0
tests/test_renderengine.py
+65
-1
tests/test_simple.py
+0
-20
No files found.
tests/common.py
View file @
ce47e5e4
...
...
@@ -51,3 +51,20 @@ 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
Attachable
(
object
):
"""A trivial object that attaches all constructor named parameters as attributes.
For instance,
>>> o = Attachable(foo=42, size="of the universe")
>>> o.foo
42
>>> o.size
of the universe
"""
def
__init__
(
self
,
**
kwargs
):
for
arg
,
value
in
kwargs
.
iteritems
():
setattr
(
self
,
arg
,
value
)
tests/test_renderengine.py
View file @
ce47e5e4
...
...
@@ -11,7 +11,7 @@ import unittest
from
pystache.context
import
Context
from
pystache.parser
import
ParsingError
from
pystache.renderengine
import
RenderEngine
from
tests.common
import
AssertStringMixin
from
tests.common
import
AssertStringMixin
,
Attachable
class
RenderEngineTestCase
(
unittest
.
TestCase
):
...
...
@@ -453,3 +453,67 @@ class RenderTests(unittest.TestCase, AssertStringMixin):
expected
=
u' {{foo}} '
self
.
_assert_render
(
expected
,
'{{=$ $=}} {{foo}} '
)
self
.
_assert_render
(
expected
,
'{{=$ $=}} {{foo}} $={{ }}=$'
)
# was yielding u' '.
def
test_dot_notation
(
self
):
"""
Check that we can use dot notation when the variable is a dict
or a used-defined object (or a combination of both)
"""
# With a dict:
template
=
'Hello, {{person.name}}. I see you are {{person.details.age}}.'
context
=
{
'person'
:
{
'name'
:
'Biggles'
,
'details'
:
{
'age'
:
42
}}}
self
.
_assert_render
(
u'Hello, Biggles. I see you are 42.'
,
template
,
context
)
# With a user-defined object:
details
=
Attachable
(
age
=
42
)
person
=
Attachable
(
name
=
'Biggles'
,
details
=
details
)
template
=
'Hello, {{person.name}}. I see you are {{person.details.age}}.'
context
=
{
'person'
:
person
}
self
.
_assert_render
(
u'Hello, Biggles. I see you are 42.'
,
template
,
context
)
# All together now!
person
=
Attachable
(
name
=
'Biggles'
,
details
=
{
'age'
:
42
})
template
=
'Hello, {{person.name}}. I see you are {{person.details.age}}.'
context
=
{
'person'
:
person
}
self
.
_assert_render
(
u'Hello, Biggles. I see you are 42.'
,
template
,
context
)
# And the other way around:
details
=
Attachable
(
age
=
42
)
person
=
{
'name'
:
'Biggles'
,
'details'
:
details
}
template
=
'Hello, {{person.name}}. I see you are {{person.details.age}}.'
context
=
{
'person'
:
person
}
self
.
_assert_render
(
u'Hello, Biggles. I see you are 42.'
,
template
,
context
)
def
test_dot_notation__missing_attributes_or_keys
(
self
):
"""
Check that, when using dot notation, if the key or attribute does not
exist then its value is rendered as empty
"""
template
=
"""I cannot see {{person.name}}'s age: {{person.age}}.
Nor {{other_person.name}}'s: ."""
expected
=
u"""I cannot see Biggles's age: .
Nor Mr. Bradshaw's: ."""
context
=
{
'person'
:
{
'name'
:
'Biggles'
},
'other_person'
:
Attachable
(
name
=
'Mr. Bradshaw'
)}
self
.
_assert_render
(
expected
,
template
,
context
)
def
test_dot_notation__multiple_levels
(
self
):
"""
Check that using multiple levels of dot attributes works as expected
"""
template
=
"""Hello, Mr. {{person.name.lastname}}.
I see you're back from {{person.travels.last.country.city}}.
I'm missing some of your details: {{person.details.private.editor}}."""
expected
=
u"""Hello, Mr. Pither.
I see you're back from Cornwall.
I'm missing some of your details: ."""
context
=
{
'person'
:
{
'name'
:
{
'firstname'
:
'unknown'
,
'lastname'
:
'Pither'
},
'travels'
:
{
'last'
:
{
'country'
:
{
'city'
:
'Cornwall'
}}},
'details'
:
{
'public'
:
'likes cycling'
}}}
self
.
_assert_render
(
expected
,
template
,
context
)
# It should also work with user-defined objects
context
=
{
'person'
:
Attachable
(
name
=
{
'firstname'
:
'unknown'
,
'lastname'
:
'Pither'
},
travels
=
Attachable
(
last
=
Attachable
(
country
=
Attachable
(
city
=
'Cornwall'
))),
details
=
Attachable
())}
self
.
_assert_render
(
expected
,
template
,
context
)
tests/test_simple.py
View file @
ce47e5e4
...
...
@@ -60,26 +60,6 @@ class TestSimple(unittest.TestCase, AssertStringMixin):
template
=
'{{not_set}} {{blank}}'
self
.
assertEquals
(
pystache
.
Renderer
()
.
render
(
template
),
' '
)
def
test_dot_notation_with_dict
(
self
):
template
=
'Name: {{person.name}}. Age: {{person.details.age}}. Intimate details (should be empty): {{person.details.intimate}}.'
renderer
=
Renderer
()
context
=
{
'person'
:
{
'name'
:
'Biggles'
,
'details'
:
{
'age'
:
42
}}}
actual
=
renderer
.
render
(
template
,
context
)
self
.
assertEquals
(
actual
,
'Name: Biggles. Age: 42. Intimate details (should be empty): .'
)
def
test_dot_notation_with_user_objects
(
self
):
template
=
'Name: {{person.name}}. Age: {{person.details.age}}. Intimate details (should be empty): {{person.details.intimate}}.'
renderer
=
Renderer
()
class
Person
(
object
):
def
__init__
(
self
,
name
,
details
):
self
.
name
=
name
self
.
details
=
details
context
=
{
'person'
:
Person
(
'Biggles'
,
{
'age'
:
42
})}
actual
=
renderer
.
render
(
template
,
context
)
self
.
assertEquals
(
actual
,
'Name: Biggles. Age: 42. Intimate details (should be empty): .'
)
def
test_template_partial_extension
(
self
):
"""
Side note:
...
...
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