Skip to content

Commit

Permalink
add span check in pretty error tests;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Aug 15, 2023
1 parent 60e25a6 commit 0b3d3a8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 109 deletions.
153 changes: 45 additions & 108 deletions ergotree-interpreter/src/eval/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<T> ExtResultEvalError<T> for Result<T, EvalError> {
}
}

#[allow(clippy::unwrap_used)]
#[allow(clippy::unwrap_used, unused_imports, dead_code)]
#[cfg(test)]
mod tests {
use std::rc::Rc;
Expand Down Expand Up @@ -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::<Context>());
let err_raw: SpannedEvalError = try_eval_out::<i32>(&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();
Expand Down Expand Up @@ -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::<Context>());
let err_raw: SpannedEvalError = try_eval_out::<i32>(&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 {
Expand All @@ -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());
}
}
1 change: 0 additions & 1 deletion ergotree-ir/src/pretty_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ impl Print for ValDef {

impl Print for Constant {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
// TODO: implement Display for Literal
write!(w, "{:?}", self.v)?;
Ok(self.clone().into())
}
Expand Down
9 changes: 9 additions & 0 deletions ergotree-ir/src/source_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceSpan> for miette::SourceSpan {
fn from(value: SourceSpan) -> Self {
miette::SourceSpan::new(value.offset.into(), value.length.into())
Expand Down

0 comments on commit 0b3d3a8

Please sign in to comment.