1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
//! ink! test diagnostics.
use ink_analyzer_ir::{InkAttributeKind, InkMacroKind, InkTest};
use super::common;
use crate::{Diagnostic, Version};
const SCOPE_NAME: &str = "test";
const ATTR_KIND: InkAttributeKind = InkAttributeKind::Macro(InkMacroKind::Test);
/// Runs all ink! test diagnostics.
///
/// The entry point for finding ink! test semantic rules is the `ink_test` module of the `ink_ir` crate.
///
/// Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/ink_test.rs#L34-L44>.
///
/// Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/ink_test.rs#L27-L30>.
pub fn diagnostics(results: &mut Vec<Diagnostic>, ink_test: &InkTest, version: Version) {
// Runs generic diagnostics, see `utils::run_generic_diagnostics` doc.
common::run_generic_diagnostics(results, ink_test, version);
// Ensures that ink! test is an `fn` item, see `utils::ensure_fn` doc.
// Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/ink_test.rs#L27>.
if let Some(diagnostic) = common::ensure_fn(ink_test, SCOPE_NAME) {
results.push(diagnostic);
}
// Ensures that ink! test has no ink! descendants, see `utils::ensure_no_ink_descendants` doc.
common::ensure_valid_quasi_direct_ink_descendants_by_kind(
results, ink_test, ATTR_KIND, version, SCOPE_NAME,
);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::*;
use crate::Severity;
use ink_analyzer_ir::syntax::{TextRange, TextSize};
use quote::quote;
use test_utils::{
parse_offset_at, quote_as_pretty_string, quote_as_str, TestResultAction,
TestResultTextRange,
};
fn parse_first_ink_test(code: &str) -> InkTest {
parse_first_ink_entity_of_type(code)
}
#[test]
fn fn_works() {
let ink_test = parse_first_ink_test(quote_as_str! {
#[ink::test]
fn it_works() {
}
});
let result = common::ensure_fn(&ink_test, SCOPE_NAME);
assert!(result.is_none());
}
#[test]
fn non_fn_fails() {
for code in [
quote! {
struct MyTest {
}
},
quote! {
enum MyTest {
}
},
quote! {
mod my_test;
},
quote! {
trait MyTest {
}
},
] {
let code = quote_as_pretty_string! {
#[ink::test]
#code
};
let ink_test = parse_first_ink_test(&code);
let result = common::ensure_fn(&ink_test, SCOPE_NAME);
// Verifies diagnostics.
assert!(result.is_some(), "ink test: {code}");
assert_eq!(
result.as_ref().unwrap().severity,
Severity::Error,
"ink test: {code}"
);
// Verifies quickfixes.
let fix = &result.as_ref().unwrap().quickfixes.as_ref().unwrap()[0];
assert!(fix.label.contains("Remove `#[ink::test]`"));
assert!(fix.edits[0].text.is_empty());
assert_eq!(
fix.edits[0].range,
TextRange::new(
TextSize::from(parse_offset_at(&code, Some("<-#[ink::test]")).unwrap() as u32),
TextSize::from(parse_offset_at(&code, Some("#[ink::test]")).unwrap() as u32)
)
);
}
}
#[test]
fn no_ink_descendants_works() {
let ink_test = parse_first_ink_test(quote_as_str! {
#[ink::test]
fn it_works() {
}
});
for version in [Version::V4, Version::V5] {
let mut results = Vec::new();
common::ensure_valid_quasi_direct_ink_descendants_by_kind(
&mut results,
&ink_test,
ATTR_KIND,
version,
SCOPE_NAME,
);
assert!(results.is_empty());
}
}
#[test]
fn ink_descendants_fails() {
let code = quote_as_pretty_string! {
#[ink::test]
fn it_works() {
#[ink(event)]
struct MyEvent {
#[ink(topic)]
value: bool,
}
}
};
let ink_test = parse_first_ink_test(&code);
for version in [Version::V4, Version::V5] {
let mut results = Vec::new();
common::ensure_valid_quasi_direct_ink_descendants_by_kind(
&mut results,
&ink_test,
ATTR_KIND,
version,
SCOPE_NAME,
);
// 2 diagnostics for `event` and `topic`.
assert_eq!(results.len(), 2);
// All diagnostics should be errors.
assert_eq!(
results
.iter()
.filter(|item| item.severity == Severity::Error)
.count(),
2
);
// Verifies quickfixes.
let expected_quickfixes = vec![
vec![
TestResultAction {
label: "Remove `#[ink(event)]`",
edits: vec![TestResultTextRange {
text: "",
start_pat: Some("<-#[ink(event)]"),
end_pat: Some("#[ink(event)]"),
}],
},
TestResultAction {
label: "Remove item",
edits: vec![TestResultTextRange {
text: "",
start_pat: Some("<-#[ink(event)]"),
end_pat: Some("}"),
}],
},
],
vec![TestResultAction {
label: "Remove `#[ink(topic)]`",
edits: vec![TestResultTextRange {
text: "",
start_pat: Some("<-#[ink(topic)]"),
end_pat: Some("#[ink(topic)]"),
}],
}],
];
for (idx, item) in results.iter().enumerate() {
let quickfixes = item.quickfixes.as_ref().unwrap();
verify_actions(&code, quickfixes, &expected_quickfixes[idx]);
}
}
}
#[test]
// Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/macro/src/lib.rs#L824-L841>.
fn compound_diagnostic_works() {
for code in [
quote_as_str! {
// Conventional unit test that works with assertions.
#[ink::test]
fn test1() {
// test code comes here as usual
}
},
quote_as_str! {
// Conventional unit test that returns some Result.
// The test code can make use of operator-`?`.
#[ink::test]
fn test2() -> Result<(), ink_env::Error> {
// test code that returns a Rust Result type
}
},
] {
let ink_test = parse_first_ink_test(code);
let mut results = Vec::new();
diagnostics(&mut results, &ink_test, Version::V4);
assert!(results.is_empty(), "ink test: {code}");
}
}
}