Is there a way to test a binary file parser?

I’m creating a PDF parser in Elm and those files contains a lot of binary information.
The parser expects Bytes as input as I want it optimized for File.load / Http.get ++

Testing is currently done by converting files to list of integers/hex as input for my tests.

Anyone know a smarter way of loading actual files as input in elm-test?

Loading data is not something possible currently in elm-test. You’d have to find a way to write that data into an elm file that’s imported in your tests.

Things that may (or not) help:

Otherwise, you’re not the first to be interested in loading stuff for tests. If you’re willing to do more free work (that may not end in something usable for you in reasonable time), that would be gather other past questions and use cases on the subject. Try drafting a way how we could make something work, maybe via a Value in flags, maybe something else. And post your findings here and/or in an issue for the elm-test package.

1 Like

Also another remark, the Bytes type from elm/bytes is not comparable with ==. It may return True (or False? mostly true in my experience) for any comparison. So if you want to check for correctness of data transformations resulting in bytes, make sure to convert it to hex before comparing it to a reference data.

1 Like

The original post asked about elm-test, specifically, but just wanted to share that you could test this with elm-spec.

Assuming your binary file parser is embedded (or embeddable) in an Elm application that uses File.Select.file or File.Select.files to allow the user to upload a file, you can write an elm-spec test that simulates uploading a file and then asserts that your application does the correct thing. Elm-spec allows you to specify a file on disk that can be selected during the test (or you can provide Bytes as part of the test itself). See Spec.File for more info and an example. Or, if your application makes an HTTP request and expects Bytes in return, elm-spec allows you to simulate that as well, where the bytes are specified in the test itself or come from a file on disk. See Spec.Http.Stub for more.

Assuming, again, that your binary file parser is embedded in some application, you could also use an end-to-end testing tool like Cypress or Playwright Test to test this behavior. The benefit of elm-spec is that it runs a bit faster than typical end-to-end testing tools, and gives you the opportunity to control and verify your program at a lower level (eg you can assert on the contents of the model, if you want).

Good luck!


I have not seen elm-spec before, seems to solve the issue.
Got it working loading files and Observe that the model contains expected results
Also nice to know about this tool for other usecases. Thanks for making+posting this! :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.