This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from 0mkara/remix-tests
Unit testing smart contracts using remix-tests
- Loading branch information
Showing
20 changed files
with
722 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
'use babel' | ||
import 'idempotent-babel-polyfill' | ||
import { Etheratom } from './lib/ethereum-interface' | ||
import 'idempotent-babel-polyfill'; | ||
import { Etheratom } from './lib/ethereum-interface'; | ||
|
||
module.exports = new Etheratom({ | ||
config: atom.config, | ||
workspace: atom.workspace | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use babel' | ||
// Copyright 2018 Etheratom Authors | ||
// This file is part of Etheratom. | ||
|
||
// Etheratom is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Etheratom is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Etheratom. If not, see <http://www.gnu.org/licenses/>. | ||
import { SET_ERRORS } from './types'; | ||
|
||
export const setErrors = (payload) => { | ||
return (dispatch) => { | ||
dispatch({ type: SET_ERRORS, payload }); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
'use babel' | ||
// Copyright 2018 Etheratom Authors | ||
// This file is part of Etheratom. | ||
|
||
// Etheratom is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Etheratom is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Etheratom. If not, see <http://www.gnu.org/licenses/>. | ||
import React from 'react'; | ||
import { connect, Provider } from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
import RemixTests from 'remix-tests'; | ||
import VirtualList from 'react-tiny-virtual-list'; | ||
import ErrorView from '../ErrorView'; | ||
import { setErrors } from '../../actions'; | ||
|
||
Object.defineProperty(String.prototype, 'regexIndexOf', { | ||
value(regex, startpos) { | ||
const indexOf = this.substring(startpos || 0).search(regex); | ||
return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf; | ||
} | ||
}); | ||
|
||
class RemixTest extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
testResults: [], | ||
running: false | ||
}; | ||
this._runRemixTests = this._runRemixTests.bind(this); | ||
this._testCallback = this._testCallback.bind(this); | ||
this._finalCallback = this._finalCallback.bind(this); | ||
this._resultsCallback = this._resultsCallback.bind(this); | ||
} | ||
componentDidUpdate(prevProps) { | ||
const { sources } = this.props; | ||
if(sources != prevProps.sources) { | ||
this._runRemixTests(); | ||
} | ||
} | ||
_testCallback(result) { | ||
try { | ||
const { testResults } = this.state; | ||
const t = testResults.slice(); | ||
t.push(result); | ||
this.setState({ testResults: t }); | ||
} catch (e) { | ||
this.props.setErrors([e]); | ||
console.error(e); | ||
} | ||
} | ||
_resultsCallback(err, result) { | ||
if(err) { | ||
this.props.setErrors([err]); | ||
console.error(err); | ||
} | ||
} | ||
_finalCallback(err, result) { | ||
if(err) { | ||
this.props.setErrors([err]); | ||
console.error(err); | ||
} | ||
this.setState({ testResult: result, running: false }); | ||
} | ||
_importFileCb(err, result) { | ||
if(err) { | ||
this.props.setErrors([err]); | ||
console.error(err); | ||
} | ||
} | ||
async _runRemixTests() { | ||
const { sources } = this.props; | ||
this.setState({ testResults: [], running: true }); | ||
const promises = []; | ||
for(let filename in sources) { | ||
if(filename.indexOf('_test.sol') > 0) { | ||
continue; | ||
} | ||
sources[filename].content = await this.injectTests(sources[filename]); | ||
promises.push(filename); | ||
} | ||
Promise.all(promises) | ||
.then(testSources => { | ||
RemixTests.runTestSources(sources, this._testCallback, this._resultsCallback, this._finalCallback, this._importFileCb); | ||
}) | ||
.catch(e => { | ||
this.props.setErrors([e]); | ||
console.error(e); | ||
}); | ||
} | ||
async injectTests(source) { | ||
const s = /^(import)\s['"](remix_tests.sol|tests.sol)['"];/gm; | ||
if(source.content && source.content.regexIndexOf(s) < 0) { | ||
return source.content.replace(/(pragma solidity \^\d+\.\d+\.\d+;)/, '$1\nimport \'remix_tests.sol\';'); | ||
} | ||
} | ||
render() { | ||
const { testResults, testResult, running } = this.state; | ||
return ( | ||
<Provider store={this.props.store}> | ||
<div id="remix-tests"> | ||
<h2 className="block test-header">Naming conventions</h2> | ||
<h3 className="block test-header">File names should end with _test, as in foo_test.sol</h3> | ||
<div className="test-selector"> | ||
<button className="btn btn-primary inline-block-tight" onClick={this._runRemixTests}> | ||
Run tests | ||
</button> | ||
{ | ||
running && | ||
<span className='loading loading-spinner-tiny inline-block'></span> | ||
} | ||
{ | ||
testResult && | ||
<div className="test-result"> | ||
<span className="text-error">Total failing: {testResult.totalFailing} </span> | ||
<span className="text-success">Total passing: {testResult.totalPassing} </span> | ||
<span className="text-info">Time: {testResult.totalTime}</span> | ||
</div> | ||
} | ||
</div> | ||
<VirtualList | ||
height="50vh" | ||
itemCount={testResults.length} | ||
itemSize={30} | ||
className="test-result-list-container" | ||
overscanCount={10} | ||
renderItem={({ index }) => | ||
<div key={index} className="test-result-list-item"> | ||
{ | ||
testResults[index].type === 'contract' && | ||
<span className="status-renamed icon icon-checklist"></span> | ||
} | ||
{ | ||
testResults[index].type === 'testPass' && | ||
<span className="status-added icon icon-check"></span> | ||
} | ||
{ | ||
testResults[index].type === 'testFailure' && | ||
<span className="status-removed icon icon-x"></span> | ||
} | ||
<span className="padded text-warning"> | ||
{testResults[index].value} | ||
</span> | ||
</div> | ||
} | ||
/> | ||
<div id="test-error" className="error-container"> | ||
<ErrorView /> | ||
</div> | ||
</div> | ||
</Provider> | ||
); | ||
} | ||
} | ||
RemixTest.propTypes = { | ||
helpers: PropTypes.any.isRequired, | ||
sources: PropTypes.object, | ||
compiled: PropTypes.object, | ||
setErrors: PropTypes.func, | ||
store: PropTypes.any.isRequired | ||
}; | ||
const mapStateToProps = ({ contract }) => { | ||
const { sources } = contract; | ||
return { sources }; | ||
}; | ||
export default connect(mapStateToProps, { setErrors })(RemixTest); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.