summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_grammar.py
diff options
context:
space:
mode:
authorThomas Wouters <thomas@python.org>2006-02-27 00:24:13 (GMT)
committerThomas Wouters <thomas@python.org>2006-02-27 00:24:13 (GMT)
commitdca3b9c797f6dd4b08d590fa2aa1031e22ab598e (patch)
treed2b7aa53793110100965906b1266296d08bd4ec1 /Lib/test/test_grammar.py
parentd3a5f53a27be821cfdff869fd8ad93a060497e8c (diff)
downloadcpython-dca3b9c797f6dd4b08d590fa2aa1031e22ab598e.zip
cpython-dca3b9c797f6dd4b08d590fa2aa1031e22ab598e.tar.gz
cpython-dca3b9c797f6dd4b08d590fa2aa1031e22ab598e.tar.bz2
PEP 308 implementation, including minor refdocs and some testcases. It
breaks the parser module, because it adds the if/else construct as well as two new grammar rules for backward compatibility. If no one else fixes parsermodule, I guess I'll go ahead and fix it later this week. The TeX code was checked with texcheck.py, but not rendered. There is actually a slight incompatibility: >>> (x for x in lambda:0) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: iteration over non-sequence changes into >>> (x for x in lambda: 0) File "<stdin>", line 1 (x for x in lambda: 0) ^ SyntaxError: invalid syntax Since there's no way the former version can be useful, it's probably a bugfix ;)
Diffstat (limited to 'Lib/test/test_grammar.py')
-rw-r--r--Lib/test/test_grammar.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index aa76b44..0d2d925 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -798,3 +798,28 @@ verify(len(list(g)) == 10)
x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
x = 5; t = True;
verify([(i,j) for i in range(10) for j in range(5)] == list(g))
+
+# Test ifelse expressions in various cases
+def _checkeval(msg, ret):
+ "helper to check that evaluation of expressions is done correctly"
+ print x
+ return ret
+
+verify([ x() for x in lambda: True, lambda: False if x() ] == [True])
+verify([ x() for x in (lambda: True, lambda: False) if x() ] == [True])
+verify([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ] == [True])
+verify((5 if 1 else _checkeval("check 1", 0)) == 5)
+verify((_checkeval("check 2", 0) if 0 else 5) == 5)
+verify((5 and 6 if 0 else 1) == 1)
+verify(((5 and 6) if 0 else 1) == 1)
+verify((5 and (6 if 1 else 1)) == 6)
+verify((0 or _checkeval("check 3", 2) if 0 else 3) == 3)
+verify((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)) == 1)
+verify((0 or 5 if 1 else _checkeval("check 6", 3)) == 5)
+verify((not 5 if 1 else 1) == False)
+verify((not 5 if 0 else 1) == 1)
+verify((6 + 1 if 1 else 2) == 7)
+verify((6 - 1 if 1 else 2) == 5)
+verify((6 * 2 if 1 else 4) == 12)
+verify((6 / 2 if 1 else 3) == 3)
+verify((6 < 4 if 0 else 2) == 2)