Skip to content

Commit

Permalink
add tests for issue 14 examples
Browse files Browse the repository at this point in the history
  • Loading branch information
coriolinus committed Aug 7, 2022
1 parent 4a839a9 commit cf8f230
Showing 1 changed file with 58 additions and 19 deletions.
77 changes: 58 additions & 19 deletions tests/shell_mode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use calc::Context;
use calc::{types::Calcable, Context};
use lazy_static::lazy_static;
use regex::Regex;

lazy_static! {
static ref INPUT_RE: Regex = Regex::new(r"^\[\d+\]: (.*)$").unwrap();
static ref INPUT_RE: Regex = Regex::new(r"^\s*\[\d+\]: (.*)$").unwrap();
}

#[derive(Debug)]
Expand All @@ -28,15 +28,24 @@ fn parse_expressions(input: &str) -> Vec<ShellCase> {
.expect("capture group 1 always exists")
.as_str()
.to_owned();
let expect = lines.next().expect("each input has an output").to_owned();
let expect = lines
.next()
.expect("each input has an output")
.trim()
.to_owned();
out.push(ShellCase { input, expect });
}

out
}

fn assert_expressions(expressions: &[ShellCase]) {
let mut context = Context::<f64>::default();
fn assert_expressions<N>(expressions: &[ShellCase])
where
N: std::fmt::Debug + Calcable,
<N as Calcable>::Err: 'static,
Context<N>: Default,
{
let mut context = Context::<N>::default();
for ShellCase { input, expect } in expressions {
let result = context.evaluate(&input).unwrap();
assert_eq!(&result.to_string(), expect);
Expand All @@ -46,20 +55,50 @@ fn assert_expressions(expressions: &[ShellCase]) {
#[test]
fn readme_2_shell_mode() {
const CASE: &str = r#"
[0]: 1 + 1
2
[1]: 3*(5/(3-4))
-15
[2]: 3*pi**2
29.608813203268074
[3]: @+1
30.608813203268074
[4]: @@@*2
-30
[5]: ln(-1)
NaN
"#;
[0]: 1 + 1
2
[1]: 3*(5/(3-4))
-15
[2]: 3*pi**2
29.608813203268074
[3]: @+1
30.608813203268074
[4]: @@@*2
-30
[5]: ln(-1)
NaN
"#;

let expressions = parse_expressions(CASE);
assert_expressions::<f64>(&expressions);
}

#[test]
fn issue_14_example_1() {
const CASE: &str = r#"
[1]: 528500/100
5285
[2]: @/2
2642
"#;

let expressions = parse_expressions(CASE);
assert_expressions::<i64>(&expressions);
}

#[test]
fn issue_14_example_2() {
const CASE: &str = r#"
[0]: 2+2*2
6
[1]: @*100
600
[2]: @*2
1200
[3]: @+1
1201
"#;

let expressions = parse_expressions(CASE);
assert_expressions(&expressions);
assert_expressions::<u64>(&expressions);
}

0 comments on commit cf8f230

Please sign in to comment.