From d01ce4c52690003013dba97941d05676513c87db Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Sat, 1 Jan 2022 03:42:49 +0100 Subject: [PATCH] Created integration tests --- kalk/src/integration_testing.rs | 40 +++++++++++++++++++++++++++++++++ kalk/src/lib.rs | 1 + kalk/src/text_utils.rs | 2 +- tests/basics.kalker | 4 ++++ tests/radix.kalker | 1 + 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 kalk/src/integration_testing.rs create mode 100644 tests/basics.kalker create mode 100644 tests/radix.kalker diff --git a/kalk/src/integration_testing.rs b/kalk/src/integration_testing.rs new file mode 100644 index 0000000..c6d630e --- /dev/null +++ b/kalk/src/integration_testing.rs @@ -0,0 +1,40 @@ +#[cfg(test)] +mod tests { + use std::{fs::File, io::Read, path::PathBuf}; + + use crate::kalk_num::KalkNum; + + fn eval_file(name: &str) -> KalkNum { + let mut path = PathBuf::new(); + path.push(env!("CARGO_MANIFEST_DIR")); + path.push(".."); + path.push("tests"); + path.push(name); + path.set_extension("kalker"); + + let mut file_content = String::new(); + File::open(path) + .unwrap() + .read_to_string(&mut file_content) + .unwrap(); + let mut context = crate::parser::Context::new(); + + crate::parser::eval(&mut context, &file_content, 58) + .unwrap() + .unwrap() + } + + #[test] + fn test_basics() { + let result = eval_file("basics"); + assert_eq!(result.to_string_real(10), "3400"); + assert!(!result.has_imaginary()); + } + + #[test] + fn test_radix() { + let result = eval_file("radix"); + assert_eq!(result.to_string_real(10), "62"); + assert_eq!(result.to_string_imaginary(10, false), "11.3125"); + } +} diff --git a/kalk/src/lib.rs b/kalk/src/lib.rs index f5a471d..fa3c9f4 100644 --- a/kalk/src/lib.rs +++ b/kalk/src/lib.rs @@ -1,5 +1,6 @@ pub mod ast; mod calculus; +mod integration_testing; mod interpreter; mod inverter; pub mod kalk_num; diff --git a/kalk/src/text_utils.rs b/kalk/src/text_utils.rs index 056e7ae..217e9f6 100644 --- a/kalk/src/text_utils.rs +++ b/kalk/src/text_utils.rs @@ -47,7 +47,7 @@ pub fn subscript_to_digits(chars: impl Iterator) -> String { }); } - return regular; + return regular.trim().to_string(); } pub fn digits_to_subscript(chars: impl Iterator) -> String { diff --git a/tests/basics.kalker b/tests/basics.kalker new file mode 100644 index 0000000..d8bc951 --- /dev/null +++ b/tests/basics.kalker @@ -0,0 +1,4 @@ +x = 2 +y = 3 +f(x) = 2x(x - 3)(y + 2) +f(f(x) + y) \ No newline at end of file diff --git a/tests/radix.kalker b/tests/radix.kalker new file mode 100644 index 0000000..35edf3d --- /dev/null +++ b/tests/radix.kalker @@ -0,0 +1 @@ +0b1101.101 + 1101.101_2 + 0o13.5 + 13.5_8 + 11.5 + 0xb.5i \ No newline at end of file