onflow / cadence-tools

Developer tools for Cadence
https://www.onflow.org/
Apache License 2.0
24 stars 20 forks source link

[Test] Unable to use events as a return type #268

Open austinkline opened 8 months ago

austinkline commented 8 months ago

Current Behavior

I am not able to use an event signature as a return type in the cadence testing framework:

image

Expected Behavior

It would be great to allow these kinds of return signatures so I can cut down on copy/paste code. Usually I need the same type of event over and over when writing tests and was hoping I could write a helper to achieve this more easily

Steps To Reproduce

import Test
import "Foo"

// some tests here
...

// this method is invalid
pub fun getLatestEvent(): Foo.SomeEvent {
    return Test.eventsOfType(Type<Foo.SomeEvent>()).removeLast() as! Foo.SomeEvent
}

Environment

- Cadence version: v1.9.2
- Network: Testing
m-Peter commented 8 months ago

Unfortunately this comes from Cadence itself. Events are currently not "first-class" citizens, in that they cannot be used as argument types / return types or be constructed and assigned to variables. However, we can use their type for type-casting, as you have done here:

let events = Test.eventsOfType(Type<Foo.SomeEvent>()).removeLast() as! Foo.SomeEvent

The above methods contain the business logic for which CompositeTypes can be used as argument types & return types.