changeset 57:e5a808ec31c0

Add ast.Assert.
author Bastien Orivel <eijebong@bananium.fr>
date Sun, 12 Jun 2016 18:21:15 +0200
parents c3cc16b933d2
children a0b23123901b
files src/ast_convert.rs src/ast_dump.rs src/python_ast.rs tests/test_parse_files/test_assert.py
diffstat 4 files changed, 29 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/ast_convert.rs
+++ b/src/ast_convert.rs
@@ -512,6 +512,7 @@ fn parse_statement(py: Python, ast: PyOb
     let delete_type = ast_module.get(py, "Delete").unwrap();
     let pass_type = ast_module.get(py, "Pass").unwrap();
     let continue_type = ast_module.get(py, "Continue").unwrap();
+    let assert_type = ast_module.get(py, "Assert").unwrap();
 
     assert!(is_instance(&ast, &ast_type));
 
@@ -653,6 +654,17 @@ fn parse_statement(py: Python, ast: PyOb
         stmt::Pass
     } else if is_instance(&ast, &continue_type) {
         stmt::Continue
+    } else if is_instance(&ast, &assert_type) {
+        let test = ast.getattr(py, "test").unwrap();
+        let test = parse_expr(py, test);
+        let msg = ast.getattr(py, "msg").unwrap();
+
+        if msg == py.None() {
+            stmt::Assert(test, None)
+        } else {
+            let msg = parse_expr(py, msg);
+            stmt::Assert(test, Some(msg))
+        }
     } else {
         println!("stmt {}", ast);
         panic!()
--- a/src/ast_dump.rs
+++ b/src/ast_dump.rs
@@ -265,7 +265,19 @@ impl stmt {
             stmt::Break => format!("{}break", current_indent),
             stmt::Delete(targets) => format!("{}del {}", current_indent, vec_to_strings_vec(targets).join(", ")),
             stmt::Pass => format!("{}pass", current_indent),
-            stmt::Continue => format!("{}continue", current_indent)
+            stmt::Continue => format!("{}continue", current_indent),
+            stmt::Assert(test, msg) => {
+                format!("{}assert {}{}",
+                    current_indent,
+                    test.to_string(),
+                    {
+                        match msg {
+                            Some(msg) => format!(", {}", msg.to_string()),
+                            None => format!("")
+                        }
+                    }
+                )
+            }
         }
     }
 }
--- a/src/python_ast.rs
+++ b/src/python_ast.rs
@@ -52,7 +52,7 @@ pub enum stmt {
     //Try(Vec<stmt>, Vec<excepthandler>, Vec<stmt>, Vec<stmt>)
 
     // Assert(expr test, expr? msg)
-    //Assert(expr, Option<expr>)
+    Assert(expr, Option<expr>),
 
     // Import(alias* names)
     Import(Vec<alias>),
new file mode 100644
--- /dev/null
+++ b/tests/test_parse_files/test_assert.py
@@ -0,0 +1,3 @@
+assert True
+assert a != b, "error"
+assert a != b, (a, b)