Skip to content

Commit

Permalink
Add interpreter test
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Nov 8, 2023
1 parent 2017e7b commit fa4cf1b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
10 changes: 5 additions & 5 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9509,7 +9509,7 @@ func TestNestedResourceMoveInDestructor(t *testing.T) {
self.temp <- nil
}
pub fun doubler(_ vault: @Bar.Vault): @Bar.Vault{
pub fun doubler(_ vault: @Bar.Vault): @Bar.Vault {
destroy <- create R(<-vault)
var doubled <- self.temp <- nil
return <- doubled!
Expand All @@ -9519,16 +9519,16 @@ func TestNestedResourceMoveInDestructor(t *testing.T) {
pub var bounty: @Bar.Vault
pub var dummy: @Bar.Vault
init(_ v: @Bar.Vault){
init(_ v: @Bar.Vault) {
self.bounty <- v
self.dummy <- Bar.createEmptyVault()
}
pub fun swap(){
pub fun swap() {
self.bounty <-> self.dummy
}
destroy(){
destroy() {
// Nested resource is moved here once
var bounty <- self.bounty
Expand Down Expand Up @@ -9623,7 +9623,7 @@ func TestNestedResourceMoveInDestructor(t *testing.T) {
import Bar from %[1]s
transaction {
prepare(acc: AuthAccount){
prepare(acc: AuthAccount) {
acc.save(<- Bar.createVault(balance: 100.0), to: /storage/vault)!
var vault = acc.borrow<&Bar.Vault>(from: /storage/vault)!
var flow <- vault.withdraw(amount: 42.0)
Expand Down
43 changes: 43 additions & 0 deletions runtime/tests/interpreter/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2938,3 +2938,46 @@ func TestInterpretInnerResourceDestruction(t *testing.T) {
var destroyedResourceErr interpreter.DestroyedResourceError
require.ErrorAs(t, err, &destroyedResourceErr)
}

func TestInterpretInnerResourceMove(t *testing.T) {

t.Parallel()

inter := parseCheckAndInterpret(t, `
pub resource OuterResource {
pub var a: @InnerResource
pub var b: @InnerResource
init() {
self.a <- create InnerResource()
self.b <- create InnerResource()
}
pub fun swap() {
self.a <-> self.b
}
destroy() {
// Nested resource is moved here once
var a <- self.a
// Nested resource is again moved here. This one should fail.
self.swap()
destroy a
destroy self.b
}
}
pub resource InnerResource {}
pub fun main() {
let a <- create OuterResource()
destroy a
}`,
)

_, err := inter.Invoke("main")
RequireError(t, err)
require.ErrorAs(t, err, &interpreter.UseBeforeInitializationError{})
}

0 comments on commit fa4cf1b

Please sign in to comment.