From 6654d441e5741013a8618907773ac54101e3fdf2 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 7 Aug 2019 21:38:01 +0200 Subject: Add binary-decode tests --- dhall/build.rs | 23 +++++++++++++++++++++-- dhall/src/phase/binary.rs | 21 +++++++++++++++++++++ dhall/src/tests.rs | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 3 deletions(-) (limited to 'dhall') diff --git a/dhall/build.rs b/dhall/build.rs index d3f63ad..1da06ca 100644 --- a/dhall/build.rs +++ b/dhall/build.rs @@ -15,11 +15,15 @@ fn dhall_files_in_dir<'a>( .filter_map(move |path| { let path = path.path(); let path = path.strip_prefix(dir).unwrap(); - if path.extension() != Some(&OsString::from("dhall")) { + let ext = path.extension(); + if ext != Some(&OsString::from("dhall")) + && ext != Some(&OsString::from("dhallb")) + { return None; } + let ext = ext.unwrap(); let path = path.to_string_lossy(); - let path = &path[..path.len() - 6]; + let path = &path[..path.len() - 1 - ext.len()]; let path = if take_a_suffix { if &path[path.len() - 1..] != "A" { return None; @@ -162,6 +166,21 @@ fn main() -> std::io::Result<()> { }, )?; + make_test_module( + &mut file, + "binary_decoding", + &tests_dir.join("binary-decode/"), + "BinaryDecoding", + |path| { + false + // TODO: projection by expression + || path == "success/unit/RecordProjectFields" + || path == "success/unit/recordProjectionByExpression" + // TODO: test is wrong + || path == "success/unit/BuiltinNaturalSubtract" + }, + )?; + make_test_module( &mut file, "beta_normalize", diff --git a/dhall/src/phase/binary.rs b/dhall/src/phase/binary.rs index bfe217f..cab3c8e 100644 --- a/dhall/src/phase/binary.rs +++ b/dhall/src/phase/binary.rs @@ -48,10 +48,21 @@ fn cbor_value_to_dhall( Bool(b) => BoolLit(*b), Array(vec) => match vec.as_slice() { [String(l), U64(n)] => { + if l.as_str() == "_" { + Err(DecodeError::WrongFormatError( + "`_` variable was encoded incorrectly".to_owned(), + ))? + } let l = Label::from(l.as_str()); Var(V(l, *n as usize)) } [U64(0), f, args..] => { + if args.is_empty() { + Err(DecodeError::WrongFormatError( + "Function application must have at least one argument" + .to_owned(), + ))? + } let mut f = cbor_value_to_dhall(&f)?; for a in args { let a = cbor_value_to_dhall(&a)?; @@ -65,6 +76,11 @@ fn cbor_value_to_dhall( Lam(Label::from("_"), x, y) } [U64(1), String(l), x, y] => { + if l.as_str() == "_" { + Err(DecodeError::WrongFormatError( + "`_` variable was encoded incorrectly".to_owned(), + ))? + } let x = cbor_value_to_dhall(&x)?; let y = cbor_value_to_dhall(&y)?; let l = Label::from(l.as_str()); @@ -76,6 +92,11 @@ fn cbor_value_to_dhall( Pi(Label::from("_"), x, y) } [U64(2), String(l), x, y] => { + if l.as_str() == "_" { + Err(DecodeError::WrongFormatError( + "`_` variable was encoded incorrectly".to_owned(), + ))? + } let x = cbor_value_to_dhall(&x)?; let y = cbor_value_to_dhall(&y)?; let l = Label::from(l.as_str()); diff --git a/dhall/src/tests.rs b/dhall/src/tests.rs index 8b32fb4..da48d15 100644 --- a/dhall/src/tests.rs +++ b/dhall/src/tests.rs @@ -45,6 +45,7 @@ pub enum Feature { Parser, Printer, BinaryEncoding, + BinaryDecoding, Import, Normalization, AlphaNormalization, @@ -84,6 +85,7 @@ pub fn run_test( Parser => "parser/", Printer => "parser/", BinaryEncoding => "parser/", + BinaryDecoding => "binary-decode/", Import => "import/", Normalization => "normalization/", AlphaNormalization => "alpha-normalization/", @@ -94,6 +96,24 @@ pub fn run_test( "../dhall-lang/tests/".to_owned() + feature_prefix + base_path; match status { Success => { + match feature { + BinaryDecoding => { + let expr_file_path = base_path.clone() + "A.dhallb"; + let expr_file_path = PathBuf::from(&expr_file_path); + let mut expr_data = Vec::new(); + { + File::open(&expr_file_path)? + .read_to_end(&mut expr_data)?; + } + let expr = Parsed::parse_binary(&expr_data)?; + let expected_file_path = base_path + "B.dhall"; + let expected = parse_file_str(&expected_file_path)?; + assert_eq_pretty!(expr, expected); + + return Ok(()); + } + _ => {} + } let expr_file_path = base_path.clone() + "A.dhall"; let expr = parse_file_str(&expr_file_path)?; @@ -164,7 +184,9 @@ pub fn run_test( .normalize(); match feature { - Parser | Printer | BinaryEncoding => unreachable!(), + Parser | Printer | BinaryEncoding | BinaryDecoding => { + unreachable!() + } Import => { let expr = expr.skip_typecheck().normalize(); assert_eq_display!(expr, expected); @@ -201,6 +223,15 @@ pub fn run_test( } } Printer | BinaryEncoding => unreachable!(), + BinaryDecoding => { + let expr_file_path = file_path + "b"; + let mut expr_data = Vec::new(); + { + File::open(&PathBuf::from(&expr_file_path))? + .read_to_end(&mut expr_data)?; + } + Parsed::parse_binary(&expr_data).unwrap_err(); + } Import => { parse_file_str(&file_path)?.resolve().unwrap_err(); } -- cgit v1.2.3