Skip to content

Commit

Permalink
chore: added tests for utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Jul 29, 2023
1 parent fa2022b commit eea6cb5
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 11 deletions.
2 changes: 1 addition & 1 deletion contracts/rewards.gm.ts
Original file line number Diff line number Diff line change
@@ -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 = "DmVvc2lvOjphYmkvMS4yAAoHYWRkdXNlcgACB2FjY291bnQEbmFtZQZ3ZWlnaHQGdWludDE2BWNsYWltAAIHYWNjb3VudARuYW1lBmFtb3VudAZhc3NldD8GY29uZmlnAAMMdG9rZW5fc3ltYm9sBnN5bWJvbA5vcmFjbGVfYWNjb3VudARuYW1lDG9yYWNsZV9wYWlycw1vcmFjbGVfcGFpcltdCWNvbmZpZ3VyZQADDHRva2VuX3N5bWJvbAZzeW1ib2wOb3JhY2xlX2FjY291bnQEbmFtZQxvcmFjbGVfcGFpcnMNb3JhY2xlX3BhaXJbXQdkZWx1c2VyAAEHYWNjb3VudARuYW1lC29yYWNsZV9wYWlyAAIEbmFtZQRuYW1lCXByZWNpc2lvbgZ1aW50MTYKcHJpY2VfaW5mbwADBHBhaXIGc3RyaW5nBXByaWNlB2Zsb2F0NjQJdGltZXN0YW1wCnRpbWVfcG9pbnQHcmVjZWlwdAADB2FjY291bnQEbmFtZQZhbW91bnQFYXNzZXQGdGlja2VyDHByaWNlX2luZm9bXQp1cGRhdGV1c2VyAAIHYWNjb3VudARuYW1lBndlaWdodAZ1aW50MTYIdXNl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4wIgp0aXRsZTogRGVsZXRlIHVzZXIKc3VtbWFyeTogJ0RlbGV0ZSB1c2VyIHt7bm93cmFwIGFjY291bnR9fScKaWNvbjogaHR0cHM6Ly9hbG1vc3QuZGlnaXRhbC9pbWFnZXMvbWlzY19pY29uLnBuZyM2ZjVlYTk3OGIwNGQwM2UwMThiMzc5YTJiYWM0YzEwYjVhOGZlMGNkNWU2ZTE1Yzg4ODI4ZGM5ODZiZTk2Y2ZmCi0tLQoKe3thY2NvdW50fX0gaXMgaXMgcmVtb3ZlZCBmcm9tIHRoZSByZXdhcmRzIHNoYXJpbmcgbGlzdC4KClVzZXJzIGNhbiBvbmx5IGJlIHJlbW92ZWQgaWYgdGhlaXIgcmV3YXJkcyBiYWxhbmNlIGlzIHplcm8uAAAAIFenkLoHcmVjZWlwdAAAwFVYq2xS1Qp1cGRhdGV1c2VygAItLS0Kc3BlY192ZXJzaW9uOiAiMC4yLjAiCnRpdGxlOiBVcGRhdGUgdXNlcgpzdW1tYXJ5OiAnVXBkYXRlIHVzZXIge3tub3dyYXAgYWNjb3VudH19JwppY29uOiBodHRwczovL2FsbW9zdC5kaWdpdGFsL2ltYWdlcy9taXNjX2ljb24ucG5nIzZmNWVhOTc4YjA0ZDAzZTAxOGIzNzlhMmJhYzRjMTBiNWE4ZmUwY2Q1ZTZlMTVjODg4MjhkYzk4NmJlOTZjZmYKLS0tCgp7e2FjY291bnR9fSBpcyB1cGRhdGVkIHRvIGhhdmUgd2VpZ2h0IHt7d2VpZ2h0fX0uAgAAAAAwtyZFA2k2NAAABmNvbmZpZwAAAAAAfBXWA2k2NAAACHVzZXJfcm93AAAAAA==";
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 4 additions & 8 deletions test/tests/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
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)

_RewardsGm = contractPackage._RewardsGm
})

teardown(() => {
// Remove the 'test/tmp' directory and its contents after each run
removeCodegenContracts()
})

Expand Down
77 changes: 77 additions & 0 deletions test/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});
1 change: 0 additions & 1 deletion test/utils/codegen.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit eea6cb5

Please sign in to comment.