Downloading External Data to Elm Kit

I am writing an Elm test suite and want to check the output of a function using a list of known good I / O pairs stored in an external file. I can choose the format of the external file, so I could use JSON, for example, but I need to save it separately, because it is available from other languages. (I basically guarantee that the Elm version of the function matches the other versions).

I do not want to hard code the values ​​in the Elm test module. Is there any way to do this with Elm and elm-test?

+6
source share
1 answer

, , , , - .

  • Elm Native - . .

  • javascript Node, . fs = require('fs') JSON.parse ..

  • Javascript, . ..

  • Elm Json Value Json.Decode.decodeValue

(Elm 0.18) - :

tests/my_function_test_data.json:

[
    [0, 1, 2],
    [3, 4, 5]
]

tests/Native/TestData.js:

var _user$project$Native_TestData = function () {
    var fs = require('fs');
    var path = require('path');
    var jsonPath = path.join(__dirname, '..', 'my_function_test_data.json');
    var myFunctionTestData = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
    return {
        myFunctionTestData: myFunctionTestData
    }
}();

tests/MyTests.elm:

import Native.TestData

myFunctionTestData : List (List Float)
myFunctionTestData =
    JD.decodeValue (JD.list (JD.list JD.float)) Native.TestData.myFunctionTestData
        |> \v -> case v of
                     Ok val ->
                         val
                     Err msg ->
                         Debug.crash msg

(, , , ), .

+3

All Articles