aboutsummaryrefslogtreecommitdiffstats
path: root/packages/metacoin/test
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-03-28 17:05:36 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-04-02 17:34:29 +0800
commit4d9029bb0e3b215efdf165704c80d3bacef0e85a (patch)
tree8f699dcd0a7b5fba3c31c065a62d640407bba8c3 /packages/metacoin/test
parentbc49dde4d5d24b9a8b01459bde061f9b23fb5898 (diff)
downloaddexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.gz
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.bz2
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.lz
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.xz
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.zst
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.zip
Add metacoin example project
Diffstat (limited to 'packages/metacoin/test')
-rw-r--r--packages/metacoin/test/global_hooks.ts10
-rw-r--r--packages/metacoin/test/metacoin_test.ts62
-rw-r--r--packages/metacoin/test/utils/chai_setup.ts13
-rw-r--r--packages/metacoin/test/utils/coverage.ts22
-rw-r--r--packages/metacoin/test/utils/deployer.ts16
-rw-r--r--packages/metacoin/test/utils/web3_wrapper.ts30
6 files changed, 153 insertions, 0 deletions
diff --git a/packages/metacoin/test/global_hooks.ts b/packages/metacoin/test/global_hooks.ts
new file mode 100644
index 000000000..509dc6837
--- /dev/null
+++ b/packages/metacoin/test/global_hooks.ts
@@ -0,0 +1,10 @@
+import { env, EnvVars } from '@0xproject/dev-utils';
+
+import { coverage } from './utils/coverage';
+
+after('generate coverage report', async () => {
+ if (env.parseBoolean(EnvVars.SolidityCoverage)) {
+ const coverageSubprovider = coverage.getCoverageSubproviderSingleton();
+ await coverageSubprovider.writeCoverageAsync();
+ }
+});
diff --git a/packages/metacoin/test/metacoin_test.ts b/packages/metacoin/test/metacoin_test.ts
new file mode 100644
index 000000000..f2b396ac2
--- /dev/null
+++ b/packages/metacoin/test/metacoin_test.ts
@@ -0,0 +1,62 @@
+import { BlockchainLifecycle, devConstants } from '@0xproject/dev-utils';
+import { LogWithDecodedArgs } from '@0xproject/types';
+import { BigNumber } from '@0xproject/utils';
+import { Web3Wrapper } from '@0xproject/web3-wrapper';
+import * as chai from 'chai';
+
+import { MetacoinContract, TransferContractEventArgs } from '../src/contract_wrappers/metacoin';
+
+import { chaiSetup } from './utils/chai_setup';
+import { deployer } from './utils/deployer';
+import { web3Wrapper } from './utils/web3_wrapper';
+
+chaiSetup.configure();
+const { expect } = chai;
+const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
+
+describe('Metacoin', () => {
+ let metacoin: MetacoinContract;
+ const ownerAddress = devConstants.TESTRPC_FIRST_ADDRESS;
+ const INITIAL_BALANCE = new BigNumber(10000);
+ before(async () => {
+ const metacoinInstance = await deployer.deployAsync('Metacoin');
+ web3Wrapper.abiDecoder.addABI(metacoinInstance.abi);
+ metacoin = new MetacoinContract(web3Wrapper, metacoinInstance.abi, metacoinInstance.address);
+ });
+ beforeEach(async () => {
+ await blockchainLifecycle.startAsync();
+ });
+ afterEach(async () => {
+ await blockchainLifecycle.revertAsync();
+ });
+ describe('#constructor', () => {
+ it(`should initialy give ${INITIAL_BALANCE} tokens to the creator`, async () => {
+ const balance = await metacoin.balances.callAsync(ownerAddress);
+ expect(balance).to.be.bignumber.equal(INITIAL_BALANCE);
+ });
+ });
+ describe('#transfer', () => {
+ it(`should successfully transfer tokens`, async () => {
+ const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
+ const amount = INITIAL_BALANCE.div(2);
+ const oldBalance = await metacoin.balances.callAsync(ZERO_ADDRESS);
+ expect(oldBalance).to.be.bignumber.equal(0);
+ const txHash = await metacoin.transfer.sendTransactionAsync(
+ {
+ to: ZERO_ADDRESS,
+ amount,
+ },
+ { from: devConstants.TESTRPC_FIRST_ADDRESS },
+ );
+ const txReceipt = await web3Wrapper.awaitTransactionMinedAsync(txHash);
+ const transferLogs = txReceipt.logs[0] as LogWithDecodedArgs<TransferContractEventArgs>;
+ expect(transferLogs.args).to.be.deep.equal({
+ _to: ZERO_ADDRESS,
+ _from: devConstants.TESTRPC_FIRST_ADDRESS,
+ _value: amount,
+ });
+ const newBalance = await metacoin.balances.callAsync(ZERO_ADDRESS);
+ expect(newBalance).to.be.bignumber.equal(amount);
+ });
+ });
+});
diff --git a/packages/metacoin/test/utils/chai_setup.ts b/packages/metacoin/test/utils/chai_setup.ts
new file mode 100644
index 000000000..078edd309
--- /dev/null
+++ b/packages/metacoin/test/utils/chai_setup.ts
@@ -0,0 +1,13 @@
+import * as chai from 'chai';
+import chaiAsPromised = require('chai-as-promised');
+import ChaiBigNumber = require('chai-bignumber');
+import * as dirtyChai from 'dirty-chai';
+
+export const chaiSetup = {
+ configure() {
+ chai.config.includeStack = true;
+ chai.use(ChaiBigNumber());
+ chai.use(dirtyChai);
+ chai.use(chaiAsPromised);
+ },
+};
diff --git a/packages/metacoin/test/utils/coverage.ts b/packages/metacoin/test/utils/coverage.ts
new file mode 100644
index 000000000..5115dec0e
--- /dev/null
+++ b/packages/metacoin/test/utils/coverage.ts
@@ -0,0 +1,22 @@
+import { CoverageSubprovider } from '@0xproject/sol-cov';
+import * as _ from 'lodash';
+
+import { devConstants } from '@0xproject/dev-utils';
+
+let coverageSubprovider: CoverageSubprovider;
+
+export const coverage = {
+ getCoverageSubproviderSingleton(): CoverageSubprovider {
+ if (_.isUndefined(coverageSubprovider)) {
+ coverageSubprovider = coverage._getCoverageSubprovider();
+ }
+ return coverageSubprovider;
+ },
+ _getCoverageSubprovider(): CoverageSubprovider {
+ const artifactsPath = 'artifacts';
+ const contractsPath = 'contracts';
+ const networkId = 50;
+ const defaultFromAddress = devConstants.TESTRPC_FIRST_ADDRESS;
+ return new CoverageSubprovider(artifactsPath, contractsPath, networkId, defaultFromAddress);
+ },
+};
diff --git a/packages/metacoin/test/utils/deployer.ts b/packages/metacoin/test/utils/deployer.ts
new file mode 100644
index 000000000..d7dbc6d36
--- /dev/null
+++ b/packages/metacoin/test/utils/deployer.ts
@@ -0,0 +1,16 @@
+import { Deployer } from '@0xproject/deployer';
+import { devConstants } from '@0xproject/dev-utils';
+import * as path from 'path';
+
+import { web3Wrapper } from './web3_wrapper';
+
+const deployerOpts = {
+ web3Provider: web3Wrapper.getProvider(),
+ artifactsDir: path.resolve('artifacts'),
+ networkId: 50,
+ defaults: {
+ from: devConstants.TESTRPC_FIRST_ADDRESS,
+ },
+};
+
+export const deployer = new Deployer(deployerOpts);
diff --git a/packages/metacoin/test/utils/web3_wrapper.ts b/packages/metacoin/test/utils/web3_wrapper.ts
new file mode 100644
index 000000000..216a1de5a
--- /dev/null
+++ b/packages/metacoin/test/utils/web3_wrapper.ts
@@ -0,0 +1,30 @@
+import { env, EnvVars } from '@0xproject/dev-utils';
+import { GanacheSubprovider } from '@0xproject/subproviders';
+import { Web3Wrapper } from '@0xproject/web3-wrapper';
+import * as fs from 'fs';
+import * as _ from 'lodash';
+import ProviderEngine = require('web3-provider-engine');
+
+import { coverage } from './coverage';
+
+export const web3Provider = new ProviderEngine();
+const isCoverageEnabled = env.parseBoolean(EnvVars.SolidityCoverage);
+if (isCoverageEnabled) {
+ web3Provider.addProvider(coverage.getCoverageSubproviderSingleton());
+}
+web3Provider.addProvider(
+ new GanacheSubprovider({
+ logger: {
+ log: (arg: any) => {
+ fs.appendFileSync('ganache.log', `${arg}\n`);
+ },
+ },
+ verbose: env.parseBoolean(EnvVars.SolidityCoverage),
+ port: 8545,
+ networkId: 50,
+ mnemonic: 'concert load couple harbor equip island argue ramp clarify fence smart topic',
+ }),
+);
+web3Provider.start();
+
+export const web3Wrapper = new Web3Wrapper(web3Provider);