Skip to main content

cairo_lang_test_plugin/
plugin.rs

1use cairo_lang_defs::plugin::{MacroPlugin, MacroPluginMetadata, PluginResult};
2use cairo_lang_syntax::attribute::structured::AttributeListStructurize;
3use cairo_lang_syntax::node::ast;
4use cairo_lang_syntax::node::db::SyntaxGroup;
5
6use super::{AVAILABLE_GAS_ATTR, IGNORE_ATTR, SHOULD_PANIC_ATTR, TEST_ATTR};
7use crate::test_config::try_extract_test_config;
8
9/// Plugin to create diagnostics for tests attributes.
10#[derive(Debug, Default)]
11#[non_exhaustive]
12pub struct TestPlugin;
13
14impl MacroPlugin for TestPlugin {
15    fn generate_code(
16        &self,
17        db: &dyn SyntaxGroup,
18        item_ast: ast::ModuleItem,
19        _metadata: &MacroPluginMetadata<'_>,
20    ) -> PluginResult {
21        PluginResult {
22            code: None,
23            diagnostics: if let ast::ModuleItem::FreeFunction(free_func_ast) = item_ast {
24                try_extract_test_config(db, free_func_ast.attributes(db).structurize(db)).err()
25            } else {
26                None
27            }
28            .unwrap_or_default(),
29            remove_original_item: false,
30        }
31    }
32
33    fn declared_attributes(&self) -> Vec<String> {
34        vec![
35            TEST_ATTR.to_string(),
36            AVAILABLE_GAS_ATTR.to_string(),
37            SHOULD_PANIC_ATTR.to_string(),
38            IGNORE_ATTR.to_string(),
39        ]
40    }
41}