From 0b3d3a899a14fd009fe23ac1590c86ff4c097dd3 Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Tue, 15 Aug 2023 15:51:19 +0300 Subject: [PATCH] add span check in pretty error tests; --- ergotree-interpreter/src/eval/error.rs | 153 ++++++++----------------- ergotree-ir/src/pretty_printer.rs | 1 - ergotree-ir/src/source_span.rs | 9 ++ 3 files changed, 54 insertions(+), 109 deletions(-) diff --git a/ergotree-interpreter/src/eval/error.rs b/ergotree-interpreter/src/eval/error.rs index d878bc8c6..ca410e829 100644 --- a/ergotree-interpreter/src/eval/error.rs +++ b/ergotree-interpreter/src/eval/error.rs @@ -167,7 +167,7 @@ impl ExtResultEvalError for Result { } } -#[allow(clippy::unwrap_used)] +#[allow(clippy::unwrap_used, unused_imports, dead_code)] #[cfg(test)] mod tests { use std::rc::Rc; @@ -212,80 +212,21 @@ mod tests { expected_tree.assert_eq(&err.to_string()); } - #[ignore = "expect test fails on self-generated string"] - #[test] - fn pretty_binop_div_zero() { - let lhs_val_id = 1.into(); - let rhs_val_id = 2.into(); - let res_val_id = 3.into(); - let expr = Expr::BlockValue( - BlockValue { - items: vec![ - ValDef { - id: lhs_val_id, - rhs: Box::new(Expr::Const(42i32.into())), - } - .into(), - ValDef { - id: rhs_val_id, - rhs: Box::new(Expr::Const(0i32.into())), - } - .into(), - ValDef { - id: res_val_id, - rhs: Box::new( - BinOp { - kind: ArithOp::Divide.into(), - left: Box::new( - ValUse { - val_id: lhs_val_id, - tpe: SType::SInt, - } - .into(), - ), - right: Box::new( - ValUse { - val_id: rhs_val_id, - tpe: SType::SInt, - } - .into(), - ), - } - .into(), - ), - } - .into(), - ], - result: Box::new( - ValUse { - val_id: res_val_id, - tpe: SType::SInt, - } - .into(), - ), - } - .into(), - ); - check( - expr, - expect![[r#" - x Evaluation error - ,-[1:1] - 1 | { - 2 | val v1 = 42 - 3 | val v2 = 0 - 4 | val v3 = v1 / v2 - : ^^^|^^^ - : `-- Arithmetic exception: (42) / (0) resulted in exception - 5 | v3 - 6 | } - `---- - "#]], - ) + fn check_error_span(expr: Expr, expected_span: SourceSpan) { + let mut w = PosTrackingWriter::new(); + let spanned_expr = expr.print(&mut w).unwrap(); + dbg!(&spanned_expr); + let ctx = Rc::new(force_any_val::()); + let err_raw: SpannedEvalError = try_eval_out::(&spanned_expr, ctx) + .err() + .unwrap() + .try_into() + .unwrap(); + assert_eq!(err_raw.source_span, expected_span); } #[test] - fn span_binop_div_zero() { + fn pretty_binop_div_zero() { let lhs_val_id = 1.into(); let rhs_val_id = 2.into(); let res_val_id = 3.into(); @@ -337,32 +278,27 @@ mod tests { } .into(), ); - let mut w = PosTrackingWriter::new(); - let spanned_expr = expr.print(&mut w).unwrap(); - dbg!(&spanned_expr); - let ctx = Rc::new(force_any_val::()); - let err_raw: SpannedEvalError = try_eval_out::(&spanned_expr, ctx) - .err() - .unwrap() - .try_into() - .unwrap(); - assert_eq!( - err_raw.source_span, - SourceSpan { - offset: 40, - length: 7 - } - ); + // check( + // expr, + // expect![[r#" + // x Evaluation error + // ,-[1:1] + // 1 | { + // 2 | val v1 = 42 + // 3 | val v2 = 0 + // 4 | val v3 = v1 / v2 + // : ^^^|^^^ + // : `-- Arithmetic exception: (42) / (0) resulted in exception + // 5 | v3 + // 6 | } + // `---- + // "#]], + // ); + check_error_span(expr, (40, 7).into()); } - #[ignore = "expect test fails on self-generated string"] #[test] fn pretty_out_of_bounds() { - // OUTPUTS(1) should be out of bounds error in: - // { - // val v1 = 1 - // OUPUTS(v1) - // } let v1_id = 1.into(); let expr = Expr::BlockValue( BlockValue { @@ -387,19 +323,20 @@ mod tests { } .into(), ); - check( - expr, - expect![[r#" - x Evaluation error - ,-[1:1] - 1 | { - 2 | val v1 = 99999999 - 3 | OUTPUTS(v1) - : ^^|^ - : `-- error: ByIndex: index Int(99999999) out of bounds for collection size 1 - 4 | } - `---- - "#]], - ) + // check( + // expr, + // expect![[r#" + // x Evaluation error + // ,-[1:1] + // 1 | { + // 2 | val v1 = 99999999 + // 3 | OUTPUTS(v1) + // : ^^|^ + // : `-- error: ByIndex: index Int(99999999) out of bounds for collection size 1 + // 4 | } + // `---- + // "#]], + // ); + check_error_span(expr, (31, 4).into()); } } diff --git a/ergotree-ir/src/pretty_printer.rs b/ergotree-ir/src/pretty_printer.rs index 7fd19d388..1894ffa69 100644 --- a/ergotree-ir/src/pretty_printer.rs +++ b/ergotree-ir/src/pretty_printer.rs @@ -78,7 +78,6 @@ impl Print for ValDef { impl Print for Constant { fn print(&self, w: &mut dyn Printer) -> Result { - // TODO: implement Display for Literal write!(w, "{:?}", self.v)?; Ok(self.clone().into()) } diff --git a/ergotree-ir/src/source_span.rs b/ergotree-ir/src/source_span.rs index cb31b8262..142e700c8 100644 --- a/ergotree-ir/src/source_span.rs +++ b/ergotree-ir/src/source_span.rs @@ -26,6 +26,15 @@ impl SourceSpan { } } +impl From<(usize, usize)> for SourceSpan { + fn from(value: (usize, usize)) -> Self { + SourceSpan { + offset: value.0, + length: value.1, + } + } +} + impl From for miette::SourceSpan { fn from(value: SourceSpan) -> Self { miette::SourceSpan::new(value.offset.into(), value.length.into())