1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
//! Molt Test Harness
//!
//! A Molt test script is a Molt script containing tests of Molt code. Each
//! test is a call of the Molt `test` command provided by the
//! `molt::test_harness` module. The tests are executed in the context of the
//! the application's `molt::Interp` (and so can test application-specific commands).
//!
//! The test harness keeps track of the number of tests executed, and whether they
//! passed, failed, or returned an unexpected error.
//!
//! The `molt-app` tool provides access to the test harness for a standard Molt
//! interpreter:
//!
//! ```bash
//! $ molt test test/all.tcl
//! Molt 0.1.0 -- Test Harness
//!
//! 171 tests, 171 passed, 0 failed, 0 errors
//! ```
//!
//! If a test fails or returns an error, the test harness outputs the details.
//!
//! See the Molt Book (or the Molt test suite) for examples of test scripts.
use crate::{check_args, molt_ok, prelude::Interp, MoltResult, ResultCode, Value};
use std::{env, fs, path::PathBuf};
/// Executes the Molt test harness, given the command-line arguments,
/// in the context of the given interpreter.
///
///
/// The first element of the `args` array must be the name of the test script
/// to execute. The remaining elements are meant to be test harness options,
/// but are currently ignored.
///
/// See [`molt::interp`](../molt/interp/index.html) for details on how to configure and
/// add commands to a Molt interpreter.
///
/// # Example
///
/// ```
/// use molt::Interp;
/// use std::env;
///
/// // FIRST, get the command line arguments.
/// let args: Vec<String> = env::args().collect();
///
/// // NEXT, create and initialize the interpreter.
/// let mut interp = Interp::new();
///
/// // NOTE: commands can be added to the interpreter here.
///
/// // NEXT, evaluate the file, if any.
/// if args.len() > 1 {
/// molt::test_harness(&mut interp, &args[1..]);
/// } else {
/// eprintln!("Usage: mytest *filename.tcl");
/// }
/// ```
pub fn test_harness<Ctx>(
interp: &mut Interp<(Ctx, TestCtx)>,
args: &[String],
) -> Result<(), ()> {
// FIRST, announce who we are.
println!("Molt {} -- Test Harness", env!("CARGO_PKG_VERSION"));
// NEXT, get the script file name
if args.is_empty() {
eprintln!("missing test script");
return Err(());
}
let path = PathBuf::from(&args[0]);
// NEXT, install the test commands into the interpreter.
// interp.add_command("test", test_cmd);
// NEXT, execute the script.
match fs::read_to_string(&args[0]) {
Ok(script) => {
if let Some(parent) = path.parent() {
let _ = env::set_current_dir(parent);
}
if let Err(exception) = interp.eval(&script) {
if exception.code() == ResultCode::Error {
eprintln!("{}", exception.value());
return Err(());
} else {
eprintln!("Unexpected eval return: {:?}", exception);
return Err(());
}
}
}
Err(e) => {
println!("{}", e);
return Err(());
}
}
// NEXT, output the test results:
let ctx = &mut interp.context.1;
println!(
"\n{} tests, {} passed, {} failed, {} errors",
ctx.num_tests, ctx.num_passed, ctx.num_failed, ctx.num_errors
);
if ctx.num_failed + ctx.num_errors == 0 {
Ok(())
} else {
Err(())
}
}
pub struct TestCtx {
num_tests: usize,
num_passed: usize,
num_failed: usize,
num_errors: usize,
}
impl TestCtx {
pub fn new() -> Self {
Self {
num_tests: 0,
num_passed: 0,
num_failed: 0,
num_errors: 0,
}
}
}
#[derive(Eq, PartialEq, Debug)]
enum Code {
Ok,
Error,
}
impl std::fmt::Display for Code {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Code::Ok => write!(f, "-ok"),
Code::Error => write!(f, "-error"),
}
}
}
#[derive(Debug)]
struct TestInfo {
name: String,
description: String,
setup: String,
body: String,
cleanup: String,
code: Code,
expect: String,
}
impl TestInfo {
fn new(name: &str, description: &str) -> Self {
Self {
name: name.into(),
description: description.into(),
setup: String::new(),
body: String::new(),
cleanup: String::new(),
code: Code::Ok,
expect: String::new(),
}
}
fn print_failure(&self, got_code: &str, received: &str) {
println!("\n*** FAILED {} {}", self.name, self.description);
println!("Expected {} <{}>", self.code.to_string(), self.expect);
println!("Received {} <{}>", got_code, received);
}
fn print_error(&self, result: &MoltResult) {
println!("\n*** ERROR {} {}", self.name, self.description);
println!("Expected {} <{}>", self.code.to_string(), self.expect);
match result {
Ok(val) => println!("Received -ok <{}>", val),
Err(exception) => match exception.code() {
ResultCode::Error => println!("Received -error <{}>", exception.value()),
ResultCode::Return => {
println!("Received -return <{}>", exception.value())
}
ResultCode::Break => println!("Received -break <>"),
ResultCode::Continue => println!("Received -continue <>"),
_ => unimplemented!(),
},
}
}
fn print_helper_error(&self, part: &str, msg: &str) {
println!("\n*** ERROR (in {}) {} {}", part, self.name, self.description);
println!(" {}", msg);
}
}
/// # test *name* *script* -ok|-error *result*
///
/// Executes the script expecting either a successful response or an error.
///
/// Note: This is an extremely minimal replacement for tcltest; at some
/// point I'll need something much more robust.
///
/// Note: See the Molt Book for the full syntax.
pub fn test_cmd<Ctx>(interp: &mut Interp<(Ctx, TestCtx)>, argv: &[Value]) -> MoltResult {
// FIRST, check the minimum command line.
check_args(1, argv, 4, 0, "name description args...")?;
// NEXT, see which kind of command it is.
let arg = argv[3].as_str();
if arg.starts_with('-') {
fancy_test(interp, argv)
} else {
simple_test(interp, argv)
}
}
// The simple version of the test command.
fn simple_test<Ctx>(interp: &mut Interp<(Ctx, TestCtx)>, argv: &[Value]) -> MoltResult {
check_args(1, argv, 6, 6, "name description script -ok|-error result")?;
// FIRST, get the test info
let mut info = TestInfo::new(argv[1].as_str(), argv[2].as_str());
info.body = argv[3].to_string();
info.expect = argv[5].to_string();
let code = argv[4].as_str();
info.code = if code == "-ok" {
Code::Ok
} else if code == "-error" {
Code::Error
} else {
incr_errors(interp);
info.print_helper_error("test command", &format!("invalid option: \"{}\"", code));
return molt_ok!();
};
// NEXT, run the test.
run_test(interp, &info);
molt_ok!()
}
// The fancier, more flexible version of the test.
fn fancy_test<Ctx>(interp: &mut Interp<(Ctx, TestCtx)>, argv: &[Value]) -> MoltResult {
check_args(1, argv, 4, 0, "name description option value ?option value...?")?;
// FIRST, get the test tinfo
let mut info = TestInfo::new(argv[1].as_str(), argv[2].as_str());
let mut iter = argv[3..].iter();
loop {
let opt = iter.next();
if opt.is_none() {
break;
}
let opt = opt.unwrap().as_str();
let val = iter.next();
if val.is_none() {
incr_errors(interp);
info.print_helper_error(
"test command",
&format!("missing value for {}", opt),
);
return molt_ok!();
}
let val = val.unwrap().as_str();
match opt {
"-setup" => info.setup = val.to_string(),
"-body" => info.body = val.to_string(),
"-cleanup" => info.cleanup = val.to_string(),
"-ok" => {
info.code = Code::Ok;
info.expect = val.to_string();
}
"-error" => {
info.code = Code::Error;
info.expect = val.to_string();
}
_ => {
incr_errors(interp);
info.print_helper_error(
"test command",
&format!("invalid option: \"{}\"", val),
);
return molt_ok!();
}
}
}
// NEXT, run the test.
run_test(interp, &info);
molt_ok!()
}
// Run the actual test and save the result.
fn run_test<Ctx>(interp: &mut Interp<(Ctx, TestCtx)>, info: &TestInfo) {
// FIRST, push a variable scope; -setup, -body, and -cleanup will share it.
interp.push_scope();
// NEXT, execute the parts of the test.
// Setup
if let Err(exception) = interp.eval(&info.setup) {
if exception.code() == ResultCode::Error {
info.print_helper_error("-setup", exception.value().as_str());
}
}
// if let Err(ResultCode::Error(msg)) = interp.eval(&info.setup) {
// info.print_helper_error("-setup", &msg.to_string());
// }
// Body
let body = Value::from(&info.body);
let result = interp.eval_value(&body);
// Cleanup
if let Err(exception) = interp.eval(&info.cleanup) {
if exception.code() == ResultCode::Error {
info.print_helper_error("-cleanup", exception.value().as_str());
}
}
// if let Err(ResultCode::Error(msg)) = interp.eval(&info.cleanup) {
// info.print_helper_error("-cleanup", &msg.to_string());
// }
// NEXT, pop the scope.
interp.pop_scope();
// NEXT, get the context and save the results.
let ctx = &mut interp.context.1;
ctx.num_tests += 1;
match &result {
Ok(out) => {
if info.code == Code::Ok {
if *out == Value::from(&info.expect) {
ctx.num_passed += 1;
} else {
ctx.num_failed += 1;
info.print_failure("-ok", &out.to_string());
}
return;
}
}
Err(exception) => {
if info.code == Code::Error {
if exception.value() == Value::from(&info.expect) {
ctx.num_passed += 1;
} else {
ctx.num_failed += 1;
info.print_failure("-error", exception.value().as_str());
}
return;
}
}
}
ctx.num_errors += 1;
info.print_error(&result);
}
// Increment the failure counter.
fn incr_errors<Ctx>(interp: &mut Interp<(Ctx, TestCtx)>) {
interp.context.1.num_errors += 1;
}