summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSkip Montanaro <skip@pobox.com>2000-08-12 18:09:51 (GMT)
committerSkip Montanaro <skip@pobox.com>2000-08-12 18:09:51 (GMT)
commit803d6e5451cbf0416bfb252eedba36ff2e354fac (patch)
treee37c0cf52e141c341e6b8a64a5d7e7691313df41 /Lib/test
parentb16b83534d008d45a2917bd2c4fd65010909dff2 (diff)
downloadcpython-803d6e5451cbf0416bfb252eedba36ff2e354fac.zip
cpython-803d6e5451cbf0416bfb252eedba36ff2e354fac.tar.gz
cpython-803d6e5451cbf0416bfb252eedba36ff2e354fac.tar.bz2
list comprehensions. see
http://sourceforge.net/patch/?func=detailpatch&patch_id=100654&group_id=5470 for details.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/output/test_grammar7
-rw-r--r--Lib/test/test_grammar.py40
2 files changed, 47 insertions, 0 deletions
diff --git a/Lib/test/output/test_grammar b/Lib/test/output/test_grammar
index 1926e4d..b075d35 100644
--- a/Lib/test/output/test_grammar
+++ b/Lib/test/output/test_grammar
@@ -45,3 +45,10 @@ selectors
atoms
classdef
+['Apple', 'Banana', 'Coco nut']
+[3, 6, 9, 12, 15]
+[3, 4, 5]
+[(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]
+[(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), (5, 'Banana'), (5, 'Coconut')]
+good: got a SyntaxError as expected
+[('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), ('Macdonalds', 'Cheeseburger')]
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index fa09e8c..8eb5522 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -542,3 +542,43 @@ class C:
def meth1(self): pass
def meth2(self, arg): pass
def meth3(self, a1, a2): pass
+
+# list comprehension tests
+nums = [1, 2, 3, 4, 5]
+strs = ["Apple", "Banana", "Coconut"]
+spcs = [" Apple", " Banana ", "Coco nut "]
+
+print [s.strip() for s in spcs]
+print [3 * x for x in nums]
+print [x for x in nums if x > 2]
+print [(i, s) for i in nums for s in strs]
+print [(i, s) for i in nums for s in [f for f in strs if "n" in f]]
+try:
+ eval("[i, s for i in nums for s in strs]")
+ print "FAIL: should have raised a SyntaxError!"
+except SyntaxError:
+ print "good: got a SyntaxError as expected"
+
+suppliers = [
+ (1, "Boeing"),
+ (2, "Ford"),
+ (3, "Macdonalds")
+]
+
+parts = [
+ (10, "Airliner"),
+ (20, "Engine"),
+ (30, "Cheeseburger")
+]
+
+suppart = [
+ (1, 10), (1, 20), (2, 20), (3, 30)
+]
+
+print [
+ (sname, pname)
+ for (sno, sname) in suppliers
+ for (pno, pname) in parts
+ for (sp_sno, sp_pno) in suppart
+ if sno == sp_sno and pno == sp_pno
+]