microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
compiler/qsc_passes/src/entry_point/tests.rs
104lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #![allow(clippy::needless_raw_string_hashes)] |
| 5 | |
| 6 | use crate::entry_point::generate_entry_expr; |
| 7 | use expect_test::{expect, Expect}; |
| 8 | use indoc::indoc; |
| 9 | use qsc_frontend::compile::{self, compile, PackageStore, RuntimeCapabilityFlags, SourceMap}; |
| 10 | |
| 11 | fn check(file: &str, expr: &str, expect: &Expect) { |
| 12 | let sources = SourceMap::new([("test".into(), file.into())], Some(expr.into())); |
| 13 | let mut unit = compile( |
| 14 | &PackageStore::new(compile::core()), |
| 15 | &[], |
| 16 | sources, |
| 17 | RuntimeCapabilityFlags::all(), |
| 18 | ); |
| 19 | assert!(unit.errors.is_empty(), "{:?}", unit.errors); |
| 20 | |
| 21 | let errors = generate_entry_expr(&mut unit.package, &mut unit.assigner); |
| 22 | if errors.is_empty() { |
| 23 | expect.assert_eq( |
| 24 | &unit |
| 25 | .package |
| 26 | .entry |
| 27 | .expect("entry should be present in success case") |
| 28 | .to_string(), |
| 29 | ); |
| 30 | } else { |
| 31 | expect.assert_debug_eq(&errors); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[test] |
| 36 | fn test_entry_point_attr_to_expr() { |
| 37 | check( |
| 38 | indoc! {" |
| 39 | namespace Test { |
| 40 | @EntryPoint() |
| 41 | operation Main() : Int { 41 + 1 } |
| 42 | }"}, |
| 43 | "", |
| 44 | &expect![[r#" |
| 45 | Expr 12 [40-73] [Type Int]: Call: |
| 46 | Expr 11 [40-73] [Type Int]: Var: Item 1 |
| 47 | Expr 10 [40-73] [Type Unit]: Unit"#]], |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn test_entry_point_attr_missing() { |
| 53 | check( |
| 54 | indoc! {" |
| 55 | namespace Test { |
| 56 | operation Main() : Int { 41 + 1 } |
| 57 | }"}, |
| 58 | "", |
| 59 | &expect![[r#" |
| 60 | [ |
| 61 | EntryPoint( |
| 62 | NotFound, |
| 63 | ), |
| 64 | ] |
| 65 | "#]], |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn test_entry_point_attr_multiple() { |
| 71 | check( |
| 72 | indoc! {" |
| 73 | namespace Test { |
| 74 | @EntryPoint() |
| 75 | operation Main() : Int { 41 + 1 } |
| 76 | |
| 77 | @EntryPoint() |
| 78 | operation Main2() : Int { 40 + 1 } |
| 79 | }"}, |
| 80 | "", |
| 81 | &expect![[r#" |
| 82 | [ |
| 83 | EntryPoint( |
| 84 | Duplicate( |
| 85 | "Main", |
| 86 | Span { |
| 87 | lo: 50, |
| 88 | hi: 54, |
| 89 | }, |
| 90 | ), |
| 91 | ), |
| 92 | EntryPoint( |
| 93 | Duplicate( |
| 94 | "Main2", |
| 95 | Span { |
| 96 | lo: 107, |
| 97 | hi: 112, |
| 98 | }, |
| 99 | ), |
| 100 | ), |
| 101 | ] |
| 102 | "#]], |
| 103 | ); |
| 104 | } |
| 105 | |