<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prawin K</title>
    <description>The latest articles on DEV Community by Prawin K (@prawink).</description>
    <link>https://dev.to/prawink</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F454872%2Ff4278d7d-735b-41a2-a470-b26229df1ac9.png</url>
      <title>DEV Community: Prawin K</title>
      <link>https://dev.to/prawink</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prawink"/>
    <language>en</language>
    <item>
      <title>*testing in GO</title>
      <dc:creator>Prawin K</dc:creator>
      <pubDate>Fri, 28 Aug 2020 10:28:34 +0000</pubDate>
      <link>https://dev.to/prawink/testing-in-go-3cnn</link>
      <guid>https://dev.to/prawink/testing-in-go-3cnn</guid>
      <description>&lt;p&gt;Recently I have been learning Golang and of all the plethora of features this language offers one specifically caught my eye.&lt;/p&gt;

&lt;p&gt;Golang has a package &lt;code&gt;testing&lt;/code&gt; which provides support for automated testing of Go packages.&lt;/p&gt;

&lt;p&gt;So we will go through a simple problem of reversing a string in-place and write a unit test to test the reverseString function.&lt;/p&gt;

&lt;p&gt;The solution to reverse a string is simple enough.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;reverse_string.go&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package devto

func reverseString(list []string) []string {

    start := 0
    end := len(list) - 1

    for start &amp;lt; end {
        // swap the values at indices
        common.Swap(list, start, end)

        start++
        end--
    }
    return list
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For testing purpose, we will first import the testing package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "testing"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Testing functions in go looks like &lt;strong&gt;TestXxx&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So let's define a function for our reverseString function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func TestReverseString(t *testing.T) {
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, we will be defining an anonymous array of structs which contains the input &amp;amp; expected values (string array).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tests := []struct {
        in       []string
        expected []string
    }{
        {[]string{}, []string{}},
        {[]string{"a"}, []string{"a"}},
        {[]string{"a", "b"}, []string{"b", "a"}},
        {[]string{"a", "b", "c"}, []string{"c", "b", "a"}},
        {[]string{"a", "b", "c", "d"}, []string{"d", "c", "b", "a"}},
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have initialized the struct with input &amp;amp; expected values,&lt;br&gt;
to test all of these values and checking if the result is what we expected we do something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "reflect"

...

for _, tt := range tests {
        result := reverseString(tt.in)
        if !reflect.DeepEqual(result, expected) {
            t.Errorf("should be %v instead of %v", expected, result)
    }
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;we have imported the reflect package to check if the result that we got from the reverseString function is deepEqual to the expected value&lt;/p&gt;

&lt;p&gt;so the final test file looks like&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;reverse_string_test.go&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package devto

import (
    "testing"
        "reflect"
)

func TestReverseString(t *testing.T) {

    tests := []struct {
        in       []string
        expected []string
    }{
        {[]string{}, []string{}},
        {[]string{"a"}, []string{"a"}},
        {[]string{"a", "b"}, []string{"b", "a"}},
        {[]string{"a", "b", "c"}, []string{"c", "b", "a"}},
        {[]string{"a", "b", "c", "d"}, []string{"d", "c", "b", "a"}},
    }

    for _, tt := range tests {
        result := reverseString(tt.in)
        if !reflect.DeepEqual(result, expected) {
            t.Errorf("should be %v instead of %v", expected, result)
    }
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;all the test file names are of format &lt;strong&gt;xxx_test.go&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now to run our test file we use the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go test devto/reverse_string_test.go
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&amp;amp; the output would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Running tool: /usr/local/bin/go test -timeout 30s -run ^TestReverseString$

PASS
ok      ***/devto   0.005s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
    </item>
    <item>
      <title>*testing in GO</title>
      <dc:creator>Prawin K</dc:creator>
      <pubDate>Fri, 28 Aug 2020 05:56:56 +0000</pubDate>
      <link>https://dev.to/prawink/testing-in-go-33bm</link>
      <guid>https://dev.to/prawink/testing-in-go-33bm</guid>
      <description>&lt;p&gt;Liquid syntax error: Unknown tag 'reflect'&lt;/p&gt;
</description>
    </item>
    <item>
      <title>Remove falsy values without lodash - Javascript</title>
      <dc:creator>Prawin K</dc:creator>
      <pubDate>Tue, 18 Aug 2020 17:25:59 +0000</pubDate>
      <link>https://dev.to/prawink/remove-falsy-value-without-lodash-javascript-5m2</link>
      <guid>https://dev.to/prawink/remove-falsy-value-without-lodash-javascript-5m2</guid>
      <description>&lt;p&gt;If you are a NodeJs developer who likes to take things in your own hands rather than relying on third-party libraries specifically lodash given the context, please read along.&lt;/p&gt;

&lt;p&gt;Lodash is a great library with a lot of tricks down its sleeves but comes with an overhead cost. So why not use our EcmaScript knowledge to use without importing unnecessary cost to our code.&lt;/p&gt;

&lt;p&gt;problem: remove falsy values from an array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1,2,"",3,null,undefined,4,false]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;lodash solution :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_.compact(array);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;ES solution :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.filter(Boolean)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here the Boolean constructor will take care of all the falsy values.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
