Skip to content

Instantly share code, notes, and snippets.

@m-Peter
Created February 14, 2023 12:44
Show Gist options
  • Save m-Peter/c16d6f0ead4a6b3495635a479626b672 to your computer and use it in GitHub Desktop.
Save m-Peter/c16d6f0ead4a6b3495635a479626b672 to your computer and use it in GitHub Desktop.
{
"coverage": {
"I.Test": {
"line_hits": {
"105": 24,
"112": 1,
"113": 1,
"12": 4,
"122": 4,
"123": 5,
"142": 16,
"143": 16,
"155": 10,
"156": 10,
"157": 10,
"167": 9,
"178": 5,
"179": 5,
"19": 10,
"190": 1,
"203": 13,
"204": 13,
"205": 13,
"206": 13,
"27": 5,
"33": 16,
"40": 17,
"47": 16,
"53": 6,
"54": 6,
"55": 6,
"56": 6,
"62": 3,
"63": 6,
"66": 3,
"67": 3,
"68": 6,
"69": 6,
"72": 3,
"73": 3,
"84": 4,
"96": 1
},
"missed_lines": [],
"statements": 38,
"percentage": "100.0%"
}
}
}
~/Dev/forks/flow-cli/cmd/flow/flow-x86_64-linux- test test_test_contract.cdc --cover
Running tests...
Test results: "test_test_contract.cdc"
- PASS: testAssertWithNoArgs
- FAIL: testAssertWithNoArgsFail
Execution failed:
error: assertion failed
--> 7465737400000000000000000000000000000000000000000000000000000000:11:4
- PASS: testAssertWithMessage
- FAIL: testAssertWithMessageFail
Execution failed:
error: assertion failed: some reason
--> 7465737400000000000000000000000000000000000000000000000000000000:19:4
- PASS: testExecuteScript
- PASS: testExecuteScriptWithArgs
- PASS: testExecuteScriptWithArrayReturnValue
- PASS: testExecuteScriptWithDictionaryReturnValue
- PASS: testAccountVerifyUsingPublicKey
- PASS: testPublicKeyReturn
- PASS: testCreateAccount
- PASS: testAddTransaction
- PASS: testRunNextTransaction
- PASS: testRunNextTransactionWithAuthorizer
- PASS: testRunTransactionWithFailure
- PASS: testRunNonExistentTransaction
- PASS: testCommitBlock
- PASS: testCommitMultipleBlocks
- PASS: testRunGivenTransaction
- PASS: testRunTransactionWithArgs
- PASS: testRunTransactionWithMultipleAuthorizers
- PASS: testRunGivenTransactionUnsuccessful
- PASS: testRunMultipleTransactions
- PASS: testRunEmptyTransaction
- PASS: testTransactionWithArrayTypedArgs
- PASS: testReadScript
- PASS: testDeployContractsNoArgs
- FAIL: testDeployContractWithArgs
Execution failed:
error: assertion failed: found: hello from Foo
--> 7465737400000000000000000000000000000000000000000000000000000000:327:4
- FAIL: testContractDeploymentError
Execution failed:
error: panic: [Error Code: 1101] error caused by: 1 error occurred:
* transaction execute failed: [Error Code: 1101] cadence runtime error: Execution failed:
error: cannot deploy invalid contract
--> 4dc5618861dbbe498bf64ad8aa7d23d4a317995d728f1e4ed115e6d388f97f95:4:7
|
4 | signer.contracts.add(name: "FooC", code: "70756220636f6e747261637420466f6f437b20696e697428297b7d20207075622066756e2073617948656c6c6f2829207b2072657475726e2030207d207d".decodeHex())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: mismatched types
--> 01cf0e2f2f715450.FooC:1:57
|
1 | pub contract FooC{ init(){} pub fun sayHello() { return 0 } }
| ^ expected `Void`, got `Int`
--> 7465737400000000000000000000000000000000000000000000000000000000:342:8
- PASS: testExecuteScriptWithError
- FAIL: testExecuteTransactionWithError
Execution failed:
error: panic: [Error Code: 1101] error caused by: 1 error occurred:
* transaction execute failed: [Error Code: 1101] cadence runtime error: Execution failed:
error: panic: some error
--> f340a9d0a8da5a3b10f491fe3ffd0d0edb782255981ae6952d6afc2dc9bef815:1:23
|
1 | transaction { execute{ panic("some error") } }
| ^^^^^^^^^^^^^^^^^^^
--> 7465737400000000000000000000000000000000000000000000000000000000:365:8
- FAIL: testCustomMatcher
Execution failed:
error: assertion failed: Not an integer
--> 7465737400000000000000000000000000000000000000000000000000000000:380:4
- PASS: testPrimitiveMatcher
- FAIL: testCustomMatcherWithInvalidTypeUsage
Execution failed:
error: type mismatch: expected `Int`, got `String`
--> 7465737400000000000000000000000000000000000000000000000000000000:399:11
- PASS: testCustomResourceMatcher
- PASS: testCustomMatcherWithExplicitType
- PASS: testEqualMatcherWithPrimitive
- PASS: testEqualMatcherWithStruct
- PASS: testMatcherWithExplicitTypes
- PASS: testMatcherOr
- FAIL: testMatcherOrNil
Execution failed:
error: assertion failed
--> 7465737400000000000000000000000000000000000000000000000000000000:466:4
- FAIL: testMatcherAnd
Execution failed:
error: assertion failed
--> 7465737400000000000000000000000000000000000000000000000000000000:475:4
- PASS: testChainedMatchers
- PASS: testInterpretExpectFunction
- PASS: testWithExplicitExpectTypes
- PASS: testFileLocationReplaceImport
Coverage: 100.0% of statements
import Test
pub var blockchain = Test.newEmulatorBlockchain()
pub var account = blockchain.createAccount()
pub fun testAssertWithNoArgs() {
Test.assert(true)
}
pub fun testAssertWithNoArgsFail() {
Test.assert(false)
}
pub fun testAssertWithMessage() {
Test.assert(true, message: "some reason")
}
pub fun testAssertWithMessageFail() {
Test.assert(false, message: "some reason")
}
pub fun testExecuteScript() {
var result = blockchain.executeScript(
"pub fun main(): Int { return 2 + 3 }",
[]
)
assert(result.status == Test.ResultStatus.succeeded)
assert((result.returnValue! as! Int) == 5)
}
pub fun testExecuteScriptWithArgs() {
var result = blockchain.executeScript(
"pub fun main(a: Int, b: Int): Int { return a + b }",
[2, 3]
)
assert(result.status == Test.ResultStatus.succeeded)
assert((result.returnValue! as! Int) == 5)
}
pub fun testExecuteScriptWithArrayReturnValue() {
var result = blockchain.executeScript(
"pub fun main(): [UInt64] { return [1, 2, 3] }",
[]
)
assert(result.status == Test.ResultStatus.succeeded)
let resultArray = result.returnValue! as! [UInt64]
assert(resultArray.length == 3)
assert(resultArray[0] == 1)
assert(resultArray[1] == 2)
assert(resultArray[2] == 3)
}
pub fun testExecuteScriptWithDictionaryReturnValue() {
var result = blockchain.executeScript(
"pub fun main(): {String: Int} { return {\"foo\": 5, \"bar\": 10} }",
[]
)
assert(result.status == Test.ResultStatus.succeeded)
let dictionary = result.returnValue! as! {String: Int}
assert(dictionary["foo"] == 5)
assert(dictionary["bar"] == 10)
}
pub fun testAccountVerifyUsingPublicKey() {
// just checking the invocation of verify function
assert(!account.publicKey.verify(
signature: [],
signedData: [],
domainSeparationTag: "something",
hashAlgorithm: HashAlgorithm.SHA2_256
))
}
pub fun testPublicKeyReturn() {
var script = "pub fun main(): PublicKey { return PublicKey(publicKey: \"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca\".decodeHex(), signatureAlgorithm: SignatureAlgorithm.ECDSA_P256) }"
var result = blockchain.executeScript(script, [])
assert(result.status == Test.ResultStatus.succeeded)
let pubKey = result.returnValue! as! PublicKey
// just checking the invocation of verify function
assert(!pubKey.verify(
signature: [],
signedData: [],
domainSeparationTag: "",
hashAlgorithm: HashAlgorithm.SHA2_256
))
}
pub fun testCreateAccount() {
var accountA = blockchain.createAccount()
var accountB = blockchain.createAccount()
assert(accountA.address != accountB.address)
}
pub fun testAddTransaction() {
let tx = Test.Transaction(
code: "transaction { execute{ assert(false) } }",
authorizers: [account.address],
signers: [account],
arguments: [],
)
blockchain.addTransaction(tx)
}
pub fun testRunNextTransaction() {
let tx = Test.Transaction(
code: "transaction { execute{ assert(true) } }",
authorizers: [],
signers: [account],
arguments: [],
)
blockchain.addTransaction(tx)
var result = blockchain.executeNextTransaction()!
assert(result.status == Test.ResultStatus.failed)
result = blockchain.executeNextTransaction()!
assert(result.status == Test.ResultStatus.succeeded)
blockchain.commitBlock()
}
pub fun testRunNextTransactionWithAuthorizer() {
let tx = Test.Transaction(
code: "transaction { prepare(acct: AuthAccount) {} execute{ assert(true) } }",
authorizers: [account.address],
signers: [account],
arguments: [],
)
blockchain.addTransaction(tx)
let result = blockchain.executeNextTransaction()!
assert(result.status == Test.ResultStatus.succeeded)
blockchain.commitBlock()
}
pub fun testRunTransactionWithFailure() {
let tx = Test.Transaction(
code: "transaction { execute{ assert(false) } }",
authorizers: [],
signers: [account],
arguments: [],
)
blockchain.addTransaction(tx)
let result = blockchain.executeNextTransaction()!
assert(result.status == Test.ResultStatus.failed)
}
pub fun testRunNonExistentTransaction() {
var blockchain = Test.newEmulatorBlockchain()
let result = blockchain.executeNextTransaction()
assert(result == nil)
}
pub fun testCommitBlock() {
var blockchain = Test.newEmulatorBlockchain()
blockchain.commitBlock()
}
pub fun testCommitMultipleBlocks() {
var blockchain = Test.newEmulatorBlockchain()
blockchain.commitBlock()
blockchain.commitBlock()
}
pub fun testRunGivenTransaction() {
let tx = Test.Transaction(
code: "transaction { execute{ assert(true) } }",
authorizers: [],
signers: [account],
arguments: [],
)
let result = blockchain.executeTransaction(tx)
assert(result.status == Test.ResultStatus.succeeded)
}
pub fun testRunTransactionWithArgs() {
let tx = Test.Transaction(
code: "transaction(a: Int, b: Int) { execute{ assert(a == b) } }",
authorizers: [],
signers: [account],
arguments: [4, 4],
)
let result = blockchain.executeTransaction(tx)
assert(result.status == Test.ResultStatus.succeeded)
}
pub fun testRunTransactionWithMultipleAuthorizers() {
var account1 = blockchain.createAccount()
var account2 = blockchain.createAccount()
let tx = Test.Transaction(
code: "transaction() { prepare(acct1: AuthAccount, acct2: AuthAccount) {} }",
authorizers: [account1.address, account2.address],
signers: [account1, account2],
arguments: [],
)
let result = blockchain.executeTransaction(tx)
assert(result.status == Test.ResultStatus.succeeded)
}
pub fun testRunGivenTransactionUnsuccessful() {
let tx = Test.Transaction(
code: "transaction { execute{ assert(fail) } }",
authorizers: [],
signers: [account],
arguments: [],
)
let result = blockchain.executeTransaction(tx)
assert(result.status == Test.ResultStatus.failed)
}
pub fun testRunMultipleTransactions() {
let tx1 = Test.Transaction(
code: "transaction { execute{ assert(true) } }",
authorizers: [],
signers: [account],
arguments: [],
)
let tx2 = Test.Transaction(
code: "transaction { prepare(acct: AuthAccount) {} execute{ assert(true) } }",
authorizers: [account.address],
signers: [account],
arguments: [],
)
let tx3 = Test.Transaction(
code: "transaction { execute{ assert(false) } }",
authorizers: [],
signers: [account],
arguments: [],
)
let firstResults = blockchain.executeTransactions([tx1, tx2, tx3])
assert(firstResults.length == 3)
assert(firstResults[0].status == Test.ResultStatus.succeeded)
assert(firstResults[1].status == Test.ResultStatus.succeeded)
assert(firstResults[2].status == Test.ResultStatus.failed)
// Execute them again: To verify the proper increment/reset of sequence numbers.
let secondResults = blockchain.executeTransactions([tx1, tx2, tx3])
assert(secondResults.length == 3)
assert(secondResults[0].status == Test.ResultStatus.succeeded)
assert(secondResults[1].status == Test.ResultStatus.succeeded)
assert(secondResults[2].status == Test.ResultStatus.failed)
}
pub fun testRunEmptyTransaction() {
let result = blockchain.executeTransactions([])
assert(result.length == 0)
}
pub fun testTransactionWithArrayTypedArgs() {
let tx = Test.Transaction(
code: "transaction(a: [Int]) { execute{ assert(a[0] == a[1]) } }",
authorizers: [],
signers: [account],
arguments: [[4, 4]],
)
let result = blockchain.executeTransaction(tx)
assert(result.status == Test.ResultStatus.succeeded)
}
pub fun testReadScript() {
var script = Test.readFile("sample_script.cdc")
var result = blockchain.executeScript(script, [])
assert(result.status == Test.ResultStatus.succeeded)
assert((result.returnValue! as! Int) == 5)
}
pub fun testDeployContractsNoArgs() {
let contractCode = "pub contract Foo { init(){} pub fun sayHello(): String { return \"hello from Foo\"} }"
let err = blockchain.deployContract(
name: "Foo",
code: contractCode,
account: account,
arguments: [],
)
var script = "import Foo from ".concat(account.address.toString()).concat("\n")
script = script.concat("pub fun main(): String { return Foo.sayHello() }")
let result = blockchain.executeScript(script, [])
let returnedStr = result.returnValue! as! String
assert(returnedStr == "hello from Foo", message: "found: ".concat(returnedStr))
}
pub fun testDeployContractWithArgs() {
let contractCode = "pub contract Foo { pub let msg: String; init(_ msg: String) { self.msg = msg } pub fun sayHello(): String { return self.msg } }"
let err = blockchain.deployContract(
name: "Foo",
code: contractCode,
account: account,
arguments: ["hello from args"],
)
var script = "import Foo from ".concat(account.address.toString()).concat("\n")
script = script.concat("pub fun main(): String { return Foo.sayHello() }")
let result = blockchain.executeScript(script, [])
let returnedStr = result.returnValue! as! String
assert(returnedStr == "hello from args", message: "found: ".concat(returnedStr))
}
pub fun testContractDeploymentError() {
blockchain.commitBlock()
let contractCode = "pub contract FooC{ init(){} pub fun sayHello() { return 0 } }"
let err = blockchain.deployContract(
name: "FooC",
code: contractCode,
account: account,
arguments: [],
)
if err != nil {
panic(err!.message)
}
}
pub fun testExecuteScriptWithError() {
let script = "import Foo from 0x01; pub fun main() {}"
let result = blockchain.executeScript(script, [])
assert(result.status == Test.ResultStatus.failed)
}
pub fun testExecuteTransactionWithError() {
blockchain.commitBlock()
let tx2 = Test.Transaction(
code: "transaction { execute{ panic(\"some error\") } }",
authorizers: [],
signers: [account],
arguments: [],
)
let result = blockchain.executeTransaction(tx2)!
if result.status == Test.ResultStatus.failed {
panic(result.error!.message)
}
}
pub fun testCustomMatcher() {
let matcher = Test.newMatcher(fun (_ value: AnyStruct): Bool {
if !value.getType().isSubtype(of: Type<Int>()) {
return false
}
return (value as! Int) > 5
})
assert(matcher.test(8))
assert(matcher.test(4) == false)
assert(matcher.test(15.0), message: "Not an integer")
}
pub fun testPrimitiveMatcher() {
let matcher = Test.newMatcher(fun (_ value: Int): Bool {
return value == 7
})
assert(matcher.test(7))
assert(matcher.test(8) == false)
}
pub fun testCustomMatcherWithInvalidTypeUsage() {
let matcher = Test.newMatcher(fun (_ value: Int): Bool {
return (value + 7) == 4
})
assert(matcher.test(10) == false)
assert(matcher.test(-3))
assert(matcher.test("Hello"))
}
pub fun testCustomResourceMatcher() {
let matcher = Test.newMatcher(fun (_ value: &Foo): Bool {
return value.a == 4
})
let f <-create Foo(4)
assert(matcher.test(&f as &Foo))
destroy f
}
pub resource Foo {
pub let a: Int
init(_ a: Int) {
self.a = a
}
}
pub fun testCustomMatcherWithExplicitType() {
let matcher = Test.newMatcher<Int>(fun (_ value: Int): Bool {
return value == 7
})
assert(matcher.test(7))
assert(matcher.test(8) == false)
}
pub fun testEqualMatcherWithPrimitive() {
let matcher = Test.equal(1)
assert(matcher.test(1))
}
pub fun testEqualMatcherWithStruct() {
let f = FooS()
let matcher = Test.equal(f)
assert(matcher.test(f))
}
pub struct FooS {}
pub fun testMatcherWithExplicitTypes() {
let matcher = Test.equal<String>("hello")
assert(matcher.test("hello"))
assert(matcher.test("world") == false)
}
pub fun testMatcherOr() {
let one = Test.equal(1)
let two = Test.equal(2)
let oneOrTwo = one.or(two)
assert(oneOrTwo.test(1))
assert(oneOrTwo.test(2))
}
pub fun testMatcherOrNil() {
let one = Test.equal(1)
let two = Test.equal(2)
let oneOrTwo = one.or(two)
assert(oneOrTwo.test(3))
}
pub fun testMatcherAnd() {
let one = Test.equal(1)
let two = Test.equal(2)
let oneAndTwo = one.and(two)
assert(oneAndTwo.test(1))
}
pub fun testChainedMatchers() {
let one = Test.equal(1)
let two = Test.equal(2)
let three = Test.equal(3)
let oneOrTwoOrThree = one.or(two).or(three)
assert(oneOrTwoOrThree.test(3))
}
pub fun testInterpretExpectFunction() {
Test.expect("this string", Test.equal("this string"))
}
pub fun testWithExplicitExpectTypes() {
Test.expect<String>("hello", Test.equal("hello"))
}
pub fun testFileLocationReplaceImport() {
var contractCode = Test.readFile("sample_contract.cdc")
let err = blockchain.deployContract(
name: "FooSample",
code: contractCode,
account: account,
arguments: [],
)
assert(err == nil)
// Set the configurations to use the address of the deployed contract.
blockchain.useConfiguration(Test.Configuration({
"./FooSample": account.address
}))
var script = Test.readFile("foo_sample_script.cdc")
var result = blockchain.executeScript(script, [])
assert(result.status == Test.ResultStatus.succeeded)
assert((result.returnValue! as! String) == "hello from Foo")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment