From eea6cb55ce164745028d0c1a091942346efdb7d6 Mon Sep 17 00:00:00 2001 From: dafuga Date: Sat, 29 Jul 2023 01:11:37 +0100 Subject: [PATCH] chore: added tests for utils functions --- contracts/rewards.gm.ts | 2 +- src/utils.ts | 2 +- test/tests/codegen.ts | 12 +++---- test/tests/utils.ts | 77 +++++++++++++++++++++++++++++++++++++++++ test/utils/codegen.ts | 1 - 5 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 test/tests/utils.ts diff --git a/contracts/rewards.gm.ts b/contracts/rewards.gm.ts index e626f0d..dcb0c41 100644 --- a/contracts/rewards.gm.ts +++ b/contracts/rewards.gm.ts @@ -1,4 +1,4 @@ -import { Contract, ContractArgs, blobStringToAbi } from "../src/index-module"; +import { Contract, ContractArgs, blobStringToAbi } from "@wharfkit/contract"; import { ABI, APIClient, Session, Struct, TransactResult, Asset, Checksum256, Float64, Name, TimePoint, TimePointSec, UInt128, UInt16, UInt32, UInt64, UInt8, AssetType, Checksum256Type, Float64Type, NameType, TimePointType, UInt128Type, UInt16Type, UInt32Type, UInt64Type, UInt8Type } from "@wharfkit/session"; export class _RewardsGm extends Contract { private static abiBlob = "DmVvc2lvOjphYmkvMS4yAAoHYWRkdXNlcgACB2FjY291bnQEbmFtZQZ3ZWlnaHQGdWludDE2BWNsYWltAAIHYWNjb3VudARuYW1lBmFtb3VudAZhc3NldD8GY29uZmlnAAMMdG9rZW5fc3ltYm9sBnN5bWJvbA5vcmFjbGVfYWNjb3VudARuYW1lDG9yYWNsZV9wYWlycw1vcmFjbGVfcGFpcltdCWNvbmZpZ3VyZQADDHRva2VuX3N5bWJvbAZzeW1ib2wOb3JhY2xlX2FjY291bnQEbmFtZQxvcmFjbGVfcGFpcnMNb3JhY2xlX3BhaXJbXQdkZWx1c2VyAAEHYWNjb3VudARuYW1lC29yYWNsZV9wYWlyAAIEbmFtZQRuYW1lCXByZWNpc2lvbgZ1aW50MTYKcHJpY2VfaW5mbwADBHBhaXIGc3RyaW5nBXByaWN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/utils.ts index c8c233d..c774a3c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -32,7 +32,7 @@ export function capitalize(string) { export function singularize(word: string): string { if (word.endsWith('ies')) { return word.slice(0, -3) + 'y' - } else if (word.endsWith('ches') || word.endsWith('sses')) { + } else if (word.endsWith('ches') || word.endsWith('ses')) { return word.slice(0, -2) } else if (word.endsWith('s') && word.length > 1 && word[word.length - 2] !== 's') { return word.slice(0, -1) diff --git a/test/tests/codegen.ts b/test/tests/codegen.ts index 7e42e6d..ba3f899 100644 --- a/test/tests/codegen.ts +++ b/test/tests/codegen.ts @@ -1,18 +1,15 @@ -import * as fs from 'fs' -import * as path from 'path' import {assert} from 'chai' -import {ABI, APIClient, Session} from '@wharfkit/session' +import {APIClient} from '@wharfkit/session' import {makeClient} from '@wharfkit/mock-data' -import {codegen} from '../../src/codegen' // replace with your actual codegen file -import { Contract } from 'src/contract' -import { generateCodegenContract, removeCodegenContracts } from '$test/utils/codegen' +import {Contract} from 'src/contract' +import {generateCodegenContract, removeCodegenContracts} from '$test/utils/codegen' let _RewardsGm suite('codegen', function () { setup(async () => { - const contractName = 'rewards.gm' // replace with your contract name + const contractName = 'rewards.gm' const contractPackage = await generateCodegenContract(contractName) @@ -20,7 +17,6 @@ suite('codegen', function () { }) teardown(() => { - // Remove the 'test/tmp' directory and its contents after each run removeCodegenContracts() }) diff --git a/test/tests/utils.ts b/test/tests/utils.ts new file mode 100644 index 0000000..c76914b --- /dev/null +++ b/test/tests/utils.ts @@ -0,0 +1,77 @@ +import { assert } from 'chai' +import fs from 'fs' + +import { + ABI, + Blob, + Name, + Serializer, + UInt128, + UInt64, +} from '@wharfkit/antelope' +import { + pascalCase, + capitalize, + singularize, + indexPositionInWords, + wrapIndexValue, + abiToBlob, + blobStringToAbi +} from '../../src/utils' // replace 'your-file' with the name of your file + +suite('Utility functions', function () { + test('Converts to pascal case', function () { + assert.equal(pascalCase('hello_world'), 'HelloWorld'); + }); + + test('Capitalizes string', function () { + assert.equal(capitalize('hello'), 'Hello'); + assert.equal(capitalize(''), ''); + }); + + test('Singularizes word', function () { + assert.equal(singularize('bodies'), 'body'); + assert.equal(singularize('watches'), 'watch'); + assert.equal(singularize('buses'), 'bus'); + assert.equal(singularize('cats'), 'cat'); + }); + + test('Gets index position in words', function () { + assert.equal(indexPositionInWords(0), 'primary'); + assert.equal(indexPositionInWords(1), 'secondary'); + }); + + test('Wraps index value', function () { + assert.isUndefined(wrapIndexValue(undefined)); + assert.deepEqual(wrapIndexValue(UInt128.from('10')), UInt128.from('10')); + assert.deepEqual(wrapIndexValue(UInt64.from('10')), UInt64.from('10')); + assert.deepEqual(wrapIndexValue(10), UInt64.from('10')); + assert.deepEqual(wrapIndexValue('name'), Name.from('name')); + }); + + const testABI = ABI.from( + fs.readFileSync(`test/data/abis/rewards.gm.json`, {encoding: 'utf8'}) + ) + + + // Blob created from the testABI + const testBlob = new Blob(Serializer.encode({object: testABI, type: ABI}).array); + + test('Converts ABI to Blob', function () { + const result = abiToBlob(testABI); + assert(result.equals(testBlob)); + }); + + test('Converts Blob string to ABI', function () { + const blobString = testBlob.toString(); // Assuming toString() gives us the string representation of the blob + const result = blobStringToAbi(blobString); + assert(result.equals(testABI)); + }); + + test('Check round trip conversion from ABI to Blob and back', function () { + const blob = abiToBlob(testABI); + const blobString = blob.toString(); // Assuming toString() gives us the string representation of the blob + const result = blobStringToAbi(blobString); + assert(result.equals(testABI)); + }); +}); diff --git a/test/utils/codegen.ts b/test/utils/codegen.ts index 8bf6cae..717cf69 100644 --- a/test/utils/codegen.ts +++ b/test/utils/codegen.ts @@ -1,7 +1,6 @@ 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(contractName: string) {