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
3f30290b
Commit
3f30290b
authored
May 04, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Getting "." from an empty context stack now raises a KeyNotFoundError.
parent
049a76c1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
8 deletions
+45
-8
pystache/context.py
+22
-2
pystache/tests/test_context.py
+23
-6
No files found.
pystache/context.py
View file @
3f30290b
...
@@ -14,6 +14,9 @@ spec, we define these categories mutually exclusively as follows:
...
@@ -14,6 +14,9 @@ spec, we define these categories mutually exclusively as follows:
"""
"""
from
pystache.common
import
PystacheError
# This equals '__builtin__' in Python 2 and 'builtins' in Python 3.
# This equals '__builtin__' in Python 2 and 'builtins' in Python 3.
_BUILTIN_MODULE
=
type
(
0
)
.
__module__
_BUILTIN_MODULE
=
type
(
0
)
.
__module__
...
@@ -73,6 +76,21 @@ def _get_value(context, key):
...
@@ -73,6 +76,21 @@ def _get_value(context, key):
return
_NOT_FOUND
return
_NOT_FOUND
class
KeyNotFoundError
(
PystacheError
):
"""
An exception raised when a key is not found in a context stack.
"""
def
__init__
(
self
,
key
,
details
):
self
.
key
=
key
self
.
details
=
details
def
__str__
(
self
):
return
"Key
%
s not found:
%
s"
%
(
repr
(
self
.
key
),
self
.
details
)
class
ContextStack
(
object
):
class
ContextStack
(
object
):
"""
"""
...
@@ -252,8 +270,10 @@ class ContextStack(object):
...
@@ -252,8 +270,10 @@ class ContextStack(object):
"""
"""
if
name
==
'.'
:
if
name
==
'.'
:
# TODO: should we add a test case for an empty context stack?
try
:
return
self
.
top
()
return
self
.
top
()
except
IndexError
:
raise
KeyNotFoundError
(
"."
,
"empty context stack"
)
parts
=
name
.
split
(
'.'
)
parts
=
name
.
split
(
'.'
)
...
...
pystache/tests/test_context.py
View file @
3f30290b
...
@@ -8,10 +8,8 @@ Unit tests of context.py.
...
@@ -8,10 +8,8 @@ Unit tests of context.py.
from
datetime
import
datetime
from
datetime
import
datetime
import
unittest
import
unittest
from
pystache.context
import
_NOT_FOUND
from
pystache.context
import
_NOT_FOUND
,
_get_value
,
KeyNotFoundError
,
ContextStack
from
pystache.context
import
_get_value
from
pystache.tests.common
import
AssertIsMixin
,
AssertStringMixin
,
AssertExceptionMixin
,
Attachable
from
pystache.context
import
ContextStack
from
pystache.tests.common
import
AssertIsMixin
,
AssertStringMixin
,
Attachable
class
SimpleObject
(
object
):
class
SimpleObject
(
object
):
...
@@ -39,7 +37,7 @@ class DictLike(object):
...
@@ -39,7 +37,7 @@ class DictLike(object):
return
self
.
_dict
[
key
]
return
self
.
_dict
[
key
]
class
GetValueTest
s
(
unittest
.
TestCase
,
AssertIsMixin
):
class
GetValueTest
Case
(
unittest
.
TestCase
,
AssertIsMixin
):
"""Test context._get_value()."""
"""Test context._get_value()."""
...
@@ -224,7 +222,8 @@ class GetValueTests(unittest.TestCase, AssertIsMixin):
...
@@ -224,7 +222,8 @@ class GetValueTests(unittest.TestCase, AssertIsMixin):
self
.
assertNotFound
(
item2
,
'pop'
)
self
.
assertNotFound
(
item2
,
'pop'
)
class
ContextStackTests
(
unittest
.
TestCase
,
AssertIsMixin
,
AssertStringMixin
):
class
ContextStackTestCase
(
unittest
.
TestCase
,
AssertIsMixin
,
AssertStringMixin
,
AssertExceptionMixin
):
"""
"""
Test the ContextStack class.
Test the ContextStack class.
...
@@ -326,6 +325,24 @@ class ContextStackTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
...
@@ -326,6 +325,24 @@ class ContextStackTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
context
=
ContextStack
.
create
({
'foo'
:
'bar'
},
foo
=
'buzz'
)
context
=
ContextStack
.
create
({
'foo'
:
'bar'
},
foo
=
'buzz'
)
self
.
assertEqual
(
context
.
get
(
'foo'
),
'buzz'
)
self
.
assertEqual
(
context
.
get
(
'foo'
),
'buzz'
)
## Test the get() method.
def
test_get__single_dot
(
self
):
"""
Test getting a single dot (".").
"""
context
=
ContextStack
(
"a"
,
"b"
)
self
.
assertEqual
(
context
.
get
(
"."
),
"b"
)
def
test_get__single_dot__missing
(
self
):
"""
Test getting a single dot (".") with an empty context stack.
"""
context
=
ContextStack
()
self
.
assertException
(
KeyNotFoundError
,
"Key '.' not found: empty context stack"
,
context
.
get
,
"."
,
"b"
)
def
test_get__key_present
(
self
):
def
test_get__key_present
(
self
):
"""
"""
Test getting a key.
Test getting a key.
...
...
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