Skip to content

Commit

Permalink
Merge pull request #106 from mobusoperandi/repl-parse-unit-tests
Browse files Browse the repository at this point in the history
test: some repl example unit tests
  • Loading branch information
ubbabeck authored Jun 1, 2024
2 parents 36700a6 + a4811e8 commit 4838493
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
141 changes: 140 additions & 1 deletion src/repl/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<ReplEntry>,
}

#[derive(Debug, PartialEq, Eq)]
struct Failure {
case: Case,
actual_output: Vec<ReplEntry>,
}

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<Case>) {
let failures: Vec<Failure> = 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",
},
],
},
];
}

0 comments on commit 4838493

Please sign in to comment.