microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/src/lib.rs
43lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #![warn(clippy::mod_module_files, clippy::pedantic, clippy::unwrap_used)] |
| 5 | |
| 6 | #[cfg(test)] |
| 7 | mod tests; |
| 8 | |
| 9 | use qsc::{ |
| 10 | interpret::{ |
| 11 | output::Receiver, |
| 12 | stateless::{self, Interpreter}, |
| 13 | Value, |
| 14 | }, |
| 15 | SourceContents, SourceMap, SourceName, |
| 16 | }; |
| 17 | |
| 18 | pub const EXAMPLE_ENTRY: &str = "Kata.RunExample()"; |
| 19 | |
| 20 | pub const EXERCISE_ENTRY: &str = "Kata.Verification.CheckSolution()"; |
| 21 | |
| 22 | /// # Errors |
| 23 | /// |
| 24 | /// Returns a vector of errors if compilation or evaluation failed. |
| 25 | /// |
| 26 | /// # Panics |
| 27 | /// |
| 28 | /// Will panic if evaluation does not return a boolean as result. |
| 29 | pub fn check_solution( |
| 30 | exercise_sources: Vec<(SourceName, SourceContents)>, |
| 31 | receiver: &mut impl Receiver, |
| 32 | ) -> Result<bool, Vec<stateless::Error>> { |
| 33 | let source_map = SourceMap::new(exercise_sources, Some(EXERCISE_ENTRY.into())); |
| 34 | let interpreter: Interpreter = Interpreter::new(true, source_map)?; |
| 35 | let mut eval_ctx = interpreter.new_eval_context(); |
| 36 | eval_ctx.eval_entry(receiver).map(|value| { |
| 37 | if let Value::Bool(success) = value { |
| 38 | success |
| 39 | } else { |
| 40 | panic!("exercise verification did not return a boolean") |
| 41 | } |
| 42 | }) |
| 43 | } |
| 44 | |