-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update test callable discovery to work within the language service (#…
…2095) This PR rewrites #2059 to trigger test discovery from within the language server, as opposed to externally in an entirely separate context. Please read the description in that PR for more detail. It also adds tests for LS state in both the Rust-based LS tests and the JS-based `basics.js` npm API tests. --------- Co-authored-by: Mine Starks <[email protected]>
- Loading branch information
Showing
45 changed files
with
1,461 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use miette::Diagnostic; | ||
use qsc_data_structures::span::Span; | ||
use qsc_hir::{hir::Attr, visit::Visitor}; | ||
use thiserror::Error; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
#[derive(Clone, Debug, Diagnostic, Error)] | ||
pub enum TestAttributeError { | ||
#[error("test callables cannot take arguments")] | ||
CallableHasParameters(#[label] Span), | ||
#[error("test callables cannot have type parameters")] | ||
CallableHasTypeParameters(#[label] Span), | ||
} | ||
|
||
pub(crate) fn validate_test_attributes( | ||
package: &mut qsc_hir::hir::Package, | ||
) -> Vec<TestAttributeError> { | ||
let mut validator = TestAttributeValidator { errors: Vec::new() }; | ||
validator.visit_package(package); | ||
validator.errors | ||
} | ||
|
||
struct TestAttributeValidator { | ||
errors: Vec<TestAttributeError>, | ||
} | ||
|
||
impl<'a> Visitor<'a> for TestAttributeValidator { | ||
fn visit_callable_decl(&mut self, decl: &'a qsc_hir::hir::CallableDecl) { | ||
if decl.attrs.iter().any(|attr| matches!(attr, Attr::Test)) { | ||
if !decl.generics.is_empty() { | ||
self.errors | ||
.push(TestAttributeError::CallableHasTypeParameters( | ||
decl.name.span, | ||
)); | ||
} | ||
if decl.input.ty != qsc_hir::ty::Ty::UNIT { | ||
self.errors | ||
.push(TestAttributeError::CallableHasParameters(decl.span)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use expect_test::{expect, Expect}; | ||
use indoc::indoc; | ||
use qsc_data_structures::{language_features::LanguageFeatures, target::TargetCapabilityFlags}; | ||
use qsc_frontend::compile::{self, compile, PackageStore, SourceMap}; | ||
use qsc_hir::{validate::Validator, visit::Visitor}; | ||
|
||
use crate::test_attribute::validate_test_attributes; | ||
|
||
fn check(file: &str, expect: &Expect) { | ||
let store = PackageStore::new(compile::core()); | ||
let sources = SourceMap::new([("test".into(), file.into())], None); | ||
let mut unit = compile( | ||
&store, | ||
&[], | ||
sources, | ||
TargetCapabilityFlags::all(), | ||
LanguageFeatures::default(), | ||
); | ||
assert!(unit.errors.is_empty(), "{:?}", unit.errors); | ||
|
||
let errors = validate_test_attributes(&mut unit.package); | ||
Validator::default().visit_package(&unit.package); | ||
if errors.is_empty() { | ||
expect.assert_eq(&unit.package.to_string()); | ||
} else { | ||
expect.assert_debug_eq(&errors); | ||
} | ||
} | ||
|
||
#[test] | ||
fn callable_cant_have_params() { | ||
check( | ||
indoc! {" | ||
namespace test { | ||
@Test() | ||
operation A(q : Qubit) : Unit { | ||
} | ||
} | ||
"}, | ||
&expect![[r#" | ||
[ | ||
CallableHasParameters( | ||
Span { | ||
lo: 33, | ||
hi: 71, | ||
}, | ||
), | ||
] | ||
"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn callable_cant_have_type_params() { | ||
check( | ||
indoc! {" | ||
namespace test { | ||
@Test() | ||
operation A<'T>() : Unit { | ||
} | ||
} | ||
"}, | ||
&expect![[r#" | ||
[ | ||
CallableHasTypeParameters( | ||
Span { | ||
lo: 43, | ||
hi: 44, | ||
}, | ||
), | ||
] | ||
"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn conditionally_compile_out_test() { | ||
check( | ||
indoc! {" | ||
namespace test { | ||
@Test() | ||
@Config(Base) | ||
operation A<'T>() : Unit { | ||
} | ||
} | ||
"}, | ||
&expect![[r#" | ||
Package: | ||
Item 0 [0-86] (Public): | ||
Namespace (Ident 0 [10-14] "test"): <empty>"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn callable_is_valid_test_callable() { | ||
check( | ||
indoc! {" | ||
namespace test { | ||
@Test() | ||
operation A() : Unit { | ||
} | ||
} | ||
"}, | ||
&expect![[r#" | ||
Package: | ||
Item 0 [0-64] (Public): | ||
Namespace (Ident 5 [10-14] "test"): Item 1 | ||
Item 1 [21-62] (Internal): | ||
Parent: 0 | ||
Test | ||
Callable 0 [33-62] (operation): | ||
name: Ident 1 [43-44] "A" | ||
input: Pat 2 [44-46] [Type Unit]: Unit | ||
output: Unit | ||
functors: empty set | ||
body: SpecDecl 3 [33-62]: Impl: | ||
Block 4 [54-62]: <empty> | ||
adj: <none> | ||
ctl: <none> | ||
ctl-adj: <none>"#]], | ||
); | ||
} |
Oops, something went wrong.