-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for the Revive WASM version (#147)
- Loading branch information
Showing
7 changed files
with
199 additions
and
18 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
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,32 @@ | ||
const soljson = require('solc/soljson'); | ||
const createRevive = require('./resolc.js'); | ||
|
||
async function compile(standardJsonInput) { | ||
if (!standardJsonInput) { | ||
throw new Error('Input JSON for the Solidity compiler is required.'); | ||
} | ||
|
||
// Initialize the compiler | ||
const compiler = createRevive(); | ||
compiler.soljson = soljson; | ||
|
||
// Provide input to the compiler | ||
compiler.writeToStdin(JSON.stringify(standardJsonInput)); | ||
|
||
// Run the compiler | ||
compiler.callMain(['--standard-json']); | ||
|
||
// Collect output | ||
const stdout = compiler.readFromStdout(); | ||
const stderr = compiler.readFromStderr(); | ||
|
||
// Check for errors and throw if stderr exists | ||
if (stderr) { | ||
throw new Error(`Compilation failed: ${stderr}`); | ||
} | ||
|
||
// Return the output if no errors | ||
return stdout; | ||
} | ||
|
||
module.exports = { compile }; |
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,102 @@ | ||
import { expect } from 'chai'; | ||
import { compile } from '../examples/node/revive.js'; | ||
|
||
const validCompilerInput = { | ||
language: 'Solidity', | ||
sources: { | ||
'MyContract.sol': { | ||
content: ` | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
contract MyContract { | ||
function greet() public pure returns (string memory) { | ||
return "Hello"; | ||
} | ||
} | ||
`, | ||
}, | ||
}, | ||
settings: { | ||
optimizer: { | ||
enabled: true, | ||
runs: 200, | ||
}, | ||
outputSelection: { | ||
'*': { | ||
'*': ['abi', 'evm.bytecode'], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('Compile Function Tests', function () { | ||
it('should successfully compile valid Solidity code', async function () { | ||
const result = await compile(validCompilerInput); | ||
|
||
// Ensure result contains compiled contract | ||
expect(result).to.be.a('string'); | ||
const output = JSON.parse(result); | ||
expect(output).to.have.property('contracts'); | ||
expect(output.contracts['MyContract.sol']).to.have.property('MyContract'); | ||
expect(output.contracts['MyContract.sol'].MyContract).to.have.property('abi'); | ||
expect(output.contracts['MyContract.sol'].MyContract).to.have.property('evm'); | ||
expect(output.contracts['MyContract.sol'].MyContract.evm).to.have.property('bytecode'); | ||
}); | ||
|
||
it('should throw an error for invalid Solidity code', async function () { | ||
const invalidCompilerInput = { | ||
...validCompilerInput, | ||
sources: { | ||
'MyContract.sol': { | ||
content: ` | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
import "nonexistent/console.sol"; | ||
contract MyContract { | ||
function greet() public pure returns (string memory) { | ||
return "Hello" // Missing semicolon | ||
} | ||
} | ||
`, | ||
}, | ||
}, | ||
}; | ||
|
||
const result = await compile(invalidCompilerInput); | ||
expect(result).to.be.a('string'); | ||
const output = JSON.parse(result); | ||
expect(output).to.have.property('errors'); | ||
expect(output.errors).to.be.an('array'); | ||
expect(output.errors.length).to.be.greaterThan(0); | ||
expect(output.errors[0].type).to.exist; | ||
expect(output.errors[0].type).to.contain("ParserError"); | ||
}); | ||
|
||
it('should return not found error for missing imports', async function () { | ||
const compilerInputWithImport = { | ||
...validCompilerInput, | ||
sources: { | ||
'MyContract.sol': { | ||
content: ` | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
import "nonexistent/console.sol"; | ||
contract MyContract { | ||
function greet() public pure returns (string memory) { | ||
return "Hello"; | ||
} | ||
} | ||
`, | ||
}, | ||
}, | ||
}; | ||
|
||
let result = await compile(compilerInputWithImport); | ||
const output = JSON.parse(result); | ||
expect(output).to.have.property('errors'); | ||
expect(output.errors).to.be.an('array'); | ||
expect(output.errors.length).to.be.greaterThan(0); | ||
expect(output.errors[0].message).to.exist; | ||
expect(output.errors[0].message).to.include('Source "nonexistent/console.sol" not found'); | ||
}); | ||
}); |
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