Skip to content

Commit

Permalink
add test for CONTEXTS; restore CONTEXTS related documentation (im…
Browse files Browse the repository at this point in the history
…proved)
  • Loading branch information
d-maurer committed Jul 5, 2020
1 parent 1e7b380 commit f2132c7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
5 changes: 2 additions & 3 deletions CHANGES.rst
Expand Up @@ -10,12 +10,11 @@ https://zope.readthedocs.io/en/2.13/CHANGES.html
4.4.5 (unreleased)
------------------

- support the ``attrs`` predefined template variable again (as
- Support the ``attrs`` predefined template variable again (as
far as ``chameleon`` allows it)
(`#860 <https://github.com/zopefoundation/Zope/issues/860>`_).

- in the "Zope Book" remove ``CONTEXTS`` from the list of predefined
template variables (because it was never implemented).
- Improve documentation of ``CONTEXTS`` in the "Zope Book".

- Update dependencies to the latest releases that still support Python 2.

Expand Down
7 changes: 7 additions & 0 deletions docs/zopebook/AppendixC.rst
Expand Up @@ -698,6 +698,13 @@ Note that the (popular) ``chameleon`` template engine implements ``attrs``
and ``default`` not as standard variables but in a special way.
Trying to change their value may have undefined effects.

Note that beside variables you can use ``CONTEXTS``
as initial element in a path expression. Its value is a mapping
from predefined variable names to their value. This can be used to
access the predefined variable when it is hidden by a user defined
definition for its name. Again, `attrs` is special; it is not covered
by `CONTEXTS`.


TALES Exists expressions
========================
Expand Down
39 changes: 39 additions & 0 deletions src/Products/PageTemplates/tests/testVariables.py
Expand Up @@ -19,6 +19,27 @@ class TestPredefinedVariables(PlacelessSetup, TestCase):
as documented by
`<https://zope.readthedocs.io/en/latest/zopebook/AppendixC.html#built-in-names`_
"""

# variables documented in the Zope Book
VARIABLES = set((
"nothing",
"default",
"options",
"repeat",
# "attrs", # special -- not contained in ``CONTEXTS``
"root",
"context",
"container",
"template",
"request",
"user",
"modules",
# special
# - only usable as initial component in path expr
# - not contained in ``CONTEXTS``
# "CONTEXTS",
)) # noqa: E123

def setUp(self):
super(TestPredefinedVariables, self).setUp()

Expand Down Expand Up @@ -87,6 +108,24 @@ def test_user(self):
def test_modules(self):
self.check("modules")

def test_CONTEXTS(self):
# the "Zope Book" describes ``CONTEXTS`` as a variable.
# But, in fact, it is not a (regular) variable but a special
# case of the initial component of a (sub)path expression.
# As a consequence, it cannot be used in a Python expression
# but only as the first component in a path expression
# Therefore, we cannot use ``check`` to access it.
t = self.g.t
t.write("""<div tal:define="
x CONTEXTS;
dummy python:options['acc'].append(x)" />""")
acc = []
t(acc=acc)
ctx = acc[0]
self.assertIsInstance(ctx, dict)
# all variables included?
self.assertEqual(len(self.VARIABLES - set(ctx)), 0)

def check(self, what):
t = self.g.t
t.write("<div attr='attr' "
Expand Down

0 comments on commit f2132c7

Please sign in to comment.