2

I have a library for string array handling functions that has functions like:

function deleteAt(string[] storage, uint pos) {...}

It's meant to be run inside a contract which has a state var of string[], and it works as expected when run inside a the contract.

But I'm trying to write a unit test for it and I can't figure out how.

For starters, I can't figure out how to call it from javascript, other than

   it(`removes an array element`, ()=> {
        const ArrayLibrary = await ethers.getContractFactory("ArrayLibrary");
        const arrayLibrary = await ArrayLibrary.deploy();
        let lib = await arrayLibrary.deployed();
        let ar = ["Apple", "Banana", "Peach];
        
        let result = await lib.deleteAt(1);  //. <== fails
    }

This fails with TypeError lib.deleteAt is not a function, and even if it worked I'm not really sure what I could do with result which I guess would be a tx receipt or something.

Short of creating a separate contract specifically to test this function on a storage array, is there some way I can test this from javascript - mocha/chai using hardhat?

I do know the problem is not with the lib instance of the contract. Other function (which work on string[] memory params passed in) work as expected in test, and a test contract which calls myStorageArray.deleteAt(1) works just fine.

In case useful, here's the deleteAt function defined inside library ArrayLibrary

/** @dev Deletes item from storage array at position pos, keeps order */
    function deleteAt(string[] storage ar, uint pos) public {
        uint len = ar.length;
        for(uint i = pos; i<len-1; i++) {
            ar[i]=ar[i+1];
        }
        ar.pop();
    }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.