summaryrefslogtreecommitdiff
path: root/dhall/src
diff options
context:
space:
mode:
authorNadrieril Feneanar2020-03-20 12:31:17 +0000
committerGitHub2020-03-20 12:31:17 +0000
commit1088394da2270fb1c53f5db2ff3bfca7709f64ab (patch)
tree08fc691ba174831cb85c0fffa474e58e59a47d8c /dhall/src
parent98b067309388d3f7c692dd82634e776113ea342b (diff)
parent4ff536cb593aa022e71042ab4fe008e2381b0680 (diff)
Merge pull request #152 from Nadrieril/test-printer
Include printer output in tests
Diffstat (limited to 'dhall/src')
-rw-r--r--dhall/src/syntax/ast/expr.rs4
-rw-r--r--dhall/src/syntax/ast/import.rs20
-rw-r--r--dhall/src/syntax/text/printer.rs21
-rw-r--r--dhall/src/tests.rs46
4 files changed, 58 insertions, 33 deletions
diff --git a/dhall/src/syntax/ast/expr.rs b/dhall/src/syntax/ast/expr.rs
index 8023771..b53e6cb 100644
--- a/dhall/src/syntax/ast/expr.rs
+++ b/dhall/src/syntax/ast/expr.rs
@@ -258,8 +258,8 @@ impl Expr {
}
// Empty enum to indicate that no error can occur
-enum X {}
-fn trivial_result<T>(x: Result<T, X>) -> T {
+pub(crate) enum X {}
+pub(crate) fn trivial_result<T>(x: Result<T, X>) -> T {
match x {
Ok(x) => x,
Err(e) => match e {},
diff --git a/dhall/src/syntax/ast/import.rs b/dhall/src/syntax/ast/import.rs
index 75d7946..c45fe51 100644
--- a/dhall/src/syntax/ast/import.rs
+++ b/dhall/src/syntax/ast/import.rs
@@ -1,3 +1,5 @@
+use crate::syntax::trivial_result;
+
/// The beginning of a file path which anchors subsequent path components
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum FilePrefix {
@@ -75,6 +77,12 @@ impl<SE> URL<SE> {
headers,
})
}
+ pub fn map_ref<'a, SE2>(
+ &'a self,
+ f: impl FnOnce(&'a SE) -> SE2,
+ ) -> URL<SE2> {
+ trivial_result(self.traverse_ref(|x| Ok(f(x))))
+ }
}
impl<SE> ImportTarget<SE> {
@@ -90,6 +98,12 @@ impl<SE> ImportTarget<SE> {
Missing => Missing,
})
}
+ pub fn map_ref<'a, SE2>(
+ &'a self,
+ f: impl FnOnce(&'a SE) -> SE2,
+ ) -> ImportTarget<SE2> {
+ trivial_result(self.traverse_ref(|x| Ok(f(x))))
+ }
}
impl<SE> Import<SE> {
@@ -103,4 +117,10 @@ impl<SE> Import<SE> {
hash: self.hash.clone(),
})
}
+ pub fn map_ref<'a, SE2>(
+ &'a self,
+ f: impl FnOnce(&'a SE) -> SE2,
+ ) -> Import<SE2> {
+ trivial_result(self.traverse_ref(|x| Ok(f(x))))
+ }
}
diff --git a/dhall/src/syntax/text/printer.rs b/dhall/src/syntax/text/printer.rs
index e9584bb..2b7bc2e 100644
--- a/dhall/src/syntax/text/printer.rs
+++ b/dhall/src/syntax/text/printer.rs
@@ -8,17 +8,23 @@ use std::fmt::{self, Display};
// of automatically getting all the parentheses and precedences right.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
enum PrintPhase {
+ // `expression`
Base,
+ // `operator-expression`
Operator,
+ // All the operator `*-expression`s
BinOp(ast::BinOp),
+ // `application-expression`
App,
+ // `import-expression`
Import,
+ // `primitive-expression`
Primitive,
}
// Wraps an Expr with a phase, so that phase selection can be done separate from the actual
// printing.
-#[derive(Clone)]
+#[derive(Copy, Clone)]
struct PhasedExpr<'a>(&'a Expr, PrintPhase);
impl<'a> PhasedExpr<'a> {
@@ -58,7 +64,7 @@ impl UnspannedExpr {
),
SomeLit(e) => SomeLit(e.phase(PrintPhase::Import)),
ExprKind::App(f, a) => ExprKind::App(
- f.phase(PrintPhase::Import),
+ f.phase(PrintPhase::App),
a.phase(PrintPhase::Import),
),
Field(a, b) => Field(a.phase(Primitive), b),
@@ -67,6 +73,9 @@ impl UnspannedExpr {
Completion(a, b) => {
Completion(a.phase(Primitive), b.phase(Primitive))
}
+ ExprKind::Import(a) => {
+ ExprKind::Import(a.map_ref(|x| x.phase(PrintPhase::Import)))
+ }
e => e,
}
}
@@ -84,7 +93,6 @@ impl UnspannedExpr {
| Pi(_, _, _)
| Let(_, _, _, _)
| EmptyListLit(_)
- | NEListLit(_)
| SomeLit(_)
| Merge(_, _, _)
| ToMap(_, _)
@@ -92,10 +100,7 @@ impl UnspannedExpr {
// Precedence is magically handled by the ordering of BinOps.
ExprKind::BinOp(op, _, _) => phase > PrintPhase::BinOp(*op),
ExprKind::App(_, _) => phase > PrintPhase::App,
- Field(_, _)
- | Projection(_, _)
- | ProjectionByExpr(_, _)
- | Completion(_, _) => phase > PrintPhase::Import,
+ Completion(_, _) => phase > PrintPhase::Import,
_ => false,
};
@@ -413,7 +418,7 @@ impl<SubExpr: Display> Display for Import<SubExpr> {
write!(f, "?{}", q)?
}
if let Some(h) = &url.headers {
- write!(f, " using ({})", h)?
+ write!(f, " using {}", h)?
}
}
Env(s) => {
diff --git a/dhall/src/tests.rs b/dhall/src/tests.rs
index 369c6cc..d305b98 100644
--- a/dhall/src/tests.rs
+++ b/dhall/src/tests.rs
@@ -3,6 +3,16 @@ use assert_eq as assert_eq_pretty;
#[cfg(test)]
use pretty_assertions::assert_eq as assert_eq_pretty;
+use std::env;
+use std::fmt::Display;
+use std::fs::{create_dir_all, read_to_string, File};
+use std::io::{Read, Write};
+use std::path::PathBuf;
+
+use crate::error::{ErrorKind, Result};
+use crate::syntax::binary;
+use crate::{Normalized, NormalizedExpr, Parsed, Resolved, Typed};
+
macro_rules! assert_eq_display {
($left:expr, $right:expr) => {{
match (&$left, &$right) {
@@ -43,15 +53,6 @@ macro_rules! assert_eq_pretty_str {
};
}
-use std::env;
-use std::fs::{create_dir_all, read_to_string, File};
-use std::io::{Read, Write};
-use std::path::PathBuf;
-
-use crate::error::{Error, ErrorKind, Result};
-use crate::syntax::binary;
-use crate::{Normalized, NormalizedExpr, Parsed, Resolved, Typed};
-
#[allow(dead_code)]
enum Test {
ParserSuccess(TestFile, TestFile),
@@ -127,17 +128,16 @@ impl TestFile {
}
Ok(())
}
- /// Write the provided error to the pointed file.
- fn write_ui(&self, err: impl Into<Error>) -> Result<()> {
+ /// Write the provided string to the pointed file.
+ fn write_ui(&self, x: impl Display) -> Result<()> {
match self {
TestFile::UI(_) => {}
_ => panic!("Can't write an error to a non-UI file"),
}
- let err = err.into();
let path = self.path();
create_dir_all(path.parent().unwrap())?;
let mut file = File::create(path)?;
- writeln!(file, "{}", err)?;
+ writeln!(file, "{}", x)?;
Ok(())
}
@@ -214,19 +214,18 @@ impl TestFile {
}
Ok(())
}
- /// Check that the provided error matches the file contents. Writes to the corresponding file
+ /// Check that the provided string matches the file contents. Writes to the corresponding file
/// if it is missing.
- pub fn compare_ui(&self, err: impl Into<Error>) -> Result<()> {
- let err = err.into();
+ pub fn compare_ui(&self, x: impl Display) -> Result<()> {
if !self.path().is_file() {
- return self.write_ui(err);
+ return self.write_ui(x);
}
let expected = read_to_string(self.path())?;
- let msg = format!("{}\n", err);
+ let msg = format!("{}\n", x);
if msg != expected {
if Self::force_update() {
- self.write_ui(err)?;
+ self.write_ui(x)?;
} else {
assert_eq_pretty_str!(msg, expected);
}
@@ -278,11 +277,12 @@ fn run_test(test: Test) -> Result<()> {
let err = expr.parse().unwrap_err();
expected.compare_ui(err)?;
}
- Printer(expr, _) => {
- let expected = expr.parse()?;
+ Printer(expr, expected) => {
+ let parsed = expr.parse()?;
// Round-trip pretty-printer
- let expr = Parsed::parse_str(&expected.to_string())?;
- assert_eq!(expr, expected);
+ let reparsed = Parsed::parse_str(&parsed.to_string())?;
+ assert_eq!(reparsed, parsed);
+ expected.compare_ui(parsed)?;
}
ImportSuccess(expr, expected) => {
let expr = expr.normalize()?;