Skip to content

Commit

Permalink
Merge pull request #242 from UnUniFi/chore/shared/test/utils/converter
Browse files Browse the repository at this point in the history
Add tests for shared/util
  • Loading branch information
YasunoriMATSUOKA authored Jul 7, 2022
2 parents 245e400 + a0ff094 commit e0e1065
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"ngschematics.schematics": ["./node_modules/angular-firebase-schematics/src/collection.json"]
"ngschematics.schematics": ["./node_modules/angular-firebase-schematics/src/collection.json"],
"jest.jestCommandLine": "ng test"
}
23 changes: 23 additions & 0 deletions projects/shared/src/lib/utils/Account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import cosmosClient from '@cosmos-client/core';

export type BaseAccount = cosmosClient.proto.cosmos.auth.v1beta1.BaseAccount;
export const BaseAccount = cosmosClient.proto.cosmos.auth.v1beta1.BaseAccount;
export type BaseVestingAccount = cosmosClient.proto.cosmos.vesting.v1beta1.BaseVestingAccount;
export const BaseVestingAccount = cosmosClient.proto.cosmos.vesting.v1beta1.BaseVestingAccount;
export type ContinuousVestingAccount =
cosmosClient.proto.cosmos.vesting.v1beta1.ContinuousVestingAccount;
export const ContinuousVestingAccount =
cosmosClient.proto.cosmos.vesting.v1beta1.ContinuousVestingAccount;
export type DelayedVestingAccount = cosmosClient.proto.cosmos.vesting.v1beta1.DelayedVestingAccount;
export const DelayedVestingAccount =
cosmosClient.proto.cosmos.vesting.v1beta1.DelayedVestingAccount;
export type PeriodicVestingAccount =
cosmosClient.proto.cosmos.vesting.v1beta1.PeriodicVestingAccount;
export const PeriodicVestingAccount =
cosmosClient.proto.cosmos.vesting.v1beta1.PeriodicVestingAccount;
export type PermanentLockedAccount =
cosmosClient.proto.cosmos.vesting.v1beta1.PermanentLockedAccount;
export const PermanentLockedAccount =
cosmosClient.proto.cosmos.vesting.v1beta1.PermanentLockedAccount;
export type ModuleAccount = cosmosClient.proto.cosmos.auth.v1beta1.ModuleAccount;
export const ModuleAccount = cosmosClient.proto.cosmos.auth.v1beta1.ModuleAccount;
141 changes: 141 additions & 0 deletions projects/shared/src/lib/utils/converter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import {
BaseAccount,
BaseVestingAccount,
ContinuousVestingAccount,
DelayedVestingAccount,
ModuleAccount,
PeriodicVestingAccount,
PermanentLockedAccount,
} from './Account';
import {
convertHexStringToUint8Array,
convertTypedAccountToTypedName,
convertUint8ArrayToHexString,
convertUnknownAccountToBaseAccount,
convertUnknownAccountToTypedAccount,
} from './converter';

describe('convertHexStringToUint8Array', () => {
it('convert hex string to uint8array', () => {
const hexString = 'bada55';
const uint8Array = convertHexStringToUint8Array(hexString);
expect(uint8Array).toEqual(new Uint8Array([186, 218, 85]));
});

it('returns undefined when passing an empty string', () => {
const hexString = '';
const uint8Array = convertHexStringToUint8Array(hexString);
expect(uint8Array).toBeUndefined();
});
});

describe('convertUint8ArrayToHexString', () => {
it('convert uint8array to hex string', () => {
const uint8Array = new Uint8Array([186, 218, 85]);
const hexString = convertUint8ArrayToHexString(uint8Array);
expect(hexString).toEqual('bada55');
});

it('returns empty string when passing an empty uint8array', () => {
const uint8Array = new Uint8Array([]);
const hexString = convertUint8ArrayToHexString(uint8Array);
expect(hexString).toBe('');
});
});

describe('convertUnknownAccountToTypedAccount', () => {
it('convert unknown account to typed account', () => {
const unknownAccount = new BaseAccount();
const typedAccount = convertUnknownAccountToTypedAccount(unknownAccount);
expect(typedAccount).toEqual(unknownAccount);
});

it('convert invalid data to undefined', () => {
const unknownAccount = 'hoge';
const typedAccount = convertUnknownAccountToTypedAccount(unknownAccount);
expect(typedAccount).toBeUndefined();
});
});

