Skip to content

Commit

Permalink
Refactor test framework code.
Browse files Browse the repository at this point in the history
  • Loading branch information
hdwalters committed Dec 16, 2024
1 parent 79fa51c commit cdec3c9
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, Message> {
fn eval_amber(code: &str) -> Result<String, Message> {
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)
}
}
}
}
Expand All @@ -73,7 +75,7 @@ pub fn eval_bash<T: Into<String>>(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>) -> String {
code.into()
.lines()
Expand All @@ -84,21 +86,22 @@ fn extract_output(code: impl Into<String>) -> 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);
Expand Down

0 comments on commit cdec3c9

Please sign in to comment.