From 8b52793f2fa21bcb23a3c38b6d3773cfc20186fd Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 16 Mar 2018 16:34:02 +0100 Subject: Add tests for compiler utils --- packages/deployer/test/compiler_utils_test.ts | 74 +++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 packages/deployer/test/compiler_utils_test.ts (limited to 'packages/deployer/test/compiler_utils_test.ts') diff --git a/packages/deployer/test/compiler_utils_test.ts b/packages/deployer/test/compiler_utils_test.ts new file mode 100644 index 000000000..f86a44862 --- /dev/null +++ b/packages/deployer/test/compiler_utils_test.ts @@ -0,0 +1,74 @@ +import * as chai from 'chai'; +import * as dirtyChai from 'dirty-chai'; +import 'mocha'; + +import { + createArtifactsDirIfDoesNotExistAsync, + getNormalizedErrMsg, + parseDependencies, + parseSolidityVersionRange, +} from '../src/utils/compiler'; +import { fsWrapper } from '../src/utils/fs_wrapper'; + +chai.use(dirtyChai); +const expect = chai.expect; + +describe.only('Compiler utils', () => { + describe('#getNormalizedErrorMessage', () => { + it('normalizes the error message', () => { + const errMsg = 'base/Token.sol:6:46: Warning: Unused local variable'; + const normalizedErrMsg = getNormalizedErrMsg(errMsg); + expect(normalizedErrMsg).to.be.equal('Token.sol:6:46: Warning: Unused local variable'); + }); + }); + describe('#createArtifactsDirIfDoesNotExistAsync', () => { + it('creates artifacts dir', async () => { + const artifactsDir = `${__dirname}/artifacts`; + expect(fsWrapper.doesPathExistSync(artifactsDir)).to.be.false(); + await createArtifactsDirIfDoesNotExistAsync(artifactsDir); + expect(fsWrapper.doesPathExistSync(artifactsDir)).to.be.true(); + fsWrapper.rmdirSync(artifactsDir); + expect(fsWrapper.doesPathExistSync(artifactsDir)).to.be.false(); + }); + }); + describe('#parseSolidityVersionRange', () => { + it('correctly parses the version range', () => { + expect(parseSolidityVersionRange('pragma solidity ^0.0.1;')).to.be.equal('^0.0.1'); + expect(parseSolidityVersionRange('\npragma solidity 0.0.1;')).to.be.equal('0.0.1'); + expect(parseSolidityVersionRange('pragma solidity <=1.0.1;')).to.be.equal('<=1.0.1'); + expect(parseSolidityVersionRange('pragma solidity ~1.0.1;')).to.be.equal('~1.0.1'); + }); + // TODO: For now that doesn't work. This will work after we switch to a grammar-based parser + it.skip('correctly parses the version range with comments', () => { + expect(parseSolidityVersionRange('// pragma solidity ~1.0.1;\npragma solidity ~1.0.2;')).to.be.equal( + '~1.0.2', + ); + }); + }); + describe('#parseDependencies', () => { + it('correctly parses Exchange dependencies', async () => { + const exchangeSource = await fsWrapper.readFileAsync(`${__dirname}/fixtures/contracts/Exchange.sol`, { + encoding: 'utf8', + }); + expect(parseDependencies(exchangeSource)).to.be.deep.equal([ + 'TokenTransferProxy.sol', + 'Token.sol', + 'SafeMath.sol', + ]); + }); + it('correctly parses TokenTransferProxy dependencies', async () => { + const exchangeSource = await fsWrapper.readFileAsync( + `${__dirname}/fixtures/contracts/TokenTransferProxy.sol`, + { + encoding: 'utf8', + }, + ); + expect(parseDependencies(exchangeSource)).to.be.deep.equal(['Token.sol', 'Ownable.sol']); + }); + // TODO: For now that doesn't work. This will work after we switch to a grammar-based parser + it.skip('correctly parses commented out dependencies', async () => { + const contractWithCommentedOutDependencies = `// import "./TokenTransferProxy.sol";`; + expect(parseDependencies(contractWithCommentedOutDependencies)).to.be.deep.equal([]); + }); + }); +}); -- cgit v1.2.3 From 2c7fdac5cd52ceb24cc2cb19ade124fc1acc8c9d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 21 Mar 2018 16:39:37 +0100 Subject: Remove .only --- packages/deployer/test/compiler_utils_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/deployer/test/compiler_utils_test.ts') diff --git a/packages/deployer/test/compiler_utils_test.ts b/packages/deployer/test/compiler_utils_test.ts index f86a44862..1867177dc 100644 --- a/packages/deployer/test/compiler_utils_test.ts +++ b/packages/deployer/test/compiler_utils_test.ts @@ -13,7 +13,7 @@ import { fsWrapper } from '../src/utils/fs_wrapper'; chai.use(dirtyChai); const expect = chai.expect; -describe.only('Compiler utils', () => { +describe('Compiler utils', () => { describe('#getNormalizedErrorMessage', () => { it('normalizes the error message', () => { const errMsg = 'base/Token.sol:6:46: Warning: Unused local variable'; -- cgit v1.2.3