Skip to content

Commit

Permalink
move test
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Feb 6, 2024
1 parent a56f68d commit 7b51eff
Showing 1 changed file with 114 additions and 114 deletions.
228 changes: 114 additions & 114 deletions runtime/tests/interpreter/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,120 @@ func TestInterpretIfLetElseBranchConfusion(t *testing.T) {
require.ErrorAs(t, err, &interpreter.InvalidatedResourceError{})
}

func TestInterpretNestedSwap(t *testing.T) {

t.Parallel()

inter, getLogs, err := parseCheckAndInterpretWithLogs(t, `
access(all) resource NFT {
access(all) var name: String
init(name: String) {
self.name = name
}
}
access(all) resource Company {
access(self) var equity: @[NFT]
init(incorporationEquityCollection: @[NFT]) {
pre {
// We make sure the incorporation collection has at least one high-value NFT
incorporationEquityCollection[0].name == "High-value NFT"
}
self.equity <- incorporationEquityCollection
}
access(all) fun logContents() {
log("Current contents of the Company (should have a High-value NFT):")
log(self.equity[0].name)
}
destroy() {
destroy self.equity
}
}
access(all) resource SleightOfHand {
access(all) var arr: @[NFT];
access(all) var company: @Company?
access(all) var trashNFT: @NFT
init() {
self.arr <- [ <- create NFT(name: "High-value NFT")]
self.company <- nil
self.trashNFT <- create NFT(name: "Trash NFT")
self.doMagic()
}
access(all) fun callback(): Int {
var x: @[NFT] <- []
log("before inner")
log(&self.arr as &AnyResource)
log(&x as &AnyResource)
self.arr <-> x
log("after inner")
log(&self.arr as &AnyResource)
log(&x as &AnyResource)
// We hand over the array to the Company object after the swap
// has already been "scheduled"
self.company <-! create Company(incorporationEquityCollection: <- x)
log("end callback")
return 0
}
access(all) fun doMagic() {
log("before outer")
log(&self.arr as &AnyResource)
log(&self.trashNFT as &AnyResource)
self.trashNFT <-> self.arr[self.callback()]
log("after outer")
log(&self.arr as &AnyResource)
log(&self.trashNFT as &AnyResource)
self.company?.logContents()
log("Look what I pickpocketd:")
log(self.trashNFT.name)
}
destroy() {
destroy self.arr
destroy self.company
destroy self.trashNFT
}
}
access(all) fun main() {
let a <- create SleightOfHand()
destroy a
}
`)

require.NoError(t, err)

_, err = inter.Invoke("main")
RequireError(t, err)

assert.ErrorAs(t, err, &interpreter.UseBeforeInitializationError{})

assert.Equal(t,
[]string{
`"before outer"`,
`[S.test.NFT(uuid: 2, name: "High-value NFT")]`,
`S.test.NFT(name: "Trash NFT", uuid: 3)`,
`"before inner"`,
},
getLogs(),
)
}

func TestInterpretMovedResourceInOptionalBinding(t *testing.T) {

t.Parallel()
Expand Down Expand Up @@ -3428,117 +3542,3 @@ func TestInterpretResourceLoss(t *testing.T) {
RequireError(t, err)
require.ErrorAs(t, err, &interpreter.ResourceLossError{})
}

func TestInterpretNestedSwap(t *testing.T) {

t.Parallel()

inter, getLogs, err := parseCheckAndInterpretWithLogs(t, `
access(all) resource NFT {
access(all) var name: String
init(name: String) {
self.name = name
}
}
access(all) resource Company {
access(self) var equity: @[NFT]
init(incorporationEquityCollection: @[NFT]) {
pre {
// We make sure the incorporation collection has at least one high-value NFT
incorporationEquityCollection[0].name == "High-value NFT"
}
self.equity <- incorporationEquityCollection
}
access(all) fun logContents() {
log("Current contents of the Company (should have a High-value NFT):")
log(self.equity[0].name)
}
destroy() {
destroy self.equity
}
}
access(all) resource SleightOfHand {
access(all) var arr: @[NFT];
access(all) var company: @Company?
access(all) var trashNFT: @NFT
init() {
self.arr <- [ <- create NFT(name: "High-value NFT")]
self.company <- nil
self.trashNFT <- create NFT(name: "Trash NFT")
self.doMagic()
}
access(all) fun callback(): Int {
var x: @[NFT] <- []
log("before inner")
log(&self.arr as &AnyResource)
log(&x as &AnyResource)
self.arr <-> x
log("after inner")
log(&self.arr as &AnyResource)
log(&x as &AnyResource)
// We hand over the array to the Company object after the swap
// has already been "scheduled"
self.company <-! create Company(incorporationEquityCollection: <- x)
log("end callback")
return 0
}
access(all) fun doMagic() {
log("before outer")
log(&self.arr as &AnyResource)
log(&self.trashNFT as &AnyResource)
self.trashNFT <-> self.arr[self.callback()]
log("after outer")
log(&self.arr as &AnyResource)
log(&self.trashNFT as &AnyResource)
self.company?.logContents()
log("Look what I pickpocketd:")
log(self.trashNFT.name)
}
destroy() {
destroy self.arr
destroy self.company
destroy self.trashNFT
}
}
access(all) fun main() {
let a <- create SleightOfHand()
destroy a
}
`)

require.NoError(t, err)

_, err = inter.Invoke("main")
RequireError(t, err)

assert.ErrorAs(t, err, &interpreter.UseBeforeInitializationError{})

assert.Equal(t,
[]string{
`"before outer"`,
`[S.test.NFT(uuid: 2, name: "High-value NFT")]`,
`S.test.NFT(name: "Trash NFT", uuid: 3)`,
`"before inner"`,
},
getLogs(),
)
}

0 comments on commit 7b51eff

Please sign in to comment.