-
Notifications
You must be signed in to change notification settings - Fork 3
/
basic.rs
66 lines (58 loc) · 1.93 KB
/
basic.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
#[cfg(test)]
mod tests {
use eyre::Result;
use solc_ast::{ast::*, visitor::ast_visitor::*};
fn read_counter() -> Result<SourceUnit> {
Ok(serde_json::from_reader(std::io::BufReader::new(
std::fs::File::open("tests/ast-json/Counter.json")?,
))?)
}
#[test]
fn deserialize_counter() -> Result<()> {
let source_unit = read_counter()?;
assert_eq!(source_unit.absolute_path, Some("Counter.sol".into()));
Ok(())
}
#[derive(Default, Debug)]
struct FunctionDefinitionCollector {
names: Vec<String>,
}
impl ASTConstVisitor for FunctionDefinitionCollector {
fn end_visit_function_definition(&mut self, node: &FunctionDefinition) -> Result<()> {
self.names.push(node.name.clone());
Ok(())
}
}
#[test]
fn functions() -> Result<()> {
let source_unit = read_counter()?;
let mut function_definition_collector = FunctionDefinitionCollector::default();
source_unit.accept(&mut function_definition_collector)?;
assert_eq!(
function_definition_collector.names,
vec![String::from("setNumber"), String::from("increment")]
);
Ok(())
}
#[derive(Default, Debug)]
struct VariableNameCollector {
names: Vec<String>,
}
impl ASTConstVisitor for VariableNameCollector {
fn end_visit_variable_declaration(&mut self, node: &VariableDeclaration) -> Result<()> {
self.names.push(node.name.clone());
Ok(())
}
}
#[test]
fn variables() -> Result<()> {
let source_unit = read_counter()?;
let mut variable_name_collector = VariableNameCollector::default();
source_unit.accept(&mut variable_name_collector)?;
assert_eq!(
variable_name_collector.names,
vec![String::from("number"), String::from("newNumber")]
);
Ok(())
}
}