use monty::MontyRun;
use monty_types::{CompileOptions, JsonErrorData, MontyException};
fn run_exc(code: &str) -> MontyException {
MontyRun::new(code.to_owned(), "test.py", vec![], CompileOptions::default())
.unwrap()
.run_no_limits(vec![])
.unwrap_err()
}
#[test]
fn json_decode_error_carries_structured_data() {
let exc = run_exc("import json\njson.loads('[1,\\n2,]')");
assert_eq!(exc.exc_type().to_string(), "json.JSONDecodeError");
assert_eq!(
exc.message(),
Some("Illegal trailing comma before end of array: line 2 column 2 (char 5)")
);
let data = exc.json_data().unwrap();
assert_eq!(data.msg, "Illegal trailing comma before end of array");
assert_eq!(data.doc.as_deref(), Some("[1,\n2,]"));
assert_eq!(data.pos, 5);
assert_eq!(data.lineno, 2);
assert_eq!(data.colno, 2);
}
#[test]
fn json_error_positions_count_characters_not_bytes() {
let exc = run_exc("import json\njson.loads('[\"日本語\", x]')");
assert_eq!(exc.message(), Some("Expecting value: line 1 column 9 (char 8)"));
let data = exc.json_data().unwrap();
assert_eq!(data.doc.as_deref(), Some("[\"日本語\", x]"));
assert_eq!(data.pos, 8);
assert_eq!(data.lineno, 1);
assert_eq!(data.colno, 9);
}
#[test]
fn json_error_data_survives_reraise() {
let exc = run_exc("import json\ntry:\n json.loads('nope')\nexcept ValueError as e:\n raise e");
assert_eq!(exc.exc_type().to_string(), "json.JSONDecodeError");
assert!(exc.json_data().is_some());
}
#[test]
fn json_error_doc_omitted_for_huge_documents() {
let exc = run_exc(&format!(
"import json\njson.loads('[' + '1,' * {} + 'x')",
JsonErrorData::MAX_DOC_LEN / 2
));
assert_eq!(exc.exc_type().to_string(), "json.JSONDecodeError");
let data = exc.json_data().unwrap();
assert_eq!(data.doc, None);
assert_eq!(data.pos, JsonErrorData::MAX_DOC_LEN + 1);
}
#[test]
fn json_error_doc_omitted_for_invalid_utf8() {
let exc = run_exc("import json\njson.loads(b'[1, \\xff]')");
assert_eq!(exc.exc_type().to_string(), "json.JSONDecodeError");
let data = exc.json_data().unwrap();
assert_eq!(data.doc, None);
assert_eq!(data.pos, 4);
}
#[test]
fn manually_raised_json_decode_error_has_no_data() {
let exc = run_exc("import json\nraise json.JSONDecodeError('nope')");
assert_eq!(exc.exc_type().to_string(), "json.JSONDecodeError");
assert_eq!(exc.json_data(), None);
}