From 77da54b23548c41be5ba50980206667c1cae3c65 Mon Sep 17 00:00:00 2001 From: juliawgraham Date: Sun, 4 Dec 2022 15:11:12 -0500 Subject: [PATCH 01/11] wrote a basic test that calls visit_Match --- src/latexify/codegen/function_codegen.py | 8 ++++++++ src/latexify/codegen/function_codegen_test.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/latexify/codegen/function_codegen.py b/src/latexify/codegen/function_codegen.py index 33f8df0..0e68c75 100644 --- a/src/latexify/codegen/function_codegen.py +++ b/src/latexify/codegen/function_codegen.py @@ -536,6 +536,14 @@ def visit_IfExp(self, node: ast.IfExp) -> str: latex += self.visit(node) return latex + r", & \mathrm{otherwise} \end{array} \right." + def visit_Match(self, node: ast.Match) -> str: + print(ast.dump(node)) + return "Match" + + def visit_MatchValue(self, node: ast.MatchValue) -> str: + print(ast.dump(node)) + return "MatchValue" + def _reduce_stop_parameter(self, node: ast.BinOp) -> str: # ast.Constant class is added in Python 3.8 # ast.Num is the relevant node type in previous versions diff --git a/src/latexify/codegen/function_codegen_test.py b/src/latexify/codegen/function_codegen_test.py index 793ab62..51a1b86 100644 --- a/src/latexify/codegen/function_codegen_test.py +++ b/src/latexify/codegen/function_codegen_test.py @@ -10,6 +10,25 @@ from latexify import exceptions, test_utils from latexify.codegen import FunctionCodegen, function_codegen +def test_matchvalue() -> None: + tree = ast.parse( + textwrap.dedent( + """ + match x: + case 0: + return 1 + """ + ) + ).body[0] + + assert FunctionCodegen().visit(tree) == r"\mathrm{f}(x) = x" + + + + + + + def test_generic_visit() -> None: class UnknownNode(ast.AST): From c213d0f30e8913176d92afa97616f9510c18e789 Mon Sep 17 00:00:00 2001 From: juliawgraham Date: Sun, 4 Dec 2022 15:22:58 -0500 Subject: [PATCH 02/11] added extra space to publish branch --- src/latexify/codegen/function_codegen_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/latexify/codegen/function_codegen_test.py b/src/latexify/codegen/function_codegen_test.py index 51a1b86..4d2c620 100644 --- a/src/latexify/codegen/function_codegen_test.py +++ b/src/latexify/codegen/function_codegen_test.py @@ -30,6 +30,7 @@ def test_matchvalue() -> None: + def test_generic_visit() -> None: class UnknownNode(ast.AST): pass From 73425a6e4c3db4e0920514097bd601ccef203032 Mon Sep 17 00:00:00 2001 From: Yuqi Gong Date: Sun, 4 Dec 2022 16:46:37 -0500 Subject: [PATCH 03/11] implementation for MatchValue --- src/latexify/codegen/function_codegen.py | 26 +++++++-- src/latexify/codegen/function_codegen_test.py | 53 +++++++++++-------- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/latexify/codegen/function_codegen.py b/src/latexify/codegen/function_codegen.py index 0e68c75..8e69122 100644 --- a/src/latexify/codegen/function_codegen.py +++ b/src/latexify/codegen/function_codegen.py @@ -537,13 +537,29 @@ def visit_IfExp(self, node: ast.IfExp) -> str: return latex + r", & \mathrm{otherwise} \end{array} \right." def visit_Match(self, node: ast.Match) -> str: - print(ast.dump(node)) - return "Match" + """Visit a match node""" + latex = r"\left\{ \begin{array}{ll} " + subject_latex = self.visit(node.subject) + for match_case in node.cases: + true_latex, cond_latex = self.visit(match_case) + if cond_latex: + latex += true_latex + r", & \mathrm{if} \ " + subject_latex + cond_latex + r" \\ " + else: + latex += true_latex + r", & \mathrm{otherwise}" + latex += r"\end{array} \right." + return latex + def visit_match_case(self, node: ast.match_case) -> str: + """Visit a match_case node""" + cond_latex = self.visit(node.pattern) + true_latex = self.visit(node.body[0]) + return true_latex, cond_latex + def visit_MatchValue(self, node: ast.MatchValue) -> str: - print(ast.dump(node)) - return "MatchValue" - + """Visit a MatchValue node""" + latex = self.visit(node.value) + return r" = " + latex + def _reduce_stop_parameter(self, node: ast.BinOp) -> str: # ast.Constant class is added in Python 3.8 # ast.Num is the relevant node type in previous versions diff --git a/src/latexify/codegen/function_codegen_test.py b/src/latexify/codegen/function_codegen_test.py index 4d2c620..4fbc570 100644 --- a/src/latexify/codegen/function_codegen_test.py +++ b/src/latexify/codegen/function_codegen_test.py @@ -10,27 +10,6 @@ from latexify import exceptions, test_utils from latexify.codegen import FunctionCodegen, function_codegen -def test_matchvalue() -> None: - tree = ast.parse( - textwrap.dedent( - """ - match x: - case 0: - return 1 - """ - ) - ).body[0] - - assert FunctionCodegen().visit(tree) == r"\mathrm{f}(x) = x" - - - - - - - - - def test_generic_visit() -> None: class UnknownNode(ast.AST): pass @@ -764,3 +743,35 @@ def test_use_set_symbols_compare(code: str, latex: str) -> None: tree = ast.parse(code).body[0].value assert isinstance(tree, ast.Compare) assert function_codegen.FunctionCodegen(use_set_symbols=True).visit(tree) == latex + + +def test_matchvalue() -> None: + tree = ast.parse( + textwrap.dedent( + """ + match x: + case 0: + return 1 + """ + ) + ).body[0] + + assert FunctionCodegen().visit(tree) == r"\left\{ \begin{array}{ll} {1}, & \mathrm{if} \ x = {0} \\ \end{array} \right." + + +def test_multiple_matchvalue() -> None: + tree = ast.parse( + textwrap.dedent( + """ + match x: + case 0: + return 1 + case 1: + return 2 + """ + ) + ).body[0] + + assert FunctionCodegen().visit(tree) == r"\left\{ \begin{array}{ll} {1}, & \mathrm{if} \ x = {0} \\ {2}, & \mathrm{if} \ x = {1} \\ \end{array} \right." + + From 80f948666c4cf944ca151411ab0e87c1b100a699 Mon Sep 17 00:00:00 2001 From: Yuqi Gong Date: Tue, 6 Dec 2022 23:19:16 -0500 Subject: [PATCH 04/11] merged match_case into Match (with juliawgraham) --- src/latexify/codegen/function_codegen.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/latexify/codegen/function_codegen.py b/src/latexify/codegen/function_codegen.py index 8e69122..1b722ab 100644 --- a/src/latexify/codegen/function_codegen.py +++ b/src/latexify/codegen/function_codegen.py @@ -540,20 +540,13 @@ def visit_Match(self, node: ast.Match) -> str: """Visit a match node""" latex = r"\left\{ \begin{array}{ll} " subject_latex = self.visit(node.subject) - for match_case in node.cases: - true_latex, cond_latex = self.visit(match_case) - if cond_latex: - latex += true_latex + r", & \mathrm{if} \ " + subject_latex + cond_latex + r" \\ " - else: - latex += true_latex + r", & \mathrm{otherwise}" + for i, match_case in enumerate(node.cases): + true_latex = self.visit(match_case.body[0]) + cond_latex = self.visit(match_case.pattern) + latex += true_latex + r", & \mathrm{if} \ " + subject_latex + cond_latex + r" \\ " + latex += r"\end{array} \right." return latex - - def visit_match_case(self, node: ast.match_case) -> str: - """Visit a match_case node""" - cond_latex = self.visit(node.pattern) - true_latex = self.visit(node.body[0]) - return true_latex, cond_latex def visit_MatchValue(self, node: ast.MatchValue) -> str: """Visit a MatchValue node""" From 590ee6c715f3a4736d1e47d05bdb14f3155710cd Mon Sep 17 00:00:00 2001 From: Yuqi Gong Date: Wed, 7 Dec 2022 14:09:08 -0500 Subject: [PATCH 05/11] 1. removed unused i; 2. added error handling for multiple statements in Match node; 3. added corresponding unit tests Co-authored-by: Lucybean-hi Co-authored-by: juliawgraham Co-authored-by: Erica Fu --- src/latexify/codegen/function_codegen.py | 8 ++++++-- src/latexify/codegen/function_codegen_test.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/latexify/codegen/function_codegen.py b/src/latexify/codegen/function_codegen.py index 1b722ab..b3e2825 100644 --- a/src/latexify/codegen/function_codegen.py +++ b/src/latexify/codegen/function_codegen.py @@ -540,7 +540,11 @@ def visit_Match(self, node: ast.Match) -> str: """Visit a match node""" latex = r"\left\{ \begin{array}{ll} " subject_latex = self.visit(node.subject) - for i, match_case in enumerate(node.cases): + for match_case in node.cases: + if len(match_case.body) != 1: + raise exceptions.LatexifySyntaxError( + "Multiple statements are not supported in Match nodes." + ) true_latex = self.visit(match_case.body[0]) cond_latex = self.visit(match_case.pattern) latex += true_latex + r", & \mathrm{if} \ " + subject_latex + cond_latex + r" \\ " @@ -551,7 +555,7 @@ def visit_Match(self, node: ast.Match) -> str: def visit_MatchValue(self, node: ast.MatchValue) -> str: """Visit a MatchValue node""" latex = self.visit(node.value) - return r" = " + latex + return " = " + latex def _reduce_stop_parameter(self, node: ast.BinOp) -> str: # ast.Constant class is added in Python 3.8 diff --git a/src/latexify/codegen/function_codegen_test.py b/src/latexify/codegen/function_codegen_test.py index 4fbc570..aca8a23 100644 --- a/src/latexify/codegen/function_codegen_test.py +++ b/src/latexify/codegen/function_codegen_test.py @@ -774,4 +774,22 @@ def test_multiple_matchvalue() -> None: assert FunctionCodegen().visit(tree) == r"\left\{ \begin{array}{ll} {1}, & \mathrm{if} \ x = {0} \\ {2}, & \mathrm{if} \ x = {1} \\ \end{array} \right." +def test_matchvalue_mutliple_statements() -> None: + tree = ast.parse( + textwrap.dedent( + """ + match x: + case 0: + x = 5 + return 1 + case 1: + return 2 + """ + ) + ).body[0] + with pytest.raises( + exceptions.LatexifySyntaxError, + match=r"Multiple statements are not supported in Match nodes.", + ): + FunctionCodegen().visit(tree) \ No newline at end of file From 98db826df417b545313d0a8612dadafbffeaddda Mon Sep 17 00:00:00 2001 From: juliawgraham Date: Thu, 8 Dec 2022 20:17:16 -0500 Subject: [PATCH 06/11] Removed unused _reduce_stop_parameter function Co-authored-by: Yuqi Co-authored-by: Erica Fu --- src/latexify/codegen/function_codegen.py | 40 ------------------------ 1 file changed, 40 deletions(-) diff --git a/src/latexify/codegen/function_codegen.py b/src/latexify/codegen/function_codegen.py index 84b827b..55e80b1 100644 --- a/src/latexify/codegen/function_codegen.py +++ b/src/latexify/codegen/function_codegen.py @@ -673,46 +673,6 @@ def visit_MatchValue(self, node: ast.MatchValue) -> str: latex = self.visit(node.value) return " = " + latex - def _reduce_stop_parameter(self, node: ast.BinOp) -> str: - # ast.Constant class is added in Python 3.8 - # ast.Num is the relevant node type in previous versions - if sys.version_info.minor < 8: - if isinstance(node.right, ast.Num): - if isinstance(node.op, ast.Add): - if node.right.n == 1: - upper = "{" + self.visit(node.left) + "}" - else: - reduced_constant = ast.Num(node.right.n - 1) - new_node = ast.BinOp(node.left, node.op, reduced_constant) - upper = "{" + self.visit(new_node) + "}" - else: - if node.right.n == -1: - upper = "{" + self.visit(node.left) + "}" - else: - reduced_constant = ast.Num(node.right.n + 1) - new_node = ast.BinOp(node.left, node.op, reduced_constant) - upper = "{" + self.visit(new_node) + "}" - else: - upper = "{" + self.visit(node) + "}" - else: - if isinstance(node.right, ast.Constant): - if isinstance(node.op, ast.Add): - if node.right.value == 1: - upper = "{" + self.visit(node.left) + "}" - else: - reduced_constant = ast.Constant(node.right.value - 1) - new_node = ast.BinOp(node.left, node.op, reduced_constant) - upper = "{" + self.visit(new_node) + "}" - else: - if node.right.value == -1: - upper = "{" + self.visit(node.left) + "}" - else: - reduced_constant = ast.Constant(node.right.value + 1) - new_node = ast.BinOp(node.left, node.op, reduced_constant) - upper = "{" + self.visit(new_node) + "}" - else: - upper = "{" + self.visit(node) + "}" - def _reduce_stop_parameter(self, node: ast.expr) -> ast.expr: """Adjusts the stop expression of the range. From fab995cf912dfdfe2e57ec3c8913f0391f9bed00 Mon Sep 17 00:00:00 2001 From: juliawgraham Date: Thu, 8 Dec 2022 20:19:25 -0500 Subject: [PATCH 07/11] updated unit test syntax --- src/latexify/codegen/function_codegen_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/latexify/codegen/function_codegen_test.py b/src/latexify/codegen/function_codegen_test.py index f0473d1..572e65b 100644 --- a/src/latexify/codegen/function_codegen_test.py +++ b/src/latexify/codegen/function_codegen_test.py @@ -925,7 +925,7 @@ def test_matchvalue() -> None: ) ).body[0] - assert FunctionCodegen().visit(tree) == r"\left\{ \begin{array}{ll} {1}, & \mathrm{if} \ x = {0} \\ \end{array} \right." + assert FunctionCodegen().visit(tree) == r"\left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ \end{array} \right." def test_multiple_matchvalue() -> None: @@ -941,7 +941,7 @@ def test_multiple_matchvalue() -> None: ) ).body[0] - assert FunctionCodegen().visit(tree) == r"\left\{ \begin{array}{ll} {1}, & \mathrm{if} \ x = {0} \\ {2}, & \mathrm{if} \ x = {1} \\ \end{array} \right." + assert FunctionCodegen().visit(tree) == r"\left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ 2, & \mathrm{if} \ x = 1 \\ \end{array} \right." def test_matchvalue_mutliple_statements() -> None: tree = ast.parse( From 2b5ad40b43c7ed5d6c80422a940284b4471960d1 Mon Sep 17 00:00:00 2001 From: juliawgraham Date: Thu, 8 Dec 2022 20:35:38 -0500 Subject: [PATCH 08/11] updated formatting using flake --- src/latexify/codegen/function_codegen.py | 9 ++-- src/latexify/codegen/function_codegen_test.py | 52 +++++++++++-------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/latexify/codegen/function_codegen.py b/src/latexify/codegen/function_codegen.py index 55e80b1..de808cd 100644 --- a/src/latexify/codegen/function_codegen.py +++ b/src/latexify/codegen/function_codegen.py @@ -654,8 +654,8 @@ def visit_IfExp(self, node: ast.IfExp) -> str: def visit_Match(self, node: ast.Match) -> str: """Visit a match node""" - latex = r"\left\{ \begin{array}{ll} " - subject_latex = self.visit(node.subject) + latex = r"\left\{ \begin{array}{ll}" + subject_latex = self.visit(node.subject) for match_case in node.cases: if len(match_case.body) != 1: raise exceptions.LatexifySyntaxError( @@ -663,8 +663,9 @@ def visit_Match(self, node: ast.Match) -> str: ) true_latex = self.visit(match_case.body[0]) cond_latex = self.visit(match_case.pattern) - latex += true_latex + r", & \mathrm{if} \ " + subject_latex + cond_latex + r" \\ " - + latex += true_latex + r", & \mathrm{if} \ " + \ + subject_latex + cond_latex + r" \\ " + latex += r"\end{array} \right." return latex diff --git a/src/latexify/codegen/function_codegen_test.py b/src/latexify/codegen/function_codegen_test.py index 572e65b..9593f12 100644 --- a/src/latexify/codegen/function_codegen_test.py +++ b/src/latexify/codegen/function_codegen_test.py @@ -10,6 +10,7 @@ from latexify import ast_utils, exceptions, test_utils from latexify.codegen import FunctionCodegen, function_codegen + def test_generic_visit() -> None: class UnknownNode(ast.AST): pass @@ -917,43 +918,49 @@ def test_use_set_symbols_compare(code: str, latex: str) -> None: def test_matchvalue() -> None: tree = ast.parse( textwrap.dedent( - """ - match x: - case 0: - return 1 - """ + """ + match x: + case 0: + return 1 + """ ) ).body[0] - assert FunctionCodegen().visit(tree) == r"\left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ \end{array} \right." + assert FunctionCodegen().visit(tree) == \ + r"\left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ \end{array} \right." def test_multiple_matchvalue() -> None: tree = ast.parse( textwrap.dedent( - """ - match x: - case 0: - return 1 - case 1: - return 2 - """ + """ + match x: + case 0: + return 1 + case 1: + return 2 + """ ) ).body[0] - assert FunctionCodegen().visit(tree) == r"\left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ 2, & \mathrm{if} \ x = 1 \\ \end{array} \right." + assert FunctionCodegen().visit(tree) == \ + r""" + \left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ + 2, & \mathrm{if} \ x = 1 \\ \end{array} \right. + """ + def test_matchvalue_mutliple_statements() -> None: tree = ast.parse( textwrap.dedent( - """ - match x: - case 0: - x = 5 - return 1 - case 1: - return 2 - """ + """ + match x: + case 0: + x = 5 + return 1 + case 1: + return 2 + """ ) ).body[0] @@ -1008,4 +1015,3 @@ def test_numpy_array(code: str, latex: str) -> None: tree = ast_utils.parse_expr(code) assert isinstance(tree, ast.Call) assert function_codegen.FunctionCodegen().visit(tree) == latex - From c4a841022cc0e3ee928eabc37b344e89b8da6604 Mon Sep 17 00:00:00 2001 From: juliawgraham Date: Thu, 8 Dec 2022 20:44:44 -0500 Subject: [PATCH 09/11] updated unit test formatting after flake changes --- src/latexify/codegen/function_codegen_test.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/latexify/codegen/function_codegen_test.py b/src/latexify/codegen/function_codegen_test.py index 9593f12..35ac184 100644 --- a/src/latexify/codegen/function_codegen_test.py +++ b/src/latexify/codegen/function_codegen_test.py @@ -927,7 +927,7 @@ def test_matchvalue() -> None: ).body[0] assert FunctionCodegen().visit(tree) == \ - r"\left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ \end{array} \right." + r"\left\{ \begin{array}{ll}1, & \mathrm{if} \ x = 0 \\ \end{array} \right." def test_multiple_matchvalue() -> None: @@ -944,10 +944,8 @@ def test_multiple_matchvalue() -> None: ).body[0] assert FunctionCodegen().visit(tree) == \ - r""" - \left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ - 2, & \mathrm{if} \ x = 1 \\ \end{array} \right. - """ + r"\left\{ \begin{array}{ll}1, & \mathrm{if} \ x = 0 \\" + \ + r" 2, & \mathrm{if} \ x = 1 \\ \end{array} \right." def test_matchvalue_mutliple_statements() -> None: From 4ea4c3c0556fcbf201edf80f1c3cac2564799f24 Mon Sep 17 00:00:00 2001 From: juliawgraham Date: Thu, 8 Dec 2022 20:47:53 -0500 Subject: [PATCH 10/11] reformatted files using black --- src/latexify/codegen/function_codegen.py | 9 +++++++-- src/latexify/codegen/function_codegen_test.py | 14 +++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/latexify/codegen/function_codegen.py b/src/latexify/codegen/function_codegen.py index de808cd..7da606e 100644 --- a/src/latexify/codegen/function_codegen.py +++ b/src/latexify/codegen/function_codegen.py @@ -663,8 +663,13 @@ def visit_Match(self, node: ast.Match) -> str: ) true_latex = self.visit(match_case.body[0]) cond_latex = self.visit(match_case.pattern) - latex += true_latex + r", & \mathrm{if} \ " + \ - subject_latex + cond_latex + r" \\ " + latex += ( + true_latex + + r", & \mathrm{if} \ " + + subject_latex + + cond_latex + + r" \\ " + ) latex += r"\end{array} \right." return latex diff --git a/src/latexify/codegen/function_codegen_test.py b/src/latexify/codegen/function_codegen_test.py index 35ac184..9bc4b75 100644 --- a/src/latexify/codegen/function_codegen_test.py +++ b/src/latexify/codegen/function_codegen_test.py @@ -926,8 +926,10 @@ def test_matchvalue() -> None: ) ).body[0] - assert FunctionCodegen().visit(tree) == \ - r"\left\{ \begin{array}{ll}1, & \mathrm{if} \ x = 0 \\ \end{array} \right." + assert ( + FunctionCodegen().visit(tree) + == r"\left\{ \begin{array}{ll}1, & \mathrm{if} \ x = 0 \\ \end{array} \right." + ) def test_multiple_matchvalue() -> None: @@ -943,9 +945,11 @@ def test_multiple_matchvalue() -> None: ) ).body[0] - assert FunctionCodegen().visit(tree) == \ - r"\left\{ \begin{array}{ll}1, & \mathrm{if} \ x = 0 \\" + \ - r" 2, & \mathrm{if} \ x = 1 \\ \end{array} \right." + assert ( + FunctionCodegen().visit(tree) + == r"\left\{ \begin{array}{ll}1, & \mathrm{if} \ x = 0 \\" + + r" 2, & \mathrm{if} \ x = 1 \\ \end{array} \right." + ) def test_matchvalue_mutliple_statements() -> None: From 2b2ae26aff3a76f63a575964c63b49a1711fb4fb Mon Sep 17 00:00:00 2001 From: odashi Date: Fri, 9 Dec 2022 02:53:37 +0000 Subject: [PATCH 11/11] refactoring --- src/latexify/codegen/function_codegen.py | 8 ++++-- src/latexify/codegen/function_codegen_test.py | 28 ++++++++++++++++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/latexify/codegen/function_codegen.py b/src/latexify/codegen/function_codegen.py index 8dc8e23..266cd5d 100644 --- a/src/latexify/codegen/function_codegen.py +++ b/src/latexify/codegen/function_codegen.py @@ -718,9 +718,11 @@ def visit_Match(self, node: ast.Match) -> str: latex = r"\left\{ \begin{array}{ll}" subject_latex = self.visit(node.subject) for match_case in node.cases: - if len(match_case.body) != 1: - raise exceptions.LatexifySyntaxError( - "Multiple statements are not supported in Match nodes." + if not ( + len(match_case.body) == 1 and isinstance(match_case.body[0], ast.Return) + ): + raise exceptions.LatexifyNotSupportedError( + "Match cases must have exactly 1 return statement." ) true_latex = self.visit(match_case.body[0]) cond_latex = self.visit(match_case.pattern) diff --git a/src/latexify/codegen/function_codegen_test.py b/src/latexify/codegen/function_codegen_test.py index 5f7a18f..0f754b7 100644 --- a/src/latexify/codegen/function_codegen_test.py +++ b/src/latexify/codegen/function_codegen_test.py @@ -915,6 +915,7 @@ def test_use_set_symbols_compare(code: str, latex: str) -> None: assert function_codegen.FunctionCodegen(use_set_symbols=True).visit(tree) == latex +@test_utils.require_at_least(10) def test_matchvalue() -> None: tree = ast.parse( textwrap.dedent( @@ -932,6 +933,7 @@ def test_matchvalue() -> None: ) +@test_utils.require_at_least(10) def test_multiple_matchvalue() -> None: tree = ast.parse( textwrap.dedent( @@ -952,6 +954,26 @@ def test_multiple_matchvalue() -> None: ) +@test_utils.require_at_least(10) +def test_matchvalue_no_return() -> None: + tree = ast.parse( + textwrap.dedent( + """ + match x: + case 0: + x = 5 + """ + ) + ).body[0] + + with pytest.raises( + exceptions.LatexifyNotSupportedError, + match=r"^Match cases must have exactly 1 return statement\.$", + ): + FunctionCodegen().visit(tree) + + +@test_utils.require_at_least(10) def test_matchvalue_mutliple_statements() -> None: tree = ast.parse( textwrap.dedent( @@ -960,15 +982,13 @@ def test_matchvalue_mutliple_statements() -> None: case 0: x = 5 return 1 - case 1: - return 2 """ ) ).body[0] with pytest.raises( - exceptions.LatexifySyntaxError, - match=r"Multiple statements are not supported in Match nodes.", + exceptions.LatexifyNotSupportedError, + match=r"^Match cases must have exactly 1 return statement\.$", ): FunctionCodegen().visit(tree)