Testing of a Sophia function using a list as an argument


#1

I’m using forgae and trying to create a test case for a function in my smart contract with the following function signature: public stateful function create_proposal(id: string, voting_ends: int, description: string, options: list(string), allowed_to_vote: list(voter)) =

In my test I’m defining the value I want to pass with the test for options (of type list) like as an array: const options = ["YES", "NO"];

The contract compiles without problems, but running the test results in the following error:
Error: While calling encodeCalldata (body), POST to http://localhost:3001/internal/v2/debug/contracts/code/encode-calldata failed with 403: Error: function_clause in
{aeso_abi,old_ast_to_erlang,
[{con,[{line,1},{col,44}],“YES”}],
[{file,"/Users/hans/Quviq/Aeternity/aesophia/_build/default/lib/aesophia/src/aeso_abi.erl"},
{line,223}]}
at e.exports (node_modules/@aeternity/aepp-sdk/dist/aepp-sdk.js:1:90607)
at e.exports (node_modules/@aeternity/aepp-sdk/dist/aepp-sdk.js:1:179154)
at IncomingMessage. (node_modules/@aeternity/aepp-sdk/dist/aepp-sdk.js:8:7866)
at IncomingMessage.EventEmitter.emit (domain.js:441:20)
at endReadableNT (_stream_readable.js:1103:12)
at process._tickCallback (internal/process/next_tick.js:63:19)

It looks like that I can’t use a javascript array to pass values to a list in Sophia, so how should I do this?

In case anyone would like to see the whole code: https://github.com/arjanvaneersel/voting


#2

Hi, you could use js array, try this way…

    function getFirstElement(someList) : string =
      head(someList)
        
    function head(xs) =
      switch(xs)
        hd::tl => hd
        [] => ""

and in the tests I pass this: '(["I am the first element: 23", "31", "69"])'.
to get rest of elements you should do some recursion: https://dev.aepps.com/aepp-sdk-docs/Sophia.html#pattern-matching-lists-and-recursion