{"id":1124,"date":"2012-01-22T11:51:55","date_gmt":"2012-01-22T11:51:55","guid":{"rendered":"http:\/\/www.navision-blog.de\/?p=1124"},"modified":"2012-01-22T11:59:08","modified_gmt":"2012-01-22T11:59:08","slug":"f-legacy-test-implants","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2012\/01\/22\/f-legacy-test-implants\/","title":{"rendered":"F# Legacy: Test implants"},"content":{"rendered":"<p>Legacy code is a problem in all languages, even F#. <img style=\"border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none\" class=\"wlEmoticon wlEmoticon-winkingsmile\" alt=\"Zwinkerndes Smiley\" src=\"http:\/\/www.navision-blog.de\/images\/F-Legacy-Test-implants_AA75\/wlEmoticon-winkingsmile.png\" \/><\/p>\n<p>The super awesome <a href=\"http:\/\/ilker.de\/about\">Ilker Cetinkaya<\/a> published an excellent <a href=\"http:\/\/ilker.de\/the-quest-of-the-test\">article about \u201ctest implants\u201d<\/a>. I really recommend to read his post before you read this one. It\u2019s a really well written story called \u201c<a href=\"http:\/\/ilker.de\/the-quest-of-the-test\">The quest of the test<\/a>\u201d.<\/p>\n<p>I destilled his nice little story to this small F# code:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1656649.js\"> <\/script>  <\/p>\n<p>We have a function <strong><em>a<\/em><\/strong> that is part of a third-party API and a function <strong><em>b<\/em><\/strong> which is part of our own legacy code base. The problem is: we want to check that <strong><em>b<\/em><\/strong> has called <strong><em>a<\/em><\/strong>. without changing <strong><em>b<\/em><\/strong>\u2019s signature. Ilker suggested to use a \u201ctest implant\u201d. Let\u2019s try to do something similar with our small F# code. Since we are in the land of functional programming our idea is to implant another function into <strong><em>b<\/em><\/strong>. So let\u2019s rename <strong><em>b<\/em><\/strong> into <strong><em>b\u2019<\/em><\/strong> and add the implant to the parameter list of <strong><em>b\u2019<\/em><\/strong>.<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1656671.js\"> <\/script>  <\/p>\n<p>Of course we restore <strong><em>b<\/em><\/strong>\u2019s behaviour by utilizing <strong><em>b\u2019<\/em><\/strong>:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1656674.js\"> <\/script>  <\/p>\n<p>As you can see, we didn\u2019t change the signature nor the behaviour of <strong><em>b<\/em><\/strong>. This is a typical refactoring in F#. The program runs as before. Now we want to write our test:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1656688.js\"> <\/script>  <\/p>\n<p>I hope you can see the similarity between this and Ilkers solution. The only difference is that we don\u2019t use new constructors here. I really like this since it brings you one step further to nice functional design. What do you think?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Legacy code is a problem in all languages, even F#. The super awesome Ilker Cetinkaya published an excellent article about \u201ctest implants\u201d. I really recommend to read his post before you read this one. It\u2019s a really well written story called \u201cThe quest of the test\u201d. I destilled his nice little story to this small [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[448],"tags":[664,531,284,612],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1124"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=1124"}],"version-history":[{"count":3,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1124\/revisions"}],"predecessor-version":[{"id":1127,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1124\/revisions\/1127"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=1124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=1124"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=1124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}