From 5a407115ba4a5ad85facc22a31b58e50681c66b7 Mon Sep 17 00:00:00 2001 From: h0ngcha0 Date: Fri, 12 Apr 2024 00:03:26 +0200 Subject: [PATCH] Verify expectAssertionError for integration tests --- .project.json | 9 ++++++++- artifacts/test/TestAssert.ral.json | 28 ++++++++++++++++++++++++++++ artifacts/ts/scripts.ts | 5 +++++ contracts/test/assert.ral | 4 ++++ test/contract.test.ts | 28 ++++++++++++++++++++++------ 5 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 artifacts/test/TestAssert.ral.json diff --git a/.project.json b/.project.json index ac7977450..5105d541e 100644 --- a/.project.json +++ b/.project.json @@ -22,7 +22,7 @@ }, "Assert": { "sourceFile": "test/assert.ral", - "sourceCodeHash": "6e29a32037532fe7c415d6a8076626ee81767ff3162d3d2f34d08f7e0594a914", + "sourceCodeHash": "65f8387941a38e88ef555f5d0b4992dab390d97ff97b2473fdea45b8d58a74a5", "bytecodeDebugPatch": "", "codeHashDebug": "5bd05924fb9a23ea105df065a8c2dfa463b9ee53cc14a60320140d19dd6151ca", "warnings": [ @@ -277,6 +277,13 @@ "codeHashDebug": "", "warnings": [] }, + "TestAssert": { + "sourceFile": "test/assert.ral", + "sourceCodeHash": "65f8387941a38e88ef555f5d0b4992dab390d97ff97b2473fdea45b8d58a74a5", + "bytecodeDebugPatch": "", + "codeHashDebug": "", + "warnings": [] + }, "TokenBalance": { "sourceFile": "test/struct.ral", "sourceCodeHash": "7690ad7d8b8c4b952fc6f439eb75648164532e055e6a47881882097c0600bb6a", diff --git a/artifacts/test/TestAssert.ral.json b/artifacts/test/TestAssert.ral.json new file mode 100644 index 000000000..3dbb2e8bc --- /dev/null +++ b/artifacts/test/TestAssert.ral.json @@ -0,0 +1,28 @@ +{ + "version": "v2.11.0", + "name": "TestAssert", + "bytecodeTemplate": "010103000000040c0c{0}0100", + "fieldsSig": { + "names": [ + "assert" + ], + "types": [ + "Assert" + ], + "isMutable": [ + false + ] + }, + "functions": [ + { + "name": "main", + "usePreapprovedAssets": true, + "useAssetsInContract": false, + "isPublic": true, + "paramNames": [], + "paramTypes": [], + "paramIsMutable": [], + "returnTypes": [] + } + ] +} \ No newline at end of file diff --git a/artifacts/ts/scripts.ts b/artifacts/ts/scripts.ts index 80cbeba0e..ef08d8ea6 100644 --- a/artifacts/ts/scripts.ts +++ b/artifacts/ts/scripts.ts @@ -18,6 +18,7 @@ import { default as MainScriptJson } from "../add/Main.ral.json"; import { default as MintNFTTestScriptJson } from "../nft/MintNFTTest.ral.json"; import { default as RemoveFromMapScriptJson } from "../test/RemoveFromMap.ral.json"; import { default as TemplateArrayVarScriptJson } from "../test/TemplateArrayVar.ral.json"; +import { default as TestAssertScriptJson } from "../test/TestAssert.ral.json"; import { default as UpdateMapValueScriptJson } from "../test/UpdateMapValue.ral.json"; import { default as UpdateUserAccountScriptJson } from "../test/UpdateUserAccount.ral.json"; import { default as WithdrawNFTCollectionTestScriptJson } from "../nft/WithdrawNFTCollectionTest.ral.json"; @@ -60,6 +61,10 @@ export const TemplateArrayVar = new ExecutableScript<{ numbers1: [bigint, bigint, bigint]; }>(Script.fromJson(TemplateArrayVarScriptJson, "", AllStructs)); +export const TestAssert = new ExecutableScript<{ assert: HexString }>( + Script.fromJson(TestAssertScriptJson, "", AllStructs) +); + export const UpdateMapValue = new ExecutableScript<{ mapTest: HexString; key: Address; diff --git a/contracts/test/assert.ral b/contracts/test/assert.ral index 478ae4581..1285ced5f 100644 --- a/contracts/test/assert.ral +++ b/contracts/test/assert.ral @@ -23,3 +23,7 @@ Contract Assert() { assert!(1 == 2, Error) } } + +TxScript TestAssert(assert: Assert) { + assert.test() +} diff --git a/test/contract.test.ts b/test/contract.test.ts index c68a3d2b0..f4cd7efb6 100644 --- a/test/contract.test.ts +++ b/test/contract.test.ts @@ -32,7 +32,8 @@ import { groupOfAddress, ProjectArtifact, DEFAULT_NODE_COMPILER_OPTIONS, - DUST_AMOUNT + DUST_AMOUNT, + DEFAULT_GAS_AMOUNT } from '../packages/web3' import { Contract, Project, Script, getContractIdFromUnsignedTx } from '../packages/web3' import { expectAssertionError, testAddress, randomContractAddress, getSigner, mintToken } from '../packages/web3-test' @@ -44,6 +45,7 @@ import { Main, RemoveFromMap, TemplateArrayVar, + TestAssert, UpdateUserAccount } from '../artifacts/ts/scripts' import { Sub, SubTypes } from '../artifacts/ts/Sub' @@ -243,12 +245,12 @@ describe('contract', function () { it('should load source files by order', async () => { const sourceFiles = await Project['loadSourceFiles']('.', './contracts') // `loadSourceFiles` is a private method - expect(sourceFiles.length).toEqual(43) + expect(sourceFiles.length).toEqual(44) sourceFiles.slice(0, 23).forEach((c) => expect(c.type).toEqual(0)) // contracts - sourceFiles.slice(23, 33).forEach((s) => expect(s.type).toEqual(1)) // scripts - sourceFiles.slice(33, 35).forEach((i) => expect(i.type).toEqual(2)) // abstract class - sourceFiles.slice(35, 40).forEach((i) => expect(i.type).toEqual(3)) // interfaces - sourceFiles.slice(40).forEach((i) => expect(i.type).toEqual(4)) // structs + sourceFiles.slice(23, 34).forEach((s) => expect(s.type).toEqual(1)) // scripts + sourceFiles.slice(34, 36).forEach((i) => expect(i.type).toEqual(2)) // abstract class + sourceFiles.slice(36, 41).forEach((i) => expect(i.type).toEqual(3)) // interfaces + sourceFiles.slice(41).forEach((i) => expect(i.type).toEqual(4)) // structs }) it('should load contract from json', () => { @@ -299,6 +301,20 @@ describe('contract', function () { await Project.build({ errorOnWarnings: false }) const contractAddress = randomContractAddress() expectAssertionError(Assert.tests.test({ address: contractAddress }), contractAddress, 3) + + const assertDeployResult = await Assert.deploy(signer, { initialFields: {} }) + const assertAddress = assertDeployResult.contractInstance.address + + expectAssertionError(TestAssert.execute(signer, { initialFields: { assert: assertAddress } }), assertAddress, 3) + + expectAssertionError( + TestAssert.execute(signer, { + initialFields: { assert: assertAddress }, + gasAmount: DEFAULT_GAS_AMOUNT + }), + assertAddress, + 3 + ) }) it('should test enums and constants', async () => {