From a4811e8a500e7d8a61ac6c45f64ce6a994c014fa Mon Sep 17 00:00:00 2001 From: "Shahar \"Dawn\" Or" Date: Sat, 1 Jun 2024 16:47:35 +0200 Subject: [PATCH] test: some repl example unit tests Co-authored-by: ruben beck Co-authored-by: Ishwar Co-authored-by: warren2k <846021+warren2k@users.noreply.github.com> --- Cargo.lock | 23 ++++++++ Cargo.toml | 1 + src/repl/example.rs | 141 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 164 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 91a2fff..3becfba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -321,6 +321,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "difflib" version = "0.4.0" @@ -349,6 +355,7 @@ dependencies = [ "indoc", "itertools", "predicates", + "pretty_assertions", "pty-process", "strip-ansi-escapes", "tokio", @@ -836,6 +843,16 @@ dependencies = [ "termtree", ] +[[package]] +name = "pretty_assertions" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "proc-macro2" version = "1.0.66" @@ -1396,3 +1413,9 @@ checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" dependencies = [ "linked-hash-map", ] + +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" diff --git a/Cargo.toml b/Cargo.toml index bd4eab9..762f06c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,3 +32,4 @@ assert_cmd = "2.0.12" assert_fs = "1.0.13" indoc = "2.0.3" predicates = "3.1.0" +pretty_assertions = "1.4.0" diff --git a/src/repl/example.rs b/src/repl/example.rs index a218cfb..86bee3b 100644 --- a/src/repl/example.rs +++ b/src/repl/example.rs @@ -83,7 +83,7 @@ impl std::str::FromStr for ReplExampleEntries { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct ReplEntry { pub(crate) query: ReplQuery, pub(crate) expected_result: ExpectedResult, @@ -99,3 +99,142 @@ impl ReplEntry { } pub(crate) const NIX_REPL_LANG_TAG: &str = "nix-repl"; + +#[cfg(test)] +mod test { + use indoc::indoc; + use pretty_assertions::assert_eq; + + use crate::repl::example::ReplExampleEntries; + + use super::{ExpectedResult, ReplEntry, ReplQuery}; + + #[derive(PartialEq, Eq, Debug)] + struct Case { + input: &'static str, + expected_output: Vec, + } + + #[derive(Debug, PartialEq, Eq)] + struct Failure { + case: Case, + actual_output: Vec, + } + + macro_rules! parse_success { + ($( + { + input: $input:expr, + expected_output: [$( + { + query: $query:expr, + expected_result: $expected_result:expr, + }, + )*], + }, + )*) => { + #[test] + fn parse_success() { + let cases = vec![$( + Case { + input: $input, + expected_output: vec![$( + ReplEntry::new( + ReplQuery::new($query.parse().unwrap()), + ExpectedResult($expected_result.to_owned()), + ), + )*], + }, + )*]; + + test_parse_success_cases(cases); + } + }; + } + + fn test_parse_success_cases(cases: Vec) { + let failures: Vec = cases + .into_iter() + .filter_map(|case| { + let actual: ReplExampleEntries = case.input.parse().unwrap(); + let actual_output = actual.0; + if actual_output == case.expected_output { + None + } else { + Some(Failure { + case, + actual_output, + }) + } + }) + .collect(); + assert_eq!(failures, vec![]); + } + + parse_success! [ + { + input: indoc! {" + nix-repl> 1 + 1 + 2 + + "}, + expected_output: [ + { + query: "1 + 1\n", + expected_result: "2", + }, + ], + }, + { + input: indoc! {" + nix-repl> a = 1 + + "}, + expected_output: [ + { + query: "a = 1\n", + expected_result: "", + }, + ], + }, + { + input: indoc! {r#" + nix-repl> 1 + 1 + 2 + + nix-repl> "a" + "b" + "ab" + + "#}, + expected_output: [ + { + query: "1 + 1\n", + expected_result: "2", + }, + { + query: "\"a\" + \"b\"\n", + expected_result: "\"ab\"", + }, + ], + }, + { + input: indoc! {r#" + nix-repl> b = "b" + + nix-repl> 1 + 1 + + "#}, + expected_output: [ + { + query: "b = \"b\"\n", + expected_result: "", + }, + { + query: "1\n", + expected_result: "1", + }, + ], + }, + ]; +}