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
//! ink! event IR.
use ra_ap_syntax::ast;
use crate::Topic;
/// An ink! event.
#[ink_analyzer_macro::entity(arg_kind = Event)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Event {
// ASTNode type.
ast: ast::Struct,
// ink! topics.
topics: Vec<Topic>,
}
impl_ast_type_trait!(Event, IsInkStruct);
impl_is_ink_event!(Event);
impl Event {
impl_pub_ink_arg_getter!(anonymous_arg, Anonymous, anonymous);
impl_pub_ink_arg_getter!(signature_arg, SignatureTopic, signature_topic);
impl_pub_ink_arg_getter!(name_arg, Name, name);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::*;
use crate::traits::{InkEntity, IsInkStruct};
use test_utils::quote_as_str;
#[test]
fn cast_works() {
for (code, is_anonymous, signature, name, expected_n_topics) in [
(
quote_as_str! {
#[ink(event)]
pub struct MyEvent {}
},
false,
None,
None,
0,
),
(
quote_as_str! {
#[ink(event, anonymous)]
pub struct MyEvent {}
},
true,
None,
None,
0,
),
(
quote_as_str! {
#[ink(event)]
#[ink(anonymous)]
pub struct MyEvent {}
},
true,
None,
None,
0,
),
(
quote_as_str! {
#[ink(event, signature_topic = "1111111111111111111111111111111111111111111111111111111111111111")]
pub struct MyEvent {}
},
false,
Some("1111111111111111111111111111111111111111111111111111111111111111"),
None,
0,
),
(
quote_as_str! {
#[ink(event, name = "Event")]
pub struct MyEvent {}
},
false,
None,
Some("Event"),
0,
),
(
quote_as_str! {
#[ink(event)]
pub struct MyEvent {
#[ink(topic)]
value: i32,
}
},
false,
None,
None,
1,
),
(
quote_as_str! {
#[ink(event)]
pub struct MyEvent {
#[ink(topic)]
value: i32,
#[ink(topic)]
value2: bool,
}
},
false,
None,
None,
2,
),
] {
let node = parse_first_syntax_node(code);
let event = Event::cast(node).unwrap();
// `anonymous` argument exists.
assert_eq!(event.anonymous_arg().is_some(), is_anonymous);
// `signature_topic` argument exists.
assert_eq!(event.signature_arg().is_some(), signature.is_some());
// `signature_topic` argument value.
assert_eq!(
event
.signature_arg()
.and_then(|arg| arg.value()?.as_string())
.as_deref(),
signature
);
// `name` argument value.
assert_eq!(
event
.name_arg()
.and_then(|arg| arg.value()?.as_string())
.as_deref(),
name
);
// Checks the expected number of topics.
assert_eq!(event.topics().len(), expected_n_topics);
// `struct` item exists.
assert!(event.struct_item().is_some());
}
}
}