describe('convertUnknownAccountToBaseAccount', () => {
const setup = () => {
const baseAccount = new BaseAccount({ address: '0x123' });
const baseVestingAccount = new BaseVestingAccount({ base_account: baseAccount });
return { baseAccount, baseVestingAccount };
};

it('convert BaseAccount to base account', () => {
const { baseAccount } = setup();
const typedAccount = convertUnknownAccountToBaseAccount(baseAccount);
expect(typedAccount).toEqual(baseAccount);
expect(typedAccount?.address).toBe(baseAccount.address);
});

it('convert BaseVestingAccount to base account', () => {
const { baseAccount, baseVestingAccount } = setup();
const typedAccount = convertUnknownAccountToBaseAccount(baseVestingAccount);
expect(typedAccount).toEqual(baseAccount);
expect(typedAccount?.address).toBe(baseAccount.address);
});

it('convert PermanentLockedAccount to base account', () => {
const { baseAccount } = setup();
const unknownAccount = new ModuleAccount({
base_account: baseAccount,
});
const typedAccount = convertUnknownAccountToBaseAccount(unknownAccount);
expect(typedAccount).toEqual(baseAccount);
expect(typedAccount?.address).toBe(baseAccount.address);
});

it.each`
Account
${ContinuousVestingAccount}
${DelayedVestingAccount}
${PeriodicVestingAccount}
${PermanentLockedAccount}
`('convert $Account to base account', ({ Account }) => {
const { baseAccount, baseVestingAccount } = setup();
const unknownAccount = new Account({ base_vesting_account: baseVestingAccount });
const typedAccount = convertUnknownAccountToBaseAccount(unknownAccount);
expect(typedAccount).toEqual(baseAccount);
expect(typedAccount?.address).toBe(baseAccount.address);
});

it.each`
arg | expected
${'hoge'} | ${undefined}
${undefined} | ${undefined}
${null} | ${null}
`('convert $arg to $expected', ({ arg, expected }) => {
const typedAccount = convertUnknownAccountToBaseAccount(arg);
expect(typedAccount).toEqual(expected);
});
});

describe('convertTypedAccountToTypedName', () => {
it.each`
Account | name
${BaseAccount} | ${'BaseAccount'}
${BaseVestingAccount} | ${'BaseVestingAccount'}
${ContinuousVestingAccount} | ${'ContinuousVestingAccount'}
${DelayedVestingAccount} | ${'DelayedVestingAccount'}
${PeriodicVestingAccount} | ${'PeriodicVestingAccount'}
${PermanentLockedAccount} | ${'PermanentLockedAccount'}
${ModuleAccount} | ${'ModuleAccount'}
`('convert $Account to $name', ({ Account, name }) => {
const account = new Account();
const typedAccount = convertTypedAccountToTypedName(account);
expect(typedAccount).toEqual(name);
});

it.each`
arg | expected
${'hoge'} | ${undefined}
${undefined} | ${undefined}
${null} | ${null}
`('convert $arg to $expected', ({ arg, expected }) => {
const typedAccount = convertTypedAccountToTypedName(arg);
expect(typedAccount).toEqual(expected);
});
});
102 changes: 51 additions & 51 deletions projects/shared/src/lib/utils/converter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import cosmosclient from '@cosmos-client/core';
import {
BaseAccount,
BaseVestingAccount,
ContinuousVestingAccount,
DelayedVestingAccount,
ModuleAccount,
PeriodicVestingAccount,
PermanentLockedAccount,
} from './Account';

