Skip to content

Commit 60c5066

Browse files
Simplify test_ifexp_* tests
1 parent 48e4796 commit 60c5066

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,38 +1967,39 @@ def test_ifexp(self):
19671967
self.expr(ast.IfExp(*args), "must have Load context")
19681968

19691969
def test_ifexp_orelse_stmt(self):
1970-
with self.assertRaisesRegex(SyntaxError, "statement given where 'orelse' expression required"):
1971-
ast.parse("x = 1 if 1 else pass")
1972-
with self.assertRaisesRegex(SyntaxError, "statement given where 'orelse' expression required"):
1973-
ast.parse("x = 1 if 1 else return")
1974-
with self.assertRaisesRegex(SyntaxError, "statement given where 'orelse' expression required"):
1975-
ast.parse("x = 1 if 1 else raise Exception('a')")
1976-
with self.assertRaisesRegex(SyntaxError, "statement given where 'orelse' expression required"):
1977-
ast.parse("a = 1; x = 1 if 1 else del a")
1978-
with self.assertRaisesRegex(SyntaxError, "statement given where 'orelse' expression required"):
1979-
ast.parse("x = 1 if 1 else yield 2")
1980-
with self.assertRaisesRegex(SyntaxError, "statement given where 'orelse' expression required"):
1981-
ast.parse("x = 1 if 1 else assert False")
1982-
with self.assertRaisesRegex(SyntaxError, "statement given where 'orelse' expression required"):
1983-
ast.parse("x = 1 if 1 else break")
1984-
with self.assertRaisesRegex(SyntaxError, "statement given where 'orelse' expression required"):
1985-
ast.parse("x = 1 if 1 else continue")
1970+
msg = "statement given where 'orelse' expression required"
1971+
for stmt in [
1972+
"x = 1 if 1 else pass",
1973+
"x = 1 if 1 else return",
1974+
"x = 1 if 1 else raise Exception('a')",
1975+
"a = 1; x = 1 if 1 else del a",
1976+
"x = 1 if 1 else yield 2",
1977+
"x = 1 if 1 else assert False",
1978+
"x = 1 if 1 else break",
1979+
"x = 1 if 1 else continue"
1980+
]:
1981+
with self.subTest(stmt), self.assertRaisesRegex(SyntaxError, msg):
1982+
ast.parse(stmt)
19861983

19871984
def test_ifexp_body_stmt_orelse_expression(self):
1988-
with self.assertRaisesRegex(SyntaxError, "statement given where 'body' expression required"):
1989-
ast.parse("x = pass if 1 else 1")
1990-
with self.assertRaisesRegex(SyntaxError, "statement given where 'body' expression required"):
1991-
ast.parse("x = break if 1 else 1")
1992-
with self.assertRaisesRegex(SyntaxError, "statement given where 'body' expression required"):
1993-
ast.parse("x = continue if 1 else 1")
1985+
msg = "statement given where 'body' expression required"
1986+
for stmt in [
1987+
"x = pass if 1 else 1",
1988+
"x = break if 1 else 1",
1989+
"x = continue if 1 else 1"
1990+
]:
1991+
with self.subTest(stmt), self.assertRaisesRegex(SyntaxError, msg):
1992+
ast.parse(stmt)
19941993

19951994
def test_ifexp_body_stmt_orelse_stmt(self):
1996-
with self.assertRaisesRegex(SyntaxError, "statement given where 'body' expression required"):
1997-
ast.parse("x = pass if 1 else pass")
1998-
with self.assertRaisesRegex(SyntaxError, "statement given where 'body' expression required"):
1999-
ast.parse("x = break if 1 else pass")
2000-
with self.assertRaisesRegex(SyntaxError, "statement given where 'body' expression required"):
2001-
ast.parse("x = continue if 1 else pass")
1995+
msg = "statement given where 'body' expression required"
1996+
for stmt in [
1997+
"x = pass if 1 else pass",
1998+
"x = break if 1 else pass",
1999+
"x = continue if 1 else pass"
2000+
]:
2001+
with self.subTest(stmt), self.assertRaisesRegex(SyntaxError, msg):
2002+
ast.parse(stmt)
20022003

20032004
def test_dict(self):
20042005
d = ast.Dict([], [ast.Name("x", ast.Load())])

0 commit comments

Comments
 (0)