Skip to content

A small example of using hs-di to unit test effectful code

License

Notifications You must be signed in to change notification settings

Wizek/hs-di-example

Repository files navigation

hs-di-example

For some time now, people have been asking me to compare hs-di (github, hackage) to other techniques available to Haskell programmers for achieving fully deterministic unit-testing for effectful code. And when I saw Alexis King's repository posted on Reddit about an mtl-style unit-testing example, and saw just how much boilerplate was required to make that style work, I thought this was a great opportunity to contrast the two approaches.

So this repository contains a working example of hs-di-style converted from the mtl-style.

According to cloc, we were able to get to:

$ cloc --exclude-dir=.stack-work .
      11 text files.
      11 unique files.
     305 files ignored.

http://cloc.sourceforge.net v 1.60  T=0.10 s (68.9 files/s, 1447.9 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Haskell                          5             19              3             66
YAML                             2             10              0             49
-------------------------------------------------------------------------------
SUM:                             7             29              3            115
-------------------------------------------------------------------------------

Coming from:

$ cloc --exclude-dir=.stack-work .
      13 text files.
      13 unique files.
     222 files ignored.

http://cloc.sourceforge.net v 1.60  T=0.30 s (29.6 files/s, 999.2 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Haskell                          7             52             34            147
YAML                             2              8              0             63
-------------------------------------------------------------------------------
SUM:                             9             60             34            210
-------------------------------------------------------------------------------

A better then two-fold improvement (147 SLOC -> 66 SLOC)!

Footnotes

  1. Few of the 4 mocks are slightly less complex than in the mtl example, so that may skew the sloc counts somewhat. But on the other hand, the current mocks are perfectly adequate for testing as much as we do, so I'd say less is more in this case.

  2. During development, a few times I ran into a type error ... is as general as its inferred signature. It's not present in the current state of the repository, and it's worked around for even when it would show up. But if you'd like to help me overcome this issue, and/or you'd like to read more, head over here.

About

A small example of using hs-di to unit test effectful code

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published