From c785b7c0c6cd8b3b1cc15eb79caf982a757020ba Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 6 Dec 2020 23:55:21 +0000 Subject: Thread cx through normalization --- dhall/tests/spec.rs | 158 ++++++++++++++++++++++++++-------------------------- 1 file changed, 80 insertions(+), 78 deletions(-) (limited to 'dhall/tests') diff --git a/dhall/tests/spec.rs b/dhall/tests/spec.rs index 646083a..ab1dc2f 100644 --- a/dhall/tests/spec.rs +++ b/dhall/tests/spec.rs @@ -15,7 +15,7 @@ use walkdir::WalkDir; use dhall::error::Error as DhallError; use dhall::error::ErrorKind; use dhall::syntax::{binary, Expr}; -use dhall::{Normalized, Parsed, Resolved, Typed}; +use dhall::{Ctxt, Normalized, Parsed, Resolved, Typed}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum FileType { @@ -108,16 +108,16 @@ impl TestFile { }) } /// Parse and resolve the target file - pub fn resolve(&self) -> Result { - Ok(self.parse()?.resolve()?) + pub fn resolve<'cx>(&self, cx: Ctxt<'cx>) -> Result> { + Ok(self.parse()?.resolve(cx)?) } /// Parse, resolve and tck the target file - pub fn typecheck(&self) -> Result { - Ok(self.resolve()?.typecheck()?) + pub fn typecheck<'cx>(&self, cx: Ctxt<'cx>) -> Result> { + Ok(self.resolve(cx)?.typecheck(cx)?) } /// Parse, resolve, tck and normalize the target file - pub fn normalize(&self) -> Result { - Ok(self.typecheck()?.normalize()) + pub fn normalize<'cx>(&self, cx: Ctxt<'cx>) -> Result> { + Ok(self.typecheck(cx)?.normalize(cx)) } /// If UPDATE_TEST_FILES is `true`, we overwrite the output files with our own output. @@ -586,79 +586,81 @@ fn run_test(test: &SpecTest) -> Result<()> { output: expected, .. } = test; - match test.kind { - ParserSuccess => { - let expr = expr.parse()?; - // This exercices both parsing and binary decoding - expected.compare_debug(expr)?; - } - ParserFailure => { - use std::io; - let err = unwrap_err(expr.parse())?; - match err.downcast_ref::() { - Some(err) => match err.kind() { - ErrorKind::Parse(_) => {} - ErrorKind::IO(e) - if e.kind() == io::ErrorKind::InvalidData => {} - e => Err(TestError(format!( - "Expected parse error, got: {:?}", - e - )))?, - }, - None => {} + Ctxt::with_new(|cx| { + match test.kind { + ParserSuccess => { + let expr = expr.parse()?; + // This exercices both parsing and binary decoding + expected.compare_debug(expr)?; + } + ParserFailure => { + use std::io; + let err = unwrap_err(expr.parse())?; + match err.downcast_ref::() { + Some(err) => match err.kind() { + ErrorKind::Parse(_) => {} + ErrorKind::IO(e) + if e.kind() == io::ErrorKind::InvalidData => {} + e => Err(TestError(format!( + "Expected parse error, got: {:?}", + e + )))?, + }, + None => {} + } + expected.compare_ui(err)?; + } + BinaryEncoding => { + let expr = expr.parse()?; + expected.compare_binary(expr)?; + } + BinaryDecodingSuccess => { + let expr = expr.parse()?; + expected.compare_debug(expr)?; + } + BinaryDecodingFailure => { + let err = unwrap_err(expr.parse())?; + expected.compare_ui(err)?; + } + Printer => { + let parsed = expr.parse()?; + // Round-trip pretty-printer + let reparsed = Parsed::parse_str(&parsed.to_string())?; + assert_eq!(reparsed, parsed); + expected.compare_ui(parsed)?; + } + ImportSuccess => { + let expr = expr.normalize(cx)?; + expected.compare(expr)?; + } + ImportFailure => { + let err = unwrap_err(expr.resolve(cx))?; + expected.compare_ui(err)?; + } + SemanticHash => { + let expr = expr.normalize(cx)?.to_expr_alpha(); + let hash = hex::encode(expr.sha256_hash()?); + expected.compare_ui(format!("sha256:{}", hash))?; + } + TypeInferenceSuccess => { + let ty = expr.typecheck(cx)?.get_type()?; + expected.compare(ty)?; + } + TypeInferenceFailure => { + let err = unwrap_err(expr.typecheck(cx))?; + expected.compare_ui(err)?; + } + Normalization => { + let expr = expr.normalize(cx)?; + expected.compare(expr)?; + } + AlphaNormalization => { + let expr = expr.normalize(cx)?.to_expr_alpha(); + expected.compare(expr)?; } - expected.compare_ui(err)?; - } - BinaryEncoding => { - let expr = expr.parse()?; - expected.compare_binary(expr)?; - } - BinaryDecodingSuccess => { - let expr = expr.parse()?; - expected.compare_debug(expr)?; - } - BinaryDecodingFailure => { - let err = unwrap_err(expr.parse())?; - expected.compare_ui(err)?; - } - Printer => { - let parsed = expr.parse()?; - // Round-trip pretty-printer - let reparsed = Parsed::parse_str(&parsed.to_string())?; - assert_eq!(reparsed, parsed); - expected.compare_ui(parsed)?; - } - ImportSuccess => { - let expr = expr.normalize()?; - expected.compare(expr)?; - } - ImportFailure => { - let err = unwrap_err(expr.resolve())?; - expected.compare_ui(err)?; - } - SemanticHash => { - let expr = expr.normalize()?.to_expr_alpha(); - let hash = hex::encode(expr.sha256_hash()?); - expected.compare_ui(format!("sha256:{}", hash))?; - } - TypeInferenceSuccess => { - let ty = expr.typecheck()?.get_type()?; - expected.compare(ty)?; - } - TypeInferenceFailure => { - let err = unwrap_err(expr.typecheck())?; - expected.compare_ui(err)?; - } - Normalization => { - let expr = expr.normalize()?; - expected.compare(expr)?; - } - AlphaNormalization => { - let expr = expr.normalize()?.to_expr_alpha(); - expected.compare(expr)?; } - } - Ok(()) + Ok(()) + }) } fn main() { -- cgit v1.2.3 From 8c5b3ff15f2125e9d731fc199e194e1993c36b37 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 7 Dec 2020 14:15:43 +0000 Subject: Thread cx everywhere else imports are read --- dhall/tests/spec.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'dhall/tests') diff --git a/dhall/tests/spec.rs b/dhall/tests/spec.rs index ab1dc2f..a0fe583 100644 --- a/dhall/tests/spec.rs +++ b/dhall/tests/spec.rs @@ -160,8 +160,7 @@ impl TestFile { } /// Check that the provided expression matches the file contents. - pub fn compare(&self, expr: impl Into) -> Result<()> { - let expr = expr.into(); + pub fn compare(&self, expr: Expr) -> Result<()> { if !self.path().is_file() { return self.write_expr(expr); } @@ -177,8 +176,7 @@ impl TestFile { Ok(()) } /// Check that the provided expression matches the file contents. - pub fn compare_debug(&self, expr: impl Into) -> Result<()> { - let expr = expr.into(); + pub fn compare_debug(&self, expr: Expr) -> Result<()> { if !self.path().is_file() { return self.write_expr(expr); } @@ -194,8 +192,7 @@ impl TestFile { Ok(()) } /// Check that the provided expression matches the file contents. - pub fn compare_binary(&self, expr: impl Into) -> Result<()> { - let expr = expr.into(); + pub fn compare_binary(&self, expr: Expr) -> Result<()> { match self { TestFile::Binary(_) => {} _ => Err(TestError(format!("This is not a binary file")))?, @@ -591,7 +588,7 @@ fn run_test(test: &SpecTest) -> Result<()> { ParserSuccess => { let expr = expr.parse()?; // This exercices both parsing and binary decoding - expected.compare_debug(expr)?; + expected.compare_debug(expr.to_expr())?; } ParserFailure => { use std::io; @@ -612,11 +609,11 @@ fn run_test(test: &SpecTest) -> Result<()> { } BinaryEncoding => { let expr = expr.parse()?; - expected.compare_binary(expr)?; + expected.compare_binary(expr.to_expr())?; } BinaryDecodingSuccess => { let expr = expr.parse()?; - expected.compare_debug(expr)?; + expected.compare_debug(expr.to_expr())?; } BinaryDecodingFailure => { let err = unwrap_err(expr.parse())?; @@ -627,24 +624,24 @@ fn run_test(test: &SpecTest) -> Result<()> { // Round-trip pretty-printer let reparsed = Parsed::parse_str(&parsed.to_string())?; assert_eq!(reparsed, parsed); - expected.compare_ui(parsed)?; + expected.compare_ui(parsed.to_expr())?; } ImportSuccess => { let expr = expr.normalize(cx)?; - expected.compare(expr)?; + expected.compare(expr.to_expr(cx))?; } ImportFailure => { let err = unwrap_err(expr.resolve(cx))?; expected.compare_ui(err)?; } SemanticHash => { - let expr = expr.normalize(cx)?.to_expr_alpha(); + let expr = expr.normalize(cx)?.to_expr_alpha(cx); let hash = hex::encode(expr.sha256_hash()?); expected.compare_ui(format!("sha256:{}", hash))?; } TypeInferenceSuccess => { let ty = expr.typecheck(cx)?.get_type()?; - expected.compare(ty)?; + expected.compare(ty.to_expr(cx))?; } TypeInferenceFailure => { let err = unwrap_err(expr.typecheck(cx))?; @@ -652,10 +649,10 @@ fn run_test(test: &SpecTest) -> Result<()> { } Normalization => { let expr = expr.normalize(cx)?; - expected.compare(expr)?; + expected.compare(expr.to_expr(cx))?; } AlphaNormalization => { - let expr = expr.normalize(cx)?.to_expr_alpha(); + let expr = expr.normalize(cx)?.to_expr_alpha(cx); expected.compare(expr)?; } } -- cgit v1.2.3 From ff070d5e5815b6cef27ff40383153226ddfd4a61 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 7 Dec 2020 15:37:29 +0000 Subject: Add tests --- dhall/tests/import/success/unit/AlternativeWithWrongVariable1A.dhall | 1 + dhall/tests/import/success/unit/AlternativeWithWrongVariable1B.dhall | 1 + dhall/tests/import/success/unit/AlternativeWithWrongVariable2A.dhall | 1 + dhall/tests/import/success/unit/AlternativeWithWrongVariable2B.dhall | 1 + 4 files changed, 4 insertions(+) create mode 100644 dhall/tests/import/success/unit/AlternativeWithWrongVariable1A.dhall create mode 100644 dhall/tests/import/success/unit/AlternativeWithWrongVariable1B.dhall create mode 100644 dhall/tests/import/success/unit/AlternativeWithWrongVariable2A.dhall create mode 100644 dhall/tests/import/success/unit/AlternativeWithWrongVariable2B.dhall (limited to 'dhall/tests') diff --git a/dhall/tests/import/success/unit/AlternativeWithWrongVariable1A.dhall b/dhall/tests/import/success/unit/AlternativeWithWrongVariable1A.dhall new file mode 100644 index 0000000..e6fa8d5 --- /dev/null +++ b/dhall/tests/import/success/unit/AlternativeWithWrongVariable1A.dhall @@ -0,0 +1 @@ +\(x: Natural) -> x ? y diff --git a/dhall/tests/import/success/unit/AlternativeWithWrongVariable1B.dhall b/dhall/tests/import/success/unit/AlternativeWithWrongVariable1B.dhall new file mode 100644 index 0000000..04a1712 --- /dev/null +++ b/dhall/tests/import/success/unit/AlternativeWithWrongVariable1B.dhall @@ -0,0 +1 @@ +λ(x : Natural) → x diff --git a/dhall/tests/import/success/unit/AlternativeWithWrongVariable2A.dhall b/dhall/tests/import/success/unit/AlternativeWithWrongVariable2A.dhall new file mode 100644 index 0000000..8089500 --- /dev/null +++ b/dhall/tests/import/success/unit/AlternativeWithWrongVariable2A.dhall @@ -0,0 +1 @@ +\(x: Natural) -> (y + missing) ? x diff --git a/dhall/tests/import/success/unit/AlternativeWithWrongVariable2B.dhall b/dhall/tests/import/success/unit/AlternativeWithWrongVariable2B.dhall new file mode 100644 index 0000000..04a1712 --- /dev/null +++ b/dhall/tests/import/success/unit/AlternativeWithWrongVariable2B.dhall @@ -0,0 +1 @@ +λ(x : Natural) → x -- cgit v1.2.3