-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a pass to detect test attribute errors and report them nicely
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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("This callable has parameters. Tests cannot have parameters.")] | ||
CallableHasParameters(#[label] Span), | ||
#[error("This callable has type parameters. Tests 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.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,79 @@ | ||
// 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: 33, | ||
hi: 66, | ||
}, | ||
), | ||
] | ||
"#]], | ||
); | ||
} |