From f82065e79b0adf2513f2017cd25ae322b779c0be Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Wed, 1 Nov 2023 10:55:57 +0200 Subject: [PATCH 1/5] Update Cadence tests to latest API features and include them in unit-tests workflow --- .github/workflows/unit-tests.yml | 4 +- cadence/contracts/AddressUtils.cdc | 10 +- cadence/contracts/StringUtils.cdc | 2 +- .../address_utils_get_current_network.cdc | 11 - ...address_utils_get_network_from_address.cdc | 11 - .../address_utils_is_valid_address.cdc | 26 -- .../scripts/address_utils_parse_address.cdc | 17 -- .../scripts/address_utils_parse_uint64.cdc | 36 --- .../scripts/address_utils_without_prefix.cdc | 18 -- cadence/scripts/string_utils_contains.cdc | 17 -- cadence/scripts/string_utils_count.cdc | 11 - cadence/scripts/string_utils_explode.cdc | 14 -- cadence/scripts/string_utils_format.cdc | 11 - cadence/scripts/string_utils_has_prefix.cdc | 17 -- cadence/scripts/string_utils_has_suffix.cdc | 17 -- cadence/scripts/string_utils_index.cdc | 17 -- cadence/scripts/string_utils_join.cdc | 11 - cadence/scripts/string_utils_replace_all.cdc | 11 - cadence/scripts/string_utils_split.cdc | 14 -- .../scripts/string_utils_substring_until.cdc | 17 -- cadence/scripts/string_utils_trim.cdc | 11 - cadence/scripts/string_utils_trim_left.cdc | 17 -- flow.json | 227 ++++++++++-------- test/AddressUtils_test.cdc | 170 +++++++++---- test/ArrayUtils_test.cdc | 56 +++-- test/StringUtils_test.cdc | 203 +++++++++++----- 26 files changed, 435 insertions(+), 541 deletions(-) delete mode 100644 cadence/scripts/address_utils_get_current_network.cdc delete mode 100644 cadence/scripts/address_utils_get_network_from_address.cdc delete mode 100644 cadence/scripts/address_utils_is_valid_address.cdc delete mode 100644 cadence/scripts/address_utils_parse_address.cdc delete mode 100644 cadence/scripts/address_utils_parse_uint64.cdc delete mode 100644 cadence/scripts/address_utils_without_prefix.cdc delete mode 100644 cadence/scripts/string_utils_contains.cdc delete mode 100644 cadence/scripts/string_utils_count.cdc delete mode 100644 cadence/scripts/string_utils_explode.cdc delete mode 100644 cadence/scripts/string_utils_format.cdc delete mode 100644 cadence/scripts/string_utils_has_prefix.cdc delete mode 100644 cadence/scripts/string_utils_has_suffix.cdc delete mode 100644 cadence/scripts/string_utils_index.cdc delete mode 100644 cadence/scripts/string_utils_join.cdc delete mode 100644 cadence/scripts/string_utils_replace_all.cdc delete mode 100644 cadence/scripts/string_utils_split.cdc delete mode 100644 cadence/scripts/string_utils_substring_until.cdc delete mode 100644 cadence/scripts/string_utils_trim.cdc delete mode 100644 cadence/scripts/string_utils_trim_left.cdc diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 2372ec8..205ea0e 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -13,7 +13,9 @@ jobs: with: node-version: ^16 - name: Install flow cli - run: sh -ci "$(curl -fsSL https://storage.googleapis.com/flow-cli/install.sh)" + run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)" -- v1.5.0 + - name: Run Cadence tests + run: flow test --cover --covercode="contracts" test/*_test.cdc - name: Start flow emulator run: nohup flow emulator & - name: Install dependencies diff --git a/cadence/contracts/AddressUtils.cdc b/cadence/contracts/AddressUtils.cdc index f861960..6272f6b 100644 --- a/cadence/contracts/AddressUtils.cdc +++ b/cadence/contracts/AddressUtils.cdc @@ -1,4 +1,4 @@ -import StringUtils from "StringUtils" +import "StringUtils" pub contract AddressUtils { @@ -94,7 +94,7 @@ pub contract AddressUtils { } pub fun getNetworkFromAddress(_ input: AnyStruct): String? { - for network in ["MAINNET", "TESTNET", "EMULATOR"]{ + for network in ["MAINNET", "TESTNET", "EMULATOR"] { if self.isValidAddress(input, forNetwork: network){ return network } @@ -103,7 +103,11 @@ pub contract AddressUtils { } pub fun currentNetwork(): String { - return self.getNetworkFromAddress(self.account.address)! + if let network = self.getNetworkFromAddress(self.account.address) { + return network! + } + + panic("unknown network!") } } diff --git a/cadence/contracts/StringUtils.cdc b/cadence/contracts/StringUtils.cdc index 6d671c9..9a97673 100644 --- a/cadence/contracts/StringUtils.cdc +++ b/cadence/contracts/StringUtils.cdc @@ -1,4 +1,4 @@ -import ArrayUtils from "ArrayUtils" +import "ArrayUtils" pub contract StringUtils { diff --git a/cadence/scripts/address_utils_get_current_network.cdc b/cadence/scripts/address_utils_get_current_network.cdc deleted file mode 100644 index eca5724..0000000 --- a/cadence/scripts/address_utils_get_current_network.cdc +++ /dev/null @@ -1,11 +0,0 @@ -import AddressUtils from "../contracts/AddressUtils.cdc" - -pub fun main(): Bool { - // Act - var network = AddressUtils.currentNetwork() - - // Assert - assert(network == "EMULATOR") - - return true -} diff --git a/cadence/scripts/address_utils_get_network_from_address.cdc b/cadence/scripts/address_utils_get_network_from_address.cdc deleted file mode 100644 index 5f4be1b..0000000 --- a/cadence/scripts/address_utils_get_network_from_address.cdc +++ /dev/null @@ -1,11 +0,0 @@ -import AddressUtils from "../contracts/AddressUtils.cdc" - -pub fun main(): Bool { - // Act - let network = AddressUtils.getNetworkFromAddress(1541) - - // Assert - assert(network == nil) - - return true -} diff --git a/cadence/scripts/address_utils_is_valid_address.cdc b/cadence/scripts/address_utils_is_valid_address.cdc deleted file mode 100644 index 37e9f24..0000000 --- a/cadence/scripts/address_utils_is_valid_address.cdc +++ /dev/null @@ -1,26 +0,0 @@ -import AddressUtils from "../contracts/AddressUtils.cdc" - -pub fun main(): Bool { - // Act - let mainnet = AddressUtils.isValidAddress("0xa340dc0a4ec828ab", forNetwork: "MAINNET") - let testnet = AddressUtils.isValidAddress("0x31ad40c07a2a9788", forNetwork: "TESTNET") - let emulator = AddressUtils.isValidAddress("0xf8d6e0586b0a20c7", forNetwork: "EMULATOR") - - // Assert - assert(mainnet && testnet && emulator) - - // Act - var valid = AddressUtils.isValidAddress(1452, forNetwork: "EMULATOR") - - // Assert - assert(valid == false) - - // Act - valid = AddressUtils.isValidAddress("0x6834ba37b3980209", forNetwork: "TESTNET") - - // Assert - assert(valid == false) - - - return true -} diff --git a/cadence/scripts/address_utils_parse_address.cdc b/cadence/scripts/address_utils_parse_address.cdc deleted file mode 100644 index e579941..0000000 --- a/cadence/scripts/address_utils_parse_address.cdc +++ /dev/null @@ -1,17 +0,0 @@ -import AddressUtils from "../contracts/AddressUtils.cdc" - -pub fun main(): Bool { - // Act - var value = AddressUtils.parseAddress("0xf8d6e0586b0a20c7") - - // Assert - assert(value! == Address(0xf8d6e0586b0a20c7)) - - // Act - value = AddressUtils.parseAddress(1005) - - // Assert - assert(value == nil) - - return true -} diff --git a/cadence/scripts/address_utils_parse_uint64.cdc b/cadence/scripts/address_utils_parse_uint64.cdc deleted file mode 100644 index 7500f86..0000000 --- a/cadence/scripts/address_utils_parse_uint64.cdc +++ /dev/null @@ -1,36 +0,0 @@ -import FlowToken from 0x0ae53cb6e3f42a79 -import AddressUtils from "../contracts/AddressUtils.cdc" - -pub fun main(): Bool { - // Act - var value = AddressUtils.parseUInt64("0xf8d6e0586b0a20c7") - - // Assert - assert(value == 17930765636779778247) - - // Act - value = AddressUtils.parseUInt64(Address(0xf8d6e0586b0a20c7)) - - // Assert - assert(value == 17930765636779778247) - - // Act - value = AddressUtils.parseUInt64(FlowToken.getType()) - - // Assert - assert(value == 785100466252163705) - - // Act - value = AddressUtils.parseUInt64(0x01) - - // Assert - assert(value == nil) - - // Act - value = AddressUtils.parseUInt64("hello".getType()) - - // Assert - assert(value == nil) - - return true -} diff --git a/cadence/scripts/address_utils_without_prefix.cdc b/cadence/scripts/address_utils_without_prefix.cdc deleted file mode 100644 index a7600ec..0000000 --- a/cadence/scripts/address_utils_without_prefix.cdc +++ /dev/null @@ -1,18 +0,0 @@ -import AddressUtils from "../contracts/AddressUtils.cdc" - -pub fun main(): Bool { - // Act - var value = AddressUtils.withoutPrefix("0xf8d6e0586b0a20c7") - - // Assert - assert(value == "f8d6e0586b0a20c7") - - // Act - // Odd length - value = AddressUtils.withoutPrefix("8d6e0586b0a20c7") - - // Assert - assert(value == "08d6e0586b0a20c7") - - return true -} diff --git a/cadence/scripts/string_utils_contains.cdc b/cadence/scripts/string_utils_contains.cdc deleted file mode 100644 index 7cf6389..0000000 --- a/cadence/scripts/string_utils_contains.cdc +++ /dev/null @@ -1,17 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.contains("Hello, World!", "orl") - - // Assert - assert(value) - - // Act - value = StringUtils.contains("Hello, World!", "wow") - - // Assert - assert(value == false) - - return true -} diff --git a/cadence/scripts/string_utils_count.cdc b/cadence/scripts/string_utils_count.cdc deleted file mode 100644 index 4f04e71..0000000 --- a/cadence/scripts/string_utils_count.cdc +++ /dev/null @@ -1,11 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.count("Hello, World!", "o") - - // Assert - assert(value == 2) - - return true -} diff --git a/cadence/scripts/string_utils_explode.cdc b/cadence/scripts/string_utils_explode.cdc deleted file mode 100644 index 6766546..0000000 --- a/cadence/scripts/string_utils_explode.cdc +++ /dev/null @@ -1,14 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.explode("Hello!") - - // Assert - var expected = ["H", "e", "l", "l", "o", "!"] - for char in expected { - assert(value.contains(char)) - } - - return true -} diff --git a/cadence/scripts/string_utils_format.cdc b/cadence/scripts/string_utils_format.cdc deleted file mode 100644 index 2c3da31..0000000 --- a/cadence/scripts/string_utils_format.cdc +++ /dev/null @@ -1,11 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.format("Hello, {name}!", {"name": "Peter"}) - - // Assert - assert(value == "Hello, Peter!") - - return true -} diff --git a/cadence/scripts/string_utils_has_prefix.cdc b/cadence/scripts/string_utils_has_prefix.cdc deleted file mode 100644 index 6f259de..0000000 --- a/cadence/scripts/string_utils_has_prefix.cdc +++ /dev/null @@ -1,17 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.hasPrefix("Hello, World!", "Hell") - - // Assert - assert(value) - - // Act - value = StringUtils.hasPrefix("Hell", "Hello, World!") - - // Assert - assert(value == false) - - return true -} diff --git a/cadence/scripts/string_utils_has_suffix.cdc b/cadence/scripts/string_utils_has_suffix.cdc deleted file mode 100644 index 47a8901..0000000 --- a/cadence/scripts/string_utils_has_suffix.cdc +++ /dev/null @@ -1,17 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.hasSuffix("Hello, World!", "ld!") - - // Assert - assert(value) - - // Act - value = StringUtils.hasSuffix("ld!", "Hello, World!") - - // Assert - assert(value == false) - - return true -} diff --git a/cadence/scripts/string_utils_index.cdc b/cadence/scripts/string_utils_index.cdc deleted file mode 100644 index a8048b9..0000000 --- a/cadence/scripts/string_utils_index.cdc +++ /dev/null @@ -1,17 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.index("Hello, Peter!", "Peter", 0) - - // Assert - assert(value == 7) - - // Act - value = StringUtils.index("Hello, Peter!", "Mark", 0) - - // Assert - assert(value == nil) - - return true -} diff --git a/cadence/scripts/string_utils_join.cdc b/cadence/scripts/string_utils_join.cdc deleted file mode 100644 index b0d1d32..0000000 --- a/cadence/scripts/string_utils_join.cdc +++ /dev/null @@ -1,11 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.join(["Hello", "How", "Are", "You", "Today?"], " ") - - // Assert - assert(value == "Hello How Are You Today?") - - return true -} diff --git a/cadence/scripts/string_utils_replace_all.cdc b/cadence/scripts/string_utils_replace_all.cdc deleted file mode 100644 index f9e932d..0000000 --- a/cadence/scripts/string_utils_replace_all.cdc +++ /dev/null @@ -1,11 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.replaceAll("Hello, World!", "l", "L") - - // Assert - assert(value == "HeLLo, WorLd!") - - return true -} diff --git a/cadence/scripts/string_utils_split.cdc b/cadence/scripts/string_utils_split.cdc deleted file mode 100644 index eccb4d0..0000000 --- a/cadence/scripts/string_utils_split.cdc +++ /dev/null @@ -1,14 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.split("Hello,How,Are,You? Today", ",") - - // Assert - var expected = ["Hello", "How", "Are", "You? Today"] - for e in expected { - assert(value.contains(e)) - } - - return true -} diff --git a/cadence/scripts/string_utils_substring_until.cdc b/cadence/scripts/string_utils_substring_until.cdc deleted file mode 100644 index 8d98dd9..0000000 --- a/cadence/scripts/string_utils_substring_until.cdc +++ /dev/null @@ -1,17 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.substringUntil("Hello, sir. How are you today?", ".", 0) - - // Assert - assert(value == "Hello, sir") - - // Act - value = StringUtils.substringUntil("Hello, sir!", ".", 0) - - // Assert - assert(value == "Hello, sir!") - - return true -} diff --git a/cadence/scripts/string_utils_trim.cdc b/cadence/scripts/string_utils_trim.cdc deleted file mode 100644 index cfdaf2d..0000000 --- a/cadence/scripts/string_utils_trim.cdc +++ /dev/null @@ -1,11 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.trim(" Hello, World!") - - // Assert - assert(value == "Hello, World!") - - return true -} diff --git a/cadence/scripts/string_utils_trim_left.cdc b/cadence/scripts/string_utils_trim_left.cdc deleted file mode 100644 index 0eeaafd..0000000 --- a/cadence/scripts/string_utils_trim_left.cdc +++ /dev/null @@ -1,17 +0,0 @@ -import StringUtils from "../contracts/StringUtils.cdc" - -pub fun main(): Bool { - // Act - var value = StringUtils.trimLeft(" Hello, World!") - - // Assert - assert(value == "Hello, World!") - - // Act - value = StringUtils.trimLeft("") - - // Assert - assert(value == "") - - return true -} diff --git a/flow.json b/flow.json index 248c81f..bde501e 100644 --- a/flow.json +++ b/flow.json @@ -1,108 +1,141 @@ { "emulators": { - "default": { - "port": 3569, - "serviceAccount": "emulator-account" - } + "default": { + "port": 3569, + "serviceAccount": "emulator-account" + } }, "contracts": { - "ArrayUtils": "./cadence/contracts/ArrayUtils.cdc", - "StringUtils": "./cadence/contracts/StringUtils.cdc", - "AddressUtils": "./cadence/contracts/AddressUtils.cdc", - "ScopedNFTProviders": "./cadence/contracts/ScopedNFTProviders.cdc", - "ScopedFTProviders": "./cadence/contracts/ScopedFTProviders.cdc", - "FungibleToken": { - "source": "./cadence/contracts/FungibleToken.cdc", - "aliases": { - "emulator": "0xee82856bf20e2aa6", - "testnet": "0x9a0766d93b6608b7", - "mainnet": "0xf233dcee88fe0abe" - } - }, - "NonFungibleToken": { - "source": "./cadence/contracts/NonFungibleToken.cdc", - "aliases": { - "emulator": "0xf8d6e0586b0a20c7", - "testnet": "0x631e88ae7f1d7c20", - "mainnet": "0x1d7e57aa55817448" - } - }, - "MetadataViews": { - "source": "./cadence/contracts/MetadataViews.cdc", - "aliases": { - "emulator": "0xf8d6e0586b0a20c7", - "testnet": "0x631e88ae7f1d7c20", - "mainnet": "0x1d7e57aa55817448" - } - }, - "ExampleNFT": "./cadence/contracts/ExampleNFT.cdc", - "ExampleToken": "./cadence/contracts/ExampleToken.cdc" + "ArrayUtils": { + "source": "./cadence/contracts/ArrayUtils.cdc", + "aliases": { + "testing": "0x0000000000000007" + } + }, + "StringUtils": { + "source": "./cadence/contracts/StringUtils.cdc", + "aliases": { + "testing": "0x0000000000000007" + } + }, + "AddressUtils": { + "source": "./cadence/contracts/AddressUtils.cdc", + "aliases": { + "testing": "0x0000000000000007" + } + }, + "ScopedNFTProviders": { + "source": "./cadence/contracts/ScopedNFTProviders.cdc", + "aliases": { + "testing": "0x0000000000000007" + } + }, + "ScopedFTProviders": { + "source": "./cadence/contracts/ScopedFTProviders.cdc", + "aliases": { + "testing": "0x0000000000000007" + } + }, + "FungibleToken": { + "source": "./cadence/contracts/FungibleToken.cdc", + "aliases": { + "emulator": "0xee82856bf20e2aa6", + "testnet": "0x9a0766d93b6608b7", + "mainnet": "0xf233dcee88fe0abe" + } + }, + "NonFungibleToken": { + "source": "./cadence/contracts/NonFungibleToken.cdc", + "aliases": { + "emulator": "0xf8d6e0586b0a20c7", + "testnet": "0x631e88ae7f1d7c20", + "mainnet": "0x1d7e57aa55817448" + } + }, + "MetadataViews": { + "source": "./cadence/contracts/MetadataViews.cdc", + "aliases": { + "emulator": "0xf8d6e0586b0a20c7", + "testnet": "0x631e88ae7f1d7c20", + "mainnet": "0x1d7e57aa55817448" + } + }, + "ExampleNFT": { + "source": "./cadence/contracts/ExampleNFT.cdc", + "aliases": { + "testing": "0x0000000000000007" + } + }, + "ExampleToken": { + "source": "./cadence/contracts/ExampleToken.cdc", + "aliases": { + "testing": "0x0000000000000007" + } + } }, "networks": { - "emulator": "127.0.0.1:3569", - "mainnet": "access.mainnet.nodes.onflow.org:9000", - "testnet": "access.devnet.nodes.onflow.org:9000" + "emulator": "127.0.0.1:3569", + "testing": "127.0.0.1:3569", + "mainnet": "access.mainnet.nodes.onflow.org:9000", + "testnet": "access.devnet.nodes.onflow.org:9000" }, "accounts": { - "emulator-account": { - "address": "f8d6e0586b0a20c7", - "key": "f2e846bd4c1fbf17839ae59e111c6b1c98579eda7a841412f102d6621ec671cb" - }, - "flow-utils-testnet": { - "address": "0x31ad40c07a2a9788", - "key": { - "type": "google-kms", - "index": 0, - "signatureAlgorithm": "ECDSA_P256", - "hashAlgorithm": "SHA2_256", - "resourceID": "projects/flow-utils/locations/global/keyRings/contract/cryptoKeys/testnet/cryptoKeyVersions/1" - } - }, - "flow-utils-mainnet": { - "address": "0xa340dc0a4ec828ab", - "key": { - "type": "google-kms", - "index": 0, - "signatureAlgorithm": "ECDSA_P256", - "hashAlgorithm": "SHA2_256", - "resourceID": "projects/flow-utils/locations/global/keyRings/contract/cryptoKeys/mainnet/cryptoKeyVersions/1" - } - } + "emulator-account": { + "address": "f8d6e0586b0a20c7", + "key": "f2e846bd4c1fbf17839ae59e111c6b1c98579eda7a841412f102d6621ec671cb" + }, + "flow-utils-testnet": { + "address": "0x31ad40c07a2a9788", + "key": { + "type": "google-kms", + "index": 0, + "signatureAlgorithm": "ECDSA_P256", + "hashAlgorithm": "SHA2_256", + "resourceID": "projects/flow-utils/locations/global/keyRings/contract/cryptoKeys/testnet/cryptoKeyVersions/1" + } + }, + "flow-utils-mainnet": { + "address": "0xa340dc0a4ec828ab", + "key": { + "type": "google-kms", + "index": 0, + "signatureAlgorithm": "ECDSA_P256", + "hashAlgorithm": "SHA2_256", + "resourceID": "projects/flow-utils/locations/global/keyRings/contract/cryptoKeys/mainnet/cryptoKeyVersions/1" + } + } }, "deployments": { - "emulator": { - "emulator-account": [ - "ArrayUtils", - "StringUtils", - "AddressUtils", - "NonFungibleToken", - "MetadataViews", - "ExampleNFT", - "ExampleToken", - "ScopedNFTProviders", - "ScopedFTProviders" - ] - }, - "testnet": { - "flow-utils-testnet": [ - "ArrayUtils", - "StringUtils", - "AddressUtils", - "ScopedNFTProviders", - "ScopedFTProviders" - ] - }, - "mainnet": { - "flow-utils-mainnet": [ - "ArrayUtils", - "StringUtils", - "AddressUtils", - "ScopedNFTProviders", - "ScopedFTProviders" - ] - } - }, - "emulatorAccounts": { - "emulator-account": "f8d6e0586b0a20c7" + "emulator": { + "emulator-account": [ + "ArrayUtils", + "StringUtils", + "AddressUtils", + "NonFungibleToken", + "MetadataViews", + "ExampleNFT", + "ExampleToken", + "ScopedNFTProviders", + "ScopedFTProviders" + ] + }, + "testnet": { + "flow-utils-testnet": [ + "ArrayUtils", + "StringUtils", + "AddressUtils", + "ScopedNFTProviders", + "ScopedFTProviders" + ] + }, + "mainnet": { + "flow-utils-mainnet": [ + "ArrayUtils", + "StringUtils", + "AddressUtils", + "ScopedNFTProviders", + "ScopedFTProviders" + ] + } } -} +} \ No newline at end of file diff --git a/test/AddressUtils_test.cdc b/test/AddressUtils_test.cdc index af6f552..486dc22 100644 --- a/test/AddressUtils_test.cdc +++ b/test/AddressUtils_test.cdc @@ -1,81 +1,151 @@ import Test +import "FlowToken" +import "StringUtils" +import "AddressUtils" -pub let blockchain = Test.newEmulatorBlockchain() -pub let account = blockchain.createAccount() - -pub fun setup() { - blockchain.useConfiguration(Test.Configuration({ - "ArrayUtils": account.address, - "StringUtils": account.address, - "../contracts/AddressUtils.cdc": account.address - })) - - let arrayUtils = Test.readFile("../cadence/contracts/ArrayUtils.cdc") - var err = blockchain.deployContract( +access(all) +fun setup() { + var err = Test.deployContract( name: "ArrayUtils", - code: arrayUtils, - account: account, + path: "../cadence/contracts/ArrayUtils.cdc", arguments: [] ) - Test.expect(err, Test.beNil()) - let stringUtils = Test.readFile("../cadence/contracts/StringUtils.cdc") - err = blockchain.deployContract( + err = Test.deployContract( name: "StringUtils", - code: stringUtils, - account: account, + path: "../cadence/contracts/StringUtils.cdc", arguments: [] ) - Test.expect(err, Test.beNil()) - let addressUtils = Test.readFile("../cadence/contracts/AddressUtils.cdc") - err = blockchain.deployContract( + err = Test.deployContract( name: "AddressUtils", - code: addressUtils, - account: account, + path: "../cadence/contracts/AddressUtils.cdc", arguments: [] ) - Test.expect(err, Test.beNil()) } -pub fun testWithoutPrefix() { - let value = executeScript("../cadence/scripts/address_utils_without_prefix.cdc") - Test.assertEqual(true, value) -} +access(all) +fun testWithoutPrefix() { + // Act + var address = AddressUtils.withoutPrefix("0xf8d6e0586b0a20c7") -pub fun testParseUInt64() { - let value = executeScript("../cadence/scripts/address_utils_parse_uint64.cdc") - Test.assertEqual(true, value) -} + // Assert + Test.assertEqual("f8d6e0586b0a20c7", address) + + // Act + // Odd length + address = AddressUtils.withoutPrefix("8d6e0586b0a20c7") -pub fun testParseAddress() { - let value = executeScript("../cadence/scripts/address_utils_parse_address.cdc") - Test.assertEqual(true, value) + // Assert + Test.assertEqual("08d6e0586b0a20c7", address) } -pub fun testIsValidAddress() { - let value = executeScript("../cadence/scripts/address_utils_is_valid_address.cdc") - Test.assertEqual(true, value) +access(all) +fun testParseUInt64() { + // Act + var address = AddressUtils.parseUInt64("0xf8d6e0586b0a20c7") + + // Assert + Test.assertEqual(17930765636779778247 as UInt64?, address) + + // Act + address = AddressUtils.parseUInt64(Address(0xf8d6e0586b0a20c7)) + + // Assert + Test.assertEqual(17930765636779778247 as UInt64?, address) + + // Act + // In the testing framework, the FlowToken address is: + // 0x0000000000000003 + address = AddressUtils.parseUInt64(FlowToken.getType()) + + // Assert + Test.assertEqual(3 as UInt64?, address) + + // Act + address = AddressUtils.parseUInt64(0x01) + + // Assert + Test.assertEqual(nil, address) + + // Act + address = AddressUtils.parseUInt64("hello".getType()) + + // Assert + Test.assertEqual(nil, address) } -pub fun testGetNetworkFromAddress() { - let value = executeScript("../cadence/scripts/address_utils_get_network_from_address.cdc") - Test.assertEqual(true, value) +access(all) +fun testParseAddress() { + // Act + var address = AddressUtils.parseAddress("0xf8d6e0586b0a20c7") + + // Assert + Test.assertEqual(Address(0xf8d6e0586b0a20c7), address!) + + // Act + address = AddressUtils.parseAddress(1005) + + // Assert + Test.assertEqual(nil, address) } -pub fun testGetCurrentNetwork() { - let value = executeScript("../cadence/scripts/address_utils_get_current_network.cdc") - Test.assertEqual(true, value) +access(all) +fun testIsValidAddress() { + // Act + let mainnet = AddressUtils.isValidAddress("0xa340dc0a4ec828ab", forNetwork: "MAINNET") + let testnet = AddressUtils.isValidAddress("0x31ad40c07a2a9788", forNetwork: "TESTNET") + let emulator = AddressUtils.isValidAddress("0xf8d6e0586b0a20c7", forNetwork: "EMULATOR") + + // Assert + Test.assert(mainnet && testnet && emulator) + + // Act + var valid = AddressUtils.isValidAddress(1452, forNetwork: "EMULATOR") + + // Assert + Test.assertEqual(false, valid) + + // Act + valid = AddressUtils.isValidAddress("0x6834ba37b3980209", forNetwork: "TESTNET") + + // Assert + Test.assertEqual(false, valid) } -priv fun executeScript(_ scriptPath: String): Bool { - let script = Test.readFile(scriptPath) - let value = blockchain.executeScript(script, []) +access(all) +fun testGetNetworkFromAddress() { + // Act + var network = AddressUtils.getNetworkFromAddress(1541) + + // Assert + Test.assertEqual(nil, network) + + // Act + network = AddressUtils.getNetworkFromAddress("0xf8d6e0586b0a20c7") + + // Assert + Test.assertEqual("EMULATOR" as String?, network) - Test.expect(value, Test.beSucceeded()) + // Act + network = AddressUtils.getNetworkFromAddress("0x31ad40c07a2a9788") + + // Assert + Test.assertEqual("TESTNET" as String?, network) + + // Act + network = AddressUtils.getNetworkFromAddress("0xa340dc0a4ec828ab") + + // Assert + Test.assertEqual("MAINNET" as String?, network) +} - return value.returnValue! as! Bool +access(all) +fun testGetCurrentNetwork() { + Test.expectFailure(fun(): Void { + AddressUtils.currentNetwork() + }, errorMessageSubstring: "unknown network!") } diff --git a/test/ArrayUtils_test.cdc b/test/ArrayUtils_test.cdc index 28cbf49..eb99c52 100644 --- a/test/ArrayUtils_test.cdc +++ b/test/ArrayUtils_test.cdc @@ -1,41 +1,53 @@ import Test -import ArrayUtils from "ArrayUtils" +import "ArrayUtils" -pub struct Token { - pub let id: Int - pub var balance: Int +access(all) struct Token { + access(all) let id: Int + access(all) var balance: Int init(id: Int, balance: Int) { self.id = id self.balance = balance } - pub fun setBalance(_ balance: Int) { + access(all) + fun setBalance(_ balance: Int) { self.balance = balance } } -pub let arrayUtils = ArrayUtils() +access(all) +fun setup() { + let err = Test.deployContract( + name: "ArrayUtils", + path: "../cadence/contracts/ArrayUtils.cdc", + arguments: [] + ) + Test.expect(err, Test.beNil()) +} -pub fun testRange() { +access(all) +fun testRange() { // Act - let range = arrayUtils.range(0, 10) + let range = ArrayUtils.range(0, 10) // Assert let expected: [Int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Test.assertEqual(expected, range) } -pub fun testReverseRange() { +access(all) +fun testReverseRange() { // Act - let range = arrayUtils.reverse(arrayUtils.range(0, 10)) + let range = ArrayUtils.reverse(ArrayUtils.range(0, 10)) // Assert let expected: [Int] = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Test.assertEqual(expected, range) } -pub fun testTransform() { +access(all) +fun testTransform() { // Arrange let tokens = [ Token(id: 0, balance: 10), @@ -44,7 +56,7 @@ pub fun testTransform() { ] // Act - arrayUtils.transform(&tokens as &[AnyStruct], fun (t: AnyStruct): AnyStruct { + ArrayUtils.transform(&tokens as &[AnyStruct], fun (t: AnyStruct): AnyStruct { var token = t as! Token token.setBalance(token.balance * 2) return token @@ -59,7 +71,8 @@ pub fun testTransform() { Test.assertEqual(expected, tokens) } -pub fun testIterate() { +access(all) +fun testIterate() { // Arrange let tokens = [ Token(id: 0, balance: 10), @@ -71,7 +84,7 @@ pub fun testIterate() { // Act var totalBalance = 0 - arrayUtils.iterate(tokens, fun (t: AnyStruct): Bool { + ArrayUtils.iterate(tokens, fun (t: AnyStruct): Bool { var token = t as! Token if token.id <= 2 { totalBalance = totalBalance + token.balance @@ -83,7 +96,8 @@ pub fun testIterate() { Test.assertEqual(30, totalBalance) } -pub fun testMap() { +access(all) +fun testMap() { // Arrange let tokens = [ Token(id: 0, balance: 10), @@ -92,7 +106,7 @@ pub fun testMap() { ] // Act - let mapped = arrayUtils.map(tokens, fun (t: AnyStruct): AnyStruct { + let mapped = ArrayUtils.map(tokens, fun (t: AnyStruct): AnyStruct { var token = t as! Token token.setBalance(token.balance - 2) return token @@ -107,12 +121,13 @@ pub fun testMap() { Test.assertEqual(expected, mapped) } -pub fun testMapStrings() { +access(all) +fun testMapStrings() { // Arrange let strings = ["Peter", "John", "Mark"] // Act - let mapped = arrayUtils.mapStrings(strings, fun (s: String): String { + let mapped = ArrayUtils.mapStrings(strings, fun (s: String): String { return "Hello, ".concat(s).concat("!") }) @@ -125,7 +140,8 @@ pub fun testMapStrings() { Test.assertEqual(expected, mapped) } -pub fun testReduce() { +access(all) +fun testReduce() { // Arrange let tokens = [ Token(id: 0, balance: 10), @@ -135,7 +151,7 @@ pub fun testReduce() { let initial = Token(id: 5, balance: 0) // Act - let token = arrayUtils.reduce(tokens, initial, fun (acc: AnyStruct, t: AnyStruct): AnyStruct { + let token = ArrayUtils.reduce(tokens, initial, fun (acc: AnyStruct, t: AnyStruct): AnyStruct { var token = t as! Token var accToken = acc as! Token accToken.setBalance(accToken.balance + token.balance) diff --git a/test/StringUtils_test.cdc b/test/StringUtils_test.cdc index 76d2c4b..a259229 100644 --- a/test/StringUtils_test.cdc +++ b/test/StringUtils_test.cdc @@ -1,105 +1,178 @@ import Test +import "StringUtils" -pub let blockchain = Test.newEmulatorBlockchain() -pub let account = blockchain.createAccount() - -pub fun setup() { - blockchain.useConfiguration(Test.Configuration({ - "ArrayUtils": account.address, - "../contracts/StringUtils.cdc": account.address - })) - - let arrayUtils = Test.readFile("../cadence/contracts/ArrayUtils.cdc") - var err = blockchain.deployContract( +access(all) +fun setup() { + var err = Test.deployContract( name: "ArrayUtils", - code: arrayUtils, - account: account, + path: "../cadence/contracts/ArrayUtils.cdc", arguments: [] ) - Test.expect(err, Test.beNil()) - let stringUtils = Test.readFile("../cadence/contracts/StringUtils.cdc") - err = blockchain.deployContract( + err = Test.deployContract( name: "StringUtils", - code: stringUtils, - account: account, + path: "../cadence/contracts/StringUtils.cdc", arguments: [] ) - Test.expect(err, Test.beNil()) } -pub fun testFormat() { - let value = executeScript("../cadence/scripts/string_utils_format.cdc") - Test.assertEqual(true, value) -} +access(all) +fun testFormat() { + // Act + let str = StringUtils.format("Hello, {name}!", {"name": "Peter"}) -pub fun testExplode() { - let value = executeScript("../cadence/scripts/string_utils_explode.cdc") - Test.assertEqual(true, value) + // Assert + Test.assertEqual("Hello, Peter!", str) } -pub fun testTrimLeft() { - let value = executeScript("../cadence/scripts/string_utils_trim_left.cdc") - Test.assertEqual(true, value) -} +access(all) +fun testExplode() { + // Act + let chars = StringUtils.explode("Hello!") -pub fun testTrim() { - let value = executeScript("../cadence/scripts/string_utils_trim.cdc") - Test.assertEqual(true, value) + // Assert + let expected = ["H", "e", "l", "l", "o", "!"] + Test.assertEqual(expected, chars) } -pub fun testReplaceAll() { - let value = executeScript("../cadence/scripts/string_utils_replace_all.cdc") - Test.assertEqual(true, value) +access(all) +fun testTrimLeft() { + // Act + var str = StringUtils.trimLeft(" Hello, World!") + + // Assert + Test.assertEqual("Hello, World!", str) + + // Act + str = StringUtils.trimLeft("") + + // Assert + Test.assertEqual("", str) } -pub fun testHasPrefix() { - let value = executeScript("../cadence/scripts/string_utils_has_prefix.cdc") - Test.assertEqual(true, value) +access(all) +fun testTrim() { + // Act + let str = StringUtils.trim(" Hello, World!") + + // Assert + Test.assertEqual("Hello, World!", str) } -pub fun testHasSuffix() { - let value = executeScript("../cadence/scripts/string_utils_has_suffix.cdc") - Test.assertEqual(true, value) +access(all) +fun testReplaceAll() { + // Act + let str = StringUtils.replaceAll("Hello, World!", "l", "L") + + // Assert + Test.assertEqual("HeLLo, WorLd!", str) } -pub fun testIndex() { - let value = executeScript("../cadence/scripts/string_utils_index.cdc") - Test.assertEqual(true, value) +access(all) +fun testHasPrefix() { + // Act + var hasPrefix = StringUtils.hasPrefix("Hello, World!", "Hell") + + // Assert + Test.assert(hasPrefix) + + // Act + hasPrefix = StringUtils.hasPrefix("Hell", "Hello, World!") + + // Assert + Test.assertEqual(false, hasPrefix) } -pub fun testCount() { - let value = executeScript("../cadence/scripts/string_utils_count.cdc") - Test.assertEqual(true, value) +access(all) +fun testHasSuffix() { + // Act + var hasSuffix = StringUtils.hasSuffix("Hello, World!", "ld!") + + // Assert + Test.assert(hasSuffix) + + // Act + hasSuffix = StringUtils.hasSuffix("ld!", "Hello, World!") + + // Assert + Test.assertEqual(false, hasSuffix) } -pub fun testContains() { - let value = executeScript("../cadence/scripts/string_utils_contains.cdc") - Test.assertEqual(true, value) +access(all) +fun testIndex() { + // Act + var index = StringUtils.index("Hello, Peter!", "Peter", 0) + + // Assert + Test.assertEqual(7 as Int?, index) + + // Act + index = StringUtils.index("Hello, Peter!", "Mark", 0) + + // Assert + Test.assertEqual(nil, index) } -pub fun testSubstringUntil() { - let value = executeScript("../cadence/scripts/string_utils_substring_until.cdc") - Test.assertEqual(true, value) +access(all) +fun testCount() { + // Act + let count = StringUtils.count("Hello, World!", "o") + + // Assert + Test.assertEqual(2, count) } -pub fun testSplit() { - let value = executeScript("../cadence/scripts/string_utils_split.cdc") - Test.assertEqual(true, value) +access(all) +fun testContains() { + // Act + var found = StringUtils.contains("Hello, World!", "orl") + + // Assert + Test.assert(found) + + // Act + found = StringUtils.contains("Hello, World!", "wow") + + // Assert + Test.assertEqual(false, found) } -pub fun testJoin() { - let value = executeScript("../cadence/scripts/string_utils_join.cdc") - Test.assertEqual(true, value) +access(all) +fun testSubstringUntil() { + // Act + var substring = StringUtils.substringUntil( + "Hello, sir. How are you today?", + ".", + 0 + ) + + // Assert + Test.assertEqual("Hello, sir", substring) + + // Act + substring = StringUtils.substringUntil("Hello, sir!", ".", 0) + + // Assert + Test.assertEqual("Hello, sir!", substring) } -priv fun executeScript(_ scriptPath: String): Bool { - let script = Test.readFile(scriptPath) - let value = blockchain.executeScript(script, []) +access(all) +fun testSplit() { + // Act + let phrases = StringUtils.split("Hello,How,Are,You? Today", ",") + + // Assert + let expected = ["Hello", "How", "Are", "You? Today"] + Test.assertEqual(expected, phrases) +} - Test.expect(value, Test.beSucceeded()) +access(all) +fun testJoin() { + // Act + let str = StringUtils.join(["Hello", "How", "Are", "You", "Today?"], " ") - return value.returnValue! as! Bool + // Assert + Test.assertEqual("Hello How Are You Today?", str) } From 921424b7b63df06e254d698ea73f5e27480b64a0 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Wed, 1 Nov 2023 11:14:33 +0200 Subject: [PATCH 2/5] Run Cadence tests in separate job --- .github/workflows/unit-tests.yml | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 205ea0e..140e168 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -5,7 +5,7 @@ on: push: branches: [main] jobs: - tests: + js-tests: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -13,12 +13,25 @@ jobs: with: node-version: ^16 - name: Install flow cli - run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)" -- v1.5.0 - - name: Run Cadence tests - run: flow test --cover --covercode="contracts" test/*_test.cdc + run: sh -ci "$(curl -fsSL https://storage.googleapis.com/flow-cli/install.sh)" - name: Start flow emulator run: nohup flow emulator & - name: Install dependencies run: npm ci - name: Run tests run: npm test -- --runInBand + cadence-tests: + name: Flow CLI Tests + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + submodules: "true" + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: 1.20 + - name: Install Flow CLI + run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)" -- v1.5.0 + - name: Run tests + run: flow test --cover --covercode="contracts" test/*_test.cdc From 72289977aea801872d913dd89af6207d790131e2 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Wed, 1 Nov 2023 13:21:43 +0200 Subject: [PATCH 3/5] Add Cadence tests for scoped providers --- cadence/contracts/ScopedFTProviders.cdc | 4 +- cadence/contracts/ScopedNFTProviders.cdc | 4 +- test/ScopedFTProviders_test.cdc | 164 +++++++++++++++++++++ test/ScopedNFTProviders_test.cdc | 180 +++++++++++++++++++++++ 4 files changed, 348 insertions(+), 4 deletions(-) create mode 100644 test/ScopedFTProviders_test.cdc create mode 100644 test/ScopedNFTProviders_test.cdc diff --git a/cadence/contracts/ScopedFTProviders.cdc b/cadence/contracts/ScopedFTProviders.cdc index 68958b9..e96ea7f 100644 --- a/cadence/contracts/ScopedFTProviders.cdc +++ b/cadence/contracts/ScopedFTProviders.cdc @@ -1,5 +1,5 @@ -import FungibleToken from "./FungibleToken.cdc" -import StringUtils from "./StringUtils.cdc" +import "FungibleToken" +import "StringUtils" // ScopedFTProviders // diff --git a/cadence/contracts/ScopedNFTProviders.cdc b/cadence/contracts/ScopedNFTProviders.cdc index 8302f28..e1e2515 100644 --- a/cadence/contracts/ScopedNFTProviders.cdc +++ b/cadence/contracts/ScopedNFTProviders.cdc @@ -1,5 +1,5 @@ -import NonFungibleToken from "./NonFungibleToken.cdc" -import StringUtils from "./StringUtils.cdc" +import "NonFungibleToken" +import "StringUtils" // ScopedNFTProviders // diff --git a/test/ScopedFTProviders_test.cdc b/test/ScopedFTProviders_test.cdc new file mode 100644 index 0000000..f475ce8 --- /dev/null +++ b/test/ScopedFTProviders_test.cdc @@ -0,0 +1,164 @@ +import Test +import BlockchainHelpers +import "ExampleToken" + +access(all) let admin = Test.getAccount(0x0000000000000007) +access(all) let alice = Test.createAccount() + +access(all) +fun setup() { + var err = Test.deployContract( + name: "ArrayUtils", + path: "../cadence/contracts/ArrayUtils.cdc", + arguments: [] + ) + Test.expect(err, Test.beNil()) + + err = Test.deployContract( + name: "StringUtils", + path: "../cadence/contracts/StringUtils.cdc", + arguments: [] + ) + Test.expect(err, Test.beNil()) + + err = Test.deployContract( + name: "ExampleToken", + path: "../cadence/contracts/ExampleToken.cdc", + arguments: [] + ) + Test.expect(err, Test.beNil()) + + err = Test.deployContract( + name: "ScopedFTProviders", + path: "../cadence/contracts/ScopedFTProviders.cdc", + arguments: [] + ) + Test.expect(err, Test.beNil()) +} + +access(all) +fun beforeEach() { + let txResult = executeTransaction( + "../cadence/transactions/exampletoken/destroy.cdc", + [], + alice + ) + Test.expect(txResult, Test.beSucceeded()) +} + +access(all) +fun testWithdrawTokensSuccessfully() { + let tokenAmount = 100.0 + let allowance = 10.0 + + setupExampleToken(account: alice) + mintExampleToken(recipient: alice, amount: tokenAmount) + + let txResult = executeTransaction( + "../cadence/transactions/scopedproviders/ft/withdraw_scoped_ft.cdc", + [allowance, allowance], + alice + ) + Test.expect(txResult, Test.beSucceeded()) + + let events = Test.eventsOfType(Type()) + let event = events[0] as! ExampleToken.TokensWithdrawn + Test.assertEqual(alice.address, event.from!) + Test.assertEqual(allowance, event.amount) +} + +access(all) +fun testWithdrawTokensSuccessfullyWithExpiration() { + let tokenAmount = 100.0 + let allowance = 10.0 + + setupExampleToken(account: alice) + mintExampleToken(recipient: alice, amount: tokenAmount) + + let txResult = executeTransaction( + "../cadence/transactions/scopedproviders/ft/withdraw_scoped_ft_before_expiration.cdc", + [allowance, allowance], + alice + ) + Test.expect(txResult, Test.beSucceeded()) + + let events = Test.eventsOfType(Type()) + let event = events[0] as! ExampleToken.TokensWithdrawn + Test.assertEqual(alice.address, event.from!) + Test.assertEqual(allowance, event.amount) +} + +access(all) +fun testCannotWithdrawMoreThanAllowance() { + let tokenAmount = 100.0 + let allowance = 10.0 + + setupExampleToken(account: alice) + mintExampleToken(recipient: alice, amount: tokenAmount) + + let txResult = executeTransaction( + "../cadence/transactions/scopedproviders/ft/withdraw_scoped_ft.cdc", + [allowance, allowance * 2.0], + alice + ) + Test.expect(txResult, Test.beFailed()) + Test.assertError(txResult, errorMessage: "not able to withdraw") +} + +access(all) +fun testCannotWithdrawPastExpiration() { + let tokenAmount = 100.0 + let allowance = 10.0 + + setupExampleToken(account: alice) + mintExampleToken(recipient: alice, amount: tokenAmount) + + let txResult = executeTransaction( + "../cadence/transactions/scopedproviders/ft/withdraw_scoped_ft_past_expiration.cdc", + [allowance, 1.0], + alice + ) + Test.expect(txResult, Test.beFailed()) + Test.assertError(txResult, errorMessage: "provider has expired") +} + +access(all) +fun testWithdrawTwiceUnderBalance() { + let tokenAmount = 100.0 + let allowance = 10.0 + + setupExampleToken(account: alice) + mintExampleToken(recipient: alice, amount: tokenAmount) + + let txResult = executeTransaction( + "../cadence/transactions/scopedproviders/ft/withdraw_scoped_ft_twice.cdc", + [allowance], + alice + ) + Test.expect(txResult, Test.beSucceeded()) + + let events = Test.eventsOfType(Type()) + let event2 = events[2] as! ExampleToken.TokensWithdrawn + let event3 = events[3] as! ExampleToken.TokensWithdrawn + Test.assertEqual(event2.amount + event3.amount, allowance) +} + +access(self) +fun setupExampleToken(account: Test.Account) { + let txResult = executeTransaction( + "../cadence/transactions/exampletoken/setup.cdc", + [], + account + ) + Test.expect(txResult, Test.beSucceeded()) +} + +access(self) +fun mintExampleToken(recipient: Test.Account, amount: UFix64) { + let txResult = executeTransaction( + "../cadence/transactions/exampletoken/mint.cdc", + [recipient.address, amount], + admin + ) + Test.expect(txResult, Test.beSucceeded()) +} diff --git a/test/ScopedNFTProviders_test.cdc b/test/ScopedNFTProviders_test.cdc new file mode 100644 index 0000000..055063e --- /dev/null +++ b/test/ScopedNFTProviders_test.cdc @@ -0,0 +1,180 @@ +import Test +import BlockchainHelpers +import "ExampleNFT" + +access(all) let admin = Test.getAccount(0x0000000000000007) +access(all) let alice = Test.createAccount() + +access(all) +fun setup() { + var err = Test.deployContract( + name: "ArrayUtils", + path: "../cadence/contracts/ArrayUtils.cdc", + arguments: [] + ) + Test.expect(err, Test.beNil()) + + err = Test.deployContract( + name: "StringUtils", + path: "../cadence/contracts/StringUtils.cdc", + arguments: [] + ) + Test.expect(err, Test.beNil()) + + err = Test.deployContract( + name: "ExampleNFT", + path: "../cadence/contracts/ExampleNFT.cdc", + arguments: [] + ) + Test.expect(err, Test.beNil()) + + err = Test.deployContract( + name: "ScopedNFTProviders", + path: "../cadence/contracts/ScopedNFTProviders.cdc", + arguments: [] + ) + Test.expect(err, Test.beNil()) +} + +access(all) +fun beforeEach() { + let txResult = executeTransaction( + "../cadence/transactions/examplenft/destroy.cdc", + [], + alice + ) + Test.expect(txResult, Test.beSucceeded()) +} + +access(all) +fun testWithdrawNFTSuccessfully() { + setupExampleNFT(account: alice) + let id = mintExampleNFT(recipient: alice) + let ids: [UInt64] = [id] + + let txResult = executeTransaction( + "../cadence/transactions/scopedproviders/nft/withdraw_scoped_nft.cdc", + [ids, id], + alice + ) + Test.expect(txResult, Test.beSucceeded()) + + let events = Test.eventsOfType(Type()) + let event = events[events.length - 1] as! ExampleNFT.Withdraw + Test.assertEqual(event.id, id) +} + +access(all) +fun testWithdrawNFTSuccessfullyBeforeExpiration() { + setupExampleNFT(account: alice) + let id = mintExampleNFT(recipient: alice) + let ids: [UInt64] = [id] + + let txResult = executeTransaction( + "../cadence/transactions/scopedproviders/nft/withdraw_scoped_nft_before_expiration.cdc", + [ids, id], + alice + ) + Test.expect(txResult, Test.beSucceeded()) + + let events = Test.eventsOfType(Type()) + let event = events[events.length - 1] as! ExampleNFT.Withdraw + Test.assertEqual(event.id, id) +} + +access(all) +fun testFailsToWithdrawNFT() { + setupExampleNFT(account: alice) + let id = mintExampleNFT(recipient: alice) + let id2 = mintExampleNFT(recipient: alice) + let ids: [UInt64] = [id] + + let txResult = executeTransaction( + "../cadence/transactions/scopedproviders/nft/withdraw_scoped_nft.cdc", + [ids, id2], + alice + ) + Test.expect(txResult, Test.beFailed()) + Test.assertError( + txResult, + errorMessage: "cannot withdraw nft. filter of type A.0000000000000007.ScopedNFTProviders.NFTIDFilter failed." + ) +} + +access(all) +fun testFailsToWithdrawNFTPastExpiration() { + setupExampleNFT(account: alice) + let id = mintExampleNFT(recipient: alice) + let ids: [UInt64] = [id] + + let txResult = executeTransaction( + "../cadence/transactions/scopedproviders/nft/withdraw_scoped_nft_past_expiration.cdc", + [ids, id], + alice + ) + Test.expect(txResult, Test.beFailed()) + Test.assertError( + txResult, + errorMessage: "provider has expired" + ) +} + +access(all) +fun testShouldWithdrawSuccessfully() { + setupExampleNFT(account: alice) + let id = mintExampleNFT(recipient: alice) + let ids: [UInt64] = [id] + + let txResult = executeTransaction( + "../cadence/transactions/scopedproviders/nft/withdraw_scoped_nft.cdc", + [ids, id], + alice + ) + Test.expect(txResult, Test.beSucceeded()) + + let events = Test.eventsOfType(Type()) + let event = events[events.length - 1] as! ExampleNFT.Withdraw + Test.assertEqual(event.id, id) +} + +access(all) +fun testFailsToWithdrawTwice() { + setupExampleNFT(account: alice) + let id = mintExampleNFT(recipient: alice) + let ids: [UInt64] = [id] + + let txResult = executeTransaction( + "../cadence/transactions/scopedproviders/nft/withdraw_scoped_nft_twice.cdc", + [ids, id], + alice + ) + Test.expect(txResult, Test.beFailed()) + Test.assertError( + txResult, + errorMessage: "cannot withdraw nft. filter of type A.0000000000000007.ScopedNFTProviders.NFTIDFilter failed." + ) +} + +access(self) +fun setupExampleNFT(account: Test.Account) { + let txResult = executeTransaction( + "../cadence/transactions/examplenft/setup.cdc", + [], + account + ) + Test.expect(txResult, Test.beSucceeded()) +} + +access(self) +fun mintExampleNFT(recipient: Test.Account): UInt64 { + let txResult = executeTransaction( + "../cadence/transactions/examplenft/mint.cdc", + [recipient.address], + admin + ) + Test.expect(txResult, Test.beSucceeded()) + + let events = Test.eventsOfType(Type()) + let event = events[events.length - 1] as! ExampleNFT.Deposit + return event.id +} From 3ef28eb9bc599b1f8ee242a65940ce939418eeca Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Wed, 1 Nov 2023 13:22:10 +0200 Subject: [PATCH 4/5] Replace JS testing with Cadence testing framework --- .github/workflows/unit-tests.yml | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 140e168..b7785e3 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -5,22 +5,7 @@ on: push: branches: [main] jobs: - js-tests: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 - with: - node-version: ^16 - - name: Install flow cli - run: sh -ci "$(curl -fsSL https://storage.googleapis.com/flow-cli/install.sh)" - - name: Start flow emulator - run: nohup flow emulator & - - name: Install dependencies - run: npm ci - - name: Run tests - run: npm test -- --runInBand - cadence-tests: + tests: name: Flow CLI Tests runs-on: ubuntu-latest steps: From 5172b23037f7a0f627143a18abef4d4d9714ed22 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Wed, 1 Nov 2023 18:15:32 +0200 Subject: [PATCH 5/5] Update unit testing workflow to use setup-go@v4 action Co-authored-by: Bjarte S. Karlsen --- .github/workflows/unit-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index b7785e3..a6dc5fa 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -13,7 +13,7 @@ jobs: with: submodules: "true" - name: Set up Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v4 with: go-version: 1.20 - name: Install Flow CLI