export const convertHexStringToUint8Array = (hexString: string): Uint8Array | undefined => {
try {
Expand Down Expand Up @@ -28,28 +36,28 @@ export const convertUint8ArrayToHexString = (uint8Array: Uint8Array): string | u
export const convertUnknownAccountToTypedAccount = (
unknownAccount: unknown,
):
| cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount
| cosmosclient.proto.cosmos.vesting.v1beta1.BaseVestingAccount
| cosmosclient.proto.cosmos.vesting.v1beta1.ContinuousVestingAccount
| cosmosclient.proto.cosmos.vesting.v1beta1.DelayedVestingAccount
| cosmosclient.proto.cosmos.vesting.v1beta1.PeriodicVestingAccount
| cosmosclient.proto.cosmos.vesting.v1beta1.PermanentLockedAccount
| cosmosclient.proto.cosmos.auth.v1beta1.ModuleAccount
| BaseAccount
| BaseVestingAccount
| ContinuousVestingAccount
| DelayedVestingAccount
| PeriodicVestingAccount
| PermanentLockedAccount
| ModuleAccount
| null
| undefined => {
if (unknownAccount instanceof cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount) {
if (unknownAccount instanceof BaseAccount) {
return unknownAccount;
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.BaseVestingAccount) {
} else if (unknownAccount instanceof BaseVestingAccount) {
return unknownAccount;
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.ContinuousVestingAccount) {
} else if (unknownAccount instanceof ContinuousVestingAccount) {
return unknownAccount;
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.DelayedVestingAccount) {
} else if (unknownAccount instanceof DelayedVestingAccount) {
return unknownAccount;
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.PeriodicVestingAccount) {
} else if (unknownAccount instanceof PeriodicVestingAccount) {
return unknownAccount;
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.PermanentLockedAccount) {
} else if (unknownAccount instanceof PermanentLockedAccount) {
return unknownAccount;
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.auth.v1beta1.ModuleAccount) {
} else if (unknownAccount instanceof ModuleAccount) {
return unknownAccount;
} else if (unknownAccount === undefined) {
return unknownAccount;
Expand All @@ -64,51 +72,43 @@ export const convertUnknownAccountToTypedAccount = (

export const convertUnknownAccountToBaseAccount = (
unknownAccount: unknown,
): cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount | null | undefined => {
): BaseAccount | null | undefined => {
if (unknownAccount === undefined) {
return unknownAccount;
} else if (unknownAccount === null) {
return unknownAccount;
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount) {
} else if (unknownAccount instanceof BaseAccount) {
return unknownAccount;
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.BaseVestingAccount) {
} else if (unknownAccount instanceof BaseVestingAccount) {
if (unknownAccount.base_account === null) {
return undefined;
}
return new cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount(unknownAccount.base_account);
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.ContinuousVestingAccount) {
return new BaseAccount(unknownAccount.base_account);
} else if (unknownAccount instanceof ContinuousVestingAccount) {
if (unknownAccount.base_vesting_account?.base_account === null) {
return undefined;
}
return new cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount(
unknownAccount.base_vesting_account?.base_account,
);
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.DelayedVestingAccount) {
return new BaseAccount(unknownAccount.base_vesting_account?.base_account);
} else if (unknownAccount instanceof DelayedVestingAccount) {
if (unknownAccount.base_vesting_account?.base_account === null) {
return undefined;
}
return new cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount(
unknownAccount.base_vesting_account?.base_account,
);
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.PeriodicVestingAccount) {
return new BaseAccount(unknownAccount.base_vesting_account?.base_account);
} else if (unknownAccount instanceof PeriodicVestingAccount) {
if (unknownAccount.base_vesting_account?.base_account === null) {
return undefined;
}
return new cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount(
unknownAccount.base_vesting_account?.base_account,
);
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.PermanentLockedAccount) {
return new BaseAccount(unknownAccount.base_vesting_account?.base_account);
} else if (unknownAccount instanceof PermanentLockedAccount) {
if (unknownAccount.base_vesting_account?.base_account === null) {
return undefined;
}
return new cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount(
unknownAccount.base_vesting_account?.base_account,
);
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.auth.v1beta1.ModuleAccount) {
return new BaseAccount(unknownAccount.base_vesting_account?.base_account);
} else if (unknownAccount instanceof ModuleAccount) {
if (unknownAccount.base_account === null) {
return undefined;
}
return new cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount(unknownAccount.base_account);
return new BaseAccount(unknownAccount.base_account);
} else {
console.error('Unsupported Account!');
console.error(unknownAccount);
Expand All @@ -118,29 +118,29 @@ export const convertUnknownAccountToBaseAccount = (

export const convertTypedAccountToTypedName = (
unknownAccount:
| cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount
| cosmosclient.proto.cosmos.vesting.v1beta1.BaseVestingAccount
| cosmosclient.proto.cosmos.vesting.v1beta1.ContinuousVestingAccount
| cosmosclient.proto.cosmos.vesting.v1beta1.DelayedVestingAccount
| cosmosclient.proto.cosmos.vesting.v1beta1.PeriodicVestingAccount
| cosmosclient.proto.cosmos.vesting.v1beta1.PermanentLockedAccount
| cosmosclient.proto.cosmos.auth.v1beta1.ModuleAccount
| BaseAccount
| BaseVestingAccount
| ContinuousVestingAccount
| DelayedVestingAccount
| PeriodicVestingAccount
| PermanentLockedAccount
| ModuleAccount
| null
| undefined,
): string | null | undefined => {
if (unknownAccount instanceof cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount) {
if (unknownAccount instanceof BaseAccount) {
return 'BaseAccount';
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.BaseVestingAccount) {
} else if (unknownAccount instanceof BaseVestingAccount) {
return 'BaseVestingAccount';
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.ContinuousVestingAccount) {
} else if (unknownAccount instanceof ContinuousVestingAccount) {
return 'ContinuousVestingAccount';
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.DelayedVestingAccount) {
} else if (unknownAccount instanceof DelayedVestingAccount) {
return 'DelayedVestingAccount';
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.PeriodicVestingAccount) {
} else if (unknownAccount instanceof PeriodicVestingAccount) {
return 'PeriodicVestingAccount';
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.vesting.v1beta1.PermanentLockedAccount) {
} else if (unknownAccount instanceof PermanentLockedAccount) {
return 'PermanentLockedAccount';
} else if (unknownAccount instanceof cosmosclient.proto.cosmos.auth.v1beta1.ModuleAccount) {
} else if (unknownAccount instanceof ModuleAccount) {
return 'ModuleAccount';
} else if (unknownAccount === undefined) {
return unknownAccount;
Expand Down
Loading

0 comments on commit e0e1065

Please sign in to comment.