Skip to content

Commit

Permalink
Add snapshot tests for module level events
Browse files Browse the repository at this point in the history
  • Loading branch information
Maltby committed Feb 6, 2022
1 parent 11f9aed commit f77dcec
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 9 deletions.
17 changes: 8 additions & 9 deletions crates/analyzer/tests/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ test_analysis! { create_contract, "features/create_contract.fe"}
test_analysis! { create_contract_from_init, "features/create_contract_from_init.fe"}
test_analysis! { empty, "features/empty.fe"}
test_analysis! { events, "features/events.fe"}
test_analysis! { module_level_events, "features/module_level_events.fe"}
test_analysis! { external_contract, "features/external_contract.fe"}
test_analysis! { for_loop_with_break, "features/for_loop_with_break.fe"}
test_analysis! { for_loop_with_continue, "features/for_loop_with_continue.fe"}
Expand Down Expand Up @@ -237,12 +238,11 @@ fn build_snapshot(file_store: &FileStore, module: items::ModuleId, db: &dyn Anal
)],
Item::Type(TypeDef::Struct(struct_)) => [
label_in_non_overlapping_groups(
&struct_
.fields(db)
.values()
.map(|field| (field.data(db).ast.span, field.typ(db).unwrap()))
.collect::<Vec<_>>(),

&struct_
.fields(db)
.values()
.map(|field| (field.data(db).ast.span, field.typ(db).unwrap()))
.collect::<Vec<_>>(),
),
struct_
.functions(db)
Expand Down Expand Up @@ -278,11 +278,10 @@ fn build_snapshot(file_store: &FileStore, module: items::ModuleId, db: &dyn Anal
Item::Function(id) => function_diagnostics(*id, db),
Item::Constant(id) => vec![build_display_diagnostic(id.span(db), &id.typ(db).unwrap())],


// Events can't be defined at the module level yet.
Item::Event(_)
Item::Event(id) => event_diagnostics(*id, db),
// Built-in stuff
| Item::Type(TypeDef::Primitive(_))
Item::Type(TypeDef::Primitive(_))
| Item::GenericType(_)
| Item::BuiltinFunction(_)
| Item::Intrinsic(_)
Expand Down
109 changes: 109 additions & 0 deletions crates/analyzer/tests/snapshots/analysis__module_level_events.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
---
source: crates/analyzer/tests/analysis.rs
expression: "build_snapshot(&files, module_id, &db)"

---
note:
┌─ features/module_level_events.fe:2:5
2idx sum: u256
^^^^^^^^^^^^^ u256

note:
┌─ features/module_level_events.fe:4:5
4 │ ╭ pub fn add(a: u256, b: u256) -> u256:
5 │ │ let sum: u256 = a + b
6 │ │ emit Added(sum=sum)
7 │ │ return a + b
│ ╰────────────────────^ attributes hash: 10094331793610550579
= FunctionSignature {
self_decl: None,
params: [
FunctionParam {
name: "a",
typ: Ok(
Base(
Numeric(
U256,
),
),
),
},
FunctionParam {
name: "b",
typ: Ok(
Base(
Numeric(
U256,
),
),
),
},
],
return_type: Ok(
Base(
Numeric(
U256,
),
),
),
}

note:
┌─ features/module_level_events.fe:5:18
5let sum: u256 = a + b
^^^^ u256

note:
┌─ features/module_level_events.fe:5:25
5let sum: u256 = a + b
^ ^ u256: Value
│ │
u256: Value

note:
┌─ features/module_level_events.fe:5:25
5let sum: u256 = a + b
^^^^^ u256: Value
6emit Added(sum=sum)
^^^ u256: Value
7return a + b
^ ^ u256: Value
│ │
u256: Value

note:
┌─ features/module_level_events.fe:7:16
7return a + b
^^^^^ u256: Value

note:
┌─ features/module_level_events.fe:6:9
6emit Added(sum=sum)
^^^^^^^^^^^^^^^^^^^ attributes hash: 3277580564244611338
= Event {
name: "Added",
fields: [
EventField {
name: "sum",
typ: Ok(
Base(
Numeric(
U256,
),
),
),
is_indexed: true,
},
],
}


1 change: 1 addition & 0 deletions crates/lowering/tests/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ test_file! { module_fn, "lowering/module_fn.fe" }
test_file! { struct_fn, "lowering/struct_fn.fe" }
test_file! { ternary, "lowering/ternary.fe" }
test_file! { and_or, "lowering/and_or.fe" }
test_file! { module_level_events, "lowering/module_level_events.fe" }
// TODO: the analyzer rejects lowered nested tuples.
// test_file!(array_tuple, "lowering/array_tuple.fe");
13 changes: 13 additions & 0 deletions crates/lowering/tests/snapshots/lowering__module_level_events.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: crates/lowering/tests/lowering.rs
expression: lowered_code

---
event Added:
idx sum: u256

contract Adder:
pub fn add(a: u256, b: u256) -> u256:
let sum: u256 = a + b
emit Added(sum=sum)
return a + b
10 changes: 10 additions & 0 deletions crates/parser/tests/cases/parse_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,13 @@ contract GuestBook:
pub fn get_msg(self, addr: address) -> BookMsg:
return self.guest_book[addr]
"# }

test_parse! { module_level_event, module::parse_module, r#"
event Added:
idx sum: u256
contract Adder:
pub fn add(a: u256, b: u256) -> u256:
let sum: u256 = a + b
emit Added(sum=sum)
return a + b
"# }
Loading

0 comments on commit f77dcec

Please sign in to comment.