<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Waqas</title>
    <description>The latest articles on DEV Community by Waqas (@totally_waqas).</description>
    <link>https://dev.to/totally_waqas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F277980%2F62b35546-8456-488a-80db-c8a6edd99dff.jpg</url>
      <title>DEV Community: Waqas</title>
      <link>https://dev.to/totally_waqas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/totally_waqas"/>
    <language>en</language>
    <item>
      <title>How to test in Haskell using comments?</title>
      <dc:creator>Waqas</dc:creator>
      <pubDate>Fri, 13 Mar 2020 07:39:08 +0000</pubDate>
      <link>https://dev.to/totally_waqas/how-to-test-in-haskell-using-comments-3h67</link>
      <guid>https://dev.to/totally_waqas/how-to-test-in-haskell-using-comments-3h67</guid>
      <description>&lt;p&gt;&lt;em&gt;This article originally appeared on my &lt;a href="https://waqasali.dev/posts/how-to-test-in-haskell-using-comments"&gt;website&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you want to easily test your functions in Haskell, you can do so by writing inputs and their expected responses right above your functions as comments. All you need is &lt;a href="https://github.com/sol/doctest"&gt;doctest&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is how:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Setup Project
&lt;/h3&gt;

&lt;p&gt;Let's setup a basic project first.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Install &lt;a href="https://docs.haskellstack.org/en/stable/README/#how-to-install"&gt;Haskell Stack&lt;/a&gt; if you don't have it already.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Setup project and install required dependencies.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stack new my-project
cd my-project
stack setup
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Build and execute project. If you see &lt;code&gt;someFunc&lt;/code&gt; in the output you are good to go.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stack build
stack exec my-project-exe
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RllyJ368--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://waqasali.dev/static/80920d9689171020443b940f83684a2b/c83ae/stack_build_exec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RllyJ368--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://waqasali.dev/static/80920d9689171020443b940f83684a2b/c83ae/stack_build_exec.png" alt="Output after running `stack build` and `stack exec my-project-exe`"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Write a function
&lt;/h3&gt;

&lt;p&gt;Let's write a simple function so we can test it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a file &lt;code&gt;Math.hs&lt;/code&gt; in &lt;code&gt;src&lt;/code&gt; with a &lt;code&gt;square&lt;/code&gt; function:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module Math where

square :: Int -&amp;gt; Int
square x = x * x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Manually test the function in &lt;a href="https://wiki.haskell.org/GHC/GHCi"&gt;GHCi&lt;/a&gt;. Load up the interactive environment using &lt;code&gt;stack ghci&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Try out the function to see everything is working as expected: &lt;code&gt;Math.square 3&lt;/code&gt; should output &lt;code&gt;9&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sJJFDpxO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://waqasali.dev/static/6d8ec9b44c5b5e0f3ae2f30834228e45/c83ae/stack_ghci.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sJJFDpxO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://waqasali.dev/static/6d8ec9b44c5b5e0f3ae2f30834228e45/c83ae/stack_ghci.png" alt="Testing our square function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Write tests
&lt;/h3&gt;

&lt;p&gt;Now let's get to the actual testing. All we need to do to is write comments.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Just above your function definition, write your set of inputs and their expected outputs. This is how my &lt;code&gt;Math.hs&lt;/code&gt; looks like:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module Math where

-- | Square numbers
--
-- Examples:
--
-- &amp;gt;&amp;gt;&amp;gt; square 3
-- 9
--
-- &amp;gt;&amp;gt;&amp;gt; square (-1)
-- 1
square :: Int -&amp;gt; Int
square x = x * x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If you try running &lt;code&gt;stack test&lt;/code&gt; you will notice these tests don't run. That's because we haven't set up &lt;code&gt;doctest&lt;/code&gt; yet.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Configure Project for doctest
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First, let's add &lt;code&gt;doctest&lt;/code&gt; to our project. Go to the end of &lt;code&gt;package.yaml&lt;/code&gt; and add it as dependency of &lt;code&gt;my-project-test&lt;/code&gt;. This is how my &lt;code&gt;package.yaml&lt;/code&gt; looks like:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name:                my-project
version:             0.1.0.0
github:              "githubuser/my-project"
license:             BSD3
author:              "Author name here"
maintainer:          "example@example.com"
copyright:           "2020 Author name here"

extra-source-files:
- README.md
- ChangeLog.md

# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at &amp;lt;https://github.com/githubuser/my-project#readme&amp;gt;

dependencies:
- base &amp;gt;= 4.7 &amp;amp;&amp;amp; &amp;lt; 5

library:
  source-dirs: src

executables:
  my-project-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - my-project

tests:
  my-project-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - my-project
    - doctest
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To install the dependency, run &lt;code&gt;stack test&lt;/code&gt;. If the installation goes well, you should see the following message &lt;code&gt;Test suite not yet implemented&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y8OJnmwc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://waqasali.dev/static/784fa395e4382021d943de5a9ef95aad/c83ae/stack_test_1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y8OJnmwc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://waqasali.dev/static/784fa395e4382021d943de5a9ef95aad/c83ae/stack_test_1.png" alt="Test suite not implemented"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you go to the actual test file in &lt;code&gt;test/Spec.hs&lt;/code&gt; you can see that there indeed are no tests. We need to somehow tell stack to look for tests in our &lt;code&gt;src&lt;/code&gt; files instead.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To do that, clear the contents of &lt;code&gt;test/Spec.hs&lt;/code&gt; file and replace it with this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Test.DocTest

main = doctest ["-isrc", "src/Math.hs"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now, if you run &lt;code&gt;stack test&lt;/code&gt; it will run the 2 tests we wrote earlier. That's it, we are done!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WpD6eOu7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://waqasali.dev/static/c77f354285d572b81c3089dc3adde1ee/c83ae/stack_test_2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WpD6eOu7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://waqasali.dev/static/c77f354285d572b81c3089dc3adde1ee/c83ae/stack_test_2.png" alt="Tests pass successfully"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you add more modules, simply add them to this list &lt;code&gt;["-isrc", "src/Math.hs"]&lt;/code&gt; in &lt;code&gt;test/Spec.hs&lt;/code&gt; to make &lt;code&gt;doctest&lt;/code&gt; look for tests in them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Note
&lt;/h3&gt;

&lt;p&gt;This was my setup while writing this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MacOS &lt;code&gt;10.15.3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Haskell Stack &lt;code&gt;2.1.3&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>haskell</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
