aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorEdvard Thörnros <edvard.thornros@gmail.com>2021-02-09 17:46:00 +0100
committerGitHub <noreply@github.com>2021-02-09 17:46:00 +0100
commitbe4559ed0af1d12401365b5ffeae99885842314f (patch)
treefa7059306b50499494c0b9b22bd9f980f30d7574 /src/lib.rs
parenta41d008487c26dae5dcdf6e0be9323ad2e06c827 (diff)
parente37fc83ce9705eb8af2c7b7bfb927b38cf382726 (diff)
downloadsylt-be4559ed0af1d12401365b5ffeae99885842314f.tar.gz
Merge pull request #56 from FredTheDino/parse-branch-macro
parse branch macro
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs41
1 files changed, 39 insertions, 2 deletions
diff --git a/src/lib.rs b/src/lib.rs
index b34b87e..ae45c45 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -736,18 +736,55 @@ mod tests {
};
}
+ use std::time::Duration;
+ use std::sync::mpsc;
+ use std::thread;
+
+ // Shamelessly stolen from https://github.com/rust-lang/rfcs/issues/2798
+ pub fn panic_after<T, F>(d: Duration, f: F) -> T
+ where
+ T: Send + 'static,
+ F: FnOnce() -> T,
+ F: Send + 'static,
+ {
+ let (done_tx, done_rx) = mpsc::channel();
+ let handle = thread::spawn(move || {
+ let val = f();
+ done_tx.send(()).expect("Unable to send completion signal");
+ val
+ });
+
+ match done_rx.recv_timeout(d) {
+ Ok(_) => handle.join().expect("Thread panicked"),
+ Err(_) => panic!("Thread took too long"),
+ }
+ }
+
#[macro_export]
macro_rules! test_string {
($fn:ident, $prog:literal) => {
#[test]
fn $fn() {
- $crate::run_string($prog, true, Vec::new()).unwrap();
+ crate::tests::panic_after(std::time::Duration::from_millis(500), || {
+ match $crate::run_string($prog, true, Vec::new()) {
+ Ok(()) => {},
+ Err(errs) => {
+ for e in errs.iter() {
+ println!("{}", e);
+ }
+ println!(" {} - FAILED\n", stringify!($fn));
+ panic!();
+ }
+ }
+ });
}
};
($fn:ident, $prog:literal, $errs:tt) => {
#[test]
fn $fn() {
- $crate::assert_errs!($crate::run_string($prog, true, Vec::new()), $errs);
+ crate::tests::panic_after(std::time::Duration::from_millis(500), || {
+ $crate::assert_errs!($crate::run_string($prog, true, Vec::new()), $errs);
+ })
}
}
}