microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/src/tests.rs
59lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use qsc::interpret::{output::CursorReceiver, Error}; |
| 5 | use std::{ |
| 6 | env, fs, |
| 7 | io::Cursor, |
| 8 | path::{Path, PathBuf}, |
| 9 | }; |
| 10 | |
| 11 | fn test_cases_dir() -> PathBuf { |
| 12 | env::current_dir() |
| 13 | .expect("test should have current directory") |
| 14 | .join("test_cases") |
| 15 | } |
| 16 | |
| 17 | fn run_check_solution(solution: &str, verification: &str) -> Result<bool, Vec<Error>> { |
| 18 | let mut cursor = Cursor::new(Vec::new()); |
| 19 | let mut receiver = CursorReceiver::new(&mut cursor); |
| 20 | let result = crate::check_solution( |
| 21 | vec![ |
| 22 | ("solution".into(), solution.into()), |
| 23 | ("verification".into(), verification.into()), |
| 24 | ], |
| 25 | &mut receiver, |
| 26 | ); |
| 27 | println!("{}", receiver.dump()); |
| 28 | result |
| 29 | } |
| 30 | |
| 31 | fn test_check_solution( |
| 32 | solution_source: impl AsRef<Path>, |
| 33 | verification_source: impl AsRef<Path>, |
| 34 | expected_result: bool, |
| 35 | ) { |
| 36 | let solution = fs::read_to_string(solution_source).expect("solution file should be readable"); |
| 37 | let verification = |
| 38 | fs::read_to_string(verification_source).expect("verification file should be readable"); |
| 39 | let result = |
| 40 | run_check_solution(&solution, &verification).expect("exercise should run successfully"); |
| 41 | assert!( |
| 42 | result == expected_result, |
| 43 | "exercise result is different than expected" |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn test_check_solution_is_correct() { |
| 49 | let solution_source = test_cases_dir().join("apply_x").join("Correct.qs"); |
| 50 | let verification_source = test_cases_dir().join("apply_x").join("Verification.qs"); |
| 51 | test_check_solution(solution_source, verification_source, true); |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn test_check_solution_is_incorrect() { |
| 56 | let solution_source = test_cases_dir().join("apply_x").join("Incorrect.qs"); |
| 57 | let verification_source = test_cases_dir().join("apply_x").join("Verification.qs"); |
| 58 | test_check_solution(solution_source, verification_source, false); |
| 59 | } |
| 60 | |