Run the file; this searches for annotations like `; run: %fn0(42)` or `; test: %fn0(42) == 2` and executes them, performing any test comparisons if necessary.
(&self)
| 98 | /// Run the file; this searches for annotations like `; run: %fn0(42)` or |
| 99 | /// `; test: %fn0(42) == 2` and executes them, performing any test comparisons if necessary. |
| 100 | pub fn run(&self) -> Result<(), FileInterpreterFailure> { |
| 101 | // parse file |
| 102 | let test = parse_test(&self.contents, ParseOptions::default()) |
| 103 | .map_err(|e| FileInterpreterFailure::ParsingClif(self.path(), e))?; |
| 104 | |
| 105 | // collect functions |
| 106 | let mut env = FunctionStore::default(); |
| 107 | let mut commands = vec![]; |
| 108 | for (func, details) in test.functions.iter() { |
| 109 | for comment in &details.comments { |
| 110 | if let Some(command) = parse_run_command(comment.text, &func.signature) |
| 111 | .map_err(|e| FileInterpreterFailure::ParsingClif(self.path(), e))? |
| 112 | { |
| 113 | commands.push(command); |
| 114 | } |
| 115 | } |
| 116 | // Note: func.name may truncate the function name |
| 117 | env.add(func.name.to_string(), func); |
| 118 | } |
| 119 | |
| 120 | // Run assertion commands |
| 121 | for command in commands { |
| 122 | command |
| 123 | .run(|func_name, args| { |
| 124 | // Because we have stored function names with a leading %, we need to re-add it. |
| 125 | let func_name = &format!("%{func_name}"); |
| 126 | let state = InterpreterState::default().with_function_store(env.clone()); |
| 127 | match Interpreter::new(state).call_by_name(func_name, args) { |
| 128 | Ok(ControlFlow::Return(results)) => Ok(results.to_vec()), |
| 129 | Ok(ControlFlow::Trap(trap)) => Err(trap.to_string()), |
| 130 | Ok(_) => panic!("Unexpected returned control flow--this is likely a bug."), |
| 131 | Err(t) => Err(t.to_string()), |
| 132 | } |
| 133 | }) |
| 134 | .map_err(|s| FileInterpreterFailure::FailedExecution(s))?; |
| 135 | } |
| 136 | |
| 137 | Ok(()) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// Possible sources of failure in this file. |