aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-cov/test/collect_coverage_entries_test.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-03-15 22:42:48 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-03-15 23:19:01 +0800
commitf6c01520ae75d173937f0847ac45e636b06f4146 (patch)
tree6c16d0a8050bf470fd9a7e93c88b8f840b35b604 /packages/sol-cov/test/collect_coverage_entries_test.ts
parent40ebb533b324bf7dd454243fcb1ee092ab85f7eb (diff)
downloaddexon-0x-contracts-f6c01520ae75d173937f0847ac45e636b06f4146.tar.gz
dexon-0x-contracts-f6c01520ae75d173937f0847ac45e636b06f4146.tar.zst
dexon-0x-contracts-f6c01520ae75d173937f0847ac45e636b06f4146.zip
Add tests for sol-cov
Diffstat (limited to 'packages/sol-cov/test/collect_coverage_entries_test.ts')
-rw-r--r--packages/sol-cov/test/collect_coverage_entries_test.ts64
1 files changed, 64 insertions, 0 deletions
diff --git a/packages/sol-cov/test/collect_coverage_entries_test.ts b/packages/sol-cov/test/collect_coverage_entries_test.ts
new file mode 100644
index 000000000..84451686f
--- /dev/null
+++ b/packages/sol-cov/test/collect_coverage_entries_test.ts
@@ -0,0 +1,64 @@
+import * as chai from 'chai';
+import * as fs from 'fs';
+import * as _ from 'lodash';
+import 'mocha';
+import * as path from 'path';
+
+import { collectCoverageEntries } from '../src/collect_coverage_entries';
+import { SingleFileSourceRange } from '../src/types';
+
+const expect = chai.expect;
+
+const getRange = (sourceCode: string, range: SingleFileSourceRange) => {
+ const lines = sourceCode.split('\n').slice(range.start.line - 1, range.end.line);
+ lines[lines.length - 1] = lines[lines.length - 1].slice(0, range.end.column);
+ lines[0] = lines[0].slice(range.start.column);
+ return lines.join('\n');
+};
+
+describe('Collect coverage entries', () => {
+ describe('#collectCoverageEntries', () => {
+ it('correctly collects coverage entries for Simplest contract', () => {
+ const simplestContractBaseName = 'Simplest.sol';
+ const simplestContractFileName = path.resolve(__dirname, 'fixtures/contracts', simplestContractBaseName);
+ const simplestContract = fs.readFileSync(simplestContractFileName).toString();
+ const coverageEntries = collectCoverageEntries(simplestContract);
+ expect(coverageEntries.fnMap).to.be.deep.equal({});
+ expect(coverageEntries.branchMap).to.be.deep.equal({});
+ expect(coverageEntries.statementMap).to.be.deep.equal({});
+ expect(coverageEntries.modifiersStatementIds).to.be.deep.equal([]);
+ });
+ it('correctly collects coverage entries for SimpleStorage contract', () => {
+ const simpleStorageContractBaseName = 'SimpleStorage.sol';
+ const simpleStorageContractFileName = path.resolve(
+ __dirname,
+ 'fixtures/contracts',
+ simpleStorageContractBaseName,
+ );
+ const simpleStorageContract = fs.readFileSync(simpleStorageContractFileName).toString();
+ const coverageEntries = collectCoverageEntries(simpleStorageContract);
+ const fnIds = _.keys(coverageEntries.fnMap);
+ expect(coverageEntries.fnMap[fnIds[0]].name).to.be.equal('set');
+ expect(coverageEntries.fnMap[fnIds[0]].line).to.be.equal(3);
+ const setFunction = `function set(uint x) {
+ storedData = x;
+ }`;
+ expect(getRange(simpleStorageContract, coverageEntries.fnMap[fnIds[0]].loc)).to.be.equal(setFunction);
+ expect(coverageEntries.fnMap[fnIds[1]].name).to.be.equal('get');
+ expect(coverageEntries.fnMap[fnIds[1]].line).to.be.equal(6);
+ const getFunction = `function get() constant returns (uint retVal) {
+ return storedData;
+ }`;
+ expect(getRange(simpleStorageContract, coverageEntries.fnMap[fnIds[1]].loc)).to.be.equal(getFunction);
+ expect(coverageEntries.branchMap).to.be.deep.equal({});
+ const statementIds = _.keys(coverageEntries.statementMap);
+ expect(getRange(simpleStorageContract, coverageEntries.statementMap[statementIds[1]])).to.be.equal(
+ 'storedData = x',
+ );
+ expect(getRange(simpleStorageContract, coverageEntries.statementMap[statementIds[3]])).to.be.equal(
+ 'return storedData;',
+ );
+ expect(coverageEntries.modifiersStatementIds).to.be.deep.equal([]);
+ });
+ });
+});