truffle icon indicating copy to clipboard operation
truffle copied to clipboard

Does truffle support testing functions with string array parameters?

Open enjoycod1ng opened this issue 3 years ago • 4 comments


Issue

I'm going to test function with string array parameters. But it seems like being passing an empty array in the truffle test.

Steps to Reproduce

*.sol

function createParent(
        string[] calldata _childrenName
    ) external{
    }

*.test.js Try to call createParent function like this.

await this.contract.createParent(["D", "E", "F"]);

Expected Behavior

Should pass string array parameter.

Actual Results

Seems like passing parameter is an empty array

Environment

  • Operating System: Windows 10
  • Ethereum client:
  • Truffle version (truffle version): v5.521
  • node version (node --version): v16.17.0
  • npm version (npm --version): v8.15.0

enjoycod1ng avatar Sep 12 '22 03:09 enjoycod1ng

Yes, passing in arrays of strings is supported. Do you have more detailed reproduction steps so we can try to determine what's going wrong here? (Say a repo we could check out?) Thanks!

haltman-at avatar Sep 12 '22 19:09 haltman-at

I'm trying to push Child struct array to Parent struct.

struct Parent {
  Child[] children;
}
struct Child {
  string name;
}
function createParent(
        string[] calldata _childrenName
) external{
  Parent storage parent = _allParents[parentCounter];
  for(uint256 i = 0; i < _childrenName.length; i ++) {
    parent.children.push(Child(_childrenName[i]));
  }
  parentCounter++;
}

After creating a parent there are no children in Parent struct. children is undefined

enjoycod1ng avatar Sep 13 '22 12:09 enjoycod1ng

The field children cannot be undefined, because Solidity doesn't have any notion of undefined. How are you determining this? Do you have more complete code we could check out?

haltman-at avatar Sep 13 '22 17:09 haltman-at

I should probably be more explicit -- I expect the problem is probably in whatever Javascript code you've written to interact with the contract, so I'd want to see that to determine what's going on. That's part of what I meant when I said I was looking for more complete code.

haltman-at avatar Sep 13 '22 18:09 haltman-at

Closing for issue maintenance. @mastercodingbear Feel free to open a new issue if you're still running into this, along with repro steps that we can follow. Thanks!

cliffoo avatar Oct 13 '22 17:10 cliffoo