From fa2022b01cbaed6d6154904b0518d3d16e9e8f74 Mon Sep 17 00:00:00 2001 From: dafuga Date: Sat, 29 Jul 2023 00:57:01 +0100 Subject: [PATCH] chore: added codegen tests --- src/codegen.ts | 2 +- test/tests/codegen.ts | 139 ++++++++++-------------------------------- test/utils/codegen.ts | 9 +-- 3 files changed, 39 insertions(+), 111 deletions(-) diff --git a/src/codegen.ts b/src/codegen.ts index 27c7952..e54e4e4 100644 --- a/src/codegen.ts +++ b/src/codegen.ts @@ -32,7 +32,7 @@ export async function codegen(contractName, abi) { ) const importContractStatement = generateImportStatement( ['Contract', 'ContractArgs', 'blobStringToAbi'], - '../src/index-module' //'@wharfkit/contract' + '@wharfkit/contract' ) const {classDeclaration} = await generateContractClass(namespaceName, contractName, abi) diff --git a/test/tests/codegen.ts b/test/tests/codegen.ts index 37ac366..7e42e6d 100644 --- a/test/tests/codegen.ts +++ b/test/tests/codegen.ts @@ -1,116 +1,43 @@ -// import * as fs from 'fs' -// import * as path from 'path' -// import {assert} from 'chai' -// import {ABI, APIClient, Session} from '@wharfkit/session' -// import {makeClient} from '@wharfkit/mock-data' +import * as fs from 'fs' +import * as path from 'path' +import {assert} from 'chai' +import {ABI, APIClient, Session} from '@wharfkit/session' +import {makeClient} from '@wharfkit/mock-data' -// import {codegen} from '../../src/codegen' // replace with your actual codegen file +import {codegen} from '../../src/codegen' // replace with your actual codegen file +import { Contract } from 'src/contract' +import { generateCodegenContract, removeCodegenContracts } from '$test/utils/codegen' -// let _RewardsGm +let _RewardsGm -// suite('codegen', function () { -// setup(async () => { -// const contractName = 'rewards.gm' // replace with your contract name +suite('codegen', function () { + setup(async () => { + const contractName = 'rewards.gm' // replace with your contract name -// // Read the ABI from a JSON file -// const abiJson = fs.readFileSync(`test/data/abis/${contractName}.json`, {encoding: 'utf8'}) -// const abi = new ABI(JSON.parse(abiJson)) + const contractPackage = await generateCodegenContract(contractName) -// // Generate the code -// let generatedCode = await codegen(contractName, abi) + _RewardsGm = contractPackage._RewardsGm + }) -// generatedCode = generatedCode.replace('@wharfkit/contract', '../../src/index') + teardown(() => { + // Remove the 'test/tmp' directory and its contents after each run + removeCodegenContracts() + }) -// // Create the tmp directory under the test directory if it does not exist -// if (!fs.existsSync('test/tmp')) { -// fs.mkdirSync('test/tmp') -// } + suite('_RewardsGm', function () { + let client: APIClient -// // Write the generated code to a file in the tmp directory -// fs.writeFileSync(path.join('test/tmp', `${contractName}.ts`), generatedCode, { -// encoding: 'utf8', -// }) + // Setup before each test + setup(function () { + client = makeClient('https://eos.greymass.com') + }) -// const contractPackage = await import(`../tmp/${contractName}`) + suite('constructor', function () { + test('constructs the Contract instance', async function () { + const rewardsContract = new _RewardsGm({client}) -// _RewardsGm = contractPackage._RewardsGm -// }) - -// teardown(() => { -// // Remove the 'test/tmp' directory and its contents after each run -// fs.rmSync('test/tmp', {recursive: true, force: true}) -// }) - -// suite('_RewardsGm', function () { -// let client: APIClient - -// // Setup before each test -// setup(function () { -// client = makeClient('https://eos.greymass.com') -// }) - -// suite('adduser', function () { -// test('adduser calls the correct contract action', async function () { -// let calledWith: any -// const session = { -// transact: async (transactParam) => { -// calledWith = transactParam -// }, -// } - -// await _RewardsGm.actions.adduser( -// { -// account: 'teamgreymass', -// weight: 100, -// }, -// session as unknown as Session -// ) - -// assert.equal(String(calledWith.action.account), 'rewards.gm') -// assert.equal(String(calledWith.action.name), 'adduser') -// }) -// }) - -// suite('where', function () { -// test('returns a cursor that lets you filter rows', async function () { -// const rows = await _RewardsGm.tables.users -// .where({account: {from: 'dafuga.gm', to: 'tony.gm'}}, {}, client) -// .all() - -// assert.equal(rows.length, 6) -// }) -// }) - -// suite('find', function () { -// test('returns a single row', async function () { -// const row = await _RewardsGm.tables.users.find({account: 'dafuga.gm'}, client) - -// assert.equal(String(row.account), 'dafuga.gm') -// }) -// }) - -// suite('first', function () { -// test('returns a cursor that lets you fetch the first n rows', async function () { -// const rows = await _RewardsGm.tables.users.first(10, client).all() - -// assert.equal(rows.length, 9) -// }) -// }) - -// suite('cursor', function () { -// test('returns a cursor that allows you to fetch all rows', async function () { -// const rows = await _RewardsGm.tables.users.cursor(client).all() - -// assert.equal(rows.length, 9) -// }) -// }) - -// suite('all', function () { -// test('returns all rows', async function () { -// const rows = await _RewardsGm.tables.users.all(client) - -// assert.equal(rows.length, 9) -// }) -// }) -// }) -// }) + assert.instanceOf(rewardsContract, Contract) + }) + }) + }) +}) diff --git a/test/utils/codegen.ts b/test/utils/codegen.ts index 9c7d851..8bf6cae 100644 --- a/test/utils/codegen.ts +++ b/test/utils/codegen.ts @@ -2,11 +2,12 @@ import fs from 'fs' import path from 'path' import {codegen} from '../../src/codegen' import {Contract} from 'src/contract' +import { ABI } from '@wharfkit/session' -export async function generateCodegenContract(contract: Contract) { - const contractName = String(contract.account) - - const abi = await contract.getAbi() +export async function generateCodegenContract(contractName: string) { + // Read the ABI from a JSON file + const abiJson = fs.readFileSync(`test/data/abis/${contractName}.json`, {encoding: 'utf8'}) + const abi = new ABI(JSON.parse(abiJson)) // Generate the code let generatedCode = await codegen(contractName, abi)