SuperblocksHQ / superblocks-lab

Superblocks Lab for DApp development
https://superblocks.com/lab/
GNU General Public License v3.0
99 stars 37 forks source link

Add support for testing #189

Open filippsen opened 6 years ago

filippsen commented 6 years ago

Summary

Add support for providing tests to be run against currently selected project.

Motivation

I would like to test my smart contracts by writing JavaScript unit tests

Additional context

Tasks

Test Runner

To enable inspecting all the tests written for the currently open project.

filippsen commented 6 years ago

Initial test reference (vanilla version)

This reference aims to: a. Demonstrate what a working test example using Web3 would look like; b. Analyze what would be the required dependencies for running it; c. Analyze what would be the inputs necessary to enable testing arbitrary contracts.

This leads to extensive and verbose code (read: explicit and raw). The testing code could be further compressed into a simpler API, focused on providing shortcuts to the most commonly used operations.

The example test reference is based on the HelloWorld.sol contract from Hello World project template.

Dependencies

Inputs

Example

Note: some details were suppressed for readability.

describe('User test action: manually check contract data', function (done) {
    var contractInstance;

    beforeEach(function (done) {
        const contractBin = "0x6060...";
        const contractABI = instance.abi;

        web3.eth.getTransactionCount(account_address, function(error, result) {
            [...]
            const tx = new Tx[...]
            web3.eth.sendRawTransaction("0x"+tx.serialize().toString("hex"),
                [...]
                getTransactionReceipt(currentContractTransactionHash, function(err, res) {
                    [...]
                    var contract = web3.eth.contract(contractABI);
                    contractInstance = contract.at(res.contractAddress);
                    done();
                });
            });
        });
    });

    it('matches message data', function (done) {
        var expectedValue = "Hello World!";
        contractInstance.message(function(error, result) {
            if(error) {
                console.error(error);
                done(error);
            } else {
                if(result !== expectedValue) {
                    done(new Error(result));
                } else {
                    done();
                }
            }
        });
    });

    it('update message data', function (done) {
        [...]
        web3.eth.getTransactionCount(account_address, function(error, result) {
            if(error) {
                done(new Error("Could not get nonce for address " + account_address));
            } else {
                account_nonce=result;
                var data = ABI.ABI.simpleEncode("update(string)", "Super Hello World!");
                [...]
                const tx = new Tx[...]
                web3.eth.sendRawTransaction("0x"+tx.serialize().toString("hex"),
                    function(error, result) {
                        if(error) {
                            console.error(error);
                            done(error);
                        } else {
                            contractInstance.message(function(error, result) {
                                var expectedValue = "Super Hello World!";
                                if(error) {
                                    console.error(error);
                                    done(error);
                                } else {
                                    if(result !== expectedValue) {
                                        done(new Error(result));
                                    } else {
                                        [...]
                                        done();
                                    }
                                }
                            });
                        }
                    }
                );
            }
        });
    });
});
filippsen commented 5 years ago

Feedback 592e5813 ("dynamic result data is now rendering")

Main objective

Get a working demonstration against the hardcoded reference contract and tests with all the UI elements in presentable state, according to the original design.

Pending issues

More information: in panes.js, the div with key set topanes2 changes position, depending whether the Tests panel is open or not. There is an in-code comment about that, which helps confirming the issue: {/*remove this condition and add proper state management for handling closing the pane.*/}

filippsen commented 5 years ago

Issues master-testing ("merge to post-refactoring code base")

filippsen commented 5 years ago

New execution environment ("safe" code path parity with non-safe environment)



filippsen commented 5 years ago

Unassigning as requested.