# Is there a way to test a binary file parser?

**URL:** <https://discourse.elm-lang.org/t/is-there-a-way-to-test-a-binary-file-parser/9412>\
**Category:** Learn\
**Created:** [October 28, 2023, 3:29pm UTC](https://discourse.elm-lang.org/t/is-there-a-way-to-test-a-binary-file-parser/9412 "2023-10-28T15:29:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atlewee](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/atlewee/32/4603_2.png) [@Atlewee](https://discourse.elm-lang.org/u/Atlewee)\
**Post date:** [October 28, 2023, 3:29pm UTC](https://discourse.elm-lang.org/t/is-there-a-way-to-test-a-binary-file-parser/9412/1 "2023-10-28T15:29:53Z")

</div>

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.  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/2X/4/4000d5f4ca6145ebcb2065029fb0373a43c54c30.png)

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

---

<div class="post-metadata">

**Author:** ![mattpiz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattpiz/32/860_2.png) [@mattpiz](https://discourse.elm-lang.org/u/mattpiz)\
**Post date:** [October 29, 2023, 12:49am UTC](https://discourse.elm-lang.org/t/is-there-a-way-to-test-a-binary-file-parser/9412/2 "2023-10-29T00:49:26Z")

</div>

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:

- [elm-codegen](https://github.com/mdgriffith/elm-codegen) to generate elm code
- [jxxcarlson/hex](https://package.elm-lang.org/packages/jxxcarlson/hex/latest/) package to convert bytes to and from hex encoded strings

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.

---

<div class="post-metadata">

**Author:** ![mattpiz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattpiz/32/860_2.png) [@mattpiz](https://discourse.elm-lang.org/u/mattpiz)\
**Post date:** [October 29, 2023, 12:53am UTC](https://discourse.elm-lang.org/t/is-there-a-way-to-test-a-binary-file-parser/9412/3 "2023-10-29T00:53:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![brian-watkins](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/brian-watkins/32/3014_2.png) [@brian-watkins](https://discourse.elm-lang.org/u/brian-watkins)\
**Post date:** [October 29, 2023, 8:19pm UTC](https://discourse.elm-lang.org/t/is-there-a-way-to-test-a-binary-file-parser/9412/4 "2023-10-29T20:19:52Z")

</div>

The original post asked about elm-test, specifically, but just wanted to share that you could test this with [elm-spec](https://package.elm-lang.org/packages/brian-watkins/elm-spec/latest/).

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](https://package.elm-lang.org/packages/brian-watkins/elm-spec/latest/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](https://package.elm-lang.org/packages/brian-watkins/elm-spec/latest/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!

---

<div class="post-metadata">

**Author:** ![Atlewee](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/atlewee/32/4603_2.png) [@Atlewee](https://discourse.elm-lang.org/u/Atlewee)\
**Post date:** [November 5, 2023, 12:56am UTC](https://discourse.elm-lang.org/t/is-there-a-way-to-test-a-binary-file-parser/9412/5 "2023-11-05T00:56:55Z")

</div>

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! 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [November 15, 2023, 12:57am UTC](https://discourse.elm-lang.org/t/is-there-a-way-to-test-a-binary-file-parser/9412/6 "2023-11-15T00:57:21Z")

</div>

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