Skip to content

Commit

Permalink
29-go-unit-testing first draft finished
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhargulati committed Jul 17, 2016
1 parent 69477ee commit da8b105
Show file tree
Hide file tree
Showing 8 changed files with 476 additions and 1 deletion.
2 changes: 2 additions & 0 deletions 29-go-unit-testing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/
pkg/
379 changes: 379 additions & 0 deletions 29-go-unit-testing/README.md

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions 29-go-unit-testing/src/problems/closestpair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package problems

import (
"math"
"sort"
)

type Pair struct {
First, Second int
}

func ClosestPair(numbers []int) Pair {
sort.Ints(numbers)
var pair Pair
var diff int = math.MaxInt32
for index := 0; index < len(numbers)-1; index++ {
cur := numbers[index]
next := numbers[index+1]
if next-cur < diff {
diff = next - cur
pair = Pair{cur, next}
}
}
return pair
}
32 changes: 32 additions & 0 deletions 29-go-unit-testing/src/problems/closestpair_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package problems_test

import (
"fmt"
"github.com/stretchr/testify/assert"
. "problems"
"testing"
)

func TestFindClosestPair(t *testing.T) {
numbers := []int{2, 10, 5, 6, 15}
pair := ClosestPair(numbers)
assert.Equal(t, 5, pair.First, fmt.Sprintf("first should be 5 but was %d", pair.First))
assert.Equal(t, 6, pair.Second, fmt.Sprintf("second should be 6 but was %d", pair.Second))
}

var closestPairTests = []struct {
in []int
out Pair
}{
{[]int{2, 10, 5, 6, 15}, Pair{5, 6}},
{[]int{2, 4, 5}, Pair{4, 5}},
{[]int{100, 5, 7, 99, 11}, Pair{99, 100}},
}

func TestClosestPair(t *testing.T) {
for _, tt := range closestPairTests {
pair := ClosestPair(tt.in)
assert.Equal(t, tt.out.First, pair.First)
assert.Equal(t, tt.out.Second, pair.Second)
}
}
26 changes: 26 additions & 0 deletions 29-go-unit-testing/src/problems/equalornoequal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package problems_test

import (
"github.com/stretchr/testify/assert"
. "problems"
"testing"
)

func TestShouldBeTrueWhenThreeNumbersAreEqual(t *testing.T) {
isThreeNumbersEqual := EqualOrNotEqual(1, 1, 1)
if !isThreeNumbersEqual {
t.Error("Expected true, got ", isThreeNumbersEqual)
}
}

func TestShouldBeFalseWhenThreeNumbersAreNotEqual(t *testing.T) {
isThreeNumbersEqual := EqualOrNotEqual(1, 2, 3)
if isThreeNumbersEqual {
t.Error("Expected false, got", isThreeNumbersEqual)
}
}

func TestShouldAssertThatFunctionReturnsFalseWhenThreeNumbersAreNotEqual(t *testing.T) {
isThreeNumbersEqual := EqualOrNotEqual(1, 2, 3)
assert.False(t, isThreeNumbersEqual, "Expected false got true")
}
9 changes: 9 additions & 0 deletions 29-go-unit-testing/src/problems/equalornotequal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package problems

func EqualOrNotEqual(first, second, third int) bool {
if first == second && second == third {
return true
} else {
return false
}
}
2 changes: 1 addition & 1 deletion 29-golang-github-slacknotification/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,4 @@ If all goes well, you will see the current GitHub Status being reported in your

That's all for this week. Please provide your valuable feedback by adding a comment to [https://github.com/shekhargulati/52-technologies-in-2016/issues/40](https://github.com/shekhargulati/52-technologies-in-2016/issues/40).

[![Analytics](https://ga-beacon.appspot.com/UA-59411913-2/shekhargulati/52-technologies-in-2016/29-golang-part2)](https://github.com/igrigorik/ga-beacon)
[![Analytics](https://ga-beacon.appspot.com/UA-59411913-2/shekhargulati/52-technologies-in-2016/29-golang-romin-http)](https://github.com/igrigorik/ga-beacon)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Below is the list of technologies covered in this series:

29. **[Week 29: July 15, 2016](./29-golang-github-slacknotification)** [Go Language - GitHub System Status API & Slack Notifications](./29-golang-github-slacknotification/README.md). In this blog, we are going to investigate a few more features of the language and combine that into a real life application where we can monitor the status of the GitHub System via its Status API and report its current status directly into a Slack Team page.

30. **[Week 29: July 17, 2016](./29-go-unit-testing)** [Learn GoLang For Great Good Part 2: Unit Testing in Go](./29-go-unit-testing/README.md). This week we will take our Go knowledge to the next level by learning how to perform unit testing in Go. Unit testing has become an essential skill set for every programmer.

-----------
You can follow me on twitter at [https://twitter.com/shekhargulati](https://twitter.com/shekhargulati) or email me at <[email protected]>. Also, you can read my blogs at [http://shekhargulati.com/](http://shekhargulati.com/)

Expand Down

0 comments on commit da8b105

Please sign in to comment.