<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ben Anderson on Medium]]></title>
        <description><![CDATA[Stories by Ben Anderson on Medium]]></description>
        <link>https://medium.com/@benanderson_80906?source=rss-744d11a710db------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1TEDjfBz1nk_Y0Ii.</url>
            <title>Stories by Ben Anderson on Medium</title>
            <link>https://medium.com/@benanderson_80906?source=rss-744d11a710db------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 09:41:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@benanderson_80906/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Testing Abstract Data Types with ReasonML and Rely]]></title>
            <link>https://medium.com/@benanderson_80906/testing-abstract-data-types-with-reasonml-and-rely-2694202e6a17?source=rss-744d11a710db------2</link>
            <guid isPermaLink="false">https://medium.com/p/2694202e6a17</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[reasonml]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[abstract-data-type]]></category>
            <dc:creator><![CDATA[Ben Anderson]]></dc:creator>
            <pubDate>Tue, 18 Jun 2019 18:31:37 GMT</pubDate>
            <atom:updated>2019-06-18T18:31:37.115Z</atom:updated>
            <content:encoded><![CDATA[<p>Proper abstraction is an important part of writing highly maintainable and usable software. In this article, I aim to address how explicitly testing against abstract data types can help you write more robust tests (that don’t depend on implementation details) and avoid duplicating code.</p><h3>What is an Abstract Data Type (ADT)?</h3><blockquote>An abstract data type is a set of values and associated operations that are specified indepent of any concrete implementation.<br><br> — Paul E. Black, <a href="https://xlinux.nist.gov/dads/HTML/abstractDataType.html">Dictionary of Algorithms and Data Structures</a></blockquote><p>While the formal definition of an abstract data type may not be extraordinarily intuitive, it is likely that you are familiar with many concrete examples. Stacks, lists, queues, and dictionaries are all common examples of ADT’s.</p><p>We can define an immutable Stack ADT in <a href="https://reasonml.github.io/en/">Reason</a> as a module that satisfies the following signature and informally expressed properties</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/148d784299e9621c188b4ac273c3b61e/href">https://medium.com/media/148d784299e9621c188b4ac273c3b61e/href</a></iframe><p>We could also express this ADT using a record type, or with an interface in an object oriented language. These approaches are also valid and can make sense depending on your use case. For the purposes of this article we will be using module types.</p><h3>How is this relevant to the applications/libraries that I am creating?</h3><p>While data structures are certainly the most obvious examples of ADT’s, they are in fact quite common in many applications. We will walk through an example that can hopefully be applied more generally. We will be using <a href="https://reason-native.com/docs/rely/">Rely</a> (which I have spent a fair amount of time working on) to write our tests.</p><h3>Explicitly Identifying our ADT</h3><p>When going through this exercise. It is helpful to first explicitly identify both the type of the ADT (which hopefully already exists in some form in your application) as well as the explicit set of rules that we expect to be true of any instance of the ADT. This set of rules will be the basis for our tests cases.</p><p>For this example, we define our ADT using the following module type (note that this could certainly be expressed as a record type as well)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3699cb1a1954ec3ba51da69edd5a314b/href">https://medium.com/media/3699cb1a1954ec3ba51da69edd5a314b/href</a></iframe><p>Based on this type and our intuitive understanding of what a payment method is, we can develop some plain English rules to describe what we expect to be true of <strong><em>any</em></strong><em> </em>payment method.</p><ul><li>If there are $N in available funds and $M is spent, there should be $(N-M) remaining</li><li>Spending more than is available should return an Error</li></ul><h3>Translating to test cases</h3><p>Now we can begin to translate the rules into actual code. In Reason, a natural way to build these tests independent of implementation is by using a functor (module function). With other representations/programming languages, ordinary functions and generic classes could be used instead.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d22b7483efe1336cfa9350f897a51be/href">https://medium.com/media/4d22b7483efe1336cfa9350f897a51be/href</a></iframe><p>Given this functor, we can now easily add test coverage for new payment methods that we might make.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2163da082c98e3a503ec44b680e1c2cf/href">https://medium.com/media/2163da082c98e3a503ec44b680e1c2cf/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e053808d1af863f45ef8b34580af10e9/href">https://medium.com/media/e053808d1af863f45ef8b34580af10e9/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*Y6Fcn4xtNqdhD9R2gQKWUQ.png" /><figcaption>Green Tests!</figcaption></figure><p>For the full code example see the <a href="https://github.com/bandersongit/testing-abstract-data-types">accompanying github repository</a>.</p><p>I hope that this concept is useful. Please let me know if there are any other testing topics you would like to see my cover by reaching out on twitter (<a href="http://twitter.com/the_banderson">@the_banderson</a>) or the <a href="https://discordapp.com/invite/reasonml">reason discord</a>.</p><p>I first learned about testing ADT’s (and much more) from Zoran Horvat’s excellent Pluaralsight course on <a href="https://www.pluralsight.com/courses/writing-highly-maintainable-unit-tests">Writing Highly Maintainable Unit Tests</a> (this is not an affiliate link), which presents similar material from an object oriented perspective using C#.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2694202e6a17" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>