diff --git a/test/blockchain_helpers.cdc b/test/blockchain_helpers.cdc index 3e44b2dc..f793abbb 100644 --- a/test/blockchain_helpers.cdc +++ b/test/blockchain_helpers.cdc @@ -1,80 +1,72 @@ import Test -pub struct BlockchainHelpers { - pub let blockchain: Test.Blockchain +/// Returns the current block height of the blockchain. +/// +pub fun getCurrentBlockHeight(): UInt64 { + let script = readFile("get_current_block_height.cdc") + let scriptResult = Test.executeScript(script, []) - init(blockchain: Test.Blockchain) { - self.blockchain = blockchain - } - - /// Returns the current block height of the blockchain. - /// - pub fun getCurrentBlockHeight(): UInt64 { - let script = self.readFile("get_current_block_height.cdc") - let scriptResult = self.blockchain.executeScript(script, []) - - if scriptResult.status == Test.ResultStatus.failed { - panic(scriptResult.error!.message) - } - return scriptResult.returnValue! as! UInt64 + if scriptResult.status == Test.ResultStatus.failed { + panic(scriptResult.error!.message) } + return scriptResult.returnValue! as! UInt64 +} - /// Returns the Flow token balance for the given account. - /// - pub fun getFlowBalance(for account: Test.Account): UFix64 { - let script = self.readFile("get_flow_balance.cdc") - let scriptResult = self.blockchain.executeScript(script, [account.address]) +/// Returns the Flow token balance for the given account. +/// +pub fun getFlowBalance(for account: Test.Account): UFix64 { + let script = readFile("get_flow_balance.cdc") + let scriptResult = Test.executeScript(script, [account.address]) - if scriptResult.status == Test.ResultStatus.failed { - panic(scriptResult.error!.message) - } - return scriptResult.returnValue! as! UFix64 + if scriptResult.status == Test.ResultStatus.failed { + panic(scriptResult.error!.message) } + return scriptResult.returnValue! as! UFix64 +} - /// Mints the given amount of Flow tokens to a specified test account. - /// The transaction is authorized and signed by the service account. - /// Returns the result of the transaction. - /// - pub fun mintFlow( - to receiver: Test.Account, - amount: UFix64 - ): Test.TransactionResult { - let code = self.readFile("mint_flow.cdc") - let tx = Test.Transaction( - code: code, - authorizers: [self.blockchain.serviceAccount().address], - signers: [], - arguments: [receiver.address, amount] - ) +/// Mints the given amount of Flow tokens to a specified test account. +/// The transaction is authorized and signed by the service account. +/// Returns the result of the transaction. +/// +pub fun mintFlow( + to receiver: Test.Account, + amount: UFix64 +): Test.TransactionResult { + let code = readFile("mint_flow.cdc") + let tx = Test.Transaction( + code: code, + authorizers: [Test.serviceAccount().address], + signers: [], + arguments: [receiver.address, amount] + ) - return self.blockchain.executeTransaction(tx) - } + return Test.executeTransaction(tx) +} - /// Burns the specified amount of Flow tokens for the given - /// test account. Returns the result of the transaction. - /// - pub fun burnFlow( - from account: Test.Account, - amount: UFix64 - ): Test.TransactionResult { - let code = self.readFile("burn_flow.cdc") - let tx = Test.Transaction( - code: code, - authorizers: [account.address], - signers: [account], - arguments: [amount] - ) +/// Burns the specified amount of Flow tokens for the given +/// test account. Returns the result of the transaction. +/// +pub fun burnFlow( + from account: Test.Account, + amount: UFix64 +): Test.TransactionResult { + let code = readFile("burn_flow.cdc") + let tx = Test.Transaction( + code: code, + authorizers: [account.address], + signers: [account], + arguments: [amount] + ) - return self.blockchain.executeTransaction(tx) - } + return Test.executeTransaction(tx) +} - /// Reads the code for the script/transaction with the given - /// file name and returns its content as a String. - /// - access(self) - fun readFile(_ name: String): String { - // The "\u{0}helper/" prefix is used in order to prevent - // conflicts with user-defined scripts/transactions. - return Test.readFile("\u{0}helper/".concat(name)) - } +/// Reads the code for the script/transaction with the given +/// file name and returns its content as a String. +/// +access(self) +fun readFile(_ name: String): String { + // The "\u{0}helper/" prefix is used in order to prevent + // conflicts with user-defined scripts/transactions. + return Test.readFile("\u{0}helper/".concat(name)) } diff --git a/test/emulator_backend.go b/test/emulator_backend.go index 852580dd..3533823f 100644 --- a/test/emulator_backend.go +++ b/test/emulator_backend.go @@ -620,13 +620,18 @@ func (e *EmulatorBackend) replaceImports(code string) string { address, ok := e.configuration.Addresses[location.String()] if !ok { - contractAddress, ok := e.contracts[importDeclaration.Identifiers[0].Identifier] - if ok { - address, _ = common.HexToAddress(contractAddress) + if len(importDeclaration.Identifiers) > 0 { + contractAddress, ok := e.contracts[importDeclaration.Identifiers[0].Identifier] + if ok { + address, _ = common.HexToAddress(contractAddress) + } else { + // keep import statement it as-is + sb.WriteString(code[prevImportDeclEnd:importDeclEnd]) + continue + } } else { - // keep import statement it as-is - sb.WriteString(code[prevImportDeclEnd:importDeclEnd]) - continue + contractAddress := e.contracts[location.String()] + address, _ = common.HexToAddress(contractAddress) } } diff --git a/test/test_framework_test.go b/test/test_framework_test.go index 0ef79fbc..5a80f716 100644 --- a/test/test_framework_test.go +++ b/test/test_framework_test.go @@ -144,8 +144,7 @@ func TestExecuteScript(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let result = blockchain.executeScript( + let result = Test.executeScript( "pub fun main(): Int { return 2 + 3 }", [] ) @@ -168,8 +167,7 @@ func TestExecuteScript(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let result = blockchain.executeScript( + let result = Test.executeScript( "pub fun main(a: Int, b: Int): Int { return a + b }", [2, 3] ) @@ -194,8 +192,7 @@ func TestExecuteScript(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let result = blockchain.executeScript("%s", []) + let result = Test.executeScript("%s", []) Test.expect(result, Test.beSucceeded()) @@ -221,8 +218,7 @@ func TestExecuteScript(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let result = blockchain.executeScript("%s", []) + let result = Test.executeScript("%s", []) Test.expect(result, Test.beSucceeded()) @@ -248,8 +244,7 @@ func TestExecuteScript(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let result = blockchain.executeScript("%s", []) + let result = Test.executeScript("%s", []) Test.expect(result, Test.beSucceeded()) @@ -275,8 +270,7 @@ func TestExecuteScript(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let result = blockchain.executeScript("%s", []) + let result = Test.executeScript("%s", []) Test.expect(result, Test.beSucceeded()) @@ -304,10 +298,8 @@ func TestImportContract(t *testing.T) { import Test import FooContract from "./FooContract" - pub let blockchain = Test.blockchain - pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "./FooContract", arguments: [] @@ -376,10 +368,8 @@ func TestImportContract(t *testing.T) { import Test import FooContract from "./FooContract" - pub let blockchain = Test.blockchain - pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "./FooContract", arguments: ["hello from Foo"] @@ -507,28 +497,22 @@ func TestImportContract(t *testing.T) { import BarContract from "./BarContract" import FooContract from "./FooContract" - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000005) + pub let account = Test.getAccount(0x0000000000000005) pub fun setup() { - var err = blockchain.deployContract( + var err = Test.deployContract( name: "BarContract", path: "./BarContract", arguments: [] ) Test.expect(err, Test.beNil()) - err = blockchain.deployContract( + err = Test.deployContract( name: "FooContract", path: "./FooContract", arguments: [] ) Test.expect(err, Test.beNil()) - - blockchain.useConfiguration(Test.Configuration({ - "FooContract": account.address, - "BarContract": account.address - })) } pub fun test() { @@ -604,14 +588,7 @@ func TestImportBuiltinContracts(t *testing.T) { testCode := ` import Test - pub let blockchain = Test.blockchain - pub let account = blockchain.createAccount() - - pub fun setup() { - blockchain.useConfiguration(Test.Configuration({ - "FooContract": account.address - })) - } + pub let account = Test.createAccount() pub fun testSetupExampleNFTCollection() { let code = Test.readFile("../transactions/setup_example_nft_collection.cdc") @@ -622,13 +599,13 @@ func TestImportBuiltinContracts(t *testing.T) { arguments: [] ) - let result = blockchain.executeTransaction(tx) + let result = Test.executeTransaction(tx) Test.expect(result, Test.beSucceeded()) } pub fun testGetIntegerTrait() { let script = Test.readFile("../scripts/import_common_contracts.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) Test.assertEqual(true, result.returnValue! as! Bool) @@ -751,10 +728,8 @@ func TestUsingEnv(t *testing.T) { import Test import FooContract from "./FooContract" - pub let blockchain = Test.blockchain - pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "./FooContract", arguments: [] @@ -824,8 +799,7 @@ func TestUsingEnv(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() // just checking the invocation of verify function Test.assert(!account.publicKey.verify( @@ -850,12 +824,11 @@ func TestUsingEnv(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let script = Test.readFile("./sample/script.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) @@ -905,11 +878,10 @@ func TestCreateAccount(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let typ = CompositeType("flow.AccountCreated")! - let events = blockchain.eventsOfType(typ) + let events = Test.eventsOfType(typ) Test.expect(events.length, Test.beGreaterThan(1)) } ` @@ -924,6 +896,33 @@ func TestCreateAccount(t *testing.T) { require.NoError(t, result.Error) } +func TestGetAccount(t *testing.T) { + t.Parallel() + + const code = ` + import Test + + pub fun test() { + let account = Test.getAccount(0x0000000000000095) + + Test.assertEqual(0x0000000000000005 as Address, account.address) + } + ` + + importResolver := func(location common.Location) (string, error) { + return "", nil + } + + runner := NewTestRunner().WithImportResolver(importResolver) + result, err := runner.RunTest(code, "test") + require.NoError(t, err) + require.ErrorContains( + t, + result.Error, + "account with address: 0x0000000000000095 was not found", + ) +} + func TestExecutingTransactions(t *testing.T) { t.Parallel() @@ -934,8 +933,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx = Test.Transaction( code: "transaction { execute{ assert(false) } }", @@ -944,7 +942,7 @@ func TestExecutingTransactions(t *testing.T) { arguments: [], ) - blockchain.addTransaction(tx) + Test.addTransaction(tx) } ` @@ -961,8 +959,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx = Test.Transaction( code: "transaction { execute{ assert(true) } }", @@ -971,9 +968,9 @@ func TestExecutingTransactions(t *testing.T) { arguments: [], ) - blockchain.addTransaction(tx) + Test.addTransaction(tx) - let result = blockchain.executeNextTransaction()! + let result = Test.executeNextTransaction()! Test.expect(result, Test.beSucceeded()) } ` @@ -991,8 +988,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx = Test.Transaction( code: "transaction { prepare(acct: AuthAccount) {} execute{ assert(true) } }", @@ -1001,9 +997,9 @@ func TestExecutingTransactions(t *testing.T) { arguments: [], ) - blockchain.addTransaction(tx) + Test.addTransaction(tx) - let result = blockchain.executeNextTransaction()! + let result = Test.executeNextTransaction()! Test.expect(result, Test.beSucceeded()) } ` @@ -1021,8 +1017,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx = Test.Transaction( code: "transaction { execute{ assert(false) } }", @@ -1031,9 +1026,9 @@ func TestExecutingTransactions(t *testing.T) { arguments: [], ) - blockchain.addTransaction(tx) + Test.addTransaction(tx) - let result = blockchain.executeNextTransaction()! + let result = Test.executeNextTransaction()! Test.expect(result, Test.beFailed()) } ` @@ -1051,8 +1046,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let result = blockchain.executeNextTransaction() + let result = Test.executeNextTransaction() Test.expect(result, Test.beNil()) } ` @@ -1070,8 +1064,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - blockchain.commitBlock() + Test.commitBlock() } ` @@ -1088,8 +1081,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx = Test.Transaction( code: "transaction { execute{ assert(false) } }", @@ -1098,9 +1090,9 @@ func TestExecutingTransactions(t *testing.T) { arguments: [], ) - blockchain.addTransaction(tx) + Test.addTransaction(tx) - blockchain.commitBlock() + Test.commitBlock() } ` @@ -1119,8 +1111,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx = Test.Transaction( code: "transaction { execute{ assert(false) } }", @@ -1130,14 +1121,14 @@ func TestExecutingTransactions(t *testing.T) { ) // Add two transactions - blockchain.addTransaction(tx) - blockchain.addTransaction(tx) + Test.addTransaction(tx) + Test.addTransaction(tx) // But execute only one - blockchain.executeNextTransaction() + Test.executeNextTransaction() // Then try to commit - blockchain.commitBlock() + Test.commitBlock() } ` @@ -1156,9 +1147,8 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - blockchain.commitBlock() - blockchain.commitBlock() + Test.commitBlock() + Test.commitBlock() } ` @@ -1175,8 +1165,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx = Test.Transaction( code: "transaction { execute{ assert(true) } }", @@ -1185,7 +1174,7 @@ func TestExecutingTransactions(t *testing.T) { arguments: [], ) - let result = blockchain.executeTransaction(tx) + let result = Test.executeTransaction(tx) Test.expect(result, Test.beSucceeded()) } ` @@ -1203,8 +1192,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx = Test.Transaction( code: "transaction(a: Int, b: Int) { execute{ assert(a == b) } }", @@ -1213,7 +1201,7 @@ func TestExecutingTransactions(t *testing.T) { arguments: [4, 4], ) - let result = blockchain.executeTransaction(tx) + let result = Test.executeTransaction(tx) Test.expect(result, Test.beSucceeded()) } ` @@ -1231,9 +1219,8 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account1 = blockchain.createAccount() - let account2 = blockchain.createAccount() + let account1 = Test.createAccount() + let account2 = Test.createAccount() let tx = Test.Transaction( code: "transaction() { prepare(acct1: AuthAccount, acct2: AuthAccount) {} }", @@ -1242,7 +1229,7 @@ func TestExecutingTransactions(t *testing.T) { arguments: [], ) - let result = blockchain.executeTransaction(tx) + let result = Test.executeTransaction(tx) Test.expect(result, Test.beSucceeded()) } ` @@ -1260,8 +1247,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx = Test.Transaction( code: "transaction { execute{ assert(fail) } }", @@ -1270,7 +1256,7 @@ func TestExecutingTransactions(t *testing.T) { arguments: [], ) - let result = blockchain.executeTransaction(tx) + let result = Test.executeTransaction(tx) Test.expect(result, Test.beFailed()) } ` @@ -1288,8 +1274,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx1 = Test.Transaction( code: "transaction { execute{ assert(true) } }", @@ -1312,7 +1297,7 @@ func TestExecutingTransactions(t *testing.T) { arguments: [], ) - let firstResults = blockchain.executeTransactions([tx1, tx2, tx3]) + let firstResults = Test.executeTransactions([tx1, tx2, tx3]) Test.assertEqual(3, firstResults.length) Test.expect(firstResults[0], Test.beSucceeded()) @@ -1321,7 +1306,7 @@ func TestExecutingTransactions(t *testing.T) { // Execute them again: To verify the proper increment/reset of sequence numbers. - let secondResults = blockchain.executeTransactions([tx1, tx2, tx3]) + let secondResults = Test.executeTransactions([tx1, tx2, tx3]) Test.assertEqual(3, secondResults.length) Test.expect(secondResults[0], Test.beSucceeded()) @@ -1343,10 +1328,9 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() - let result = blockchain.executeTransactions([]) + let result = Test.executeTransactions([]) Test.assertEqual(0, result.length) } ` @@ -1364,8 +1348,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx1 = Test.Transaction( code: "transaction { execute{ assert(true) } }", @@ -1374,7 +1357,7 @@ func TestExecutingTransactions(t *testing.T) { arguments: [], ) - blockchain.addTransaction(tx1) + Test.addTransaction(tx1) let tx2 = Test.Transaction( code: "transaction { execute{ assert(true) } }", @@ -1383,7 +1366,7 @@ func TestExecutingTransactions(t *testing.T) { arguments: [], ) - let result = blockchain.executeTransaction(tx2) + let result = Test.executeTransaction(tx2) Test.expect(result, Test.beSucceeded()) } ` @@ -1403,8 +1386,7 @@ func TestExecutingTransactions(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx = Test.Transaction( code: "transaction(a: [Int]) { execute{ assert(a[0] == a[1]) } }", @@ -1413,7 +1395,7 @@ func TestExecutingTransactions(t *testing.T) { arguments: [[4, 4]], ) - let result = blockchain.executeTransaction(tx) + let result = Test.executeTransaction(tx) Test.expect(result, Test.beSucceeded()) } ` @@ -1708,12 +1690,11 @@ func TestLoadingProgramsFromLocalFile(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let script = Test.readFile("./sample/script.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) Test.assertEqual(5, result.returnValue! as! Int) @@ -1750,12 +1731,11 @@ func TestLoadingProgramsFromLocalFile(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let script = Test.readFile("./sample/script.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) Test.assertEqual(5, result.returnValue! as! Int) @@ -1787,8 +1767,7 @@ func TestLoadingProgramsFromLocalFile(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let script = Test.readFile("./sample/script.cdc") } @@ -1831,24 +1810,19 @@ func TestDeployingContracts(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.getAccount(0x0000000000000005) + let account = Test.getAccount(0x0000000000000005) - let err = blockchain.deployContract( + let err = Test.deployContract( name: "Foo", path: "Foo.cdc", arguments: [], ) - blockchain.useConfiguration(Test.Configuration({ - "Foo.cdc": account.address - })) - Test.expect(err, Test.beNil()) let script = Test.readFile("say_hello.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) @@ -1909,24 +1883,19 @@ func TestDeployingContracts(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.getAccount(0x0000000000000005) + let account = Test.getAccount(0x0000000000000005) - let err = blockchain.deployContract( + let err = Test.deployContract( name: "Foo", path: "Foo.cdc", arguments: ["hello from args"], ) - blockchain.useConfiguration(Test.Configuration({ - "Foo.cdc": account.address - })) - Test.expect(err, Test.beNil()) let script = Test.readFile("say_hello.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) @@ -1979,9 +1948,7 @@ func TestErrors(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - - let err = blockchain.deployContract( + let err = Test.deployContract( name: "Foo", path: "Foo.cdc", arguments: [], @@ -2022,10 +1989,8 @@ func TestErrors(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let script = "import Foo from 0x01; pub fun main() {}" - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) if result.status == Test.ResultStatus.failed { panic(result.error!.message) @@ -2051,8 +2016,7 @@ func TestErrors(t *testing.T) { import Test pub fun test() { - let blockchain = Test.blockchain - let account = blockchain.createAccount() + let account = Test.createAccount() let tx2 = Test.Transaction( code: "transaction { execute{ panic(\"some error\") } }", @@ -2061,7 +2025,7 @@ func TestErrors(t *testing.T) { arguments: [], ) - let result = blockchain.executeTransaction(tx2)! + let result = Test.executeTransaction(tx2)! Test.assertError(result, errorMessage: "some error") if result.status == Test.ResultStatus.failed { @@ -2753,29 +2717,22 @@ func TestReplacingImports(t *testing.T) { const code = ` import Test - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000005) + pub let account = Test.getAccount(0x0000000000000005) pub fun setup() { // Deploy the contract - let err = blockchain.deployContract( + let err = Test.deployContract( name: "Foo", path: "./sample/contract.cdc", arguments: [], ) Test.expect(err, Test.beNil()) - - // Set the configurations to use the address of the deployed contract. - - blockchain.useConfiguration(Test.Configuration({ - "./FooContract": account.address - })) } pub fun test() { let script = Test.readFile("./sample/script.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) Test.assertEqual("hello from Foo", result.returnValue! as! String) @@ -2830,28 +2787,21 @@ func TestReplacingImports(t *testing.T) { const code = ` import Test - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000005) + pub let account = Test.getAccount(0x0000000000000005) pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "Foo", path: "./sample/contract.cdc", arguments: [], ) Test.expect(err, Test.beNil()) - - // Address locations are not replaceable! - - blockchain.useConfiguration(Test.Configuration({ - "0x01": account.address - })) } pub fun test() { let script = Test.readFile("./sample/script.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beFailed()) if result.status == Test.ResultStatus.failed { @@ -2914,11 +2864,10 @@ func TestReplacingImports(t *testing.T) { const code = ` import Test - pub let blockchain = Test.blockchain - pub let account = blockchain.createAccount() + pub let account = Test.createAccount() pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "Foo", path: "./sample/contract.cdc", arguments: [], @@ -2931,7 +2880,7 @@ func TestReplacingImports(t *testing.T) { pub fun test() { let script = Test.readFile("./sample/script.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) if result.status == Test.ResultStatus.failed { @@ -2989,27 +2938,21 @@ func TestReplacingImports(t *testing.T) { const code = ` import Test - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000005) + pub let account = Test.getAccount(0x0000000000000005) pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "Foo", path: "./FooContract", arguments: [], ) Test.expect(err, Test.beNil()) - - // Configurations provided, but some imports are missing. - blockchain.useConfiguration(Test.Configuration({ - "./FooContract": account.address - })) } pub fun test() { let script = Test.readFile("./sample/script.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beFailed()) if result.status == Test.ResultStatus.failed { @@ -3108,15 +3051,12 @@ func TestGetAccountFlowBalance(t *testing.T) { import Test import BlockchainHelpers - pub let blockchain = Test.blockchain - pub let helpers = BlockchainHelpers(blockchain: blockchain) - pub fun testGetFlowBalance() { // Arrange - let account = blockchain.serviceAccount() + let account = Test.serviceAccount() // Act - let balance = helpers.getFlowBalance(for: account) + let balance = getFlowBalance(for: account) // Assert Test.assertEqual(1000000000.0, balance) @@ -3137,19 +3077,16 @@ func TestGetCurrentBlockHeight(t *testing.T) { import Test import BlockchainHelpers - pub let blockchain = Test.blockchain - pub let helpers = BlockchainHelpers(blockchain: blockchain) - pub fun testGetCurrentBlockHeight() { // Act - let height = helpers.getCurrentBlockHeight() + let height = getCurrentBlockHeight() // Assert Test.expect(height, Test.beGreaterThan(1 as UInt64)) // Act - blockchain.commitBlock() - let newHeight = helpers.getCurrentBlockHeight() + Test.commitBlock() + let newHeight = getCurrentBlockHeight() // Assert Test.assertEqual(newHeight, height + 1) @@ -3170,18 +3107,15 @@ func TestMintFlow(t *testing.T) { import Test import BlockchainHelpers - pub let blockchain = Test.blockchain - pub let helpers = BlockchainHelpers(blockchain: blockchain) - pub fun testMintFlow() { // Arrange - let account = blockchain.createAccount() + let account = Test.createAccount() // Act - helpers.mintFlow(to: account, amount: 1500.0) + mintFlow(to: account, amount: 1500.0) // Assert - let balance = helpers.getFlowBalance(for: account) + let balance = getFlowBalance(for: account) Test.assertEqual(1500.0, balance) } ` @@ -3200,25 +3134,22 @@ func TestBurnFlow(t *testing.T) { import Test import BlockchainHelpers - pub let blockchain = Test.blockchain - pub let helpers = BlockchainHelpers(blockchain: blockchain) - pub fun testBurnFlow() { // Arrange - let account = blockchain.createAccount() + let account = Test.createAccount() // Act - helpers.mintFlow(to: account, amount: 1500.0) + mintFlow(to: account, amount: 1500.0) // Assert - var balance = helpers.getFlowBalance(for: account) + var balance = getFlowBalance(for: account) Test.assertEqual(1500.0, balance) // Act - helpers.burnFlow(from: account, amount: 500.0) + burnFlow(from: account, amount: 500.0) // Assert - balance = helpers.getFlowBalance(for: account) + balance = getFlowBalance(for: account) Test.assertEqual(1000.0, balance) } ` @@ -3254,11 +3185,9 @@ func TestServiceAccount(t *testing.T) { const testCode = ` import Test - pub let blockchain = Test.blockchain - pub fun testGetServiceAccount() { // Act - let account = blockchain.serviceAccount() + let account = Test.serviceAccount() // Assert Test.assertEqual(Type
(), account.address.getType()) @@ -3281,15 +3210,12 @@ func TestServiceAccount(t *testing.T) { import Test import BlockchainHelpers - pub let blockchain = Test.blockchain - pub let helpers = BlockchainHelpers(blockchain: blockchain) - pub fun testGetServiceAccountBalance() { // Arrange - let account = blockchain.serviceAccount() + let account = Test.serviceAccount() // Act - let balance = helpers.getFlowBalance(for: account) + let balance = getFlowBalance(for: account) // Assert Test.assertEqual(1000000000.0, balance) @@ -3297,8 +3223,8 @@ func TestServiceAccount(t *testing.T) { pub fun testTransferFlowTokens() { // Arrange - let account = blockchain.serviceAccount() - let receiver = blockchain.createAccount() + let account = Test.serviceAccount() + let receiver = Test.createAccount() let code = Test.readFile("../transactions/transfer_flow_tokens.cdc") let tx = Test.Transaction( @@ -3309,11 +3235,11 @@ func TestServiceAccount(t *testing.T) { ) // Act - let txResult = blockchain.executeTransaction(tx) + let txResult = Test.executeTransaction(tx) Test.expect(txResult, Test.beSucceeded()) // Assert - let balance = helpers.getFlowBalance(for: receiver) + let balance = getFlowBalance(for: receiver) Test.assertEqual(1500.0, balance) } ` @@ -3405,11 +3331,8 @@ func TestCoverageReportForUnitTests(t *testing.T) { import Test import FooContract from "../contracts/FooContract.cdc" - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000011) - pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "../contracts/FooContract.cdc", arguments: [] @@ -3624,26 +3547,21 @@ func TestCoverageReportForIntegrationTests(t *testing.T) { const testCode = ` import Test - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000011) + pub let account = Test.getAccount(0x0000000000000011) pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "../contracts/FooContract.cdc", arguments: [] ) Test.expect(err, Test.beNil()) - - blockchain.useConfiguration(Test.Configuration({ - "../contracts/FooContract.cdc": account.address - })) } pub fun testGetIntegerTrait() { let script = Test.readFile("../scripts/get_integer_traits.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) Test.assert(result.returnValue! as! Bool) @@ -3658,7 +3576,7 @@ func TestCoverageReportForIntegrationTests(t *testing.T) { arguments: [78557, "Sierpinski"] ) - let result = blockchain.executeTransaction(tx) + let result = Test.executeTransaction(tx) Test.expect(result, Test.beSucceeded()) } ` @@ -3805,10 +3723,8 @@ func TestRetrieveLogsFromUnitTests(t *testing.T) { import Test import FooContract from "FooContract.cdc" - pub let blockchain = Test.blockchain - pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "FooContract.cdc", arguments: [] @@ -3920,10 +3836,8 @@ func TestRetrieveEmptyLogsFromUnitTests(t *testing.T) { import Test import FooContract from "FooContract.cdc" - pub let blockchain = Test.blockchain - pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "FooContract.cdc", arguments: [] @@ -4037,26 +3951,21 @@ func TestRetrieveLogsFromIntegrationTests(t *testing.T) { const testCode = ` import Test - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000005) + pub let account = Test.getAccount(0x0000000000000005) pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "../contracts/FooContract.cdc", arguments: [] ) Test.expect(err, Test.beNil()) - - blockchain.useConfiguration(Test.Configuration({ - "../contracts/FooContract.cdc": account.address - })) } pub fun testGetIntegerTrait() { let script = Test.readFile("../scripts/get_integer_traits.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) Test.assert(result.returnValue! as! Bool) @@ -4071,7 +3980,7 @@ func TestRetrieveLogsFromIntegrationTests(t *testing.T) { arguments: [78557, "Sierpinski"] ) - let result = blockchain.executeTransaction(tx) + let result = Test.executeTransaction(tx) Test.expect(result, Test.beSucceeded()) } @@ -4082,7 +3991,7 @@ func TestRetrieveLogsFromIntegrationTests(t *testing.T) { "specialNumbers updated", "addSpecialNumber works" ] - Test.assertEqual(expectedLogs, blockchain.logs()) + Test.assertEqual(expectedLogs, Test.logs()) } ` @@ -4173,26 +4082,21 @@ func TestRetrieveEmptyLogsFromIntegrationTests(t *testing.T) { const testCode = ` import Test - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000005) + pub let account = Test.getAccount(0x0000000000000005) pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "../contracts/FooContract.cdc", arguments: [] ) Test.expect(err, Test.beNil()) - - blockchain.useConfiguration(Test.Configuration({ - "../contracts/FooContract.cdc": account.address - })) } pub fun testGetIntegerTrait() { let script = Test.readFile("../scripts/get_integer_traits.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) Test.assert(result.returnValue! as! Bool) @@ -4207,12 +4111,12 @@ func TestRetrieveEmptyLogsFromIntegrationTests(t *testing.T) { arguments: [78557, "Sierpinski"] ) - let result = blockchain.executeTransaction(tx) + let result = Test.executeTransaction(tx) Test.expect(result, Test.beSucceeded()) } pub fun tearDown() { - Test.assertEqual([] as [String], blockchain.logs() ) + Test.assertEqual([] as [String], Test.logs() ) } ` @@ -4307,32 +4211,27 @@ func TestGetEventsFromIntegrationTests(t *testing.T) { import Test import FooContract from "../contracts/FooContract.cdc" - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000005) + pub let account = Test.getAccount(0x0000000000000005) pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "../contracts/FooContract.cdc", arguments: [] ) Test.expect(err, Test.beNil()) - - blockchain.useConfiguration(Test.Configuration({ - "../contracts/FooContract.cdc": account.address - })) } pub fun testGetIntegerTrait() { let script = Test.readFile("../scripts/get_integer_traits.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) Test.assert(result.returnValue! as! Bool) let typ = Type() - let events = blockchain.eventsOfType(typ) + let events = Test.eventsOfType(typ) Test.assertEqual(1, events.length) } @@ -4345,18 +4244,18 @@ func TestGetEventsFromIntegrationTests(t *testing.T) { arguments: [78557, "Sierpinski"] ) - let result = blockchain.executeTransaction(tx) + let result = Test.executeTransaction(tx) Test.expect(result, Test.beSucceeded()) let typ = Type() - let events = blockchain.eventsOfType(typ) + let events = Test.eventsOfType(typ) Test.assertEqual(1, events.length) let event = events[0] as! FooContract.NumberAdded Test.assertEqual(78557, event.n) Test.assertEqual("Sierpinski", event.trait) - let evts = blockchain.events() + let evts = Test.events() Test.expect(evts.length, Test.beGreaterThan(1)) } ` @@ -4455,8 +4354,7 @@ func TestImportingHelperFile(t *testing.T) { import Test import "test_helpers.cdc" - pub let blockchain = Test.blockchain - pub let account = blockchain.createAccount() + pub let account = Test.createAccount() pub fun testRunTransaction() { let tx = createTransaction( @@ -4465,7 +4363,7 @@ func TestImportingHelperFile(t *testing.T) { args: [] ) - let result = blockchain.executeTransaction(tx) + let result = Test.executeTransaction(tx) Test.expect(result, Test.beSucceeded()) } ` @@ -4508,30 +4406,27 @@ func TestBlockchainReset(t *testing.T) { import Test import BlockchainHelpers - pub let blockchain = Test.blockchain - pub let helpers = BlockchainHelpers(blockchain: blockchain) - pub fun testBlockchainReset() { // Arrange - let account = blockchain.createAccount() - var balance = helpers.getFlowBalance(for: account) + let account = Test.createAccount() + var balance = getFlowBalance(for: account) Test.assertEqual(0.0, balance) - let height = helpers.getCurrentBlockHeight() + let height = getCurrentBlockHeight() - helpers.mintFlow(to: account, amount: 1500.0) + mintFlow(to: account, amount: 1500.0) - balance = helpers.getFlowBalance(for: account) + balance = getFlowBalance(for: account) Test.assertEqual(1500.0, balance) - Test.assertEqual(helpers.getCurrentBlockHeight(), height + 1) + Test.assertEqual(getCurrentBlockHeight(), height + 1) // Act - blockchain.reset(to: height) + Test.reset(to: height) // Assert - balance = helpers.getFlowBalance(for: account) + balance = getFlowBalance(for: account) Test.assertEqual(0.0, balance) - Test.assertEqual(helpers.getCurrentBlockHeight(), height) + Test.assertEqual(getCurrentBlockHeight(), height) } ` @@ -4636,31 +4531,26 @@ func TestBlockchainMoveTime(t *testing.T) { const testCode = ` import Test - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000005) + pub let account = Test.getAccount(0x0000000000000005) pub var lockedAt: UFix64 = 0.0 pub fun setup() { let currentBlockTimestamp = Test.readFile("current_block_timestamp.cdc") - let result = blockchain.executeScript(currentBlockTimestamp, []) + let result = Test.executeScript(currentBlockTimestamp, []) lockedAt = result.returnValue! as! UFix64 - let err = blockchain.deployContract( + let err = Test.deployContract( name: "TimeLocker", path: "TimeLocker.cdc", arguments: [lockedAt] ) Test.expect(err, Test.beNil()) - - blockchain.useConfiguration(Test.Configuration({ - "TimeLocker": account.address - })) } pub fun testIsNotOpen() { let isLockerOpen = Test.readFile("is_locker_open.cdc") - let result = blockchain.executeScript(isLockerOpen, []) + let result = Test.executeScript(isLockerOpen, []) Test.expect(result, Test.beSucceeded()) Test.assertEqual(false, result.returnValue! as! Bool) @@ -4669,25 +4559,25 @@ func TestBlockchainMoveTime(t *testing.T) { pub fun testIsOpen() { // timeDelta is the representation of 20 days, in seconds let timeDelta = Fix64(20 * 24 * 60 * 60) - blockchain.moveTime(by: timeDelta) + Test.moveTime(by: timeDelta) let isLockerOpen = Test.readFile("is_locker_open.cdc") - var result = blockchain.executeScript(isLockerOpen, []) + var result = Test.executeScript(isLockerOpen, []) Test.expect(result, Test.beSucceeded()) Test.assertEqual(false, result.returnValue! as! Bool) // We move time forward by another 20 days - blockchain.moveTime(by: timeDelta) + Test.moveTime(by: timeDelta) - result = blockchain.executeScript(isLockerOpen, []) + result = Test.executeScript(isLockerOpen, []) Test.assertEqual(true, result.returnValue! as! Bool) // We move time backward by 20 days - blockchain.moveTime(by: timeDelta * -1.0) + Test.moveTime(by: timeDelta * -1.0) - result = blockchain.executeScript(isLockerOpen, []) + result = Test.executeScript(isLockerOpen, []) Test.assertEqual(false, result.returnValue! as! Bool) } @@ -4826,25 +4716,20 @@ func TestReferenceDeployedContractTypes(t *testing.T) { import Test import FooContract from "../contracts/FooContract.cdc" - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000005) + pub let account = Test.getAccount(0x0000000000000005) pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "../contracts/FooContract.cdc", arguments: [] ) Test.expect(err, Test.beNil()) - - blockchain.useConfiguration(Test.Configuration({ - "../contracts/FooContract.cdc": account.address - })) } pub fun testGetSpecialNumber() { let script = Test.readFile("../scripts/get_special_number.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) let specialNumbers = result.returnValue! as! [FooContract.SpecialNumber] @@ -4956,25 +4841,20 @@ func TestReferenceDeployedContractTypes(t *testing.T) { import Test import FooContract from "../contracts/FooContract.cdc" - pub let blockchain = Test.blockchain - pub let account = blockchain.getAccount(0x0000000000000005) + pub let account = Test.getAccount(0x0000000000000005) pub fun setup() { - let err = blockchain.deployContract( + let err = Test.deployContract( name: "FooContract", path: "../contracts/FooContract.cdc", arguments: [{1729: "Harshad"}] ) Test.expect(err, Test.beNil()) - - blockchain.useConfiguration(Test.Configuration({ - "../contracts/FooContract.cdc": account.address - })) } pub fun testGetSpecialNumber() { let script = Test.readFile("../scripts/get_special_number.cdc") - let result = blockchain.executeScript(script, []) + let result = Test.executeScript(script, []) Test.expect(result, Test.beSucceeded()) let specialNumbers = result.returnValue! as! [FooContract.SpecialNumber] @@ -5038,27 +4918,24 @@ func TestEmulatorBlockchainSnapshotting(t *testing.T) { import Test import BlockchainHelpers - pub let blockchain = Test.blockchain - pub let helpers = BlockchainHelpers(blockchain: blockchain) - pub fun test() { - let admin = blockchain.createAccount() - blockchain.createSnapshot(name: "adminCreated") + let admin = Test.createAccount() + Test.createSnapshot(name: "adminCreated") - helpers.mintFlow(to: admin, amount: 1000.0) - blockchain.createSnapshot(name: "adminFunded") + mintFlow(to: admin, amount: 1000.0) + Test.createSnapshot(name: "adminFunded") - var balance = helpers.getFlowBalance(for: admin) + var balance = getFlowBalance(for: admin) Test.assertEqual(1000.0, balance) - blockchain.loadSnapshot(name: "adminCreated") + Test.loadSnapshot(name: "adminCreated") - balance = helpers.getFlowBalance(for: admin) + balance = getFlowBalance(for: admin) Test.assertEqual(0.0, balance) - blockchain.loadSnapshot(name: "adminFunded") + Test.loadSnapshot(name: "adminFunded") - balance = helpers.getFlowBalance(for: admin) + balance = getFlowBalance(for: admin) Test.assertEqual(1000.0, balance) } `