Skip to content

Commit

Permalink
added comments to each test
Browse files Browse the repository at this point in the history
  • Loading branch information
bjartek committed Feb 6, 2024
1 parent b3f12e4 commit 969b26c
Show file tree
Hide file tree
Showing 11 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion scripts/test1.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ access(all) fun main(user:Address): String{
signer.storage.save("foo", to:collectionData.storagePath)


// Return early if the account already has a collection
//This will panic
if signer.storage.borrow<&ExampleNFT.Collection>(from: collectionData.storagePath) != nil {
return "ok"
}
Expand Down
1 change: 1 addition & 0 deletions scripts/test10.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ access(all) fun main(user:Address): [String]{
signer.storage.save(<- collection, to:collectionData.storagePath)


//this works
let col= signer.storage.borrow<&AnyResource>(from: collectionData.storagePath) as? &{NonFungibleToken.Collection}?
if col != nil {
messages.append("borrow using anyResource and then cast as restricted type")
Expand Down
1 change: 1 addition & 0 deletions scripts/test11.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ access(all) fun main(user:Address): [String]{
let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData? ?? panic("ViewResolver does not resolve NFTCollectionData view")


//this also works
let col= signer.storage.borrow<&AnyResource>(from: collectionData.storagePath) as? &{NonFungibleToken.Collection}?
if col != nil {
messages.append("borrow using anyResource and then cast as restricted type")
Expand Down
2 changes: 1 addition & 1 deletion scripts/test2.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ access(all) fun main(user:Address): String{
signer.storage.save(<- collection, to:collectionData.storagePath)


// Return early if the account already has a collection
//This will panic
if signer.storage.borrow<&ExampleNFT.Collection>(from: collectionData.storagePath) != nil {
return "ok"
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/test3.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ access(all) fun main(user:Address): String{
signer.storage.save(<- collection, to:collectionData.storagePath)


// Return early if the account already has a collection
//this if check will fail, but it does not panic
if signer.storage.check<&ExampleNFT.Collection>(from: collectionData.storagePath) {
return "fail"
}
Expand Down
1 change: 1 addition & 0 deletions scripts/test4.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ access(all) fun main(user:Address): [String]{
if signer.storage.check<&{NonFungibleToken.Collection}>(from: collectionData.storagePath) {
messages.append("is an nft collection")
}else {
//this check fails here, and to me that is not what i expect
messages.append("cannot check that this implements interface {NonFungibleToken.Collection}")
}
return messages
Expand Down
1 change: 1 addition & 0 deletions scripts/test5.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ access(all) fun main(user:Address): [String]{
// Return early if the account already has a collection
let storedType =signer.storage.type(at: collectionData.storagePath)!
if storedType.isSubtype(of: Type<@{NonFungibleToken.Collection}>()) {
//this works, you need to fint the type and check that it is a subtype of what we want
messages.append("is nft collection")
}

Expand Down
1 change: 1 addition & 0 deletions scripts/test6.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ access(all) fun main(user:Address): [String]{
messages.append("is nft collection")
}
} else {
//since we do not store anything here this works
messages.append("does not store anything")
}

Expand Down
3 changes: 1 addition & 2 deletions scripts/test7.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ access(all) fun main(user:Address): [String]{

let collection <- ExampleNFT.createEmptyCollection(nftType: Type<@ExampleNFT.NFT>())

//this will not work if the storage has something else there already
// save it to the account
signer.storage.save(<-collection, to: collectionData.storagePath)
messages.append("we could load and destory and add new collection")
//this works as expected we get here
}
}
return messages
Expand Down
5 changes: 1 addition & 4 deletions scripts/test8.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,21 @@ access(all) fun main(user:Address): [String]{
signer.storage.save("foo", to:collectionData.storagePath)



// Return early if the account already has a collection
if let storedType =signer.storage.type(at: collectionData.storagePath) {
if storedType.isSubtype(of: Type<@{NonFungibleToken.Collection}>()) {
messages.append("store something that is NFT collection")
}

if storedType != Type<&ExampleNFT.Collection>() {

//this also works as intended, if we want to be absolutely sure we need to check the type to unload/destory properly
if storedType.isSubtype(of: Type<@AnyResource>()) {
destroy signer.storage.load<@AnyResource>(from: collectionData.storagePath)
} else {
signer.storage.load<AnyStruct>(from: collectionData.storagePath)
}
let collection <- ExampleNFT.createEmptyCollection(nftType: Type<@ExampleNFT.NFT>())

//this will not work if the storage has something else there already
// save it to the account
signer.storage.save(<-collection, to: collectionData.storagePath)
messages.append("we could load and destory and add new collection")
}
Expand Down
3 changes: 1 addition & 2 deletions scripts/test9.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ access(all) fun main(user:Address): [String]{

signer.storage.save("foo", to:collectionData.storagePath)

// Return early if the account already has a collection
//this will panic as the thing we are trying to load is not a resource
let col= signer.storage.borrow<&AnyResource>(from: collectionData.storagePath) as? &{NonFungibleToken.Collection}?
if col == nil {
messages.append("could not borrow string as AnyResource")
Expand Down

0 comments on commit 969b26c

Please sign in to comment.