summaryrefslogtreecommitdiff
path: root/tests/src/tests.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-11-04 10:17:49 +0100
committerGitHub <noreply@github.com>2024-11-04 10:17:49 +0100
commitcb1aad3a0cc862c5ff57a557e196ba49a02917de (patch)
tree80cd62cbeb0f8d2bb999cc984d213f42293ebd24 /tests/src/tests.rs
parent6b636167ef2e84c761777261ce1ca3087a75f765 (diff)
parent2c9728f53b318a6cae092f30ad0956a536af7ccb (diff)
Refactor Parser (#5310)
Diffstat (limited to 'tests/src/tests.rs')
-rw-r--r--tests/src/tests.rs76
1 files changed, 72 insertions, 4 deletions
diff --git a/tests/src/tests.rs b/tests/src/tests.rs
index 940c9e3c..2b09b29c 100644
--- a/tests/src/tests.rs
+++ b/tests/src/tests.rs
@@ -1,13 +1,19 @@
//! Typst's test runner.
+#![cfg_attr(not(feature = "default"), allow(dead_code, unused_imports))]
+
mod args;
mod collect;
-mod custom;
mod logger;
+
+#[cfg(feature = "default")]
+mod custom;
+#[cfg(feature = "default")]
mod run;
+#[cfg(feature = "default")]
mod world;
-use std::path::Path;
+use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use std::time::Duration;
@@ -16,7 +22,8 @@ use parking_lot::Mutex;
use rayon::iter::{ParallelBridge, ParallelIterator};
use crate::args::{CliArguments, Command};
-use crate::logger::Logger;
+use crate::collect::Test;
+use crate::logger::{Logger, TestResult};
/// The parsed command line arguments.
static ARGS: LazyLock<CliArguments> = LazyLock::new(CliArguments::parse);
@@ -27,6 +34,9 @@ const SUITE_PATH: &str = "tests/suite";
/// The directory where the full test results are stored.
const STORE_PATH: &str = "tests/store";
+/// The directory where syntax trees are stored.
+const SYNTAX_PATH: &str = "tests/store/syntax";
+
/// The directory where the reference images are stored.
const REF_PATH: &str = "tests/ref";
@@ -89,6 +99,21 @@ fn test() {
return;
}
+ let parser_dirs = ARGS.parser_compare.clone().map(create_syntax_store);
+ #[cfg(not(feature = "default"))]
+ let parser_dirs = parser_dirs.or_else(|| Some(create_syntax_store(None)));
+
+ let runner = |test: &Test| {
+ if let Some((live_path, ref_path)) = &parser_dirs {
+ run_parser_test(test, live_path, ref_path)
+ } else {
+ #[cfg(feature = "default")]
+ return run::run(test);
+ #[cfg(not(feature = "default"))]
+ unreachable!();
+ }
+ };
+
// Run the tests.
let logger = Mutex::new(Logger::new(selected, skipped));
std::thread::scope(|scope| {
@@ -112,7 +137,7 @@ fn test() {
// to `typst::utils::Deferred` yielding.
tests.iter().par_bridge().for_each(|test| {
logger.lock().start(test);
- let result = std::panic::catch_unwind(|| run::run(test));
+ let result = std::panic::catch_unwind(|| runner(test));
logger.lock().end(test, result);
});
@@ -142,3 +167,46 @@ fn undangle() {
}
}
}
+
+fn create_syntax_store(ref_path: Option<PathBuf>) -> (&'static Path, Option<PathBuf>) {
+ if ref_path.as_ref().is_some_and(|p| !p.exists()) {
+ eprintln!("syntax reference path doesn't exist");
+ std::process::exit(1);
+ }
+
+ let live_path = Path::new(SYNTAX_PATH);
+ std::fs::remove_dir_all(live_path).ok();
+ std::fs::create_dir_all(live_path).unwrap();
+ (live_path, ref_path)
+}
+
+fn run_parser_test(
+ test: &Test,
+ live_path: &Path,
+ ref_path: &Option<PathBuf>,
+) -> TestResult {
+ let mut result = TestResult {
+ errors: String::new(),
+ infos: String::new(),
+ mismatched_image: false,
+ };
+
+ let syntax_file = live_path.join(format!("{}.syntax", test.name));
+ let tree = format!("{:#?}\n", test.source.root());
+ std::fs::write(syntax_file, &tree).unwrap();
+
+ let Some(ref_path) = ref_path else { return result };
+ let ref_file = ref_path.join(format!("{}.syntax", test.name));
+ match std::fs::read_to_string(&ref_file) {
+ Ok(ref_tree) => {
+ if tree != ref_tree {
+ result.errors = "differs".to_string();
+ }
+ }
+ Err(_) => {
+ result.errors = format!("missing reference: {}", ref_file.display());
+ }
+ }
+
+ result
+}