summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Breitmoser <look@my.amazin.horse>2019-01-16 22:32:22 +0100
committerVincent Breitmoser <look@my.amazin.horse>2019-01-16 22:32:22 +0100
commit6ba7135380d747d249da3daeaad03752e081fea1 (patch)
tree2975002432f47b46de2bea2928661d5dc80f8f7e
parent35019b53f28cb8716ea6826f579afc4f70173818 (diff)
tests: add "khprintln!" macro to check stdout in tests
-rw-r--r--src/actions/show.rs20
-rw-r--r--src/lib.rs3
-rw-r--r--src/macros.rs32
-rw-r--r--src/testutils.rs17
-rw-r--r--testdata/testdir_with_seq/seq2
5 files changed, 72 insertions, 2 deletions
diff --git a/src/actions/show.rs b/src/actions/show.rs
index 2fa74ef..dc13712 100644
--- a/src/actions/show.rs
+++ b/src/actions/show.rs
@@ -9,8 +9,26 @@ pub fn do_show(_args: &[String]) -> Result<(), String> {
for line in lines {
let khline = line.parse::<KhLine>().map_err(|err| err.to_string())?;
let output = fileutil::read_file_to_string(&khline.path).unwrap();
- println!("{}", output);
+ khprintln!("{}", output);
}
Ok(())
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ use testutils::*;
+
+ #[test]
+ fn test_() {
+ let _testdir = prepare_testdir("testdir_with_seq");
+
+ do_show(&[]).unwrap();
+
+ let stdout = test_stdout_clear();
+ assert_eq!(784, stdout.len());
+ assert_eq!(32, stdout.lines().count());
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 912f4f2..3949cd4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,6 @@
+#[macro_use]
+mod macros;
+
pub mod actions;
pub mod backup;
pub mod khline;
diff --git a/src/macros.rs b/src/macros.rs
new file mode 100644
index 0000000..a393399
--- /dev/null
+++ b/src/macros.rs
@@ -0,0 +1,32 @@
+#[macro_export]
+macro_rules! khprint {
+ () => ();
+ ($($arg:tt)*) => ({
+ let line = format!($($arg)*);
+ #[cfg(test)] {
+ use testutils;
+ testutils::test_stdout_write(&line);
+ }
+ print!("{}", line);
+ })
+}
+
+#[macro_export]
+macro_rules! khprintln {
+ () => ({
+ #[cfg(test)] {
+ use testutils;
+ testutils::test_stdout_write("\n");
+ }
+ println!();
+ });
+ ($($arg:tt)*) => ({
+ let line = format!($($arg)*);
+ #[cfg(test)] {
+ use testutils;
+ testutils::test_stdout_write(&line);
+ testutils::test_stdout_write("\n");
+ }
+ println!("{}", line);
+ })
+}
diff --git a/src/testutils.rs b/src/testutils.rs
index 72c5f67..308a793 100644
--- a/src/testutils.rs
+++ b/src/testutils.rs
@@ -2,6 +2,11 @@ use assert_fs::prelude::*;
use assert_fs::TempDir;
use std::path::PathBuf;
+use std::cell::RefCell;
+thread_local! {
+ pub static STDOUT_BUF: RefCell<String> = RefCell::new(String::new())
+}
+
use defaults;
pub fn path_to(artifact: &str) -> PathBuf {
@@ -19,3 +24,15 @@ pub fn prepare_testdir(template: &str) -> TempDir {
testdir.child(".khaleesi/").copy_from(path_to(template), &["*"]).unwrap();
testdir
}
+
+pub fn test_stdout_write(line: &str) {
+ STDOUT_BUF.with(|cell| cell.borrow_mut().push_str(&line));
+}
+
+pub fn test_stdout_clear() -> String {
+ STDOUT_BUF.with(|cell| {
+ let result = cell.borrow().clone();
+ *cell.borrow_mut() = String::new();
+ result
+ })
+}
diff --git a/testdata/testdir_with_seq/seq b/testdata/testdir_with_seq/seq
index bfa5234..204ff7a 100644
--- a/testdata/testdir_with_seq/seq
+++ b/testdata/testdir_with_seq/seq
@@ -1 +1 @@
-1544740200 .khaleesi/cal/twodaysacrossbuckets.ics
+1544740200 twodaysacrossbuckets.ics