aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-tracing-utils/test/collect_coverage_entries_test.ts
blob: 7832ec316464515ccc0cfe700a60fbf642167680 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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 { utils } from '../src/utils';

const expect = chai.expect;

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');
            // tslint:disable-next-line:custom-no-magic-numbers
            expect(coverageEntries.fnMap[fnIds[0]].line).to.be.equal(5);
            const setFunction = `function set(uint x) {
        storedData = x;
    }`;
            expect(utils.getRange(simpleStorageContract, coverageEntries.fnMap[fnIds[0]].loc)).to.be.equal(setFunction);
            expect(coverageEntries.fnMap[fnIds[1]].name).to.be.equal('get');
            // tslint:disable-next-line:custom-no-magic-numbers
            expect(coverageEntries.fnMap[fnIds[1]].line).to.be.equal(8);
            const getFunction = `function get() constant returns (uint retVal) {
        return storedData;
    }`;
            expect(utils.getRange(simpleStorageContract, coverageEntries.fnMap[fnIds[1]].loc)).to.be.equal(getFunction);
            expect(coverageEntries.branchMap).to.be.deep.equal({});
            const statementIds = _.keys(coverageEntries.statementMap);
            expect(utils.getRange(simpleStorageContract, coverageEntries.statementMap[statementIds[1]])).to.be.equal(
                'storedData = x',
            );
            expect(utils.getRange(simpleStorageContract, coverageEntries.statementMap[statementIds[3]])).to.be.equal(
                'return storedData;',
            );
            expect(coverageEntries.modifiersStatementIds).to.be.deep.equal([]);
        });
        it('correctly collects coverage entries for AllSolidityFeatures contract', () => {
            const simpleStorageContractBaseName = 'AllSolidityFeatures.sol';
            const simpleStorageContractFileName = path.resolve(
                __dirname,
                'fixtures/contracts',
                simpleStorageContractBaseName,
            );
            const simpleStorageContract = fs.readFileSync(simpleStorageContractFileName).toString();
            const coverageEntries = collectCoverageEntries(simpleStorageContract);
            const fnDescriptions = _.values(coverageEntries.fnMap);
            const fnNames = _.map(fnDescriptions, fnDescription => fnDescription.name);
            const expectedFnNames = [
                'f',
                'c',
                'test',
                'getChoice',
                'Base',
                'Derived',
                'f',
                'f',
                '',
                'g',
                'setData',
                'getData',
                'sendHalf',
                'insert',
                'remove',
                'contains',
                'iterate_start',
                'iterate_valid',
                'iterate_advance',
                'iterate_get',
                'insert',
                'sum',
                'restricted',
                'DualIndex',
                'set',
                'transfer_ownership',
                'lookup',
                '',
                '',
                'sum',
                'someFunction',
                'fun',
                'at',
                'test',
                'get',
                'returnNumber',
                'alloc',
                'ham',
                'getMyTuple',
                'ham',
                'abstain',
                'foobar',
                'foobar',
                'a',
            ];
            expect(fnNames).to.be.deep.equal(expectedFnNames);

            const branchDescriptions = _.values(coverageEntries.branchMap);
            const branchLines = _.map(branchDescriptions, branchDescription => branchDescription.line);
            // tslint:disable-next-line:custom-no-magic-numbers
            expect(branchLines).to.be.deep.equal([94, 115, 119, 130, 151, 187]);
            const branchTypes = _.map(branchDescriptions, branchDescription => branchDescription.type);
            expect(branchTypes).to.be.deep.equal(['if', 'if', 'if', 'if', 'binary-expr', 'if']);
        });

        it('correctly ignores all coverage entries for Ignore contract', () => {
            const solcovIgnoreContractBaseName = 'SolcovIgnore.sol';
            const solcovIgnoreContractFileName = path.resolve(
                __dirname,
                'fixtures/contracts',
                solcovIgnoreContractBaseName,
            );
            const solcovIgnoreContract = fs.readFileSync(solcovIgnoreContractFileName).toString();
            const coverageEntries = collectCoverageEntries(solcovIgnoreContract);
            const fnIds = _.keys(coverageEntries.fnMap);

            expect(fnIds.length).to.be.equal(1);
            expect(coverageEntries.fnMap[fnIds[0]].name).to.be.equal('set');
            // tslint:disable-next-line:custom-no-magic-numbers
            expect(coverageEntries.fnMap[fnIds[0]].line).to.be.equal(6);
            const setFunction = `function set(uint x) public {
        /* solcov ignore next */
        storedData = x;
    }`;
            expect(utils.getRange(solcovIgnoreContract, coverageEntries.fnMap[fnIds[0]].loc)).to.be.equal(setFunction);

            expect(coverageEntries.branchMap).to.be.deep.equal({});
            const statementIds = _.keys(coverageEntries.statementMap);
            expect(utils.getRange(solcovIgnoreContract, coverageEntries.statementMap[statementIds[0]])).to.be.equal(
                setFunction,
            );
            expect(statementIds.length).to.be.equal(1);
            expect(coverageEntries.modifiersStatementIds.length).to.be.equal(0);
        });
    });
});