elm-explorations / test

Write unit and fuzz tests for Elm code.
https://package.elm-lang.org/packages/elm-explorations/test/latest
BSD 3-Clause "New" or "Revised" License
237 stars 39 forks source link

Allow passing in external data #196

Closed Tails closed 2 years ago

Tails commented 2 years ago

I have to test/profile a custom JSON Decoder to parse a 4MB JSON file. I'm looking for a way to pass in the JSON string so the test can output logs on the performance.

Would there be a way for passing in custom strings in a flags-like manner?

The only alternative would be to generate an Elm file and dump the file in a string that can be read from the runtime, but that seems suboptimal.

Janiczek commented 2 years ago

I would say having some preprocess script that converts the JSON files to Elm files (one file and test per JSON file), and then just running elm-test on that would work.

There is a tool+package elm-codegen:

Particularly if you look at the last few flags mentioned in here, the JSON flags sound interesting.

elm-test itself doesn't have support for flags.

Tails commented 2 years ago

It was indeed the most direct way available.

Here a Makefile rule for future reference:

test-generate-large-file-json-elm-file:
    - rm ./tests/LargeFile.elm
    # the \c is to prevent a trailing newline somehow
    echo "module LargeFile exposing (..)\n\nlargeFileJsonString = \"\"\"\c" > ./tests/LargeFile.elm
    sed \
        -e 's%\"%\\"%g' \
        ../parser/output/A5B65AEED15ED5AC93E5A75D09E2C312A13A8ABF865BFB8EB3A07220C95943BE.json \
            >> ./tests/LargeFile.elm
    echo "\t\t\"\"\"" >> ./tests/LargeFile.elm
    elm-test ./tests/Perf.elm

The 'Perf.elm' test file can then require LargeFile.elm and import its JSON string contents.

Janiczek commented 2 years ago

Right, never underestimate a bit of shell script 🙂 Cool to see the problem solved!