testing improvements

This commit is contained in:
Simon Gardling 2022-04-26 08:51:17 -04:00
parent 2c9fc4c5d8
commit a90a07530a

View File

@ -247,7 +247,7 @@ mod tests {
use crate::suggestions::SUPPORTED_FUNCTIONS; use crate::suggestions::SUPPORTED_FUNCTIONS;
use std::collections::HashMap; use std::collections::HashMap;
/// returns if function with string `func_str` is valid after processing through [`process_func_str`] /// Returns if function with string `func_str` is valid after processing through [`process_func_str`]
fn func_is_valid(func_str: &str) -> bool { fn func_is_valid(func_str: &str) -> bool {
BackingFunction::new(&process_func_str(func_str)).is_ok() BackingFunction::new(&process_func_str(func_str)).is_ok()
} }
@ -255,60 +255,52 @@ mod tests {
/// Used for testing: passes function to [`process_func_str`] before running [`test_func`]. if `expect_valid` == `true`, it expects no errors to be created. /// Used for testing: passes function to [`process_func_str`] before running [`test_func`]. if `expect_valid` == `true`, it expects no errors to be created.
fn test_func_helper(func_str: &str, expect_valid: bool) { fn test_func_helper(func_str: &str, expect_valid: bool) {
let is_valid = func_is_valid(func_str); let is_valid = func_is_valid(func_str);
println!( let string = format!(
"function: {} (expected: {}, got: {})", "function: {} (expected: {}, got: {})",
func_str, expect_valid, is_valid func_str, expect_valid, is_valid
); );
assert!(is_valid == expect_valid); if is_valid == expect_valid {
println!("{}", string);
} else {
panic!("{}", string);
}
} }
/// Tests to make sure functions that are expected to succeed, succeed. /// Tests to make sure functions that are expected to succeed, succeed.
#[test] #[test]
fn test_expected_func_successes() { fn test_expected() {
let functions = vec![ let values = HashMap::from([
"x^2", ("", true),
"2x", ("x^2", true),
"E^x", ("2x", true),
"log10(x)", ("E^x", true),
"xxxxx", // test variables side-by-side ("log10(x)", true),
"sin(x)", ("xxxxx", true),
"xsin(x)", // Tests `x{letter}` pattern ("sin(x)", true),
"sin(x)cos(x)", // Tests `){letter}` pattern ("xsin(x)", true),
"x/0", // always returns NaN ("sin(x)cos(x)", true),
"(x+1)(x-3)", // tests 2 parentheses in `)(` pattern ("x/0", true),
"(2x+1)x", ("(x+1)(x-3)", true),
"(2x+1)pi", ("(2x+1)x", true),
"pi(2x+1)", ("(2x+1)pi", true),
"pipipipipipix", ("pi(2x+1)", true),
"e^sin(x)", ("pipipipipipix", true),
"E^sin(x)", ("e^sin(x)", true),
"e^x", ("E^sin(x)", true),
"x**2", ("e^x", true),
]; ("x**2", true),
("a", false),
("log222(x)", false),
("abcdef", false),
("log10(x", false),
("x^a", false),
("sin(cos(x)))", false),
("0/0", false),
]);
for func_str in functions.iter().cloned() { for (key, value) in values {
test_func_helper(func_str, true); test_func_helper(key, value);
}
}
/// Tests to make sure functions that are expected to fail, fail.
#[test]
fn test_expected_func_failures() {
let functions = vec![
"a", // Invalid variable
"l^2", // Invalid variable
"log222(x)", // Invalid function
"abcdef", // Invalid variables
"log10(x", // unclosed bracket
"x^a", // Invalid variable
"sin(cos(x)))", // extra bracket
"((())", // extra opening bracket
"0/0",
];
for func_str in functions.iter().cloned() {
test_func_helper(func_str, false);
} }
} }