Test-Driven Development with Golang

Photo by Chris Liverani on Unsplash

TDD

Why TDD?

Guess Age

  • when user guess too high, return the message “too high”
  • when user guess too low, return the message “too low”
  • when user guesses correctly, return message “correct”

1. Create Struct

package humanimport (
"errors"
"math/rand"
)
var (
// TOOHIGHERROR will be the error message when guessed too high
TOOHIGHERROR = "too high"
// TOOLOWERROR will be the error message when guessed too low
TOOLOWERROR = "too low"
)
// Person will be the type of person
type Person struct {
hiddenAge int
}
// NewPerson will initialize person
func NewPerson() * Person {
age: = rand.Intn(101)
person: = Person {}
person.hiddenAge = age
return &person
}
// GuessAge will guess the age of the person
func(p * Person) GuessAge(age int) error {
return nil
}

2. Red Cycle

package humanimport (
"testing"
)
func TestGuessAgeTooHigh(t * testing.T) {
person: = NewPerson()
actual: = person.GuessAge(person.hiddenAge + 1)
expeced: = TOOHIGHERROR
if actual == nil {
t.Errorf("expected: %s - actual: nil", expeced)
return
}
if actual.Error() != expeced {
t.Errorf("expected: %s - actual: %s",
expeced, actual.Error())
}
}

3. Green Cycle

// GuessAge will guess the age of the person
func(p * Person) GuessAge(age int) error {
if age > p.hiddenAge {
return errors.New(TOOHIGHERROR)
}
return nil
}
// GuessAge will guess the age of the person
func(p * Person) GuessAge(age int) error {
if age > p.hiddenAge {
return errors.New(TOOHIGHERROR)
}
if age < p.hiddenAge {
return errors.New(TOOLOWERROR)
}
return nil
}
package humanimport (
"testing"
)
func TestGuessAgeTooHigh(t * testing.T) {
person: = NewPerson()
actual: = person.GuessAge(person.hiddenAge + 1)
expeced: = TOOHIGHERROR
if actual == nil {
t.Errorf("expected: %s - actual: nil", expeced)
return
}
if actual.Error() != expeced {
t.Errorf("expected: %s - actual: %s",
expeced, actual.Error())
}
}
func TestGuessAgeTooLow(t * testing.T) {
person: = NewPerson()
actual: = person.GuessAge(person.hiddenAge - 1)
expeced: = TOOLOWERROR
if actual == nil {
t.Errorf("expected: %s - actual: nil", expeced)
return
}
if actual.Error() != expeced {
t.Errorf("expected: %s - actual: %s",
expeced, actual.Error())
}
}
func TestGuessAgeCorrect(t * testing.T) {
person: = NewPerson()
actual: = person.GuessAge(person.hiddenAge)
if actual != nil {
t.Errorf("expected: nil - actual: %s", actual.Error())
return
}
}

4. Refactor

Wrapup

--

--

--

I like artificial intelligence.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

iOS Biometric Authentication with Xamarin Forms

Howard isn’t aggrieved by people gaming the platform to get their music heard, and he highlights…

A Robust and Powerful Alternative of the python, R, and Matlab

Supply and demand: hacking the future of work

A Hackathon Later…

Flutter MVVM + Providers

Keywords To Remember “While” Learning To Code…

The Most Common Causes Why Programmers Get Fired

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anwar Hidayat

Anwar Hidayat

I like artificial intelligence.

More from Medium

Golang Field Alignment

Publish a Go module in a monorepo

Using open source packages — Golang