microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
library/tests/src/lib.rs
36lines · 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 test_arrays; |
| 8 | #[cfg(test)] |
| 9 | mod test_convert; |
| 10 | #[cfg(test)] |
| 11 | mod test_measurement; |
| 12 | #[cfg(test)] |
| 13 | mod tests; |
| 14 | |
| 15 | use qsc::{ |
| 16 | interpret::{stateless, GenericReceiver, Value}, |
| 17 | SourceMap, |
| 18 | }; |
| 19 | |
| 20 | /// # Panics |
| 21 | /// |
| 22 | /// Will panic if compilation fails or the result is not the same as expected. |
| 23 | pub fn test_expression(expr: &str, expected: &Value) { |
| 24 | let mut stdout = vec![]; |
| 25 | let mut out = GenericReceiver::new(&mut stdout); |
| 26 | |
| 27 | let sources = SourceMap::new([("test".into(), "".into())], Some(expr.into())); |
| 28 | |
| 29 | let interpreter = stateless::Interpreter::new(true, sources).expect("test should compile"); |
| 30 | let mut eval_ctx = interpreter.new_eval_context(); |
| 31 | let result = eval_ctx |
| 32 | .eval_entry(&mut out) |
| 33 | .expect("test should run successfully"); |
| 34 | |
| 35 | assert_eq!(expected, &result); |
| 36 | } |
| 37 | |