-
Notifications
You must be signed in to change notification settings - Fork 490
/
test_config.rs
182 lines (175 loc) · 6.26 KB
/
test_config.rs
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
use cairo_felt::Felt252;
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_syntax::attribute::structured::{Attribute, AttributeArg, AttributeArgVariant};
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::{ast, TypedSyntaxNode};
use cairo_lang_utils::OptionHelper;
use num_traits::ToPrimitive;
use serde::{Deserialize, Serialize};
use super::{AVAILABLE_GAS_ATTR, IGNORE_ATTR, SHOULD_PANIC_ATTR, TEST_ATTR};
use crate::STATIC_GAS_ARG;
/// Expectation for a panic case.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub enum PanicExpectation {
/// Accept any panic value.
Any,
/// Accept only this specific vector of panics.
Exact(Vec<Felt252>),
}
/// Expectation for a result of a test.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub enum TestExpectation {
/// Running the test should not panic.
Success,
/// Running the test should result in a panic.
Panics(PanicExpectation),
}
/// The configuration for running a single test.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub struct TestConfig {
/// The amount of gas the test requested.
pub available_gas: Option<usize>,
/// The expected result of the run.
pub expectation: TestExpectation,
/// Should the test be ignored.
pub ignored: bool,
}
/// Extracts the configuration of a tests from attributes, or returns the diagnostics if the
/// attributes are set illegally.
pub fn try_extract_test_config(
db: &dyn SyntaxGroup,
attrs: Vec<Attribute>,
) -> Result<Option<TestConfig>, Vec<PluginDiagnostic>> {
let test_attr = attrs.iter().find(|attr| attr.id.as_str() == TEST_ATTR);
let ignore_attr = attrs.iter().find(|attr| attr.id.as_str() == IGNORE_ATTR);
let available_gas_attr = attrs.iter().find(|attr| attr.id.as_str() == AVAILABLE_GAS_ATTR);
let should_panic_attr = attrs.iter().find(|attr| attr.id.as_str() == SHOULD_PANIC_ATTR);
let mut diagnostics = vec![];
if let Some(attr) = test_attr {
if !attr.args.is_empty() {
diagnostics.push(PluginDiagnostic {
stable_ptr: attr.id_stable_ptr.untyped(),
message: "Attribute should not have arguments.".into(),
});
}
} else {
for attr in [ignore_attr, available_gas_attr, should_panic_attr].into_iter().flatten() {
diagnostics.push(PluginDiagnostic {
stable_ptr: attr.id_stable_ptr.untyped(),
message: "Attribute should only appear on tests.".into(),
});
}
}
let ignored = if let Some(attr) = ignore_attr {
if !attr.args.is_empty() {
diagnostics.push(PluginDiagnostic {
stable_ptr: attr.id_stable_ptr.untyped(),
message: "Attribute should not have arguments.".into(),
});
}
true
} else {
false
};
let available_gas = extract_available_gas(available_gas_attr, db, &mut diagnostics);
let (should_panic, expected_panic_value) = if let Some(attr) = should_panic_attr {
if attr.args.is_empty() {
(true, None)
} else {
(
true,
extract_panic_values(db, attr).on_none(|| {
diagnostics.push(PluginDiagnostic {
stable_ptr: attr.args_stable_ptr.untyped(),
message: "Expected panic must be of the form `expected: <tuple of \
felt252s>`."
.into(),
});
}),
)
}
} else {
(false, None)
};
if !diagnostics.is_empty() {
return Err(diagnostics);
}
Ok(if test_attr.is_none() {
None
} else {
Some(TestConfig {
available_gas,
expectation: if should_panic {
TestExpectation::Panics(if let Some(values) = expected_panic_value {
PanicExpectation::Exact(values)
} else {
PanicExpectation::Any
})
} else {
TestExpectation::Success
},
ignored,
})
})
}
/// Extract the available gas from the attribute.
fn extract_available_gas(
available_gas_attr: Option<&Attribute>,
db: &dyn SyntaxGroup,
diagnostics: &mut Vec<PluginDiagnostic>,
) -> Option<usize> {
let Some(attr) = available_gas_attr else {
// If no gas is specified, we assume the reasonably large possible gas, such that inifinte
// loops will run out of gas.
return Some(u32::MAX as usize);
};
match &attr.args[..] {
[
AttributeArg {
variant: AttributeArgVariant::Unnamed { value: ast::Expr::Literal(literal), .. },
..
},
] => literal.numeric_value(db).unwrap_or_default().to_usize(),
[
AttributeArg {
variant: AttributeArgVariant::Unnamed { value: ast::Expr::Path(path), .. },
..
},
] if path.as_syntax_node().get_text_without_trivia(db) == STATIC_GAS_ARG => None,
_ => {
diagnostics.push(PluginDiagnostic {
stable_ptr: attr.id_stable_ptr.untyped(),
message: format!(
"Attribute should have a single value argument or `{STATIC_GAS_ARG}`."
),
});
None
}
}
}
/// Tries to extract the relevant expected panic values.
fn extract_panic_values(db: &dyn SyntaxGroup, attr: &Attribute) -> Option<Vec<Felt252>> {
let [AttributeArg { variant: AttributeArgVariant::Named { name, value: panics, .. }, .. }] =
&attr.args[..]
else {
return None;
};
if name != "expected" {
return None;
}
let ast::Expr::Tuple(panics) = panics else { return None };
panics
.expressions(db)
.elements(db)
.into_iter()
.map(|value| match value {
ast::Expr::Literal(literal) => {
Some(literal.numeric_value(db).unwrap_or_default().into())
}
ast::Expr::ShortString(literal) => {
Some(literal.numeric_value(db).unwrap_or_default().into())
}
_ => None,
})
.collect::<Option<Vec<_>>>()
}