<?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 Nicolas Dubien on Medium]]></title>
        <description><![CDATA[Stories by Nicolas Dubien on Medium]]></description>
        <link>https://medium.com/@nicolasdubien?source=rss-5bed092e8bd0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*lCC0JdxgygvX5Rjz.</url>
            <title>Stories by Nicolas Dubien on Medium</title>
            <link>https://medium.com/@nicolasdubien?source=rss-5bed092e8bd0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 04:34:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nicolasdubien/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[Your own property based testing framework — Part 4: Runners with shrink]]></title>
            <link>https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-4-runners-with-shrink-92b0711130e5?source=rss-5bed092e8bd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/92b0711130e5</guid>
            <category><![CDATA[deep-dives]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[property-based-testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Nicolas Dubien]]></dc:creator>
            <pubDate>Sat, 18 Jul 2020 10:16:43 GMT</pubDate>
            <atom:updated>2023-04-17T06:58:53.594Z</atom:updated>
            <content:encoded><![CDATA[<h3>Your own property based testing framework — Part 4: Runners with shrink</h3><p>In <a href="https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-1-generators-eeb6cccb84d0">Part 1</a>, we covered the concept of generators. In <a href="https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-2-runners-5c5e083c1b8">Part 2</a>, we added a runner on top of them. In <a href="https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-3-shrinkers-564fa7a180eb">Part 3</a>, we added shrinking capabilities to all our generators.</p><p>At this point, our runner is not using shrinker capabilities we introduced in previous part.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*qoZoDWqOjBNF2wX-P2YVUQ.jpeg" /></figure><p>Same article on dev.to at: <a href="https://dev.to/dubzzz/your-own-property-based-testing-framework-part-4-runners-with-shrink-53f7">https://dev.to/dubzzz/your-own-property-based-testing-framework-part-4-runners-with-shrink-53f7</a></p><p>Shrinkers have been introduced in an attempt to find the smallest failing case. They achieve one main purpose: given one value they compute strictly smaller ones.</p><p>From a runner perspective, in order to get the smallest possible failing case we want to shrink as soon as we find an issue. So we generate values and run the predicate on it and as soon as we find an issue we take this value and shrink it. Moreover while iterating over those shrunk entries we might find another one that will fail, we shrink it and iterate over it until we cannot find smaller bugs.</p><p>The process is summarized into the diagram below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/983/0*Z1fR-yEp-iSJ7Cny.png" /></figure><p>In terms of code, we can adapt our miniFc.assert as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9a53297271295f1af21723d3ba51fec/href">https://medium.com/media/c9a53297271295f1af21723d3ba51fec/href</a></iframe><p>Here we are, our home-made property based framework is now able to generate values, run tests and find failures, and in case of failure to shrink it towards something smaller to help our users.</p><p>A playable example is available at <a href="https://runkit.com/dubzzz/part-4-runners-with-shrink">https://runkit.com/dubzzz/part-4-runners-with-shrink</a></p><blockquote>But unfortunately this design was not solid enough…</blockquote><p>Several things will not be enough in this framework, some of those limitations are detailed into the official documentation of <a href="https://fast-check.dev/">fast-check</a> at <a href="https://github.com/dubzzz/fast-check/blob/master/documentation/1-Guides/HowItWorks.md">https://github.com/dubzzz/fast-check/blob/master/documentation/1-Guides/HowItWorks.md</a></p><p>In a nutshell:</p><ul><li>Our way to map from a generator to another requires both a map and an un-map function. Unfortunately, reversing an entity is not always feasible and may require an extra work just to get shrinking capabilities</li><li>Our way to map from a generator makes it difficult to provide a oneof generator with shrinking capabilities</li><li>Current generators are too dummy, they miss lots of corner cases. While uniform random is good, it may takes an infinite time to find bugs…</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92b0711130e5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Your own property based testing framework — Part 3: Shrinkers]]></title>
            <link>https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-3-shrinkers-564fa7a180eb?source=rss-5bed092e8bd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/564fa7a180eb</guid>
            <dc:creator><![CDATA[Nicolas Dubien]]></dc:creator>
            <pubDate>Tue, 07 Jul 2020 07:01:44 GMT</pubDate>
            <atom:updated>2023-04-17T06:57:48.899Z</atom:updated>
            <content:encoded><![CDATA[<h3>Your own property based testing framework — Part 3: Shrinkers</h3><p>In <a href="https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-1-generators-eeb6cccb84d0">Part 1</a>, we covered the concept of generators. In <a href="https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-2-runners-5c5e083c1b8">Part 2</a>, we added a runner on top of them.</p><p>We are now closer from a real property based testing framework but we still miss one important feature: shrinking capabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*qoZoDWqOjBNF2wX-P2YVUQ.jpeg" /></figure><p>In <a href="https://fast-check.dev/">fast-check</a> all generated values can be shrunk. Shrinking proves pretty useful whenever a property fails as it will reduce the failure to something easier to analyze and debug with.</p><p>Same article on dev.to at <a href="https://dev.to/dubzzz/your-own-property-based-testing-framework-part-3-shrinkers-5a9j">https://dev.to/dubzzz/your-own-property-based-testing-framework-part-3-shrinkers-5a9j</a></p><p>In property based testing, whenever a failure occurs the framework will try to reduce it to something smaller so that the end-user only has to cope with a very simple and small input. It is designed to help developers focusing on the real cause without having to manually investigate several potential sources of bugs.</p><p>For instance: If our isSubstring reported an error like: [&quot;&quot;, &quot;null$¤¤&quot;, &quot;\\undefined.NaN&quot;]. We may have investigate potentially useless sources of bugs like: &quot;null&quot;, &quot;NaN&quot; or even &quot;\\&quot; .</p><p>Before designing complex shrinkers let’s see how they integrate themselves in our current design. First we need to adapt the Generator type.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ed6ef9caf78c0c36cb2f2529ee63ed2/href">https://medium.com/media/7ed6ef9caf78c0c36cb2f2529ee63ed2/href</a></iframe><p>As you can see in the snippet above, we added a method called shrink onto our Generator type. This method takes a value - <em>that have been generated by this </em><em>Generator</em> and builds a stream of potential shrinks for this value.</p><p>In other words, if I take back our generator of integers, I’d expect the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8cbd26153a0e54118fd39ff6713d05f4/href">https://medium.com/media/8cbd26153a0e54118fd39ff6713d05f4/href</a></iframe><p>For integers, the implementation will use a classical technique used in programming: dichotomy. Given the technique we want to use we can adapt the code for integer as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3628947e9da8c13ba6a4074c81c3c38/href">https://medium.com/media/d3628947e9da8c13ba6a4074c81c3c38/href</a></iframe><p>Let’s now consider our next root generators: the one for tuples and the one for arrays.</p><p>As tuples is simpler let’s consider it first. As described in the diagram below a possible way to implement shrink on tuples is to shrink on the first item only, then on the second one only and so on and so forth.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/0*TKJ70YmJwE-XJ1Wd.png" /></figure><p>While it does not cover all the possible smaller combinations it should be good enough while keeping a great convergence speed. It can be implemented as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/618bf9f233f91bdd5a365152989622eb/href">https://medium.com/media/618bf9f233f91bdd5a365152989622eb/href</a></iframe><p>Concerning arrays, the algorithm needs to shrink on two dimensions:</p><ul><li>the size of the array</li><li>the content of the array</li></ul><p>When impacting the size, it should not only cover the cases in which we keep the N first items. It should also consider cases in which last item is required to reproduce the failure while the first ones can be partially removed.</p><p>For instance, if we consider an algorithm that fails whenever an array contains twice the same item. If it first failed on [4, 1, 2, 1, 3] shrinker will have to be able to remove one item at the beginning, one at the middle and one at the end in order to converge towards the minimal case [1, 1].</p><p>Shrinking strategy on arrays is divided into three different steps.</p><ol><li>As most of the time, smaller arrays are simpler to understand, the first step will focus on reducing the size of the array and preserving only the N last items</li><li>As for tuples, arrays are made of values and shrinking them might also simplify investigations. The second step shrink the first element and concatenate it to the remaining ones (not impacted).</li><li>We keep the first item as is and apply recursively 1. and 2. on the tail of the array.</li></ol><p>Let’s apply this logic onto our example [4, 1, 2, 1, 3]:</p><ul><li>[2, 1, 3] (due to 1.)</li><li>[1, 2, 1, 3] (due to 1.)</li><li>[2, 1, 2, 1, 3] (due to 2.)</li><li>[1, 1, 2, 1, 3] (due to 2.)</li><li>[0, 1, 2, 1, 3] (due to 2.)</li><li>[4, 1, 3] = [4] :: [1, 3] (due to 3.1.)</li><li>[4, 2, 1, 3] = [4] :: [2, 1, 3] (due to 3.1.)</li><li>[4, 0, 2, 1, 3] = [4] :: [0, 2, 1, 3] (due to 3.2.)</li><li>…</li></ul><p>We can adapt our implementation of array to to support shrinking as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5873258e66bdecd679b9cc6ae14397e3/href">https://medium.com/media/5873258e66bdecd679b9cc6ae14397e3/href</a></iframe><p>Now that all our root generators have been covered let’s adapt the generators based on them. When defined those derived generators we introduced a map function to help us in our task.</p><p>Generator for characters has been defined as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac63fdeaa5a2bec77b85716764f9ae2f/href">https://medium.com/media/ac63fdeaa5a2bec77b85716764f9ae2f/href</a></iframe><p>As it generates string (of a single character), shrinker will consume string and produce smaller string. But how can map know how to convert the string received as input of the shrinker to passe it the shrinker of mapped generator (or miniFc.integer(0, 25) in our case)?</p><p>Actually if we wanted to define the shrinker function for miniFc.character we would have written something like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a00a7d0c3c0b661163b71936aaed61b/href">https://medium.com/media/5a00a7d0c3c0b661163b71936aaed61b/href</a></iframe><p>In other words, it means that mapping a generator to derive it requires the user to declare two helper functions:</p><ul><li>one to map — <em>eg.: from integer to character in our example</em></li><li>another one to unmap — <em>eg.: from character to integer in our example</em></li></ul><p>With that in mind, map can be changed into:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51b6504f014fca29e71e01ebe101b64b/href">https://medium.com/media/51b6504f014fca29e71e01ebe101b64b/href</a></iframe><p>And all our derived generators can be adapted to add support for shrink:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cdc4e959448545728d71126e118e3d8/href">https://medium.com/media/0cdc4e959448545728d71126e118e3d8/href</a></iframe><p>Given all the work above, you should be able to shrink values including complex ones such as dictionaries.</p><p>A playable example is available at <a href="https://runkit.com/dubzzz/part-3-shrinkers">https://runkit.com/dubzzz/part-3-shrinkers</a></p><p>Next part: <a href="https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-3-shrinkers-564fa7a180eb">https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-3-shrinkers-564fa7a180eb</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=564fa7a180eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Your own property based testing framework — Part 2: Runners]]></title>
            <link>https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-2-runners-5c5e083c1b8?source=rss-5bed092e8bd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/5c5e083c1b8</guid>
            <category><![CDATA[deep-dives]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[property-based-testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Nicolas Dubien]]></dc:creator>
            <pubDate>Sun, 05 Jul 2020 20:47:01 GMT</pubDate>
            <atom:updated>2023-04-17T06:56:18.188Z</atom:updated>
            <content:encoded><![CDATA[<h3>Your own property based testing framework — Part 2: Runners</h3><p>In <a href="https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-1-generators-eeb6cccb84d0">Part 1</a>, we covered the concept of generators. Without them property based testing would not be possible. Alone they do not offer a lot.</p><p>In this article, we will cover an other main piece of property based testing frameworks: runners.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*qoZoDWqOjBNF2wX-P2YVUQ.jpeg" /></figure><p>In fast-check there is one main runner called fc.assert. Let’s see how to add such feature into our minimal framework.</p><p>Same article on dev.to at <a href="https://dev.to/dubzzz/your-own-property-based-testing-framework-part-2-runners-12fl">https://dev.to/dubzzz/your-own-property-based-testing-framework-part-2-runners-12fl</a></p><p>First of all, we will have to define the concept of property. For simplicity, a property can be seen as a super generator:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66217f214a5fc30c087b120a7a152f89/href">https://medium.com/media/66217f214a5fc30c087b120a7a152f89/href</a></iframe><p>In our case, properties will be created using the following helper:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d7444e7229ee3c38d5c63b4d00bbb2b/href">https://medium.com/media/8d7444e7229ee3c38d5c63b4d00bbb2b/href</a></iframe><p>Now, let’s consider a simple example to understand how we want to use our minimal version of <a href="https://fast-check.dev/">fast-check</a>. The code under test will be an implementation of isSubstring with obviously a bug into it in order to check that our framework can find it. As a user we would like to be able to write the following test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/481ad9f5ef99e03b24cb9d40c7c5c3d0/href">https://medium.com/media/481ad9f5ef99e03b24cb9d40c7c5c3d0/href</a></iframe><p>In terms of typings we have the following signature to fulfill on assert:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3806d5f2eed3f369d459e27c29c9a4ad/href">https://medium.com/media/3806d5f2eed3f369d459e27c29c9a4ad/href</a></iframe><p>By default, in most frameworks, runners run the property a hundred times and stop if everything is working fine after those hundred runs.</p><p>A basic implementation for the runner can be written as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26be4bd6f44b213ed81557bbdc1f6662/href">https://medium.com/media/26be4bd6f44b213ed81557bbdc1f6662/href</a></iframe><p>Additionally in property based testing, seed is supposed not to be fixed except if specified on call-site. Implementation above can be updated as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0e871ccedc2c3947cd0832a83b37fd9/href">https://medium.com/media/d0e871ccedc2c3947cd0832a83b37fd9/href</a></iframe><p>In the previous article, we did not cover the reason why we opted for pure random generators. In property based we want properties to be reproducible no matter the seed, no matter the hardware and no matter the unix time… But we also want to have independent runs for each iteration in the loop.</p><p>For instance, in the implementation defined above, we call generate with the following instances of Random:</p><ul><li>runId = 0<br>Call with new Random(prand.xoroshiro128plus(seed))</li><li>runId = 1<br>Call with new Random(prand.xoroshiro128plus(seed)).jump()</li><li>runId = 2<br>Call with new Random(prand.xoroshiro128plus(seed)).jump().jump()</li><li>…</li></ul><p>jump offsets a random number generator. In the context of xoroshiro128plus calling jump is equivalent to 2^64 calls to next. In the case of <a href="https://github.com/dubzzz/pure-rand/">pure-rand</a> neither jump nor next alter the original instance, they both create a new instance of the generator while keeping the original one unchanged.</p><p>No matter how many times our property will call the passed mutable random generator, we will always ignore it to build the generator required for the next iteration. While it might appear strange at first glance, this feature is important as we don’t really know what will happen to this instance of our random generator. Among the possible scenario:</p><ul><li>relying on the offset applied by the property to the passed instance of Random is problematic as it makes replays difficult to implement except if we re-generate all the values one-by-one whenever replaying stuff</li><li>instance of Random might be kept and re-used later by the property and its Generator (we will see that it might be the case in some implementations of shrink), thus calls to generate in subsequent iterations might alter it</li></ul><p>We can now use our small framework on the property we discussed earlier in this section. As we were expecting, it finds an issue and reports it. When running it locally you should have an output similar to:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c43b70f2ab9463f118df41d48d396d96/href">https://medium.com/media/c43b70f2ab9463f118df41d48d396d96/href</a></iframe><p>Given all the work above, you should be able to write and test properties <em>as if</em> you were using <a href="https://fast-check.dev/">fast-check</a>.</p><p>A playable example is available at <a href="https://runkit.com/dubzzz/part-2-runners">https://runkit.com/dubzzz/part-2-runners</a></p><p>Next part: <a href="https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-3-shrinkers-564fa7a180eb">https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-3-shrinkers-564fa7a180eb</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c5e083c1b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Your own property based testing framework — Part 1: Generators]]></title>
            <link>https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-1-generators-eeb6cccb84d0?source=rss-5bed092e8bd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/eeb6cccb84d0</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[property-based-testing]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[deep-dives]]></category>
            <dc:creator><![CDATA[Nicolas Dubien]]></dc:creator>
            <pubDate>Fri, 03 Jul 2020 07:52:54 GMT</pubDate>
            <atom:updated>2023-04-17T06:54:52.945Z</atom:updated>
            <content:encoded><![CDATA[<h3>Your own property based testing framework — Part 1: Generators</h3><p>While understanding the internals of each tool is not required to use them properly, sometimes I personally feel the need to dig a bit into the tools I am using regularly.</p><p>If you never heard about property based testing, you might be interested by reading <a href="https://medium.com/criteo-labs/introduction-to-property-based-testing-f5236229d237">this quick introduction</a> first.</p><p>Same article on dev.to at <a href="https://dev.to/dubzzz/your-own-property-based-testing-framework-part-1-generators-3afi">https://dev.to/dubzzz/your-own-property-based-testing-framework-part-1-generators-3afi</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*qoZoDWqOjBNF2wX-P2YVUQ.jpeg" /></figure><p><em>Concerning property based testing everything started at the end of 2017…</em></p><p>At that time I was trying to apply property based testing on User Interfaces as explained by <a href="https://engineering.atspotify.com/2015/06/25/rapid-check/">this article from the author of RapidCheck</a> or <a href="https://medium.com/criteo-labs/detecting-the-unexpected-in-web-ui-fuzzing-1f3822c8a3a5">my article</a>. So I took the leading JavaScript property based testing library and tried to <a href="https://www.npmjs.com/package/jsverify-commands">add model based testing support to it</a>… but failed.</p><p>So after weeks of trial and errors, I finally changed my mind and wanted to understand how things worked under the hood to get why it did not work. The result of this analysis is <a href="https://fast-check.dev/">fast-check </a>— a property based testing framework written in TypeScript for both JavaScript and TypeScript users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*AM1vDic7stXroi0R87xkCA.png" /><figcaption>Logo of fast-check</figcaption></figure><p>Throughout this serie of articles we will see how to build our own property based testing library from scratch. Our wonderful library will be called miniFc, it will expose all the features required for property based testing from generation of random values to shrinking capabilities.</p><p><em>But let’s start from the beginning…</em></p><p>In property based testing everything starts with generators. Generators take a random number generator and build anything out of it.</p><p>You can see generators as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83bab26bf19b6cda72e841e9e3e583b2/href">https://medium.com/media/83bab26bf19b6cda72e841e9e3e583b2/href</a></iframe><p>In the signature above mrng is a mutable random generator. It is a simple wrapper around <a href="https://github.com/dubzzz/pure-rand/">pure-rand</a> that provides a usable random instance. The class Random can be implemented as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd670970d759f60e42b584b3218e0201/href">https://medium.com/media/bd670970d759f60e42b584b3218e0201/href</a></iframe><p>Let’s build our first generator: the one responsible to build random integers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72ccd56a31f16ed82b57ecad0908e3a2/href">https://medium.com/media/72ccd56a31f16ed82b57ecad0908e3a2/href</a></iframe><p>Our random integer Generator is simply returning the value forged by the passed random number generator.</p><p>We want to go further as generators should be able to generate not only integers, they are supposed to generate barely any kind of values. In <a href="https://fast-check.dev/">fast-check</a>, you can generate numbers, strings, arrays but also objects, functions, recursive structures and also infinite streams of any kind of stuff. In order to produce so many different kind of values without copying the same code again and again, they rely on taking root structures and deriving them into more complex ones as described below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/0*ouZkQn-bE2wcHfRc.png" /></figure><p>Let’s first consider boolean and character. In the diagram above they are constructed out of integer generator. In order to do so, we need to introduce an extra function called map. It will take an existing generator and a mapper function and create another generator out of it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6bfd8a68673af9a8a2ace1c94b40a4d/href">https://medium.com/media/a6bfd8a68673af9a8a2ace1c94b40a4d/href</a></iframe><p>It can be implemented as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f154fac95f09de9a0d9794e0eadf6dd/href">https://medium.com/media/3f154fac95f09de9a0d9794e0eadf6dd/href</a></iframe><p>Now that we have map we can implement some of our missing generators:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa08827218aa37c5bf401527c4c87a2f/href">https://medium.com/media/fa08827218aa37c5bf401527c4c87a2f/href</a></iframe><p>In order to build others, we first need to implement a generator for tuples and a generator for arrays:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7f8a20bc32ba625bfcc2678a7ffcbaf/href">https://medium.com/media/e7f8a20bc32ba625bfcc2678a7ffcbaf/href</a></iframe><p>Now we can build our last generators:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/732307bff8cfc4d68abfb7aa30d05cc6/href">https://medium.com/media/732307bff8cfc4d68abfb7aa30d05cc6/href</a></iframe><p>Given all the work above, you should be able to generate values ranging from simple booleans to complex dictionaries.</p><p>A playable example is available at <a href="https://runkit.com/dubzzz/part-1-generators">https://runkit.com/dubzzz/part-1-generators</a></p><p>Next part: <a href="https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-2-runners-5c5e083c1b8">https://medium.com/@nicolasdubien/your-own-property-based-testing-framework-part-2-runners-5c5e083c1b8</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eeb6cccb84d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Detecting the unexpected in (Web) UI]]></title>
            <link>https://medium.com/criteo-engineering/detecting-the-unexpected-in-web-ui-fuzzing-1f3822c8a3a5?source=rss-5bed092e8bd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f3822c8a3a5</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[property-based-testing]]></category>
            <category><![CDATA[quickcheck]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Nicolas Dubien]]></dc:creator>
            <pubDate>Wed, 06 Feb 2019 14:22:14 GMT</pubDate>
            <atom:updated>2023-04-17T06:53:50.206Z</atom:updated>
            <content:encoded><![CDATA[<h4>What if my CI turned into my preferred QA? Let’s fuzz UI</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/1*mkLYuWO73DIagk7lbGNqzA.png" /><figcaption>Inspired from <a href="https://fast-check.dev/">fast-check</a> logo combined with famous web frameworks</figcaption></figure><p>And then one day, it happened. Despite all the unit, integration and end-to-end tests that have been implemented. A customer of yours complained about what appears to be a bug you should have caught before releasing:</p><blockquote>[…] Steps to reproduce the bug:</blockquote><blockquote>1- Start a new project<br>2- Save it<br>3- Refresh the page<br>4- Click on ‘go back home’ 💥</blockquote><p>Some of those uncaught bugs turned out to be even weirder than expected:</p><ul><li>Android issue: vulnerability allowing anyone to bypass the lock screen by making it crash (<a href="https://sites.utexas.edu/iso/2015/09/15/android-5-lockscreen-bypass/">more details</a>)</li><li>GitHub issue: <em>submitting a comment after clicking the “Start a new conversation” button on a pull request diff raised an error under some circumstances</em> (<a href="https://enterprise.github.com/releases/2.15.3/notes">more details</a>)</li><li>Jira issue: <em>unable to save comments containing emojis</em> (<a href="https://confluence.atlassian.com/jirakb/jira-does-not-work-with-emoji-4-byte-characters-429919955.html?_ga=2.77298517.1911035267.1548406979-148409369.1536076147">more details</a>)</li></ul><p>Found with the technic explained in the article:</p><ul><li>Spotify issue: <em>enabling repeat mode ignored all tracks added after the user pressed play</em> (<a href="https://labs.spotify.com/2015/06/25/rapid-check/">more details</a>, see “…<em>and there were bugs</em>”)</li><li>Google LevelDB issue: <em>reappearing “ghost” key after 17 steps</em> (<a href="https://github.com/google/leveldb/issues/50">more details</a>)</li></ul><p>Here you’ll find an easier and quicker way to detect this typology of issue. While the issue chosen to illustrate the problem is a web-app, the solution can easily be extended to other technologies.</p><h4>Hard to catch</h4><p>Most of those bugs relied on scenarios which were not the mainstream. For instance, in the Android issue, the attacker had to open the “emergency dialer”, tap hundreds of characters into it, open the camera.</p><p>Thinking about covering such scenario with classical end-to-end or unit tests beforehand would have seemed crazy. Indeed we do not want to hard-code or consider unlikely strange scenarios that a user can execute. Truth is, it is not even possible as the cardinality of such problem would be infinite and therefore we might miss lots of them.</p><h4>Yet, humans can</h4><p>But humans can. You, me or your two-year-old nephew randomly playing with your phone for hours may have detected the Android issue. This statement is a direct consequence of the <a href="https://en.wikipedia.org/wiki/Infinite_monkey_theorem">infinite monkey theorem</a>: “<em>a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type any given text, such as the complete works of William Shakespeare</em>”.</p><p>Instead of applying very strict scenarios, as our tests usually do, we apply each time a new path to do the “same” operation. We also have the knowledge of what are the expected outcomes of all our actions — <em>eg.: when I click on open camera, I expect the camera to open itself</em> or <em>when I log with random credentials, I expect not to be logged in</em>.</p><h4>Computers can too</h4><p>What if we were able to give such knowledge to computers?</p><p>Basically, a scenario is a succession of valid actions on a UI or automaton. Before applying an action we need to ensure it can be applied given the current state of the UI. Only then, we run it and check if it executed what it was supposed to do.</p><p>“commands” can be summarized as being a “combination of an action plus conditions” such as:</p><ul><li>preconditions: can the command be executed given the current context?</li><li>action: what is the command supposed to do?</li><li>post-conditions: what are the expected outcomes of the command visible?</li></ul><p>In addition to that, we have to define and build a “model” or “current context” so that commands know what they are dealing with and update it if needed. The model can be the UI itself but it can also include additional data such as history, user settings… or any data we do not want to get back from the UI.</p><p>Given that we defined all the possible commands for our system, we are able to generate our own random scenario. In order to do so, we generate a random array of commands, we take them one by one and we execute them in order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*3biRdlPH37sGuB_zTeNmUQ.png" /><figcaption>Execution of a single command</figcaption></figure><p>If everything is running fine with this array, we can restart with another random scenario. If not, we can try to reduce to a smaller scenario or return it directly.</p><h4>Property based testing to the rescue</h4><p>Generating random arrays is quite trivial. Generating them in a stable and reproducible way requires seeded generators.</p><p>Wait a minute…</p><p>If a bug occurs, we expect the scenario to be marked as “failed”. As it was randomly generated there is no certainty that it will be easy to replay manually. At worst it can contain commands that will guide us on the wrong tracks. For all those reasons, reducing it to the minimal scenario is more than useful.</p><p>Property based testing frameworks are built with that purpose in mind. They have been designed to generate random datasets and reduce them in case they make a program fail.</p><p>Frameworks like <a href="https://fast-check.dev/">fast-check</a> in JavaScript or <a href="https://github.com/emil-e/rapidcheck/">RapidCheck</a> in C++ already provide a built-in way to handle the generation of such scenarios.</p><p>For more details on property based testing, you can refer to <a href="https://medium.com/criteo-labs/introduction-to-property-based-testing-f5236229d237">my previous publication on this subject</a>.</p><h4>Hands-on commands</h4><p>Let’s apply it to a <a href="https://dubzzz.github.io/connect-four-react/">Connect Four</a> game having the following capabilities:</p><ul><li>User can play a token if there is no winner and if the column is not full,</li><li>User can restart the game by pressing “New Game” button,</li><li>User can copy the url to share the state of the game to someone else,</li><li>User can undo or redo any move…</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*mDFzM5bn0Jd-DZZ8hCg9RA.png" /><figcaption>Connect Four UI</figcaption></figure><h4>Let’s try the system manually</h4><p>The Connect Four project comes with two distinct implementations. One of them has a bug that can be detected with command-based approach: <a href="https://dubzzz.github.io/connect-four-react/bug/">https://dubzzz.github.io/connect-four-react/bug/</a></p><p>Do you find it?</p><p>The bugged version is available on branch “buggy-implementation”.</p><h4>User capabilities</h4><p>In order to extract what will be our commands and our model, we need to identify the actions an user can execute. Given the system above, an user can:</p><ul><li>click on the grid</li><li>click on one of the available buttons: undo, redo or new game</li><li>re-use a bookmarked url</li></ul><p>In terms of commands we can extract the following ones , among others:</p><ul><li>play a valid move</li><li>undo a move</li><li>redo a move</li><li>restart the game</li><li>bookmark a url</li><li>open a previously bookmarked url</li></ul><h4>From human specs to machine</h4><p>For all those commands we can expect some pre-conditions and post-conditions to be fulfilled. All the commands defined to test this project are available at <a href="https://github.com/dubzzz/connect-four-react/tree/master/e2e/limited-model/commands">https://github.com/dubzzz/connect-four-react/tree/master/e2e/limited-model/commands</a></p><p><em>“play a valid move”</em> [<a href="https://github.com/dubzzz/connect-four-react/blob/master/e2e/limited-model/commands/PlayTokenCommand.ts">code</a>]:</p><ul><li>precondition: no winner yet, column is not full — or the column is clickable</li><li>action: click on the column</li><li>postcondition: a new token has been added into the column with the color of the current player</li></ul><p>“play an invalid move” [<a href="https://github.com/dubzzz/connect-four-react/blob/master/e2e/limited-model/commands/PlayForbiddenTokenCommand.ts">code</a>]:</p><ul><li>precondition: winner or the column is full — or the column is forbidden</li><li>action: click on the column</li><li>postcondition: the grid has not changed</li></ul><p><em>“undo a move”</em> [<a href="https://github.com/dubzzz/connect-four-react/blob/master/e2e/limited-model/commands/UndoCommand.ts">code</a>]:</p><ul><li>precondition: board is not empty</li><li>action: click on undo</li><li>postcondition: last played token as been removed</li></ul><p>In all the commands above the “model” could have been reduced to the UI itself. Undo might require additional details if we really want to check that it removed the last played token and not another one. The attached implementation just checks that one token has been removed.</p><p>For commands like “bookmark an url” or “open a previously bookmarked url”, the model has to include an additional field responsible to store all the bookmarked urls. Commands might be implemented as follow:</p><p><em>“bookmark an url”</em> [<a href="https://github.com/dubzzz/connect-four-react/blob/master/e2e/limited-model/commands/SaveUrlCommand.ts">code</a>]:</p><ul><li>precondition: none, it is always possible</li><li>action: add the url and its associated grid into the model</li><li>postcondition: it is the first time we bookmark this url or it was already bookmarked with the same grid content</li></ul><p><em>“open a previously bookmarked url” </em>[<a href="https://github.com/dubzzz/connect-four-react/blob/master/e2e/limited-model/commands/ReopenViaUrlCommand.ts">code</a>]:</p><ul><li>precondition: model has at least one bookmarked url</li><li>action: open the bookmarked url</li><li>postcondition: the grid is the same as the one we bookmarked</li></ul><h4>And computer found a bug</h4><p>If you have not found the bug manually, no worries, the computer found it for you. The failure after shrinking was:</p><blockquote>0- (Not a command) Open the game<br>1- Bookmark the <a href="https://dubzzz.github.io/connect-four-react/bug/#/">url </a>— empty grid and player 1 has to play<br>2- Play token in the 6th column<br>3- Click on “new game”<br>4- Bookmark the <a href="https://dubzzz.github.io/connect-four-react/bug/#/">url </a>— empty grid and player 2 has to play<br> — The url is the same but the state is not</blockquote><p>And the shrinking was more than necessary. The original failure the framework found was less clear. It included 27 steps:</p><blockquote>0- (Not a command) Open the game<br>1- Click on “new game”<br>2- Bookmark the url<br>3- Open bookmarked url: <a href="http://localhost:3000/#/">http://localhost:3000/#/</a><br>4- Press F5 to refresh the page<br>5- Bookmark the url<br>6- Play token in the 2nd column<br>7- Click on “new game”<br>8- Play token in the 2nd column<br>9- Play token in the 5th column<br>10- Play token in the 5th column<br>11- Play token in the 3rd column<br>12- Play token in the 7th column<br>13- Play token in the 7th column<br>14- Bookmark the url<br>15- Bookmark the url<br>16- Click on “undo”<br>17- Play token in the 6th column<br>18- Bookmark the url<br>19- Check the player stated in the top bar<br>20- Play token in the 1st column<br>21- Undo all then redo all<br>22- Check the player stated in the top bar<br>23- Click on “undo”<br>24- Bookmark the url<br>25- Click on “new game”<br>26- Press F5 to refresh the page<br>27- Bookmark the url</blockquote><h3>Nicolas DUBIEN on Twitter</h3><p>Recently had fun turning #Selenium into a #QAtester for a #reactjs #ui and discovered funny bugs with #PropertyBased framework https://t.co/mNOwddVnJL - more to come soon https://t.co/LHLfciPdEV</p><h4>Going further</h4><p>Throughout this article we introduced ways to detect unexpected bugs by turning property based testing frameworks like <a href="https://fast-check.dev/">fast-check</a> into QA for our applications. With that setup enabled, we are now ready to detect bugs, regressions automatically and come with very succinct counterexamples.</p><p>I personally used it successfully at work to detect unexpected behaviours on a Web UI Single Page App we are working on.</p><p>Additionally, it proved how property can be used outside of the simple scenario of a pure function which is often considered when explaining the technic.</p><h3>Brenan Keller on Twitter: &quot;A QA engineer walks into a bar. Orders a beer. Orders 0 beers. Orders 99999999999 beers. Orders a lizard. Orders -1 beers. Orders a ueicbksjdhd. First real customer walks in and asks where the bathroom is. The bar bursts into flames, killing everyone. / Twitter&quot;</h3><p>A QA engineer walks into a bar. Orders a beer. Orders 0 beers. Orders 99999999999 beers. Orders a lizard. Orders -1 beers. Orders a ueicbksjdhd. First real customer walks in and asks where the bathroom is. The bar bursts into flames, killing everyone.</p><h4>Additional readings</h4><ul><li>Introduction to Property Based Testing — <a href="https://medium.com/@nicolasdubien/introduction-to-property-based-testing-f5236229d237">https://medium.com/@nicolasdubien/introduction-to-property-based-testing-f5236229d237</a></li><li>Generating test cases so you don’t have to — <a href="https://labs.spotify.com/2015/06/25/rapid-check/">https://labs.spotify.com/2015/06/25/rapid-check/</a></li><li>Model-based testing with fast-check — <a href="https://github.com/dubzzz/fast-check/blob/master/documentation/Tips.md#model-based-testing-or-ui-test">https://github.com/dubzzz/fast-check/blob/master/documentation/Tips.md#model-based-testing-or-ui-test</a></li></ul><h4>👋 I really thank folks of Criteo for the proofreading and support, with special thanks to <a href="https://twitter.com/sinedied">@<strong>sinedied</strong> </a>‏, <a href="http://twitter.com/zhenyi2697">@zhenyi2697</a></h4><h4>👏 Feedbacks or questions more than welcome</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f3822c8a3a5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/criteo-engineering/detecting-the-unexpected-in-web-ui-fuzzing-1f3822c8a3a5">Detecting the unexpected in (Web) UI</a> was originally published in <a href="https://medium.com/criteo-engineering">Criteo Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Find the best properties for Property Based Testing]]></title>
            <link>https://medium.com/@nicolasdubien/find-the-best-properties-for-property-based-testing-ee2ed9d442e1?source=rss-5bed092e8bd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee2ed9d442e1</guid>
            <category><![CDATA[property-based-testing]]></category>
            <category><![CDATA[quickcheck]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[fast-check]]></category>
            <dc:creator><![CDATA[Nicolas Dubien]]></dc:creator>
            <pubDate>Mon, 07 May 2018 22:13:25 GMT</pubDate>
            <atom:updated>2023-04-17T06:51:47.778Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Tips to build properties — Examples written using fast-check</em></p><p>Property based testing became famous thanks to QuickCheck in Haskell. It is another way to test your code which is fully complementary to classical unit-test methods.</p><p>It tries to discover inputs causing a property to be falsy by testing it against multiple — <em>in general a hundred is enough</em> — eligible inputs. A property can be seen as:</p><blockquote>for any (x, y, …)<br>such as precondition(x, y, …) holds<br>property(x, y, …) is true</blockquote><p>You may refer to <a href="https://medium.com/@nicolasdubien/introduction-to-property-based-testing-f5236229d237">https://medium.com/@nicolasdubien/introduction-to-property-based-testing-f5236229d237</a> for more details about property based testing.</p><h3>Let’s find our properties</h3><p>The main difficulty when moving to property based testing is: f<em>inding the right properties, for a given algorithm</em>. Most of the time we tend to test the code against… itself which is not the right way to do.</p><p>This article describes several kinds of properties that you should think about when trying to find yours. It provides examples written using <a href="https://fast-check.dev/">fast-check</a>, a property based testing framework for JavaScript and TypeScript. Everything can easily be transposed to other frameworks.</p><p>Basically fast-check itself uses properties to test itself, so you might be able to run properties on your own code too.</p><p>The examples below are all available at <a href="http://runkit.com/dubzzz/property-based-find-the-best-properties">http://runkit.com/dubzzz/property-based-find-the-best-properties</a>. Try to run them locally and play with them.</p><p>You may also clone the repository <a href="https://github.com/dubzzz/fast-check-examples">https://github.com/dubzzz/fast-check-examples</a> to have other examples — <em>and maybe add some</em>.</p><h3>Characteristics independent of the inputs</h3><p><strong>When?</strong> Some characteristics of the output are independent of its inputs. If those characteristics are strong enough they can be defined as properties. Being fully agnostic of the inputs is most of the time too restrictive to have a useful property but can constitute a first try.</p><p>Here are some examples of such properties:</p><ul><li>for any floating point number d, Math.floor(d) is an integer</li><li>for any integer n, Math.abs(n) ≥ 0</li></ul><h3>Characteristics derived from the inputs</h3><p><strong>When? </strong>The output of your algorithm is easy to check: it has a relationship with the input. The relationship might just be a set of traits you expect to observe in the output based on the input.</p><p>Property #1: The average of a and b must be between a and b</p><blockquote>for any a and b integers<br>the average of a and b is between a and b</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06fc3bf0fa5f5c1123f2443e48ec546d/href">https://medium.com/media/06fc3bf0fa5f5c1123f2443e48ec546d/href</a></iframe><p>Property #2: The decomposition in prime numbers of n must be such that the product of all numbers in the decomposition equals n</p><blockquote>for any n<br>the product of all numbers in the prime factor decomposition of n equals n</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3edf0aed8a378eedeab850bb8e180ad4/href">https://medium.com/media/3edf0aed8a378eedeab850bb8e180ad4/href</a></iframe><p>Other examples:</p><ul><li>for any array — data, sorted(data) and data contains the same elements</li><li>for any n1, n2 integers such that n1 != n2, romanString(n1) != romanString(n2)</li><li>for any floating point number d, Math.floor(d) is an integer such as d-1 ≤ Math.floor(d) ≤ d</li></ul><h3>Restricted set of inputs with useful characteristics</h3><p><strong>When?</strong> Some inputs have a very simple output. In some cases, you might be able to extract some very specific inputs for which you can easily compute the output.</p><p>Property #3: removing duplicates of an array of unique values returns the array itself</p><blockquote>for any array data with no duplicates — called data<br>the result of removing duplicates from data is data itself</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48064254f4f8a544cbcfb99ab8046f00/href">https://medium.com/media/48064254f4f8a544cbcfb99ab8046f00/href</a></iframe><p>Property #4: the concatenation of a, b and c always contains string b</p><blockquote>for any a, b and c strings<br>the concatenation of a, b and c always contains b</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e354013b5348888de7a3f0cf03cea56/href">https://medium.com/media/0e354013b5348888de7a3f0cf03cea56/href</a></iframe><p>Other examples:</p><ul><li>for any prime number p, its decomposition into prime factors is itself</li></ul><h3>Characteristics on combination of functions</h3><p><strong>When?</strong> Two or more functions can be combined to compute something easy to check.</p><p>Property #5: zipping then unzipping a file should result in the original file</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/072549f0665321674ca100333a33f6d8/href">https://medium.com/media/072549f0665321674ca100333a33f6d8/href</a></iframe><p>Property #6: lcm(a,b) times gcd(a,b) must be equal to a times b</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/968e3b80a84b2db579dcb1e277203387/href">https://medium.com/media/968e3b80a84b2db579dcb1e277203387/href</a></iframe><h3>Comparison with a simpler implementation</h3><p><strong>When?</strong> A simpler implementation of the algorithm exists.</p><p>Property #7: c is contained inside sorted array data for binary search is equivalent to c is contained inside data for linear search</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d0c794a37e42189fcbcd641129db796/href">https://medium.com/media/9d0c794a37e42189fcbcd641129db796/href</a></iframe><h3>More examples…</h3><p>You can check for additional examples at: <a href="https://github.com/dubzzz/fast-check/tree/main/examples">https://github.com/dubzzz/fast-check/tree/main/examples</a> and more are coming…</p><p>Please leave a clap or a comment if you liked this article ;)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee2ed9d442e1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Things you should know about Unicode]]></title>
            <link>https://medium.com/@nicolasdubien/things-you-should-know-about-unicode-658f185a242d?source=rss-5bed092e8bd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/658f185a242d</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[unicode]]></category>
            <category><![CDATA[ascii]]></category>
            <category><![CDATA[utf-8]]></category>
            <category><![CDATA[encoding]]></category>
            <dc:creator><![CDATA[Nicolas Dubien]]></dc:creator>
            <pubDate>Sun, 15 Apr 2018 22:08:04 GMT</pubDate>
            <atom:updated>2019-02-03T20:20:12.137Z</atom:updated>
            <content:encoded><![CDATA[<p>You might already have heard about one of those terms — <em>ASCII, UTF-8, UTF-16, Unicode</em> — without exactly knowing what they were dealing with.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*vwyArWBhryiE9ko4L2SyAQ.png" /><figcaption>Unicode characters might seem magic</figcaption></figure><p>Before diving into all this complexity, I will briefly explain the history of encoding throughout the ages…</p><h3>Quick story</h3><p>All started in 1960 with <a href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> — <em>American Standard Code for Information Interchange</em>. ASCII is a 7 bits encoding able to encode 128 characters among which 95 are printable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*DODTwq55h_oOCfItXL46dg.png" /><figcaption>Source: Wikipedia FR — ASCII</figcaption></figure><p>With the soaring of computers and the need to encode more and more languages it has been extended by a variety of so called <a href="https://en.wikipedia.org/wiki/Extended_ASCII">Extended ASCII</a>. There are not one but multiple Extended ASCII. The idea behind this change was to use the bit that was not used by ASCII to encode 128 additional characters while keeping the first 128 characters unchanged.</p><p>So far so good, users in America and Europe were able to encode all their characters simply by using the right charset. But in reality it raised some issues when exchanging messages across different regions.</p><p>For instance, if you wanted to share a document written using <a href="https://en.wikipedia.org/wiki/ISO/IEC_8859-15">Latin-9</a> and featuring the euro symbol — € — to someone using <a href="https://en.wikipedia.org/wiki/ISO/IEC_8859-1">Latin-1</a>, she or he would have received another character instead — ¤.</p><h3>Unicode to the rescue</h3><p>While Extended ASCII looked good and useful, many issues were still unsolved:</p><ul><li>not all languages can be encoded using 8-bits</li><li>characters are ambiguous: character 0xA4 is either “unspecified currency” — <em>in Latin-1 </em>— or “euro” —<em> in Latin-9</em></li></ul><p>With those drawbacks in mind, Unicode 1.0.0 was released in 1991. It introduced a strict distinction between:</p><ul><li>code unit: The minimal bit combination that can represent a unit of encoded text for processing or interchange</li><li>code point: A value, or position, for a character, in any coded character set</li><li>grapheme: What a user thinks of as a character</li></ul><p>Source: Glossary — <a href="http://www.unicode.org/glossary/">http://www.unicode.org/glossary/</a></p><p>Unicode defines a range of code points going from 0x0000 to 0x10FFFF with some of them still undefined and others reserved. It can define up to 1,114,112 code points.</p><p>In addition to bringing a universal and unambiguous definition of a given code point, Unicode also defines its name, rules to combine it with others and gives a visual reference of it.</p><p>In terms of implementation, Unicode can be implemented by different character encodings. It suggests three encodings — UTF-8, UTF-16 and UTF-32 — but others are also commonly used in the industry.</p><p>For instance, in Unicode the EURO SIGN has the code point 0x20AC but ways to represent it differ from one encoding to another:</p><ul><li>One code unit / One byte: 0xA4 in Latin-9</li><li>Three code units / Three bytes: 0xE2 0x82 0xAC in UTF-8</li><li>One code unit / Two bytes: 0x20AC in UTF-16</li></ul><h3>Major encodings</h3><p>Unicode would be nothing without ways to encode it. It only defines the correspondance of a code point — <em>that can be seen like an index or row number</em> — to a name, visual representation… not how to store it in memory.</p><p>This is the reason why Unicode needs encodings. Encoding is like a mapping from an in-memory “code unit” to the code point it represents and vice-versa.</p><p>In the list below I explicitly chose to add the size of one code unit for each of the encodings. It is important to underline that depending on the encoding a code point can be represented by multiple code units — <em>often denoted Character in common languages</em>.</p><p>UTF-8</p><ul><li>Code unit is 1 byte long,</li><li>From 1 to 4 bytes per code point,</li><li>No byte ordering issue — <em>BOM explained later</em>,</li><li>Backward compatible with ASCII — <em>characters in the range 0x00 to 0x7F are encoded as in ASCII </em>and on a single byte,</li><li>The standard for communication between services</li></ul><p>UCS-2</p><ul><li>Understand a subset of Unicode: Only compatible with the BMP plan of Unicode — <em>from 0x0000 to 0xFFFF</em>,</li><li>Code unit is 2 bytes long,</li><li>Always 2 bytes long,</li><li>Some unassigned characters — <em>from 0xD800 to 0xDFFF</em>, — <em>thanks </em><a href="https://twitter.com/FakeUnicode"><em>@FakeUnicode</em></a><em> for the update</em></li><li>Byte ordering required</li></ul><p>UTF-16</p><ul><li>Code unit is 2 bytes long,</li><li>Renewed UCS-2,</li><li>Backward compatible with UCS-2 — <em>for the BMP plan</em>,</li><li>Some forbidden characters called surrogate characters — <em>from 0xD800 to 0xDFFF</em>, — <em>thanks </em><a href="https://twitter.com/FakeUnicode"><em>@FakeUnicode</em></a><em> for the update</em></li><li>Either 2 or 4 bytes,</li><li>Byte ordering required</li></ul><p>UTF-32</p><ul><li>Code unit is 4 bytes long,</li><li>Always 4 bytes long,</li><li>Byte ordering required</li></ul><p>Encodings above constitute the most frequent ones in western countries. Nonetheless, it worth mentioning the existence of GB 18030 which is a very commonly found encoding in China.</p><p>More on encodings on <a href="https://en.wikipedia.org/wiki/Comparison_of_Unicode_encodings">https://en.wikipedia.org/wiki/Comparison_of_Unicode_encodings</a></p><h3>Byte Order Mark</h3><p>But what is this unexplained “byte ordering” / “byte order mark”? And why is it so important?</p><p>Byte Order Mark or BOM is a marker added at the beginning of a file to specify the endianness used when storing the document. Basically when storing data structures longer than one byte on a storage, there are two ways to do it:</p><ul><li>Big endian notation of “1” (int32) is 0x00 0x00 0x00 0x01</li><li>Little endian notation of “1” (int32) is 0x01 0x00 0x00 0x00</li></ul><p>As a consequence any encoding using more than one byte to store a character — <em>not to be confused with code point</em> — has to come with a BOM in order to help the reader understanding which character they wanted to use.</p><p>Indeed in UTF-16 both 0x00 0x01 and 0x01 0x00 are valid code units and there is no way — except the BOM — to select which one has been encoded.</p><p>The second usage of BOM is helping the reader to distinguish which encoding has been used. For that reason even if unnecessary for UTF-8 encoding, using it will tell the user that this is a UTF-8 content — <em>unnecessary for UTF-8 because code units are one byte long</em>.</p><p>Table below is an example mixing both encoding and byte ordering on three code points declared by Unicode:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EircemXowkJWMbT9vVf16g.png" /><figcaption>Some examples of how characters can be encoded</figcaption></figure><p>As stated when describing the second usage of BOM, it differs from one encoding to another. Here is the list of those markers:</p><ul><li>UTF-8: 0xEF 0xBB 0xBF</li><li>UTF-16 (big): 0xFE 0xFF</li><li>UTF-16 (little): 0xFF 0xFE</li><li>UTF-32 (big): 0x00 0x00 0xFE 0xFF</li><li>UTF-32 (little): 0xFF 0xFE 0x00 0x00</li></ul><h3>Character in common languages</h3><p>While the topic of encoding and Unicode was at least partially covered — <em>we will come back to it just after — </em>an important problem remains: what do I use everyday when running code? Sending emails?…</p><p>It worth noting that most data transmission — file storage or internet communication — use UTF-8 encoding.</p><p>Most of modern languages — C#, Java, JavaScript, Scala — chose UTF-16 to encode code points. C/C++ encode characters on a single byte. In this part, we will focus on the UTF-16 choice. But it also applies to all choices that require multiple code units to encode a single code point.</p><p>One of the problems related to this choice is that neither of those languages properly handles characters outside of the first 65,536 first characters.</p><p>For instance in JavaScript, the CAT FACE code point defined at 0x1f431 is considered to be of length two, can be split… Basically most of string methods do not handle this code point as expected:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2935e06a3a622793a504ecfd0e057a30/href">https://medium.com/media/2935e06a3a622793a504ecfd0e057a30/href</a></iframe><p>Most of the time the choice has been to handle at code unit level and not code point level. It leads to some strange issues with � characters popping everywhere because of bad unicode code point handling.</p><h3>Additional traps you need to worry about</h3><p>Before closing this story and because many traps remain, this last section gives a quick insight on some of those remaining traps.</p><p><strong>Lower/Upper case</strong></p><p>The lower or upper case value of a character might need more storage than the original value. For instance, the upper case value of the character ‘ß’ is ‘SS’ which is two characters long.</p><p>Moreover, the lower or upper case value of a character depends on the locale. A same character could have different upper case values depending on the locale used by the computer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1950b4aec6b913279f0236d7f049a394/href">https://medium.com/media/1950b4aec6b913279f0236d7f049a394/href</a></iframe><p><strong>Composable characters</strong></p><p>Unicode also defines how we can compose characters together. It can be used to build known and already defined code points or build new ones.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*zzvTpeBLthC0szn5NV9XfA.png" /><figcaption>Composable characters</figcaption></figure><p>The two notations above are totally equivalent for Unicode standard but they are considered different in most programming languages.</p><p>In UTF-16, the first notation ‘\u0061\u0308’ has a length of 2 char points while ‘\u00e4’ has a length of 1. For instance, in JavaScript the two strings are considered different even if they represent the same grapheme.</p><p><strong>Graphemes</strong></p><p>After code units which can be seen as the minimal unit of storage for a character. After code points which can be seen as the definition of a character representation, way to be composed… Let’s have a few words about graphemes. Graphemes are the result of the combination of one or multiple code points to form complex ones.</p><p>The feature of composable characters perfectly allow to define new characters based on known code points. For instance, nothing forbid the definition of a ‘a’ character having multiple ¨ on top of it even if it does not have a single code point equivalent:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/35/1*CcAN6gEAVBzJptPFbpMoWw.png" /><figcaption>‘a’ character with three ¨ on top of it: ‘\u0061\u0308\u0308\u0308’</figcaption></figure><p>This feature is also highly used by the standard to define emojis. Indeed most of them result from the combination of multiple code units:</p><ul><li>a man farmer is the combination of code points ‘man’ + ‘farmer’</li><li>a woman farmer is the combination of code points ‘woman’ + ‘farmer’</li></ul><p>More on emojis at <a href="http://unicode.org/reports/tr51/">http://unicode.org/reports/tr51/</a></p><h3>Conclusion</h3><p>One of the most important distinction to keep in mind when dealing with characters in general is the difference between code units, code points and graphemes.</p><p>It is also important to understand and know how the programming languages you might be using encode your code points and deal with them. Depending on the language and the methods, they can either act at code unit level or code point level. Or even grapheme level if you are lucky enough.</p><p>Please leave a clap or a comment if you liked this article ;)</p><p>Additional readings:</p><ul><li>Char in C#: <a href="https://docs.microsoft.com/fr-fr/dotnet/api/system.char?view=netframework-4.7.1">https://docs.microsoft.com/fr-fr/dotnet/api/system.char?view=netframework-4.7.1</a></li><li>Characters and Combining marks: <a href="http://unicode.org/faq/char_combmark.html">http://unicode.org/faq/char_combmark.html</a></li><li>Emojis in Unicode standard: <a href="https://unicode.org/emoji/">https://unicode.org/emoji/</a></li><li>JavaScript has a Unicode problem: <a href="https://mathiasbynens.be/notes/javascript-unicode">https://mathiasbynens.be/notes/javascript-unicode</a></li><li>Precomposed characters: <a href="https://en.wikipedia.org/wiki/Precomposed_character">https://en.wikipedia.org/wiki/Precomposed_character</a></li><li>Unicode Glossary: <a href="http://www.unicode.org/glossary/">http://www.unicode.org/glossary/</a></li><li>Unicode in C++: <a href="https://www.youtube.com/watch?v=n0GK-9f4dl8">https://www.youtube.com/watch?v=n0GK-9f4dl8</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=658f185a242d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Beware of auto-fixes done by linters]]></title>
            <link>https://medium.com/@nicolasdubien/beware-of-linters-41c82b075890?source=rss-5bed092e8bd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/41c82b075890</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[linter]]></category>
            <category><![CDATA[prettier]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Nicolas Dubien]]></dc:creator>
            <pubDate>Sat, 31 Mar 2018 18:00:56 GMT</pubDate>
            <atom:updated>2018-04-01T00:19:23.363Z</atom:updated>
            <content:encoded><![CDATA[<p>The title might seem alarmist but auto-fixes provided by linters might really break down your code if you don’t mind so much about using them.</p><p>This article comes as a result of a past debug and review session following a regression introduced by a linter. See it as a simple experience sharing. The bug occurred while running <a href="https://palantir.github.io/tslint/">tslint</a> on <a href="https://github.com/dubzzz/fast-check/">fast-check</a> source code. It took me a bit of time to fully understand what went wrong.</p><p>Before moving on to the issue itself, I just want to draw the line that differentiates linters from formatters.</p><h3>Formatters vs.Linters</h3><p>Formatters like <a href="https://prettier.io/">Prettier</a> are not Linters. They should be used for different purposes and in conjunction:</p><ul><li><strong>Formatters </strong>handle all the formatting related challenges going from line length to parameters alignment. They should not alter the way the code behave. They deal with formatting only.<br>More examples on <a href="https://prettier.io/docs/en/options.html">Prettier options</a></li><li><strong>Linters</strong>, in another hand, deal with code style in terms of language features you can or can’t use. For instance a TypeScript linter will deal with problems like: Can I use var keyword? Can I use index-based loop or do I use for-of? Can I use require?<br>More examples on <a href="https://palantir.github.io/tslint/rules/">TSLint rules</a></li></ul><p>Another explanation of the differences is available at: <a href="https://prettier.io/docs/en/comparison.html">https://prettier.io/docs/en/comparison.html</a></p><h3>Problem in a nutshell</h3><p>The idea to apply formatters and linters to my code came as a need. Indeed having a well formatted, structured and not too dangerous code style became an important point.</p><p>So I decided to run <a href="https://palantir.github.io/tslint/">tslint </a>in conjunction with <a href="https://github.com/Microsoft/tslint-microsoft-contrib">tslint-microsoft-contrib</a> and <a href="https://github.com/alexjoverm/tslint-config-prettier">tslint-config-prettier</a>. Unfortunately I did not imagine that it will introduce changes in the way my code behaves. But it did.</p><p>Indeed some rules might be a bit more dangerous than what they look. For instance the <a href="https://palantir.github.io/tslint/rules/no-unnecessary-callback-wrapper/">no-unnecessary-callback-wrapper</a> rule can breaks your code if you don’t check the produced code in details. For instance the following code:</p><blockquote>data.map(v=&gt; f(v))</blockquote><p>Will be changed into:</p><blockquote>data.map(f)</blockquote><p>But they are not fully equivalent. Indeed map has the following signature:</p><blockquote>map&lt;U&gt;(mapFunction: (value: T, index: number) =&gt; U)</blockquote><p>In my code, <em>f</em> function is called <em>array. </em>It accepts the following signatures:</p><blockquote>array&lt;T&gt;(generator: Arbitrary&lt;T&gt;)<br>array&lt;T&gt;(generator: Arbitrary&lt;T&gt;, max: number)</blockquote><p>The change suggested by the linter was changing the way the code worked by calling the second signature instead of the first one.</p><h3>Possible solutions</h3><p>Whenever you encounter this kind of issue with linters you can opt for one of the following solutions:</p><ul><li>do not use auto-fix anymore</li><li>remove the rule from the set of rules you are using</li><li>ask the linter to ignore this line</li><li>revamp the way you write your code</li></ul><p>Do not hesitate to leave a comment or clap if you liked or not ;)</p><p>Linting on going. I might update the article to provide a list of rules introducing possible unexpected changes.</p><p>If you also encountered this problem on eslint or tslint, I would be glad to know the rule that caused you the problem.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41c82b075890" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Property Based Testing]]></title>
            <link>https://medium.com/criteo-engineering/introduction-to-property-based-testing-f5236229d237?source=rss-5bed092e8bd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5236229d237</guid>
            <category><![CDATA[quickcheck]]></category>
            <category><![CDATA[property-based-testing]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[fast-check]]></category>
            <dc:creator><![CDATA[Nicolas Dubien]]></dc:creator>
            <pubDate>Fri, 23 Mar 2018 19:00:31 GMT</pubDate>
            <atom:updated>2023-04-17T06:50:12.642Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Another test philosophy introduced by QuickCheck</em></p><p>Property based testing has become quite famous in the functional world. Mainly introduced by <strong>QuickCheck</strong> framework in <strong>Haskell</strong>, it suggests another way to test software. It targets all the scope covered by example based testing: from unit tests to integration tests.</p><p>In order to introduce property based testing, this article will use <a href="https://fast-check.dev/">fast-check </a>— <em>a property based testing framework for JavaScript</em> — but examples can easily be adapted to other frameworks and languages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d1VAnHXiXlAM9wmgZFbiYQ.jpeg" /><figcaption>Logo of “fast-check”</figcaption></figure><p>It is good to notice that this test framework helped me to discover bugs in famous npm packages with only a few lines of code — checkout <a href="https://fast-check.dev/">fast-check documentation</a> for more details.</p><h3>Existing ways</h3><p>Multiple ways are currently available in the industry in order to prove the correctness of a code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*g1acsJSuH1bK7i7I-vgaLw.png" /><figcaption>Available automated test technics</figcaption></figure><p>Axis:</p><ul><li>Feature compliance: Does it really test the feature I am trying to provide?</li><li>Input scope covered: How well do I cover all possible inputs?</li></ul><p>As we can see in the diagram above, there is maybe a gap to fill. This is where property based testing comes into play.</p><h3>Definition</h3><p>Property based testing relies on properties. It checks that a function, program or whatever system under test abides by a property. Most of the time, properties do not have to go into too much details about the output. They just have to check for useful characteristics that must be seen in the output.</p><p>A property is just something like:</p><blockquote>for all (x, y, ...)<br>such as precondition(x, y, ...) holds<br>property(x, y, ...) is true</blockquote><p>Here is a simple <em>hello world</em> property:</p><pre>for all (a, b, c) strings<br>the concatenation of a, b and c always <strong>contains</strong> b</pre><p>In other words, without any precise knowledge of the shape and content of the strings a, b and c I can surely say that b is a substring of a + b + c.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*4naI7GwRj50KnS_Ih1gL7g.png" /><figcaption>In the example above “rt” is a substring of “azerty”</figcaption></figure><p>The executor understanding of a property can be summarized as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/913/1*g6eBDX6dsznOxhxcGQPYsg.png" /><figcaption>Property based testing: Executor understanding</figcaption></figure><h3>Benefits</h3><p>They are numerous:</p><ul><li><strong>Cover the scope of all possible inputs:</strong> by construct, it does not restrict the generated inputs if not asked to do so. Consequently it can theorically generate all possible inputs and cover the whole range of strings, integers or whatever type required by the system under test.</li><li><strong>Shrink the input in case of failure:</strong> whenever it fails, the framework tries to reduce the input to a smaller input. For instance: if the condition of the failure is the existence of a given character in a string it should return the one-character string having only this character. This is certainly one of the most interesting features of such approach as most of the time the failure can be summarized by a very small case.</li><li><strong>Reproducible and replayable:</strong> each time it runs a property test, a seed is produced in order to be able to re-run the test again on the same datasets. Any run failure causes the framework to print both the failing case and the seed in order to be able to fully reproduce the run.</li></ul><p>It is also important to note that it does not — <em>by any means</em> — replace unit testing. It only provides an additional layer of tests that might prove very efficient to reduce some boilerplate tests.</p><h3>Hands on property based testing</h3><p>A full “hands on fast-check” is available at <a href="https://fast-check.dev/docs/category/quick-start/">https://fast-check.dev/docs/category/quick-start/</a>.</p><p>Bootstrapping an empty project:</p><pre>mkdir sample-fast-check<br>cd sample-fast-check<br>npm init --yes<br>npm install fast-check --save-dev</pre><p>Let’s consider the property we described in the previous section:</p><pre>for all (a, b, c) strings<br>the concatenation of a, b and c always contains b</pre><p>Let’s create a file called script.js with the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77c07a06f60a69c9224b88befba31182/href">https://medium.com/media/77c07a06f60a69c9224b88befba31182/href</a></iframe><p>You also need to implement a basic contains method (eg.: const contains = (q, text) =&gt; text.indexOf(q) !== -1;).</p><p>Then you can run the property script with:</p><pre>node script.js</pre><p>Content of script.js:</p><ul><li>fc.assert(&lt;property&gt;(, parameters)): It executes the test and checks that the property stays true for all the a, b, c strings produced by the framework. In case of failure, it shrinks the input to the minimal failing example to help the user during the analysis. By default, it runs the property check against 100 generated inputs.</li><li>fc.property(&lt;...arbitraries&gt;, &lt;predicate&gt;): It describes the property. arbitraries are the instances responsible to build the inputs while predicate is the function doing the test against those inputs. predicate should either return a boolean or not return anything and throw in case of issue.</li><li>fc.string(): It is a string generator — also called arbitrary in property based. An arbitrary is responsible to generate and shrink values.</li></ul><p>If you wonder what are the generated inputs you can replace fc.assert by fc.sample as follows and wrap it into a console.log:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f42b3b06ba8d14e65da3cd319321eb44/href">https://medium.com/media/f42b3b06ba8d14e65da3cd319321eb44/href</a></iframe><p>You might get generated inputs like:</p><pre>{a: &quot;)|2&quot;, b: &quot;&quot;, c: &quot;$&amp;RJh%% &quot;}<br>{a: &quot;\\\&quot;&quot;, b: &quot;Y\\\&quot;\\\&quot;&quot;, c: &quot;$S#K3&quot;}<br>{a: &quot;$&quot;, b: &quot;\\\\cx%wf&quot;, c: &quot;&#39;t4qRA&quot;}<br>{a: &quot;&quot;, b: &quot;&quot;, c: &quot;n?h.0%&quot;}<br>{a: &quot;6_#7&quot;, b: &quot;b&quot;, c: &quot;4%E&quot;}<br>...</pre><h3>Shrinking in action</h3><p>Let’s suppose that we have the following implementation of contains — <em>which obviously does not work</em>. The idea is to see what the framework generates in case of failure and how it shrinks the input:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d57ebad4caeb06fb769ed684df6710c/href">https://medium.com/media/3d57ebad4caeb06fb769ed684df6710c/href</a></iframe><p>Multiple inputs will be generated and as soon as it finds a failing case it will start the shrinking process. If we take the example above, when running it locally I got a first failure for the entry<br>{&quot;a&quot;:&quot;&quot;,&quot;b&quot;:&quot;1%Eg+]]2&quot;,&quot;c&quot;:&quot;\\\&quot;F#\\\&quot; g&quot;},<br>then the shrinking process took place:</p><pre>{a: &quot;)|2&quot;, b: &quot;&quot;, c: &quot;$&amp;RJh%% &quot;} -&gt; success<br>{a: &quot;\\\&quot;&quot;, b: &quot;Y\\\&quot;\\\&quot;&quot;, c: &quot;$S#K3&quot;} -&gt; success<br>{a: &quot;$&quot;, b: &quot;\\\\cx%wf&quot;, c: &quot;&#39;t4qRA&quot;} -&gt; success<br>{a: &quot;&quot;, b: &quot;&quot;, c: &quot;n?h.0%&quot;} -&gt; success<br>{a: &quot;6_#7&quot;, b: &quot;b&quot;, c: &quot;4%E&quot;} -&gt; success<br>{a: &quot;&quot;, b: &quot;l%Eg+]]2&quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; <strong>first failure encountered: start shrinking process</strong><br>{a: &quot;&quot;, b: &quot;&quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; success<br>{a: &quot;&quot;, b: &quot;+]]2&quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; <strong>failure</strong><br>{a: &quot;&quot;, b: &quot;]2&quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; <strong>failure</strong><br>{a: &quot;&quot;, b: &quot;2&quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; <strong>failure</strong><br>{a: &quot;&quot;, b: &quot; &quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; success<br>{a: &quot;&quot;, b: &quot;)&quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; <strong>failure</strong><br>{a: &quot;&quot;, b: &quot;%&quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; <strong>failure</strong><br>{a: &quot;&quot;, b: &quot;#&quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; success<br>{a: &quot;&quot;, b: &quot;$&quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; <strong>failure</strong><br>{a: &quot;&quot;, b: &quot;\\\&quot;&quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; success<br>{a: &quot;&quot;, b: &quot;#&quot;, c: &quot;\\\&quot;F#}\\\&quot; g&quot;} -&gt; success<br>{a: &quot;&quot;, b: &quot;$&quot;, c: &quot;&quot;} -&gt; <strong>failure</strong><br>{a: &quot;&quot;, b: &quot;\\\&quot;&quot;, c: &quot;&quot;} -&gt; <strong>failure</strong><br>{a: &quot;&quot;, b: &quot;!&quot;, c: &quot;&quot;} -&gt; <strong>failure</strong></pre><h3>Going further</h3><p>Property based testing is a useful and powerful tool. I personally used it for multiple problems going from testing famous npm repositories to personal codes including UI. Here are some of the interesting topics I covered with this kind of tool.</p><p><strong>Bug in famous npm packages: js-yaml</strong></p><p>While <a href="https://www.npmjs.com/package/js-yaml">js-yaml</a> is a 1 million downloads per day package, property based testing was able to find a problem in it. The story is available at <a href="https://github.com/nodeca/js-yaml/pull/398">https://github.com/nodeca/js-yaml/pull/398</a> and the test at <a href="https://runkit.com/dubzzz/multiple-properties-for-js-yaml">https://runkit.com/dubzzz/multiple-properties-for-js-yaml</a></p><p>Basically I adopted a black-box technic to test it against the most simple property possible: whatever the object I convert into yaml, I should be able to have it back by reading the generated yaml.</p><p><strong>UI testing</strong></p><p>Certainly one of the most surprising usage is to derive it for UI testing. Video below relies on fast-check and Selenium.</p><p>More at: <a href="https://medium.com/criteo-labs/detecting-the-unexpected-in-web-ui-fuzzing-1f3822c8a3a5">https://medium.com/criteo-labs/detecting-the-unexpected-in-web-ui-fuzzing-1f3822c8a3a5</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FTYzYteiZPxw%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DTYzYteiZPxw&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FTYzYteiZPxw%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/066207bdc15cfcc489c1706c33a765bf/href">https://medium.com/media/066207bdc15cfcc489c1706c33a765bf/href</a></iframe><h3>Conclusion</h3><p>I highly recommend that you give it a try on some snippets. Some code examples based on fast-check are available at: <a href="https://github.com/dubzzz/fast-check/tree/main/examples">https://github.com/dubzzz/fast-check/tree/main/examples</a> in the files called test.js.</p><p>By using it you will see how simply it can be used to diagnose and find issues.</p><p><strong>Additional readings:</strong></p><ul><li><a href="https://www.youtube.com/watch?v=hXnS_Xjwk2Y">John Hughes — Don’t Write Tests</a></li><li><a href="https://labs.spotify.com/2015/06/25/rapid-check/">Generating test cases so you don’t have to</a></li><li><a href="https://dubzzz.github.io/fast-check.github.com/talks/meetup-typescript-04092018/property-based-testing.html">Slides introducing fast-check at a TypeScript meetup</a></li></ul><p><strong>Frameworks I tried in other languages:</strong></p><ul><li><a href="https://hypothesis.readthedocs.io/en/latest/">Hypothesis </a>— Python</li><li><a href="https://github.com/emil-e/rapidcheck/">RapidCheck</a> — C++</li><li><a href="https://www.scalacheck.org/">ScalaCheck </a>— Scala</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5236229d237" width="1" height="1" alt=""><hr><p><a href="https://medium.com/criteo-engineering/introduction-to-property-based-testing-f5236229d237">Introduction to Property Based Testing</a> was originally published in <a href="https://medium.com/criteo-engineering">Criteo Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Breaking down ‘return true to win’ using properties]]></title>
            <link>https://medium.com/@nicolasdubien/breaking-down-return-true-to-win-using-properties-1e2a3eac2a80?source=rss-5bed092e8bd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e2a3eac2a80</guid>
            <category><![CDATA[property-based-testing]]></category>
            <category><![CDATA[quickcheck]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[fast-check]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Nicolas Dubien]]></dc:creator>
            <pubDate>Tue, 27 Feb 2018 19:27:47 GMT</pubDate>
            <atom:updated>2023-04-17T06:46:31.928Z</atom:updated>
            <content:encoded><![CDATA[<p>The idea of <em>return true to win</em> exercises is to build a value such as a given function returns true. Values can be of any type but can also be restricted to objects, arrays, functions depending on the problem statement. This article focuses on applying property based testing to solve such problems.</p><p>Examples of such puzzles are available at: <a href="https://alf.nu/ReturnTrue">https://alf.nu/ReturnTrue</a></p><p>A proof of concept based on <a href="https://github.com/dubzzz/fast-check">fast-check</a> framework is available at <a href="https://github.com/dubzzz/breaking-return-true-to-win/">https://github.com/dubzzz/breaking-return-true-to-win/</a></p><h3>Property based testing</h3><p>Property based testing originally born for testing reasons. Its purpose is to find out values that make a property fail. A property can be described as any statement like:</p><pre>Given inputs satisfying a pre-requisite,<br>The statement is true</pre><p>Whenever a failing input is encountered (ie. the statement is false), the framework shrinks it to the minimal failing input. It constitutes one of the most important and useful feature of this methodology as it gets automatically rid of all the unnecessary noise surrounding the failing case.</p><h3>Application to return true to win</h3><p><em>Return true to win</em> statements can easily be switched to a property based testing statement.</p><p>Let’s take the following example:</p><p>Find a JavaScript expression such as: x == !x</p><p>Instead of looking for falsy values we look for truthy ones. So we just have to take the negative and we get the corresponding property.</p><h3>Implementation</h3><p>Given that, implementation using <a href="https://github.com/dubzzz/fast-check">fast-check</a> is quite straight forward. For instance, our implementation could have been the following:</p><pre>function negationEqualsOriginal(x) {<br>    return x == !x;<br>}<br>fc.assert(<br>    fc.property(<br>        fc.anything(),<br>        x =&gt; ! negationEqualsOriginal(x)<br>    )<br>);</pre><p>Let’s take a look at this implementation a bit closer:</p><ul><li>fc.assert is responsible to execute the property multiple times in an attempt to check for potential issues. It also handles the shrinking part in order to output values as small as possible (no need to have an object with hundreds of keys, if a single key makes it fail)</li><li>fc.property declares the property in terms of what it takes for input and how to assess it worked properly</li><li>fc.anything() is an arbitrary object, it is responsible to generate any possible object or primitive. We can use multiple arbitraries for a same property just by putting them directly after the first one. For more details on this, have a look to the <a href="https://fast-check.dev/">documentation of fast-check</a></li><li>x =&gt; ! negationEqualsOriginal(x) is the predicate that the framework expect to see always true. In our case we want it to find the case where it becomes falsy</li></ul><p>What about shrinking?</p><p>In the example above both [[[[undefined]]]] == ![[[[undefined]]]], [undefined] == ![undefined] or [] == ![] are true but due to shrinking the framework will only provide us the minimal failing example which would be the empty array in this case.</p><h3>Going further</h3><p>Our approach is quite complete but it might accept false positives. Indeed, with the above construction we consider an exception as a true. Instead of simply taking the negation we should takes something like:</p><pre>const wrapIt = function(fn) {<br>    return (...args) =&gt; {<br>        try { return !fn(...args); }<br>        catch (err) { return true; }<br>    };<br>};</pre><p>And replace x =&gt; ! negationEqualsOriginal(x) by x =&gt; wrapIt(negationEqualsOriginal)(x).</p><h3>Conclusion</h3><p>Using this technic we can easily solve problems that require to find out structures such as arrays, strings or objects. Making it work for examples with functions or manipulations of types through the usage of prototype would require more work and the creation of adapted arbitraries to generate the inputs.</p><p>The proof of concept is available on <a href="https://github.com/dubzzz/breaking-return-true-to-win">https://github.com/dubzzz/breaking-return-true-to-win</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e2a3eac2a80" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>