Skip to content

Commit

Permalink
Verify expectAssertionError for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
h0ngcha0 committed Apr 11, 2024
1 parent a6759f8 commit 5a40711
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
9 changes: 8 additions & 1 deletion .project.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"Assert": {
"sourceFile": "test/assert.ral",
"sourceCodeHash": "6e29a32037532fe7c415d6a8076626ee81767ff3162d3d2f34d08f7e0594a914",
"sourceCodeHash": "65f8387941a38e88ef555f5d0b4992dab390d97ff97b2473fdea45b8d58a74a5",
"bytecodeDebugPatch": "",
"codeHashDebug": "5bd05924fb9a23ea105df065a8c2dfa463b9ee53cc14a60320140d19dd6151ca",
"warnings": [
Expand Down Expand Up @@ -277,6 +277,13 @@
"codeHashDebug": "",
"warnings": []
},
"TestAssert": {
"sourceFile": "test/assert.ral",
"sourceCodeHash": "65f8387941a38e88ef555f5d0b4992dab390d97ff97b2473fdea45b8d58a74a5",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
},
"TokenBalance": {
"sourceFile": "test/struct.ral",
"sourceCodeHash": "7690ad7d8b8c4b952fc6f439eb75648164532e055e6a47881882097c0600bb6a",
Expand Down
28 changes: 28 additions & 0 deletions artifacts/test/TestAssert.ral.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
5 changes: 5 additions & 0 deletions artifacts/ts/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions contracts/test/assert.ral
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ Contract Assert() {
assert!(1 == 2, Error)
}
}

TxScript TestAssert(assert: Assert) {
assert.test()
}
28 changes: 22 additions & 6 deletions test/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -44,6 +45,7 @@ import {
Main,
RemoveFromMap,
TemplateArrayVar,
TestAssert,
UpdateUserAccount
} from '../artifacts/ts/scripts'
import { Sub, SubTypes } from '../artifacts/ts/Sub'
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 5a40711

Please sign in to comment.