From 4afbc082ed3e880dce77a996b388bcaa796dc3d4 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Sat, 17 Oct 2020 21:54:47 -0500 Subject: [PATCH] Use disassembly for snapshot testing --- Cargo.toml | 2 +- src/compile.rs | 31 ++++- ...hon_compiler__compile__tests__if_ands.snap | 97 ++------------ ...on_compiler__compile__tests__if_mixed.snap | 118 ++---------------- ...thon_compiler__compile__tests__if_ors.snap | 98 ++------------- 5 files changed, 60 insertions(+), 286 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4e184f4..6ca8881 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,4 @@ log = "0.4" arrayvec = "0.5" [dev-dependencies] -insta = { version = "1.1", features = ["ron"] } +insta = "1.1" diff --git a/src/compile.rs b/src/compile.rs index bbc5123..4be7c30 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -2351,20 +2351,43 @@ mod tests { compiler.pop_code_object() } + macro_rules! assert_dis_snapshot { + ($value:expr) => { + insta::assert_snapshot!( + insta::internals::AutoName, + $value.display_expand_codeobjects().to_string(), + stringify!($value) + ) + }; + } + #[test] fn test_if_ors() { - insta::assert_ron_snapshot!(compile_exec("if True or False or False:\n pass\n")); + assert_dis_snapshot!(compile_exec( + "\ +if True or False or False: + pass +" + )); } #[test] fn test_if_ands() { - insta::assert_ron_snapshot!(compile_exec("if True and False and False:\n pass\n")); + assert_dis_snapshot!(compile_exec( + "\ +if True and False and False: + pass +" + )); } #[test] fn test_if_mixed() { - insta::assert_ron_snapshot!(compile_exec( - "if (True and False) or (False and True):\n pass\n" + assert_dis_snapshot!(compile_exec( + "\ +if (True and False) or (False and True): + pass +" )); } } diff --git a/src/snapshots/rustpython_compiler__compile__tests__if_ands.snap b/src/snapshots/rustpython_compiler__compile__tests__if_ands.snap index e89c82e..0d5953c 100644 --- a/src/snapshots/rustpython_compiler__compile__tests__if_ands.snap +++ b/src/snapshots/rustpython_compiler__compile__tests__if_ands.snap @@ -1,90 +1,13 @@ --- source: compiler/src/compile.rs -expression: "compile_exec(\"if True and False and False:\\n pass\\n\")" +expression: "compile_exec(\"\\\nif True and False and False:\n pass\n\")" --- -CodeObject( - instructions: [ - LoadConst( - idx: 0, - ), - JumpIfFalse( - target: Label(0), - ), - LoadConst( - idx: 1, - ), - JumpIfFalse( - target: Label(0), - ), - LoadConst( - idx: 2, - ), - JumpIfFalse( - target: Label(0), - ), - LoadConst( - idx: 3, - ), - ReturnValue, - ], - label_map: { - Label(0): 6, - }, - locations: [ - Location( - row: 1, - column: 4, - ), - Location( - row: 1, - column: 4, - ), - Location( - row: 1, - column: 13, - ), - Location( - row: 1, - column: 13, - ), - Location( - row: 1, - column: 23, - ), - Location( - row: 1, - column: 23, - ), - Location( - row: 2, - column: 2, - ), - Location( - row: 2, - column: 2, - ), - ], - flags: CodeFlags( - bits: 8, - ), - posonlyarg_count: 0, - arg_names: [], - varargs_name: None, - kwonlyarg_names: [], - varkeywords_name: None, - source_path: "source_path", - first_line_number: 0, - obj_name: "", - constants: [ - Boolean( - value: true, - ), - Boolean( - value: false, - ), - Boolean( - value: false, - ), - None, - ], -) + 0 LoadConst (true) + 1 JumpIfFalse (6) + 2 LoadConst (false) + 3 JumpIfFalse (6) + 4 LoadConst (false) + 5 JumpIfFalse (6) + >> 6 LoadConst (None) + 7 ReturnValue + diff --git a/src/snapshots/rustpython_compiler__compile__tests__if_mixed.snap b/src/snapshots/rustpython_compiler__compile__tests__if_mixed.snap index 302667f..5476053 100644 --- a/src/snapshots/rustpython_compiler__compile__tests__if_mixed.snap +++ b/src/snapshots/rustpython_compiler__compile__tests__if_mixed.snap @@ -1,109 +1,15 @@ --- source: compiler/src/compile.rs -expression: "compile_exec(\"if (True and False) or (False and True):\\n pass\\n\")" +expression: "compile_exec(\"\\\nif (True and False) or (False and True):\n pass\n\")" --- -CodeObject( - instructions: [ - LoadConst( - idx: 0, - ), - JumpIfFalse( - target: Label(2), - ), - LoadConst( - idx: 1, - ), - JumpIfTrue( - target: Label(1), - ), - LoadConst( - idx: 2, - ), - JumpIfFalse( - target: Label(0), - ), - LoadConst( - idx: 3, - ), - JumpIfFalse( - target: Label(0), - ), - LoadConst( - idx: 4, - ), - ReturnValue, - ], - label_map: { - Label(0): 8, - Label(1): 8, - Label(2): 4, - }, - locations: [ - Location( - row: 1, - column: 5, - ), - Location( - row: 1, - column: 5, - ), - Location( - row: 1, - column: 14, - ), - Location( - row: 1, - column: 14, - ), - Location( - row: 1, - column: 25, - ), - Location( - row: 1, - column: 25, - ), - Location( - row: 1, - column: 35, - ), - Location( - row: 1, - column: 35, - ), - Location( - row: 2, - column: 2, - ), - Location( - row: 2, - column: 2, - ), - ], - flags: CodeFlags( - bits: 8, - ), - posonlyarg_count: 0, - arg_names: [], - varargs_name: None, - kwonlyarg_names: [], - varkeywords_name: None, - source_path: "source_path", - first_line_number: 0, - obj_name: "", - constants: [ - Boolean( - value: true, - ), - Boolean( - value: false, - ), - Boolean( - value: false, - ), - Boolean( - value: true, - ), - None, - ], -) + 0 LoadConst (true) + 1 JumpIfFalse (4) + 2 LoadConst (false) + 3 JumpIfTrue (8) + >> 4 LoadConst (false) + 5 JumpIfFalse (8) + 6 LoadConst (true) + 7 JumpIfFalse (8) + >> 8 LoadConst (None) + 9 ReturnValue + diff --git a/src/snapshots/rustpython_compiler__compile__tests__if_ors.snap b/src/snapshots/rustpython_compiler__compile__tests__if_ors.snap index 21a5e47..d2bc385 100644 --- a/src/snapshots/rustpython_compiler__compile__tests__if_ors.snap +++ b/src/snapshots/rustpython_compiler__compile__tests__if_ors.snap @@ -1,91 +1,13 @@ --- source: compiler/src/compile.rs -expression: "compile_exec(\"if True or False or False:\\n pass\\n\")" +expression: "compile_exec(\"\\\nif True or False or False:\n pass\n\")" --- -CodeObject( - instructions: [ - LoadConst( - idx: 0, - ), - JumpIfTrue( - target: Label(1), - ), - LoadConst( - idx: 1, - ), - JumpIfTrue( - target: Label(1), - ), - LoadConst( - idx: 2, - ), - JumpIfFalse( - target: Label(0), - ), - LoadConst( - idx: 3, - ), - ReturnValue, - ], - label_map: { - Label(0): 6, - Label(1): 6, - }, - locations: [ - Location( - row: 1, - column: 4, - ), - Location( - row: 1, - column: 4, - ), - Location( - row: 1, - column: 12, - ), - Location( - row: 1, - column: 12, - ), - Location( - row: 1, - column: 21, - ), - Location( - row: 1, - column: 21, - ), - Location( - row: 2, - column: 2, - ), - Location( - row: 2, - column: 2, - ), - ], - flags: CodeFlags( - bits: 8, - ), - posonlyarg_count: 0, - arg_names: [], - varargs_name: None, - kwonlyarg_names: [], - varkeywords_name: None, - source_path: "source_path", - first_line_number: 0, - obj_name: "", - constants: [ - Boolean( - value: true, - ), - Boolean( - value: false, - ), - Boolean( - value: false, - ), - None, - ], -) + 0 LoadConst (true) + 1 JumpIfTrue (6) + 2 LoadConst (false) + 3 JumpIfTrue (6) + 4 LoadConst (false) + 5 JumpIfFalse (6) + >> 6 LoadConst (None) + 7 ReturnValue +