diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 8c9a5392..c295f93a 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,6 +1,6 @@ extern crate test_generator; -use heraclitus_compiler::prelude::Message; use crate::compiler::{AmberCompiler, CompilerOptions}; +use heraclitus_compiler::prelude::Message; use itertools::Itertools; use pretty_assertions::assert_eq; use std::fs; @@ -14,39 +14,41 @@ mod stdlib; mod validity; mod erroring; +const SUCCEEDED: &str = "Succeeded"; + pub enum TestOutcomeTarget { Success, Failure, } -pub fn eval_amber_code(code: &str) -> Result { +fn eval_amber(code: &str) -> Result { let options = CompilerOptions::default(); let mut compiler = AmberCompiler::new(code.to_string(), None, options); compiler.test_eval() } -/// Tests script output in case of success or failure +/// Tests script output in case of success or failure. pub fn test_amber(code: &str, result: &str, target: TestOutcomeTarget) { - let evaluated = eval_amber_code(code); + let evaluated = eval_amber(code); match target { TestOutcomeTarget::Success => match evaluated { Ok(stdout) => { - assert_eq!( - stdout.trim_end_matches('\n'), - result.trim_end_matches('\n'), - ) - }, + let stdout = stdout.trim_end_matches('\n'); + let result = result.trim_end_matches('\n'); + assert_eq!(stdout, result) + } Err(err) => { panic!("ERROR: {}", err.message.unwrap()) - }, + } } TestOutcomeTarget::Failure => match evaluated { Ok(stdout) => { panic!("Expected error, got: {}", stdout) - }, + } Err(err) => { - assert_eq!(err.message.expect("Error message expected"), result) - }, + let message = err.message.expect("Error message expected"); + assert_eq!(message, result) + } } } } @@ -73,7 +75,7 @@ pub fn eval_bash>(code: T) -> (String, String) { ) } -/// Extracts the output from the comment of amber code +/// Extracts the output from the comment of Amber code. fn extract_output(code: impl Into) -> String { code.into() .lines() @@ -84,21 +86,22 @@ fn extract_output(code: impl Into) -> String { .join("\n") } -/// Inner test logic for testing script output in case of success or failure +/// Inner test logic for testing script output in case of success or failure. pub fn script_test(input: &str, target: TestOutcomeTarget) { - let code = - fs::read_to_string(input).unwrap_or_else(|_| panic!("Failed to open {input} test file")); + let code = fs::read_to_string(input) + .unwrap_or_else(|_| panic!("Failed to open {input} test file")); - // extract Output from script comment + // Extract output from script comment. let mut output = extract_output(&code); - // if output is not in comment, try to read from .output.txt file + // If output is not in comment, try to read from .output.txt file. if output.is_empty() { let output_path = PathBuf::from(input.replace(".ab", ".output.txt")); - output = match output_path.exists() { - true => fs::read_to_string(output_path) - .unwrap_or_else(|_| panic!("Failed to open {input}.output.txt file")), - _ => "Succeeded".to_string(), + output = if output_path.exists() { + fs::read_to_string(output_path) + .unwrap_or_else(|_| panic!("Failed to open {input}.output.txt file")) + } else { + SUCCEEDED.to_string() }; } test_amber(&code, &output, target);